bootstrap-switch.js 13 KB

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