bootstrapSwitch.js 8.0 KB

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