Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

350 lines
11 KiB

  1. // Generated by CoffeeScript 1.6.2
  2. (function() {
  3. var iOSCheckbox, matched, userAgent,
  4. __slice = [].slice;
  5. if ($.browser == null) {
  6. userAgent = navigator.userAgent || "";
  7. jQuery.uaMatch = function(ua) {
  8. var match;
  9. ua = ua.toLowerCase();
  10. match = /(chrome)[ \/]([\w.]+)/.exec(ua) || /(webkit)[ \/]([\w.]+)/.exec(ua) || /(opera)(?:.*version)?[ \/]([\w.]+)/.exec(ua) || /(msie) ([\w.]+)/.exec(ua) || ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+))?/.exec(ua) || [];
  11. return {
  12. browser: match[1] || "",
  13. version: match[2] || "0"
  14. };
  15. };
  16. matched = jQuery.uaMatch(userAgent);
  17. jQuery.browser = {};
  18. if (matched.browser) {
  19. jQuery.browser[matched.browser] = true;
  20. jQuery.browser.version = matched.version;
  21. }
  22. if (jQuery.browser.webkit) {
  23. jQuery.browser.safari = true;
  24. }
  25. }
  26. iOSCheckbox = (function() {
  27. function iOSCheckbox(elem, options) {
  28. var key, opts, value;
  29. this.elem = $(elem);
  30. opts = $.extend({}, iOSCheckbox.defaults, options);
  31. for (key in opts) {
  32. value = opts[key];
  33. this[key] = value;
  34. }
  35. this.elem.data(this.dataName, this);
  36. this.wrapCheckboxWithDivs();
  37. this.attachEvents();
  38. this.disableTextSelection();
  39. this.calculateDimensions();
  40. }
  41. iOSCheckbox.prototype.calculateDimensions = function() {
  42. if (this.resizeHandle) {
  43. this.optionallyResize('handle');
  44. }
  45. if (this.resizeContainer) {
  46. this.optionallyResize('container');
  47. }
  48. return this.initialPosition();
  49. };
  50. iOSCheckbox.prototype.isDisabled = function() {
  51. return this.elem.is(':disabled');
  52. };
  53. iOSCheckbox.prototype.wrapCheckboxWithDivs = function() {
  54. this.elem.wrap("<div class='" + this.containerClass + "' />");
  55. this.container = this.elem.parent();
  56. this.offLabel = $("<label class='" + this.labelOffClass + "'>\n <span>" + this.uncheckedLabel + "</span>\n</label>").appendTo(this.container);
  57. this.offSpan = this.offLabel.children('span');
  58. this.onLabel = $("<label class='" + this.labelOnClass + "'>\n <span>" + this.checkedLabel + "</span>\n</label>").appendTo(this.container);
  59. this.onSpan = this.onLabel.children('span');
  60. return this.handle = $("<div class='" + this.handleClass + "'>\n <div class='" + this.handleRightClass + "'>\n <div class='" + this.handleCenterClass + "' />\n </div>\n</div>").appendTo(this.container);
  61. };
  62. iOSCheckbox.prototype.disableTextSelection = function() {
  63. if ($.browser.msie) {
  64. return $([this.handle, this.offLabel, this.onLabel, this.container]).attr("unselectable", "on");
  65. }
  66. };
  67. iOSCheckbox.prototype._getDimension = function(elem, dimension) {
  68. if ($.fn.actual != null) {
  69. return elem.actual(dimension);
  70. } else {
  71. return elem[dimension]();
  72. }
  73. };
  74. iOSCheckbox.prototype.optionallyResize = function(mode) {
  75. var newWidth, offLabelWidth, offSpan, onLabelWidth, onSpan;
  76. onSpan = this.onLabel.find('span');
  77. onLabelWidth = this._getDimension(onSpan, "width");
  78. onLabelWidth += parseInt(onSpan.css('padding-left'), 10);
  79. offSpan = this.offLabel.find('span');
  80. offLabelWidth = this._getDimension(offSpan, "width");
  81. offLabelWidth += parseInt(offSpan.css('padding-right'), 10);
  82. if (mode === "container") {
  83. newWidth = onLabelWidth > offLabelWidth ? onLabelWidth : offLabelWidth;
  84. newWidth += this._getDimension(this.handle, "width") + this.handleMargin;
  85. return this.container.css({
  86. width: newWidth
  87. });
  88. } else {
  89. newWidth = onLabelWidth > offLabelWidth ? onLabelWidth : offLabelWidth;
  90. return this.handle.css({
  91. width: newWidth
  92. });
  93. }
  94. };
  95. iOSCheckbox.prototype.onMouseDown = function(event) {
  96. var x;
  97. event.preventDefault();
  98. if (this.isDisabled()) {
  99. return;
  100. }
  101. x = event.pageX || event.originalEvent.changedTouches[0].pageX;
  102. iOSCheckbox.currentlyClicking = this.handle;
  103. iOSCheckbox.dragStartPosition = x;
  104. return iOSCheckbox.handleLeftOffset = parseInt(this.handle.css('left'), 10) || 0;
  105. };
  106. iOSCheckbox.prototype.onDragMove = function(event, x) {
  107. var newWidth, p;
  108. if (iOSCheckbox.currentlyClicking !== this.handle) {
  109. return;
  110. }
  111. p = (x + iOSCheckbox.handleLeftOffset - iOSCheckbox.dragStartPosition) / this.rightSide;
  112. if (p < 0) {
  113. p = 0;
  114. }
  115. if (p > 1) {
  116. p = 1;
  117. }
  118. newWidth = p * this.rightSide;
  119. this.handle.css({
  120. left: newWidth
  121. });
  122. this.onLabel.css({
  123. width: newWidth + this.handleRadius
  124. });
  125. this.offSpan.css({
  126. marginRight: -newWidth
  127. });
  128. return this.onSpan.css({
  129. marginLeft: -(1 - p) * this.rightSide
  130. });
  131. };
  132. iOSCheckbox.prototype.onDragEnd = function(event, x) {
  133. var p;
  134. if (iOSCheckbox.currentlyClicking !== this.handle) {
  135. return;
  136. }
  137. if (this.isDisabled()) {
  138. return;
  139. }
  140. if (iOSCheckbox.dragging) {
  141. p = (x - iOSCheckbox.dragStartPosition) / this.rightSide;
  142. this.elem.prop('checked', p >= 0.5).change();
  143. } else {
  144. this.elem.prop('checked', !this.elem.prop('checked')).change();
  145. }
  146. iOSCheckbox.currentlyClicking = null;
  147. iOSCheckbox.dragging = null;
  148. if (typeof this.onChange === "function") {
  149. this.onChange(this.elem, this.elem.prop('checked'));
  150. }
  151. return this.didChange();
  152. };
  153. iOSCheckbox.prototype.refresh = function() {
  154. return this.didChange();
  155. };
  156. iOSCheckbox.prototype.didChange = function() {
  157. var new_left;
  158. if (this.isDisabled()) {
  159. this.container.addClass(this.disabledClass);
  160. return false;
  161. } else {
  162. this.container.removeClass(this.disabledClass);
  163. }
  164. new_left = this.elem.prop('checked') ? this.rightSide : 0;
  165. this.handle.animate({
  166. left: new_left
  167. }, this.duration);
  168. this.onLabel.animate({
  169. width: new_left + this.handleRadius
  170. }, this.duration);
  171. this.offSpan.animate({
  172. marginRight: -new_left
  173. }, this.duration);
  174. return this.onSpan.animate({
  175. marginLeft: new_left - this.rightSide
  176. }, this.duration);
  177. };
  178. iOSCheckbox.prototype.attachEvents = function() {
  179. var localMouseMove, localMouseUp, self;
  180. self = this;
  181. localMouseMove = function(event) {
  182. return self.onGlobalMove.apply(self, arguments);
  183. };
  184. localMouseUp = function(event) {
  185. self.onGlobalUp.apply(self, arguments);
  186. $(document).unbind('mousemove touchmove', localMouseMove);
  187. return $(document).unbind('mouseup touchend', localMouseUp);
  188. };
  189. this.elem.change(function() {
  190. return self.refresh();
  191. });
  192. return this.container.bind('mousedown touchstart', function(event) {
  193. self.onMouseDown.apply(self, arguments);
  194. $(document).bind('mousemove touchmove', localMouseMove);
  195. return $(document).bind('mouseup touchend', localMouseUp);
  196. });
  197. };
  198. iOSCheckbox.prototype.initialPosition = function() {
  199. var containerWidth, offset;
  200. containerWidth = this._getDimension(this.container, "width");
  201. this.offLabel.css({
  202. width: containerWidth - this.containerRadius
  203. });
  204. offset = this.containerRadius + 1;
  205. if ($.browser.msie && $.browser.version < 7) {
  206. offset -= 3;
  207. }
  208. this.rightSide = containerWidth - this._getDimension(this.handle, "width") - offset;
  209. if (this.elem.is(':checked')) {
  210. this.handle.css({
  211. left: this.rightSide
  212. });
  213. this.onLabel.css({
  214. width: this.rightSide + this.handleRadius
  215. });
  216. this.offSpan.css({
  217. marginRight: -this.rightSide
  218. });
  219. } else {
  220. this.onLabel.css({
  221. width: 0
  222. });
  223. this.onSpan.css({
  224. marginLeft: -this.rightSide
  225. });
  226. }
  227. if (this.isDisabled()) {
  228. return this.container.addClass(this.disabledClass);
  229. }
  230. };
  231. iOSCheckbox.prototype.onGlobalMove = function(event) {
  232. var x;
  233. if (!(!this.isDisabled() && iOSCheckbox.currentlyClicking)) {
  234. return;
  235. }
  236. event.preventDefault();
  237. x = event.pageX || event.originalEvent.changedTouches[0].pageX;
  238. if (!iOSCheckbox.dragging && (Math.abs(iOSCheckbox.dragStartPosition - x) > this.dragThreshold)) {
  239. iOSCheckbox.dragging = true;
  240. }
  241. return this.onDragMove(event, x);
  242. };
  243. iOSCheckbox.prototype.onGlobalUp = function(event) {
  244. var x;
  245. if (!iOSCheckbox.currentlyClicking) {
  246. return;
  247. }
  248. event.preventDefault();
  249. x = event.pageX || event.originalEvent.changedTouches[0].pageX;
  250. this.onDragEnd(event, x);
  251. return false;
  252. };
  253. iOSCheckbox.defaults = {
  254. duration: 200,
  255. checkedLabel: 'ON',
  256. uncheckedLabel: 'OFF',
  257. resizeHandle: true,
  258. resizeContainer: true,
  259. disabledClass: 'iPhoneCheckDisabled',
  260. containerClass: 'iPhoneCheckContainer',
  261. labelOnClass: 'iPhoneCheckLabelOn',
  262. labelOffClass: 'iPhoneCheckLabelOff',
  263. handleClass: 'iPhoneCheckHandle',
  264. handleCenterClass: 'iPhoneCheckHandleCenter',
  265. handleRightClass: 'iPhoneCheckHandleRight',
  266. dragThreshold: 5,
  267. handleMargin: 15,
  268. handleRadius: 4,
  269. containerRadius: 5,
  270. dataName: "iphoneStyle",
  271. onChange: function() {}
  272. };
  273. return iOSCheckbox;
  274. })();
  275. $.iphoneStyle = this.iOSCheckbox = iOSCheckbox;
  276. $.fn.iphoneStyle = function() {
  277. var args, checkbox, dataName, existingControl, method, params, _i, _len, _ref, _ref1, _ref2, _ref3;
  278. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  279. dataName = (_ref = (_ref1 = args[0]) != null ? _ref1.dataName : void 0) != null ? _ref : iOSCheckbox.defaults.dataName;
  280. _ref2 = this.filter(':checkbox');
  281. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  282. checkbox = _ref2[_i];
  283. existingControl = $(checkbox).data(dataName);
  284. if (existingControl != null) {
  285. method = args[0], params = 2 <= args.length ? __slice.call(args, 1) : [];
  286. if ((_ref3 = existingControl[method]) != null) {
  287. _ref3.apply(existingControl, params);
  288. }
  289. } else {
  290. new iOSCheckbox(checkbox, args[0]);
  291. }
  292. }
  293. return this;
  294. };
  295. $.fn.iOSCheckbox = function(options) {
  296. var opts;
  297. if (options == null) {
  298. options = {};
  299. }
  300. opts = $.extend({}, options, {
  301. resizeHandle: false,
  302. disabledClass: 'iOSCheckDisabled',
  303. containerClass: 'iOSCheckContainer',
  304. labelOnClass: 'iOSCheckLabelOn',
  305. labelOffClass: 'iOSCheckLabelOff',
  306. handleClass: 'iOSCheckHandle',
  307. handleCenterClass: 'iOSCheckHandleCenter',
  308. handleRightClass: 'iOSCheckHandleRight',
  309. dataName: 'iOSCheckbox'
  310. });
  311. return this.iphoneStyle(opts);
  312. };
  313. }).call(this);