bootstrapSwitch.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* ============================================================
  2. * bootstrapSwitch v1.0 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 = ''; // reset value
  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.append($switchLeft);
  56. $div.append($label);
  57. $div.append($switchRight);
  58. $element.find('>div').addClass(
  59. $element.find('input').is(':checked') ? 'switch-on' : 'switch-off'
  60. );
  61. if ($element.find('input').is(':disabled'))
  62. $(this).addClass('deactivate');
  63. var changeStatus = function ($this) {
  64. $this.siblings('label').trigger('mousedown').trigger('mouseup').trigger('click');
  65. };
  66. $switchLeft.on('click', function (e) {
  67. changeStatus($(this));
  68. });
  69. $switchRight.on('click', function (e) {
  70. changeStatus($(this));
  71. });
  72. $element.find('input').on('change', function (e) {
  73. var $element = $(this).parent();
  74. e.preventDefault();
  75. e.stopImmediatePropagation();
  76. $element.css('left', '');
  77. if ($(this).is(':checked'))
  78. $element.removeClass('switch-off').addClass('switch-on');
  79. else $element.removeClass('switch-on').addClass('switch-off');
  80. if ($element.data('animated') !== false)
  81. $element.addClass("switch-animate");
  82. $element.parent().trigger('switch-change', {'el':$(this), 'value':$(this).is(':checked')})
  83. });
  84. $element.find('label').on('mousedown touchstart', function (e) {
  85. var $this = $(this);
  86. moving = false;
  87. e.preventDefault();
  88. e.stopImmediatePropagation();
  89. $this.closest('div').removeClass('switch-animate');
  90. if ($this.closest('.switch').is('.deactivate'))
  91. $this.unbind('click');
  92. else {
  93. $this.on('mousemove touchmove', function (e) {
  94. var $element = $(this).closest('.switch')
  95. , relativeX = (e.pageX || e.originalEvent.targetTouches[0].pageX) - $element.offset().left
  96. , percent = (relativeX / $element.width()) * 100
  97. , left = 25
  98. , right = 75;
  99. moving = true;
  100. if (percent < left)
  101. percent = left;
  102. else if (percent > right)
  103. percent = right;
  104. $element.find('>div').css('left', (percent - right) + "%")
  105. });
  106. $this.on('click touchend', function (e) {
  107. var $this = $(this)
  108. , $target = $(e.target)
  109. , $myCheckBox = $target.siblings('input');
  110. e.stopImmediatePropagation();
  111. e.preventDefault();
  112. $this.unbind('mouseleave');
  113. if (moving)
  114. $myCheckBox.prop('checked', !(parseInt($this.parent().css('left')) < -25));
  115. else $myCheckBox.prop("checked", !$myCheckBox.is(":checked"));
  116. moving = false;
  117. $myCheckBox.trigger('change');
  118. });
  119. $this.on('mouseleave', function (e) {
  120. var $this = $(this)
  121. , $myCheckBox = $this.siblings('input');
  122. e.preventDefault();
  123. e.stopImmediatePropagation();
  124. $this.unbind('mouseleave');
  125. $this.trigger('mouseup');
  126. $myCheckBox.prop('checked', !(parseInt($this.parent().css('left')) < -25)).trigger('change');
  127. });
  128. $this.on('mouseup', function (e) {
  129. e.stopImmediatePropagation();
  130. e.preventDefault();
  131. $(this).unbind('mousemove');
  132. });
  133. }
  134. });
  135. }
  136. );
  137. },
  138. toggleActivation:function () {
  139. $(this).toggleClass('deactivate');
  140. },
  141. toggleState:function (skipOnChange) {
  142. var $input = $(this).find('input:checkbox');
  143. $input.prop('checked', !$input.is(':checked')).trigger('change', skipOnChange);
  144. },
  145. setState:function (value, skipOnChange) {
  146. $(this).find('input:checkbox').prop('checked', value).trigger('change', skipOnChange);
  147. },
  148. status:function () {
  149. return $(this).find('input:checkbox').is(':checked');
  150. },
  151. destroy:function () {
  152. var $div = $(this).find('div')
  153. , $checkbox;
  154. $div.find(':not(input:checkbox)').remove();
  155. $checkbox = $div.children();
  156. $checkbox.unwrap().unwrap();
  157. $checkbox.unbind('change');
  158. return $checkbox;
  159. }
  160. };
  161. if (methods[method])
  162. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  163. else if (typeof method === 'object' || !method)
  164. return methods.init.apply(this, arguments);
  165. else
  166. $.error('Method ' + method + ' does not exist!');
  167. };
  168. }(jQuery);
  169. $(function () {
  170. $('.switch')['bootstrapSwitch']();
  171. });