bootstrapSwitch.js 7.6 KB

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