semantic.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 SemanticUI form (http://semantic-ui.com/)
  11. */
  12. (function($) {
  13. FormValidation.Framework.Semantic = function(element, options) {
  14. options = $.extend(true, {
  15. button: {
  16. selector: '[type="submit"]:not([formnovalidate])',
  17. // CSS class of disabled button
  18. // http://semantic-ui.com/elements/button.html#disabled
  19. disabled: 'disabled'
  20. },
  21. control: {
  22. valid: '',
  23. invalid: ''
  24. },
  25. err: {
  26. // http://semantic-ui.com/elements/label.html#pointing
  27. clazz: 'ui red pointing label',
  28. parent: '^.*(field|column).*$'
  29. },
  30. // When using feedback icon, the input must place inside 'ui input icon' element
  31. // <div class="ui input icon">
  32. // <input type="text" />
  33. // </div>
  34. // See http://semantic-ui.com/elements/input.html#icon
  35. icon: {
  36. // http://semantic-ui.com/elements/icon.html
  37. valid: null, // 'checkmark icon'
  38. invalid: null, // 'remove icon'
  39. validating: null, // 'refresh icon'
  40. feedback: 'fv-control-feedback'
  41. },
  42. row: {
  43. // http://semantic-ui.com/collections/form.html
  44. selector: '.fields',
  45. valid: 'fv-has-success',
  46. invalid: 'error',
  47. feedback: 'fv-has-feedback'
  48. }
  49. }, options);
  50. FormValidation.Base.apply(this, [element, options]);
  51. };
  52. FormValidation.Framework.Semantic.prototype = $.extend({}, FormValidation.Base.prototype, {
  53. /**
  54. * Specific framework might need to adjust the icon position
  55. *
  56. * @param {jQuery} $field The field element
  57. * @param {jQuery} $icon The icon element
  58. */
  59. _fixIcon: function($field, $icon) {
  60. var type = $field.attr('type');
  61. if ('checkbox' === type || 'radio' === type) {
  62. var $fieldParent = $field.parent();
  63. if ($fieldParent.hasClass(type)) {
  64. $icon.insertAfter($fieldParent);
  65. }
  66. }
  67. },
  68. /**
  69. * Create a tooltip or popover
  70. * It will be shown when focusing on the field
  71. *
  72. * @param {jQuery} $field The field element
  73. * @param {String} message The message
  74. * @param {String} type Can be 'tooltip' or 'popover'
  75. */
  76. _createTooltip: function($field, message, type) {
  77. var $icon = $field.data('fv.icon');
  78. if ($icon) {
  79. // Remove the popup if it's already exists
  80. if ($icon.popup('exists')) {
  81. $icon.popup('remove popup')
  82. .popup('destroy');
  83. }
  84. // http://semantic-ui.com/modules/popup.html
  85. switch (type) {
  86. case 'popover':
  87. $icon
  88. .css({
  89. 'cursor': 'pointer'
  90. })
  91. .popup({
  92. content: message,
  93. position: 'top center'
  94. });
  95. break;
  96. case 'tooltip':
  97. /* falls through */
  98. default:
  99. $icon
  100. .css({
  101. 'cursor': 'pointer'
  102. })
  103. .popup({
  104. content: message,
  105. position: 'top center',
  106. variation: 'inverted'
  107. });
  108. break;
  109. }
  110. }
  111. },
  112. /**
  113. * Destroy the tooltip or popover
  114. *
  115. * @param {jQuery} $field The field element
  116. * @param {String} type Can be 'tooltip' or 'popover'
  117. */
  118. _destroyTooltip: function($field, type) {
  119. var $icon = $field.data('fv.icon');
  120. if ($icon && $icon.popup('exists')) {
  121. $icon
  122. .css({
  123. 'cursor': ''
  124. })
  125. .popup('remove popup')
  126. .popup('destroy');
  127. }
  128. },
  129. /**
  130. * Hide a tooltip or popover
  131. *
  132. * @param {jQuery} $field The field element
  133. * @param {String} type Can be 'tooltip' or 'popover'
  134. */
  135. _hideTooltip: function($field, type) {
  136. var $icon = $field.data('fv.icon');
  137. if ($icon) {
  138. $icon.popup('hide');
  139. }
  140. },
  141. /**
  142. * Show a tooltip or popover
  143. *
  144. * @param {jQuery} $field The field element
  145. * @param {String} type Can be 'tooltip' or 'popover'
  146. */
  147. _showTooltip: function($field, type) {
  148. var $icon = $field.data('fv.icon');
  149. if ($icon) {
  150. $icon.popup('show');
  151. }
  152. }
  153. });
  154. }(jQuery));