bootstrap-switch.js 9.1 KB

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