foundation5.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * FormValidation (http://formvalidation.io)
  3. * The best jQuery plugin to validate form fields. Support Bootstrap, Foundation, Pure, SemanticUI, UIKit and custom frameworks
  4. *
  5. * @author https://twitter.com/formvalidation
  6. * @copyright (c) 2013 - 2016 Nguyen Huu Phuoc
  7. * @license http://formvalidation.io/license/
  8. */
  9. /**
  10. * This class supports validating Foundation v5 form
  11. * The latest supported version is Foundation v5.3.3
  12. * @see http://foundation.zurb.com/sites/docs/v/5.5.3/
  13. */
  14. /* global Foundation: false */
  15. (function($) {
  16. FormValidation.Framework.Foundation5 = function(element, options) {
  17. options = $.extend(true, {
  18. button: {
  19. selector: '[type="submit"]:not([formnovalidate])',
  20. // The class for disabled button
  21. // http://foundation.zurb.com/sites/docs/v/5.5.3/components/buttons.html
  22. disabled: 'disabled'
  23. },
  24. err: {
  25. // http://foundation.zurb.com/sites/docs/v/5.5.3/components/forms.html
  26. clazz: 'error',
  27. parent: '^.*((small|medium|large)-[0-9]+)\\s.*(columns).*$'
  28. },
  29. // Foundation doesn't support feedback icon
  30. icon: {
  31. valid: null,
  32. invalid: null,
  33. validating: null,
  34. feedback: 'fv-control-feedback'
  35. },
  36. row: {
  37. // http://foundation.zurb.com/sites/docs/v/5.5.3/components/forms.html
  38. selector: '.row',
  39. valid: 'fv-has-success',
  40. invalid: 'error',
  41. feedback: 'fv-has-feedback'
  42. }
  43. }, options);
  44. FormValidation.Base.apply(this, [element, options]);
  45. };
  46. FormValidation.Framework.Foundation5.prototype = $.extend({}, FormValidation.Base.prototype, {
  47. /**
  48. * Specific framework might need to adjust the icon position
  49. *
  50. * @param {jQuery} $field The field element
  51. * @param {jQuery} $icon The icon element
  52. */
  53. _fixIcon: function($field, $icon) {
  54. var ns = this._namespace,
  55. type = $field.attr('type'),
  56. field = $field.attr('data-' + ns + '-field'),
  57. row = this.options.fields[field].row || this.options.row.selector,
  58. $parent = $field.closest(row);
  59. if ('checkbox' === type || 'radio' === type) {
  60. var $next = $icon.next();
  61. if ($next.is('label')) {
  62. $icon.insertAfter($next);
  63. }
  64. }
  65. },
  66. /**
  67. * Create a tooltip or popover
  68. * It will be shown when focusing on the field
  69. *
  70. * @param {jQuery} $field The field element
  71. * @param {String} message The message
  72. * @param {String} type Can be 'tooltip' or 'popover'
  73. */
  74. _createTooltip: function($field, message, type) {
  75. var that = this,
  76. $icon = $field.data('fv.icon');
  77. if ($icon) {
  78. $icon
  79. .attr('title', message)
  80. .css({
  81. 'cursor': 'pointer'
  82. })
  83. .off('mouseenter.container.fv focusin.container.fv')
  84. .on('mouseenter.container.fv', function() {
  85. that._showTooltip($field, type);
  86. })
  87. .off('mouseleave.container.fv focusout.container.fv')
  88. .on('mouseleave.container.fv focusout.container.fv', function() {
  89. that._hideTooltip($field, type);
  90. });
  91. Foundation.libs.tooltip.create($icon);
  92. $icon.data('fv.foundation.tooltip', $icon);
  93. }
  94. },
  95. /**
  96. * Destroy the tooltip or popover
  97. *
  98. * @param {jQuery} $field The field element
  99. * @param {String} type Can be 'tooltip' or 'popover'
  100. */
  101. _destroyTooltip: function($field, type) {
  102. var $icon = $field.data('fv.icon');
  103. if ($icon) {
  104. $icon.css({
  105. 'cursor': ''
  106. });
  107. var $tooltip = $icon.data('fv.foundation.tooltip');
  108. if ($tooltip) {
  109. // Foundation doesn't provide method to destroy particular tooltip instance
  110. $tooltip.off('.fndtn.tooltip');
  111. Foundation.libs.tooltip.hide($tooltip);
  112. $icon.removeData('fv.foundation.tooltip');
  113. }
  114. }
  115. },
  116. /**
  117. * Hide a tooltip or popover
  118. *
  119. * @param {jQuery} $field The field element
  120. * @param {String} type Can be 'tooltip' or 'popover'
  121. */
  122. _hideTooltip: function($field, type) {
  123. var $icon = $field.data('fv.icon');
  124. if ($icon) {
  125. $icon.css({
  126. 'cursor': ''
  127. });
  128. var $tooltip = $icon.data('fv.foundation.tooltip');
  129. if ($tooltip) {
  130. Foundation.libs.tooltip.hide($tooltip);
  131. }
  132. }
  133. },
  134. /**
  135. * Show a tooltip or popover
  136. *
  137. * @param {jQuery} $field The field element
  138. * @param {String} type Can be 'tooltip' or 'popover'
  139. */
  140. _showTooltip: function($field, type) {
  141. var $icon = $field.data('fv.icon');
  142. if ($icon) {
  143. var $tooltip = $icon.data('fv.foundation.tooltip');
  144. if ($tooltip) {
  145. $icon.css({
  146. 'cursor': 'pointer'
  147. });
  148. Foundation.libs.tooltip.show($tooltip);
  149. }
  150. }
  151. }
  152. });
  153. }(jQuery));