bootstrapSwitch.js 8.6 KB

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