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.

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