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.

364 lines
11 KiB

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