PageRenderTime 331ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/yiiwheels/widgets/toggle/assets/js/jquery.toggle.buttons.js

https://github.com/vikaspa/uploadMultipleImage
JavaScript | 271 lines | 217 code | 47 blank | 7 comment | 42 complexity | 595807fa293a5e02ce53e4bc12daaa3c MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. !function ($) {
  2. "use strict";
  3. // version: 2.7
  4. // by Mattia Larentis - follow me on twitter! @SpiritualGuru
  5. var addToAttribute = function (obj, array, value) {
  6. var i = 0
  7. , length = array.length;
  8. for (; i < length; i++) {
  9. obj = obj[array[i]] = obj[array[i]] || i == ( length - 1) ? value : {}
  10. }
  11. };
  12. $.fn.toggleButtons = function (method) {
  13. var $element
  14. , $div
  15. , transitionSpeed = 0.05
  16. , methods = {
  17. init: function (opt) {
  18. this.each(function () {
  19. var $spanLeft
  20. , $spanRight
  21. , options
  22. , moving
  23. , dataAttribute = {};
  24. $element = $(this);
  25. $element.addClass('toggle-button');
  26. $.each($element.data(), function (i, el) {
  27. var key
  28. , tmp = {};
  29. if (i.indexOf("togglebutton") === 0) {
  30. key = i.match(/[A-Z][a-z]+/g);
  31. key = $.map(key, function (n) {
  32. return (n.toLowerCase());
  33. });
  34. addToAttribute(tmp, key, el);
  35. dataAttribute = $.extend(true, dataAttribute, tmp);
  36. }
  37. });
  38. options = $.extend(true, {}, $.fn.toggleButtons.defaults, opt, dataAttribute);
  39. $(this).data('options', options);
  40. $spanLeft = $('<span></span>').addClass("labelLeft").text(options.label.enabled === undefined ? "ON" : options.label.enabled);
  41. $spanRight = $('<span></span>').addClass("labelRight").text(options.label.disabled === undefined ? "OFF " : options.label.disabled);
  42. // html layout
  43. $div = $element.find('input:checkbox').wrap($('<div></div>')).parent();
  44. $div.append($spanLeft);
  45. $div.append($('<label></label>').attr('for', $element.find('input').attr('id')));
  46. $div.append($spanRight);
  47. if ($element.find('input').is(':checked'))
  48. $element.find('>div').css('left', "0");
  49. else $element.find('>div').css('left', "-50%");
  50. if (options.animated) {
  51. if (options.transitionspeed !== undefined)
  52. if (/^(\d*%$)/.test(options.transitionspeed)) // is a percent value?
  53. transitionSpeed = 0.05 * parseInt(options.transitionspeed) / 100;
  54. else
  55. transitionSpeed = options.transitionspeed;
  56. }
  57. else transitionSpeed = 0;
  58. $(this).data('transitionSpeed', transitionSpeed * 1000);
  59. options["width"] /= 2;
  60. // width of the bootstrap-toggle-button
  61. $element
  62. .css('width', options.width * 2)
  63. .find('>div').css('width', options.width * 3)
  64. .find('>span, >label').css('width', options.width);
  65. // height of the bootstrap-toggle-button
  66. $element
  67. .css('height', options.height)
  68. .find('span, label')
  69. .css('height', options.height)
  70. .filter('span')
  71. .css('line-height', options.height + "px");
  72. if ($element.find('input').is(':disabled'))
  73. $(this).addClass('deactivate');
  74. $element.find('span').css(options.font);
  75. // enabled custom color
  76. if (options.style.enabled === undefined) {
  77. if (options.style.custom !== undefined && options.style.custom.enabled !== undefined && options.style.custom.enabled.background !== undefined) {
  78. $spanLeft.css('color', options.style.custom.enabled.color);
  79. if (options.style.custom.enabled.gradient === undefined)
  80. $spanLeft.css('background', options.style.custom.enabled.background);
  81. else $.each(["-webkit-", "-moz-", "-o-", ""], function (i, el) {
  82. $spanLeft.css('background-image', el + 'linear-gradient(top, ' + options.style.custom.enabled.background + ',' + options.style.custom.enabled.gradient + ')');
  83. });
  84. }
  85. }
  86. else $spanLeft.addClass(options.style.enabled);
  87. // disabled custom color
  88. if (options.style.disabled === undefined) {
  89. if (options.style.custom !== undefined && options.style.custom.disabled !== undefined && options.style.custom.disabled.background !== undefined) {
  90. $spanRight.css('color', options.style.custom.disabled.color);
  91. if (options.style.custom.disabled.gradient === undefined)
  92. $spanRight.css('background', options.style.custom.disabled.background);
  93. else $.each(["-webkit-", "-moz-", "-o-", ""], function (i, el) {
  94. $spanRight.css('background-image', el + 'linear-gradient(top, ' + options.style.custom.disabled.background + ',' + options.style.custom.disabled.gradient + ')');
  95. });
  96. }
  97. }
  98. else $spanRight.addClass(options.style.disabled);
  99. var changeStatus = function ($this) {
  100. $this.siblings('label')
  101. .trigger('mousedown')
  102. .trigger('mouseup')
  103. .trigger('click');
  104. };
  105. $spanLeft.on('click', function (e) {
  106. changeStatus($(this));
  107. });
  108. $spanRight.on('click', function (e) {
  109. changeStatus($(this));
  110. });
  111. $('.toggle-button').find('input').on('change', function (e, skipOnChange) {
  112. var $element = $(this).parent()
  113. , active = $(this).is(':checked')
  114. , $toggleButton = $(this).closest('.toggle-button');
  115. e.preventDefault();
  116. e.stopImmediatePropagation();
  117. $element.stop().animate({'left': active ? '0' : '-50%'}, $toggleButton.data('transitionSpeed'));
  118. options = $toggleButton.data('options');
  119. if (!skipOnChange)
  120. options.onChange($element, active, e);
  121. });
  122. $('.toggle-button').find('label').on('mousedown touchstart', function (e) {
  123. moving = false;
  124. e.preventDefault();
  125. e.stopImmediatePropagation();
  126. if ($(this).closest('.toggle-button').is('.deactivate'))
  127. $(this).off('click');
  128. else {
  129. $(this).on('mousemove touchmove', function (e) {
  130. var $element = $(this).closest('.toggle-button')
  131. , relativeX = (e.pageX || e.originalEvent.targetTouches[0].pageX) - $element.offset().left
  132. , percent = ((relativeX / (options.width * 2)) * 100);
  133. moving = true;
  134. e.stopImmediatePropagation();
  135. e.preventDefault();
  136. if (percent < 25)
  137. percent = 25;
  138. else if (percent > 75)
  139. percent = 75;
  140. $element.find('>div').css('left', (percent - 75) + "%");
  141. });
  142. $(this).on('click touchend', function (e) {
  143. var $target = $(e.target)
  144. , $myCheckBox = $target.siblings('input');
  145. e.stopImmediatePropagation();
  146. e.preventDefault();
  147. $(this).off('mouseleave');
  148. if (moving)
  149. if (parseInt($(this).parent().css('left')) < -25)
  150. $myCheckBox.attr('checked', false);
  151. else $myCheckBox.attr('checked', true);
  152. else $myCheckBox.attr("checked", !$myCheckBox.is(":checked"));
  153. $myCheckBox.trigger('change');
  154. });
  155. $(this).on('mouseleave', function (e) {
  156. var $myCheckBox = $(this).siblings('input');
  157. e.preventDefault();
  158. e.stopImmediatePropagation();
  159. $(this).off('mouseleave');
  160. $(this).trigger('mouseup');
  161. if (parseInt($(this).parent().css('left')) < -25)
  162. $myCheckBox.attr('checked', false);
  163. else $myCheckBox.attr('checked', true);
  164. $myCheckBox.trigger('change');
  165. });
  166. $(this).on('mouseup', function (e) {
  167. e.stopImmediatePropagation();
  168. e.preventDefault();
  169. $(this).off('mousemove');
  170. });
  171. }
  172. });
  173. }
  174. );
  175. return this;
  176. },
  177. toggleActivation: function () {
  178. $(this).toggleClass('deactivate');
  179. },
  180. toggleState: function (skipOnChange) {
  181. var $input = $(this).find('input');
  182. $input.attr('checked', !$input.is(':checked')).trigger('change', skipOnChange);
  183. },
  184. setState: function(value, skipOnChange) {
  185. $(this).find('input').attr('checked', value).trigger('change', skipOnChange);
  186. }
  187. };
  188. if (methods[method])
  189. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  190. else if (typeof method === 'object' || !method)
  191. return methods.init.apply(this, arguments);
  192. else
  193. $.error('Method ' + method + ' does not exist!');
  194. };
  195. $.fn.toggleButtons.defaults = {
  196. onChange: function () {
  197. },
  198. width: 100,
  199. height: 25,
  200. font: {},
  201. animated: true,
  202. transitionspeed: undefined,
  203. label: {
  204. enabled: undefined,
  205. disabled: undefined
  206. },
  207. style: {
  208. enabled: undefined,
  209. disabled: undefined,
  210. custom: {
  211. enabled: {
  212. background: undefined,
  213. gradient: undefined,
  214. color: "#FFFFFF"
  215. },
  216. disabled: {
  217. background: undefined,
  218. gradient: undefined,
  219. color: "#FFFFFF"
  220. }
  221. }
  222. }
  223. };
  224. }(jQuery);