bootstrapSwitch.js 7.7 KB

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