bootstrap-switch.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*! ============================================================
  2. * bootstrapSwitch v1.7 by Larentis Mattia @SpiritualGuru
  3. * http://www.larentis.eu/
  4. *
  5. * Enhanced for radiobuttons by Stein, Peter @BdMdesigN
  6. * http://www.bdmdesign.org/
  7. *
  8. * Project site:
  9. * http://www.larentis.eu/switch/
  10. * ============================================================
  11. * Licensed under the Apache License, Version 2.0
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. * ============================================================ */
  14. !function ($) {
  15. "use strict";
  16. $.fn['bootstrapSwitch'] = function (method) {
  17. var inputSelector = 'input[type!="hidden"]';
  18. var methods = {
  19. init: function () {
  20. return this.each(function () {
  21. var $element = $(this)
  22. , $div
  23. , $switchLeft
  24. , $switchRight
  25. , $label
  26. , $form = $element.closest('form')
  27. , myClasses = ""
  28. , classes = $element.attr('class')
  29. , color
  30. , moving
  31. , onLabel = "ON"
  32. , offLabel = "OFF"
  33. , icon = false;
  34. $.each(['switch-mini', 'switch-small', 'switch-large'], function (i, el) {
  35. if (classes.indexOf(el) >= 0)
  36. myClasses = el;
  37. });
  38. $element.addClass('has-switch');
  39. if ($element.data('on') !== undefined)
  40. color = "switch-" + $element.data('on');
  41. if ($element.data('on-label') !== undefined)
  42. onLabel = $element.data('on-label');
  43. if ($element.data('off-label') !== undefined)
  44. offLabel = $element.data('off-label');
  45. if ($element.data('icon') !== undefined)
  46. icon = $element.data('icon');
  47. $switchLeft = $('<span>')
  48. .addClass("switch-left")
  49. .addClass(myClasses)
  50. .addClass(color)
  51. .html(onLabel);
  52. color = '';
  53. if ($element.data('off') !== undefined)
  54. color = "switch-" + $element.data('off');
  55. $switchRight = $('<span>')
  56. .addClass("switch-right")
  57. .addClass(myClasses)
  58. .addClass(color)
  59. .html(offLabel);
  60. $label = $('<label>')
  61. .html("&nbsp;")
  62. .addClass(myClasses)
  63. .attr('for', $element.find(inputSelector).attr('id'));
  64. if (icon) {
  65. $label.html('<i class="icon icon-' + icon + '"></i>');
  66. }
  67. $div = $element.find(inputSelector).wrap($('<div>')).parent().data('animated', false);
  68. if ($element.data('animated') !== false)
  69. $div.addClass('switch-animate').data('animated', true);
  70. $div
  71. .append($switchLeft)
  72. .append($label)
  73. .append($switchRight);
  74. $element.find('>div').addClass(
  75. $element.find(inputSelector).is(':checked') ? 'switch-on' : 'switch-off'
  76. );
  77. if ($element.find(inputSelector).is(':disabled'))
  78. $(this).addClass('deactivate');
  79. var changeStatus = function ($this) {
  80. $this.siblings('label').trigger('mousedown').trigger('mouseup').trigger('click');
  81. };
  82. $element.on('keydown', function (e) {
  83. if (e.keyCode === 32) {
  84. e.stopImmediatePropagation();
  85. e.preventDefault();
  86. changeStatus($(e.target).find('span:first'));
  87. }
  88. });
  89. $switchLeft.on('click', function (e) {
  90. changeStatus($(this));
  91. });
  92. $switchRight.on('click', function (e) {
  93. changeStatus($(this));
  94. });
  95. $element.find(inputSelector).on('change', function (e, skipOnChange) {
  96. var $this = $(this)
  97. , $element = $this.parent()
  98. , thisState = $this.is(':checked')
  99. , state = $element.is('.switch-off');
  100. e.preventDefault();
  101. $element.css('left', '');
  102. if (state === thisState) {
  103. if (thisState)
  104. $element.removeClass('switch-off').addClass('switch-on');
  105. else $element.removeClass('switch-on').addClass('switch-off');
  106. if ($element.data('animated') !== false)
  107. $element.addClass("switch-animate");
  108. if (typeof skipOnChange === 'boolean' && skipOnChange)
  109. return;
  110. $element.parent().trigger('switch-change', {'el': $this, 'value': thisState})
  111. }
  112. });
  113. $element.find('label').on('mousedown touchstart', function (e) {
  114. var $this = $(this);
  115. moving = false;
  116. e.preventDefault();
  117. e.stopImmediatePropagation();
  118. $this.closest('div').removeClass('switch-animate');
  119. if ($this.closest('.has-switch').is('.deactivate')) {
  120. $this.unbind('click');
  121. } else if ( $this.closest('.switch-on').parent().is('.radio-no-uncheck') ) {
  122. $this.unbind('click');
  123. } else {
  124. $this.on('mousemove touchmove', function (e) {
  125. var $element = $(this).closest('.make-switch')
  126. , relativeX = (e.pageX || e.originalEvent.targetTouches[0].pageX) - $element.offset().left
  127. , percent = (relativeX / $element.width()) * 100
  128. , left = 25
  129. , right = 75;
  130. moving = true;
  131. if (percent < left)
  132. percent = left;
  133. else if (percent > right)
  134. percent = right;
  135. $element.find('>div').css('left', (percent - right) + "%")
  136. });
  137. $this.on('click touchend', function (e) {
  138. var $this = $(this)
  139. , $target = $(e.target)
  140. , $myRadioCheckBox = $target.siblings('input');
  141. e.stopImmediatePropagation();
  142. e.preventDefault();
  143. $this.unbind('mouseleave');
  144. if (moving)
  145. $myRadioCheckBox.prop('checked', !(parseInt($this.parent().css('left')) < -25));
  146. else
  147. $myRadioCheckBox.prop("checked", !$myRadioCheckBox.is(":checked"));
  148. moving = false;
  149. $myRadioCheckBox.trigger('change');
  150. });
  151. $this.on('mouseleave', function (e) {
  152. var $this = $(this)
  153. , $myInputBox = $this.siblings('input');
  154. e.preventDefault();
  155. e.stopImmediatePropagation();
  156. $this.unbind('mouseleave');
  157. $this.trigger('mouseup');
  158. $myInputBox.prop('checked', !(parseInt($this.parent().css('left')) < -25)).trigger('change');
  159. });
  160. $this.on('mouseup', function (e) {
  161. e.stopImmediatePropagation();
  162. e.preventDefault();
  163. $(this).unbind('mousemove');
  164. });
  165. }
  166. });
  167. if ($form.data('bootstrapSwitch') !== 'injected') {
  168. $form.bind('reset', function () {
  169. setTimeout(function () {
  170. $form.find('.make-switch').each(function () {
  171. var $input = $(this).find(inputSelector);
  172. $input.prop('checked', $input.is(':checked')).trigger('change');
  173. });
  174. }, 1);
  175. });
  176. $form.data('bootstrapSwitch', 'injected');
  177. }
  178. }
  179. );
  180. },
  181. toggleActivation: function () {
  182. var $this = $(this);
  183. $this.toggleClass('deactivate');
  184. $this.find(inputSelector).prop('disabled', $this.is('.deactivate'));
  185. },
  186. isActive: function () {
  187. return !$(this).hasClass('deactivate');
  188. },
  189. setActive: function (active) {
  190. var $this = $(this);
  191. if (active) {
  192. $this.removeClass('deactivate');
  193. $this.find(inputSelector).removeAttr('disabled');
  194. }
  195. else {
  196. $this.addClass('deactivate');
  197. $this.find(inputSelector).attr('disabled', 'disabled');
  198. }
  199. },
  200. toggleState: function (skipOnChange) {
  201. var $input = $(this).find(':checkbox');
  202. $input.prop('checked', !$input.is(':checked')).trigger('change', skipOnChange);
  203. },
  204. toggleRadioState: function (skipOnChange) {
  205. var $radioinput = $(this).find(':radio');
  206. $radioinput.not(':checked').prop('checked', !$radioinput.is(':checked')).trigger('change', skipOnChange);
  207. },
  208. toggleRadioStateAllowUncheck: function (uncheck, skipOnChange) {
  209. var $radioinput = $(this).find(':radio');
  210. if (uncheck) {
  211. $radioinput.not(':checked').trigger('change', skipOnChange);
  212. }
  213. else {
  214. $radioinput.not(':checked').prop('checked', !$radioinput.is(':checked')).trigger('change', skipOnChange);
  215. }
  216. },
  217. setState: function (value, skipOnChange) {
  218. $(this).find(inputSelector).prop('checked', value).trigger('change', skipOnChange);
  219. },
  220. setOnLabel: function(value) {
  221. var $switchLeft = $(this).find(".switch-left");
  222. $switchLeft.html(value);
  223. },
  224. setOffLabel: function(value) {
  225. var $switchRight = $(this).find(".switch-right");
  226. $switchRight.html(value);
  227. },
  228. setOnClass: function(value) {
  229. var $switchLeft = $(this).find(".switch-left");
  230. var color = '';
  231. if (value !== undefined) {
  232. if ($(this).attr('data-on') !== undefined) {
  233. color = "switch-" + $(this).attr('data-on')
  234. }
  235. $switchLeft.removeClass(color);
  236. color = "switch-" + value;
  237. $switchLeft.addClass(color);
  238. }
  239. },
  240. setOffClass: function(value) {
  241. var $switchRight = $(this).find(".switch-right");
  242. var color = '';
  243. if (value !== undefined) {
  244. if ($(this).attr('data-off') !== undefined) {
  245. color = "switch-" + $(this).attr('data-off')
  246. }
  247. $switchRight.removeClass(color);
  248. color = "switch-" + value;
  249. $switchRight.addClass(color);
  250. }
  251. },
  252. setAnimated: function(value) {
  253. var $element = $(this).find('input[type!="hidden"]').parent();
  254. if (value === undefined) value = false;
  255. $element.data('animated', value);
  256. $element.attr('data-animated', value);
  257. if ($element.data('animated') !== false) {
  258. $element.addClass("switch-animate");
  259. } else {
  260. $element.removeClass("switch-animate");
  261. }
  262. },
  263. setSizeClass: function(value) {
  264. var $element = $(this);
  265. var $switchLeft = $element.find(".switch-left");
  266. var $switchRight = $element.find(".switch-right");
  267. var $label = $element.find("label");
  268. $.each(['switch-mini', 'switch-small', 'switch-large'], function (i, el) {
  269. if (el !== value) {
  270. $switchLeft.removeClass(el)
  271. $switchRight.removeClass(el);
  272. $label.removeClass(el);
  273. } else {
  274. $switchLeft.addClass(el);
  275. $switchRight.addClass(el);
  276. $label.addClass(el);
  277. }
  278. });
  279. },
  280. status: function () {
  281. return $(this).find(inputSelector).is(':checked');
  282. },
  283. destroy: function () {
  284. var $element = $(this)
  285. , $div = $element.find('div')
  286. , $form = $element.closest('form')
  287. , $inputbox;
  288. $div.find(':not(inputSelector)').remove();
  289. $inputbox = $div.children();
  290. $inputbox.unwrap().unwrap();
  291. $inputbox.unbind('change');
  292. if ($form) {
  293. $form.unbind('reset');
  294. $form.removeData('bootstrapSwitch');
  295. }
  296. return $inputbox;
  297. }
  298. };
  299. if (methods[method])
  300. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  301. else if (typeof method === 'object' || !method)
  302. return methods.init.apply(this, arguments);
  303. else
  304. $.error('Method ' + method + ' does not exist!');
  305. };
  306. }(jQuery);
  307. (function($) { // creates scope for $ sign assigned to jQuery
  308. $(function () { // on dom ready
  309. $('.make-switch')['bootstrapSwitch'](); // attach bootstrapswitch
  310. });
  311. })(jQuery);