bootstrap-switch.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* ============================================================
  2. * bootstrapSwitch v1.5 by Larentis Mattia @SpiritualGuru
  3. * http://www.larentis.eu/
  4. *
  5. * Enhanced for radiobuttons by Stein, Peter @BdMdesigN
  6. * http://www.bdmdesign.org/
  7. *
  8. * Project site:
  9. * http://www.larentis.eu/switch/
  10. * ============================================================
  11. * Licensed under the Apache License, Version 2.0
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. * ============================================================ */
  14. !function ($) {
  15. "use strict";
  16. $.fn['bootstrapSwitch'] = function (method) {
  17. var methods = {
  18. init: function () {
  19. return this.each(function () {
  20. var $element = $(this)
  21. , $div
  22. , $switchLeft
  23. , $switchRight
  24. , $label
  25. , $form = $element.closest('form')
  26. , myClasses = ""
  27. , classes = $element.attr('class')
  28. , color
  29. , moving
  30. , onLabel = "ON"
  31. , offLabel = "OFF"
  32. , icon = false;
  33. $.each(['switch-mini', 'switch-small', 'switch-large'], function (i, el) {
  34. if (classes.indexOf(el) >= 0)
  35. myClasses = el;
  36. });
  37. $element.addClass('has-switch');
  38. if ($element.data('on') !== undefined)
  39. color = "switch-" + $element.data('on');
  40. if ($element.data('on-label') !== undefined)
  41. onLabel = $element.data('on-label');
  42. if ($element.data('off-label') !== undefined)
  43. offLabel = $element.data('off-label');
  44. if ($element.data('icon') !== undefined)
  45. icon = $element.data('icon');
  46. $switchLeft = $('<span>')
  47. .addClass("switch-left")
  48. .addClass(myClasses)
  49. .addClass(color)
  50. .html(onLabel);
  51. color = '';
  52. if ($element.data('off') !== undefined)
  53. color = "switch-" + $element.data('off');
  54. $switchRight = $('<span>')
  55. .addClass("switch-right")
  56. .addClass(myClasses)
  57. .addClass(color)
  58. .html(offLabel);
  59. $label = $('<label>')
  60. .html("&nbsp;")
  61. .addClass(myClasses)
  62. .attr('for', $element.find('input').attr('id'));
  63. if (icon) {
  64. $label.html('<i class="icon icon-' + icon + '"></i>');
  65. }
  66. $div = $element.find('input').wrap($('<div>')).parent().data('animated', false);
  67. if ($element.data('animated') !== false)
  68. $div.addClass('switch-animate').data('animated', true);
  69. $div
  70. .append($switchLeft)
  71. .append($label)
  72. .append($switchRight);
  73. $element.find('>div').addClass(
  74. $element.find('input').is(':checked') ? 'switch-on' : 'switch-off'
  75. );
  76. if ($element.find('input').is(':disabled'))
  77. $(this).addClass('deactivate');
  78. var changeStatus = function ($this) {
  79. $this.siblings('label').trigger('mousedown').trigger('mouseup').trigger('click');
  80. };
  81. $element.on('keydown', function (e) {
  82. if (e.keyCode === 32) {
  83. e.stopImmediatePropagation();
  84. e.preventDefault();
  85. changeStatus($(e.target).find('span:first'));
  86. }
  87. });
  88. $switchLeft.on('click', function (e) {
  89. changeStatus($(this));
  90. });
  91. $switchRight.on('click', function (e) {
  92. changeStatus($(this));
  93. });
  94. $element.find('input').on('change', function (e, skipOnChange) {
  95. var $this = $(this)
  96. , $element = $this.parent()
  97. , thisState = $this.is(':checked')
  98. , state = $element.is('.switch-off');
  99. e.preventDefault();
  100. $element.css('left', '');
  101. if (state === thisState) {
  102. if (thisState)
  103. $element.removeClass('switch-off').addClass('switch-on');
  104. else $element.removeClass('switch-on').addClass('switch-off');
  105. if ($element.data('animated') !== false)
  106. $element.addClass("switch-animate");
  107. if (typeof skipOnChange === 'boolean' && skipOnChange)
  108. return;
  109. $element.parent().trigger('switch-change', {'el': $this, 'value': thisState})
  110. }
  111. });
  112. $element.find('label').on('mousedown touchstart', function (e) {
  113. var $this = $(this);
  114. moving = false;
  115. e.preventDefault();
  116. e.stopImmediatePropagation();
  117. $this.closest('div').removeClass('switch-animate');
  118. if ($this.closest('.has-switch').is('.deactivate'))
  119. $this.unbind('click');
  120. else {
  121. $this.on('mousemove touchmove', function (e) {
  122. var $element = $(this).closest('.switch')
  123. , relativeX = (e.pageX || e.originalEvent.targetTouches[0].pageX) - $element.offset().left
  124. , percent = (relativeX / $element.width()) * 100
  125. , left = 25
  126. , right = 75;
  127. moving = true;
  128. if (percent < left)
  129. percent = left;
  130. else if (percent > right)
  131. percent = right;
  132. $element.find('>div').css('left', (percent - right) + "%")
  133. });
  134. $this.on('click touchend', function (e) {
  135. var $this = $(this)
  136. , $target = $(e.target)
  137. , $myRadioCheckBox = $target.siblings('input');
  138. e.stopImmediatePropagation();
  139. e.preventDefault();
  140. $this.unbind('mouseleave');
  141. if (moving)
  142. $myRadioCheckBox.prop('checked', !(parseInt($this.parent().css('left')) < -25));
  143. else
  144. $myRadioCheckBox.prop("checked", !$myRadioCheckBox.is(":checked"));
  145. moving = false;
  146. $myRadioCheckBox.trigger('change');
  147. });
  148. $this.on('mouseleave', function (e) {
  149. var $this = $(this)
  150. , $myInputBox = $this.siblings('input');
  151. e.preventDefault();
  152. e.stopImmediatePropagation();
  153. $this.unbind('mouseleave');
  154. $this.trigger('mouseup');
  155. $myInputBox.prop('checked', !(parseInt($this.parent().css('left')) < -25)).trigger('change');
  156. });
  157. $this.on('mouseup', function (e) {
  158. e.stopImmediatePropagation();
  159. e.preventDefault();
  160. $(this).unbind('mousemove');
  161. });
  162. }
  163. });
  164. if ($form.data('bootstrapSwitch') !== 'injected') {
  165. $form.bind('reset', function () {
  166. setTimeout(function () {
  167. $form.find('.switch').each(function () {
  168. var $input = $(this).find('input');
  169. $input.prop('checked', $input.is(':checked')).trigger('change');
  170. });
  171. }, 1);
  172. });
  173. $form.data('bootstrapSwitch', 'injected');
  174. }
  175. }
  176. );
  177. },
  178. toggleActivation: function () {
  179. var $this = $(this);
  180. $this.toggleClass('deactivate');
  181. $this.find('input').prop('disabled', $this.is('.deactivate'));
  182. },
  183. isActive: function () {
  184. return !$(this).hasClass('deactivate');
  185. },
  186. setActive: function (active) {
  187. var $this = $(this);
  188. if (active) {
  189. $this.removeClass('deactivate');
  190. $this.find('input').prop('disabled', false);
  191. }
  192. else {
  193. $this.addClass('deactivate');
  194. $this.find('input').prop('disabled', true);
  195. }
  196. },
  197. toggleState: function (skipOnChange) {
  198. var $input = $(this).find('input[type=checkbox]');
  199. $input.prop('checked', !$input.is(':checked')).trigger('change', skipOnChange);
  200. },
  201. toggleRadioState: function (skipOnChange) {
  202. $(this).find('input[type=radio]').not(':checked').trigger('change', skipOnChange);
  203. },
  204. setState: function (value, skipOnChange) {
  205. $(this).find('input').prop('checked', value).trigger('change', skipOnChange);
  206. },
  207. status: function () {
  208. return $(this).find('input').is(':checked');
  209. },
  210. destroy: function () {
  211. var $element = $(this)
  212. , $div = $element.find('div')
  213. , $form = $element.closest('form')
  214. , $inputbox;
  215. $div.find(':not(input)').remove();
  216. $inputbox = $div.children();
  217. $inputbox.unwrap().unwrap();
  218. $inputbox.unbind('change');
  219. if ($form) {
  220. $form.unbind('reset');
  221. $form.removeData('bootstrapSwitch');
  222. }
  223. return $inputbox;
  224. }
  225. };
  226. if (methods[method])
  227. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  228. else if (typeof method === 'object' || !method)
  229. return methods.init.apply(this, arguments);
  230. else
  231. $.error('Method ' + method + ' does not exist!');
  232. };
  233. }(jQuery);
  234. (function () {
  235. jQuery('.switch')['bootstrapSwitch']();
  236. })();