jquery.customforms.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * jQuery Custom Forms Plugin 1.0
  3. * www.ZURB.com
  4. * Copyright 2010, ZURB
  5. * Free to use under the MIT license.
  6. * http://www.opensource.org/licenses/mit-license.php
  7. */
  8. jQuery.foundation = jQuery.foundation || {};
  9. jQuery.foundation.customForms = jQuery.foundation.customForms || {};
  10. jQuery(document).ready(function ($) {
  11. $.foundation.customForms.appendCustomMarkup = function (options) {
  12. var defaults = {
  13. disable_class: "js-disable-custom"
  14. };
  15. var options = $.extend(defaults, options);
  16. function appendCustomMarkup(idx, sel) {
  17. var $this = $(sel).hide(),
  18. type = $this.attr('type'),
  19. $span = $this.next('span.custom.' + type);
  20. if ($span.length === 0) {
  21. $span = $('<span class="custom ' + type + '"></span>').insertAfter($this);
  22. }
  23. $span.toggleClass('checked', $this.is(':checked'));
  24. $span.toggleClass('disabled', $this.is(':disabled'));
  25. }
  26. function appendCustomSelect(idx, sel) {
  27. var $this = $(sel),
  28. $customSelect = $this.next('div.custom.dropdown'),
  29. $options = $this.find('option'),
  30. $seloptions = $this.find('option:selected'),
  31. maxWidth = 0,
  32. $li;
  33. if ($this.hasClass('no-custom')) { return; }
  34. if ($customSelect.length === 0) {
  35. $customSelectSize = '';
  36. if ($(sel).hasClass('small')) {
  37. $customSelectSize = 'small';
  38. } else if ($(sel).hasClass('medium')) {
  39. $customSelectSize = 'medium';
  40. } else if ($(sel).hasClass('large')) {
  41. $customSelectSize = 'large';
  42. } else if ($(sel).hasClass('expand')) {
  43. $customSelectSize = 'expand';
  44. }
  45. $customSelect = $('<div class="custom dropdown ' + $customSelectSize + '"><a href="#" class="selector"></a><ul></ul></div>"');
  46. $options.each(function () {
  47. $li = $('<li>' + $(this).html() + '</li>');
  48. $customSelect.find('ul').append($li);
  49. });
  50. $customSelect.prepend('<a href="#" class="current">' + $seloptions.html() + '</a>');
  51. $this.after($customSelect);
  52. $this.hide();
  53. } else {
  54. // refresh the ul with options from the select in case the supplied markup doesn't match
  55. $customSelect.find('ul').html('');
  56. $options.each(function () {
  57. $li = $('<li>' + $(this).html() + '</li>');
  58. $customSelect.find('ul').append($li);
  59. });
  60. }
  61. $customSelect.toggleClass('disabled', $this.is(':disabled'));
  62. $options.each(function (index) {
  63. if (this.selected) {
  64. $customSelect.find('li').eq(index).addClass('selected');
  65. $customSelect.find('.current').html($(this).html());
  66. }
  67. });
  68. $customSelect.css('width', 'inherit');
  69. $customSelect.find('ul').css('width', 'inherit');
  70. $customSelect.find('li').each(function () {
  71. $customSelect.addClass('open');
  72. if ($(this).outerWidth() > maxWidth) {
  73. maxWidth = $(this).outerWidth();
  74. }
  75. $customSelect.removeClass('open');
  76. });
  77. if (!$customSelect.is('.small, .medium, .large, .expand')) {
  78. $customSelect.css('width', maxWidth + 18 + 'px');
  79. $customSelect.find('ul').css('width', maxWidth + 16 + 'px');
  80. }
  81. }
  82. $('form.custom input:radio[data-customforms!=disabled]').each(appendCustomMarkup);
  83. $('form.custom input:checkbox[data-customforms!=disabled]').each(appendCustomMarkup);
  84. $('form.custom select[data-customforms!=disabled]').each(appendCustomSelect);
  85. };
  86. });
  87. (function ($) {
  88. function refreshCustomSelect($select) {
  89. var maxWidth = 0,
  90. $customSelect = $select.next();
  91. $options = $select.find('option');
  92. $customSelect.find('ul').html('');
  93. $options.each(function () {
  94. $li = $('<li>' + $(this).html() + '</li>');
  95. $customSelect.find('ul').append($li);
  96. });
  97. // re-populate
  98. $options.each(function (index) {
  99. if (this.selected) {
  100. $customSelect.find('li').eq(index).addClass('selected');
  101. $customSelect.find('.current').html($(this).html());
  102. }
  103. });
  104. // fix width
  105. $customSelect.removeAttr('style')
  106. .find('ul').removeAttr('style');
  107. $customSelect.find('li').each(function () {
  108. $customSelect.addClass('open');
  109. if ($(this).outerWidth() > maxWidth) {
  110. maxWidth = $(this).outerWidth();
  111. }
  112. $customSelect.removeClass('open');
  113. });
  114. $customSelect.css('width', maxWidth + 18 + 'px');
  115. $customSelect.find('ul').css('width', maxWidth + 16 + 'px');
  116. }
  117. function toggleCheckbox($element) {
  118. var $input = $element.prev(),
  119. input = $input[0];
  120. if (false == $input.is(':disabled')) {
  121. input.checked = ((input.checked) ? false : true);
  122. $element.toggleClass('checked');
  123. $input.trigger('change');
  124. }
  125. }
  126. function toggleRadio($element) {
  127. var $input = $element.prev(),
  128. input = $input[0];
  129. if (false == $input.is(':disabled')) {
  130. $('input:radio[name="' + $input.attr('name') + '"]').each(function () {
  131. $(this).next().removeClass('checked');
  132. });
  133. input.checked = ((input.checked) ? false : true);
  134. $element.toggleClass('checked');
  135. $input.trigger('change');
  136. }
  137. }
  138. $('form.custom span.custom.checkbox').live('click', function (event) {
  139. event.preventDefault();
  140. event.stopPropagation();
  141. toggleCheckbox($(this));
  142. });
  143. $('form.custom span.custom.radio').live('click', function (event) {
  144. event.preventDefault();
  145. event.stopPropagation();
  146. toggleRadio($(this));
  147. });
  148. $('form.custom select').live('change', function (event) {
  149. refreshCustomSelect($(this));
  150. });
  151. $('form.custom label').live('click', function (event) {
  152. var $associatedElement = $('#' + $(this).attr('for')),
  153. $customCheckbox,
  154. $customRadio;
  155. if ($associatedElement.length !== 0) {
  156. if ($associatedElement.attr('type') === 'checkbox') {
  157. event.preventDefault();
  158. $customCheckbox = $(this).find('span.custom.checkbox');
  159. toggleCheckbox($customCheckbox);
  160. } else if ($associatedElement.attr('type') === 'radio') {
  161. event.preventDefault();
  162. $customRadio = $(this).find('span.custom.radio');
  163. toggleRadio($customRadio);
  164. }
  165. }
  166. });
  167. $('form.custom div.custom.dropdown a.current, form.custom div.custom.dropdown a.selector').live('click', function (event) {
  168. var $this = $(this),
  169. $dropdown = $this.closest('div.custom.dropdown'),
  170. $select = $dropdown.prev();
  171. event.preventDefault();
  172. $('div.dropdown').removeClass('open');
  173. if (false == $select.is(':disabled')) {
  174. $dropdown.toggleClass('open');
  175. if ($dropdown.hasClass('open')) {
  176. $(document).bind('click.customdropdown', function (event) {
  177. $dropdown.removeClass('open');
  178. $(document).unbind('.customdropdown');
  179. });
  180. } else {
  181. $(document).unbind('.customdropdown');
  182. }
  183. return false;
  184. }
  185. });
  186. $('form.custom div.custom.dropdown li').live('click', function (event) {
  187. var $this = $(this),
  188. $customDropdown = $this.closest('div.custom.dropdown'),
  189. $select = $customDropdown.prev(),
  190. selectedIndex = 0;
  191. event.preventDefault();
  192. event.stopPropagation();
  193. $('div.dropdown').removeClass('open');
  194. $this
  195. .closest('ul')
  196. .find('li')
  197. .removeClass('selected');
  198. $this.addClass('selected');
  199. $customDropdown
  200. .removeClass('open')
  201. .find('a.current')
  202. .html($this.html());
  203. $this.closest('ul').find('li').each(function (index) {
  204. if ($this[0] == this) {
  205. selectedIndex = index;
  206. }
  207. });
  208. $select[0].selectedIndex = selectedIndex;
  209. $select.trigger('change');
  210. });
  211. })(jQuery);