iphone.check.js 9.0 KB

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