foundation.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 v6+ form (http://foundation.zurb.com/)
  11. */
  12. /* global Foundation: false */
  13. (function($) {
  14. FormValidation.Framework.Foundation = function(element, options) {
  15. options = $.extend(true, {
  16. button: {
  17. selector: '[type="submit"]:not([formnovalidate])',
  18. // The class for disabled button
  19. // http://foundation.zurb.com/docs/components/buttons.html#button-colors
  20. disabled: 'disabled'
  21. },
  22. err: {
  23. // http://foundation.zurb.com/sites/docs/abide.html
  24. clazz: 'form-error',
  25. parent: '^.*((small|medium|large)-[0-9]+)\\s.*(columns).*$'
  26. },
  27. // Foundation doesn't support feedback icon
  28. icon: {
  29. valid: null,
  30. invalid: null,
  31. validating: null,
  32. feedback: 'fv-control-feedback'
  33. },
  34. row: {
  35. selector: '.row',
  36. valid: 'fv-has-success',
  37. invalid: 'fv-has-error',
  38. feedback: 'fv-has-feedback'
  39. }
  40. }, options);
  41. FormValidation.Base.apply(this, [element, options]);
  42. };
  43. FormValidation.Framework.Foundation.prototype = $.extend({}, FormValidation.Base.prototype, {
  44. /**
  45. * Specific framework might need to adjust the icon position
  46. *
  47. * @param {jQuery} $field The field element
  48. * @param {jQuery} $icon The icon element
  49. */
  50. _fixIcon: function($field, $icon) {
  51. var ns = this._namespace,
  52. type = $field.attr('type'),
  53. field = $field.attr('data-' + ns + '-field'),
  54. row = this.options.fields[field].row || this.options.row.selector;
  55. if ('checkbox' === type || 'radio' === type) {
  56. var $next = $icon.next();
  57. if ($next.is('label')) {
  58. $icon.insertAfter($next);
  59. }
  60. }
  61. },
  62. /**
  63. * Create a tooltip or popover
  64. * It will be shown when focusing on the field
  65. *
  66. * @see http://foundation.zurb.com/sites/docs/tooltip.html
  67. * @param {jQuery} $field The field element
  68. * @param {String} message The message
  69. * @param {String} type Can be 'tooltip' or 'popover'
  70. */
  71. _createTooltip: function($field, message, type) {
  72. var $icon = $field.data('fv.icon');
  73. if ($icon) {
  74. var tooltip = $icon.data('fv.foundation.tooltip');
  75. if (tooltip) {
  76. tooltip.destroy();
  77. }
  78. $icon.css('cursor', 'pointer')
  79. .off('.zf.tooltip')
  80. .data('fv.foundation.tooltip', new Foundation.Tooltip($icon, {
  81. templateClasses: 'fv-foundation-tooltip',
  82. tipText: message
  83. }));
  84. }
  85. },
  86. /**
  87. * Destroy the tooltip or popover
  88. *
  89. * @param {jQuery} $field The field element
  90. * @param {String} type Can be 'tooltip' or 'popover'
  91. */
  92. _destroyTooltip: function($field, type) {
  93. var $icon = $field.data('fv.icon');
  94. if ($icon) {
  95. $icon.removeAttr('title')
  96. .removeAttr('data-tooltip')
  97. .css('cursor', '')
  98. .off('.zf.tooltip');
  99. var tooltip = $icon.data('fv.foundation.tooltip');
  100. if (tooltip) {
  101. tooltip.destroy();
  102. $icon.removeData('fv.foundation.tooltip');
  103. }
  104. }
  105. },
  106. /**
  107. * Hide a tooltip or popover
  108. *
  109. * @param {jQuery} $field The field element
  110. * @param {String} type Can be 'tooltip' or 'popover'
  111. */
  112. _hideTooltip: function($field, type) {
  113. var $icon = $field.data('fv.icon');
  114. if ($icon) {
  115. $icon.css('cursor', '');
  116. var tooltip = $icon.data('fv.foundation.tooltip');
  117. if (tooltip) {
  118. tooltip.hide();
  119. }
  120. }
  121. },
  122. /**
  123. * Show a tooltip or popover
  124. *
  125. * @param {jQuery} $field The field element
  126. * @param {String} type Can be 'tooltip' or 'popover'
  127. */
  128. _showTooltip: function($field, type) {
  129. var $icon = $field.data('fv.icon');
  130. if ($icon) {
  131. $icon.css('cursor', 'pointer');
  132. var tooltip = $icon.data('fv.foundation.tooltip');
  133. if (tooltip) {
  134. tooltip.show();
  135. }
  136. }
  137. }
  138. });
  139. }(jQuery));