bootstrap-switch.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*! ============================================================
  2. * bootstrapSwitch v1.8.1 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.bootstrap-switch.org/
  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'))
  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'))
  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. , $myInputBox = $this.siblings('input');
  149. e.stopImmediatePropagation();
  150. e.preventDefault();
  151. $this.unbind('mouseleave');
  152. if (moving)
  153. $myInputBox.prop('checked', !(parseInt($this.parent().css('left')) < -25));
  154. else
  155. $myInputBox.prop("checked", !$myInputBox.is(":checked"));
  156. moving = false;
  157. $myInputBox.trigger('change');
  158. });
  159. $this.on('mouseleave', function (e) {
  160. var $this = $(this)
  161. , $myInputBox = $this.siblings('input');
  162. e.preventDefault();
  163. e.stopImmediatePropagation();
  164. $this.unbind('mouseleave mousemove');
  165. $this.trigger('mouseup');
  166. $myInputBox.prop('checked', !(parseInt($this.parent().css('left')) < -25)).trigger('change');
  167. });
  168. $this.on('mouseup', function (e) {
  169. e.stopImmediatePropagation();
  170. e.preventDefault();
  171. $(this).trigger('mouseleave');
  172. });
  173. }
  174. });
  175. if ($form.data('bootstrapSwitch') !== 'injected') {
  176. $form.bind('reset', function () {
  177. setTimeout(function () {
  178. $form.find('.make-switch').each(function () {
  179. var $input = $(this).find(inputSelector);
  180. $input.prop('checked', $input.is(':checked')).trigger('change');
  181. });
  182. }, 1);
  183. });
  184. $form.data('bootstrapSwitch', 'injected');
  185. }
  186. }
  187. );
  188. },
  189. toggleActivation: function () {
  190. var $this = $(this);
  191. $this.toggleClass('deactivate');
  192. $this.find(inputSelector).prop('disabled', $this.is('.deactivate'));
  193. },
  194. isActive: function () {
  195. return !$(this).hasClass('deactivate');
  196. },
  197. setActive: function (active) {
  198. var $this = $(this);
  199. if (active) {
  200. $this.removeClass('deactivate');
  201. $this.find(inputSelector).removeAttr('disabled');
  202. }
  203. else {
  204. $this.addClass('deactivate');
  205. $this.find(inputSelector).attr('disabled', 'disabled');
  206. }
  207. },
  208. toggleState: function (skipOnChange) {
  209. var $input = $(this).find(':checkbox');
  210. $input.prop('checked', !$input.is(':checked')).trigger('change', skipOnChange);
  211. },
  212. toggleRadioState: function (skipOnChange) {
  213. var $radioinput = $(this).find(':radio');
  214. $radioinput.not(':checked').prop('checked', !$radioinput.is(':checked')).trigger('change', skipOnChange);
  215. },
  216. toggleRadioStateAllowUncheck: function (uncheck, skipOnChange) {
  217. var $radioinput = $(this).find(':radio');
  218. if (uncheck) {
  219. $radioinput.not(':checked').trigger('change', skipOnChange);
  220. }
  221. else {
  222. $radioinput.not(':checked').prop('checked', !$radioinput.is(':checked')).trigger('change', skipOnChange);
  223. }
  224. },
  225. setState: function (value, skipOnChange) {
  226. $(this).find(inputSelector).prop('checked', value).trigger('change', skipOnChange);
  227. },
  228. setOnLabel: function (value) {
  229. var $switchLeft = $(this).find(".switch-left");
  230. $switchLeft.html(value);
  231. },
  232. setOffLabel: function (value) {
  233. var $switchRight = $(this).find(".switch-right");
  234. $switchRight.html(value);
  235. },
  236. setOnClass: function (value) {
  237. var $switchLeft = $(this).find(".switch-left");
  238. var color = '';
  239. if (value) {
  240. // Delete the old color
  241. if ($(this).attr('data-on') !== undefined) {
  242. color = "switch-" + $(this).attr('data-on');
  243. }
  244. $switchLeft.removeClass(color);
  245. // Add the new color
  246. color = "switch-" + value;
  247. $switchLeft.addClass(color);
  248. }
  249. },
  250. setOffClass: function (value) {
  251. var $switchRight = $(this).find(".switch-right");
  252. var color = '';
  253. if (value) {
  254. // Delete the old color
  255. if ($(this).attr('data-off') !== undefined) {
  256. color = "switch-" + $(this).attr('data-off');
  257. }
  258. $switchRight.removeClass(color);
  259. // Add the new color
  260. color = "switch-" + value;
  261. $switchRight.addClass(color);
  262. }
  263. },
  264. setAnimated: function (value) {
  265. var $element = $(this).find(inputSelector).parent();
  266. if (value === undefined) value = false;
  267. $element.data('animated', value);
  268. $element.attr('data-animated', value);
  269. if ($element.data('animated') !== false) {
  270. $element.addClass("switch-animate");
  271. } else {
  272. $element.removeClass("switch-animate");
  273. }
  274. },
  275. setSizeClass: function (value) {
  276. var $element = $(this);
  277. var $switchLeft = $element.find(".switch-left");
  278. var $switchRight = $element.find(".switch-right");
  279. var $label = $element.find("label");
  280. $.each(['switch-mini', 'switch-small', 'switch-large'], function (i, el) {
  281. if (el !== value) {
  282. $switchLeft.removeClass(el);
  283. $switchRight.removeClass(el);
  284. $label.removeClass(el);
  285. } else {
  286. $switchLeft.addClass(el);
  287. $switchRight.addClass(el);
  288. $label.addClass(el);
  289. }
  290. });
  291. },
  292. setTextLabel: function(value) {
  293. var $label = $(this).find("label");
  294. $label.html('' + value || '&nbsp' + '');
  295. },
  296. setTextIcon: function(value) {
  297. var $label = $(this).find("label");''
  298. $label.html(value ? '<i class="icon ' + value + '"></i>' : '&nbsp;');
  299. },
  300. status: function () {
  301. return $(this).find(inputSelector).is(':checked');
  302. },
  303. destroy: function () {
  304. var $element = $(this)
  305. , $div = $element.find('div')
  306. , $form = $element.closest('form')
  307. , $inputbox;
  308. $div.find(':not(input)').remove();
  309. $inputbox = $div.children();
  310. $inputbox.unwrap().unwrap();
  311. $inputbox.unbind('change');
  312. if ($form) {
  313. $form.unbind('reset');
  314. $form.removeData('bootstrapSwitch');
  315. }
  316. return $inputbox;
  317. }
  318. };
  319. if (methods[method])
  320. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  321. else if (typeof method === 'object' || !method)
  322. return methods.init.apply(this, arguments);
  323. else
  324. $.error('Method ' + method + ' does not exist!');
  325. };
  326. }(jQuery);
  327. (function ($) {
  328. $(function () {
  329. $('.make-switch')['bootstrapSwitch']();
  330. });
  331. })(jQuery);