bootstrap-switch.js 13 KB

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