bootstrapSwitch.js 8.4 KB

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