uikit.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 UIKit form (http://getuikit.com/)
  11. */
  12. (function($) {
  13. FormValidation.Framework.Uikit = function(element, options) {
  14. options = $.extend(true, {
  15. button: {
  16. selector: '[type="submit"]:not([formnovalidate])',
  17. // The class for disabled button
  18. // http://getuikit.com/docs/button.html
  19. disabled: 'disabled'
  20. },
  21. control: {
  22. valid: 'uk-form-success',
  23. invalid: 'uk-form-danger'
  24. },
  25. err: {
  26. // http://getuikit.com/docs/text.html#text-styles
  27. clazz: 'uk-text-danger',
  28. parent: '^.*(uk-form-controls|uk-width-[\\d+]-[\\d+]).*$'
  29. },
  30. // UIKit doesn't support feedback icon
  31. icon: {
  32. valid: null,
  33. invalid: null,
  34. validating: null,
  35. feedback: 'fv-control-feedback'
  36. },
  37. row: {
  38. // http://getuikit.com/docs/form.html
  39. selector: '.uk-form-row',
  40. valid: 'fv-has-success',
  41. invalid: 'fv-has-error',
  42. feedback: 'fv-has-feedback'
  43. }
  44. }, options);
  45. FormValidation.Base.apply(this, [element, options]);
  46. };
  47. FormValidation.Framework.Uikit.prototype = $.extend({}, FormValidation.Base.prototype, {
  48. /**
  49. * Specific framework might need to adjust the icon position
  50. *
  51. * @param {jQuery} $field The field element
  52. * @param {jQuery} $icon The icon element
  53. */
  54. _fixIcon: function($field, $icon) {
  55. var ns = this._namespace,
  56. type = $field.attr('type'),
  57. field = $field.attr('data-' + ns + '-field'),
  58. $parent = $field.parent();
  59. if ('checkbox' === type || 'radio' === type) {
  60. if ($parent.is('label')) {
  61. $icon.insertAfter($parent);
  62. }
  63. }
  64. // Support UIKit form-password component
  65. // http://getuikit.com/docs/form-password.html
  66. if ($parent.hasClass('uk-form-password')) {
  67. $icon.insertAfter($parent);
  68. }
  69. },
  70. /**
  71. * Create a tooltip or popover
  72. * It will be shown when focusing on the field
  73. *
  74. * @param {jQuery} $field The field element
  75. * @param {String} message The message
  76. * @param {String} type Can be 'tooltip' or 'popover'
  77. */
  78. _createTooltip: function($field, message, type) {
  79. var $icon = $field.data('fv.icon');
  80. if ($icon) {
  81. // Remove the tooltip if it's already exists
  82. if ($icon.data('tooltip')) {
  83. $icon.data('tooltip').off();
  84. $icon.removeData('tooltip');
  85. }
  86. $icon
  87. .attr('title', message)
  88. .css({
  89. 'cursor': 'pointer'
  90. });
  91. new $.UIkit.tooltip($icon);
  92. // UIKit auto set the 'tooltip' data for the element
  93. // so I can retrieve the tooltip later via $icon.data('tooltip')
  94. }
  95. },
  96. /**
  97. * Destroy the tooltip or popover
  98. *
  99. * @param {jQuery} $field The field element
  100. * @param {String} type Can be 'tooltip' or 'popover'
  101. */
  102. _destroyTooltip: function($field, type) {
  103. var $icon = $field.data('fv.icon');
  104. if ($icon) {
  105. var tooltip = $icon.data('tooltip');
  106. if (tooltip) {
  107. tooltip.hide();
  108. tooltip.off();
  109. $icon.off('focus mouseenter')
  110. .removeData('tooltip');
  111. }
  112. $icon.css({
  113. 'cursor': ''
  114. });
  115. }
  116. },
  117. /**
  118. * Hide a tooltip or popover
  119. *
  120. * @param {jQuery} $field The field element
  121. * @param {String} type Can be 'tooltip' or 'popover'
  122. */
  123. _hideTooltip: function($field, type) {
  124. var $icon = $field.data('fv.icon');
  125. if ($icon) {
  126. var tooltip = $icon.data('tooltip');
  127. if (tooltip) {
  128. tooltip.hide();
  129. }
  130. $icon.css({
  131. 'cursor': ''
  132. });
  133. }
  134. },
  135. /**
  136. * Show a tooltip or popover
  137. *
  138. * @param {jQuery} $field The field element
  139. * @param {String} type Can be 'tooltip' or 'popover'
  140. */
  141. _showTooltip: function($field, type) {
  142. var $icon = $field.data('fv.icon');
  143. if ($icon) {
  144. $icon.css({
  145. 'cursor': 'pointer'
  146. });
  147. var tooltip = $icon.data('tooltip');
  148. if (tooltip) {
  149. tooltip.show();
  150. }
  151. }
  152. }
  153. });
  154. }(jQuery));