bootstrapSwitch.js 7.1 KB

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