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.

366 lines
11 KiB

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