jquery.ibutton.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*!
  2. * iButton jQuery Plug-in
  3. *
  4. * Copyright 2011 Giva, Inc. (http://www.givainc.com/labs/)
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Date: 2011-07-26
  19. * Rev: 1.0.03
  20. */
  21. ;(function($){
  22. // set default options
  23. $.iButton = {
  24. version: "1.0.03",
  25. setDefaults: function(options){
  26. $.extend(defaults, options);
  27. }
  28. };
  29. $.fn.iButton = function(options) {
  30. var method = typeof arguments[0] == "string" && arguments[0];
  31. var args = method && Array.prototype.slice.call(arguments, 1) || arguments;
  32. // get a reference to the first iButton found
  33. var self = (this.length == 0) ? null : $.data(this[0], "iButton");
  34. // if a method is supplied, execute it for non-empty results
  35. if( self && method && this.length ){
  36. // if request a copy of the object, return it
  37. if( method.toLowerCase() == "object" ) return self;
  38. // if method is defined, run it and return either it's results or the chain
  39. else if( self[method] ){
  40. // define a result variable to return to the jQuery chain
  41. var result;
  42. this.each(function (i){
  43. // apply the method to the current element
  44. var r = $.data(this, "iButton")[method].apply(self, args);
  45. // if first iteration we need to check if we're done processing or need to add it to the jquery chain
  46. if( i == 0 && r ){
  47. // if this is a jQuery item, we need to store them in a collection
  48. if( !!r.jquery ){
  49. result = $([]).add(r);
  50. // otherwise, just store the result and stop executing
  51. } else {
  52. result = r;
  53. // since we're a non-jQuery item, just cancel processing further items
  54. return false;
  55. }
  56. // keep adding jQuery objects to the results
  57. } else if( !!r && !!r.jquery ){
  58. result = result.add(r);
  59. }
  60. });
  61. // return either the results (which could be a jQuery object) or the original chain
  62. return result || this;
  63. // everything else, return the chain
  64. } else return this;
  65. // initializing request (only do if iButton not already initialized)
  66. } else {
  67. // create a new iButton for each object found
  68. return this.each(function (){
  69. new iButton(this, options);
  70. });
  71. };
  72. };
  73. // count instances
  74. var counter = 0;
  75. // detect iPhone
  76. $.browser.iphone = (navigator.userAgent.toLowerCase().indexOf("iphone") > -1);
  77. var iButton = function (input, options){
  78. var self = this
  79. , $input = $(input)
  80. , id = ++counter
  81. , disabled = false
  82. , width = {}
  83. , mouse = {dragging: false, clicked: null}
  84. , dragStart = {position: null, offset: null, time: null }
  85. // make a copy of the options and use the metadata if provided
  86. , options = $.extend({}, defaults, options, (!!$.metadata ? $input.metadata() : {}))
  87. // check to see if we're using the default labels
  88. , bDefaultLabelsUsed = (options.labelOn == ON && options.labelOff == OFF)
  89. // set valid field types
  90. , allow = ":checkbox, :radio";
  91. // only do for checkboxes buttons, if matches inside that node
  92. if( !$input.is(allow) ) return $input.find(allow).iButton(options);
  93. // if iButton already exists, stop processing
  94. else if($.data($input[0], "iButton") ) return;
  95. // store a reference to this marquee
  96. $.data($input[0], "iButton", self);
  97. // if using the "auto" setting, then don't resize handle or container if using the default label (since we'll trust the CSS)
  98. if( options.resizeHandle == "auto" ) options.resizeHandle = !bDefaultLabelsUsed;
  99. if( options.resizeContainer == "auto" ) options.resizeContainer = !bDefaultLabelsUsed;
  100. // toggles the state of a button (or can turn on/off)
  101. this.toggle = function (t){
  102. var toggle = (arguments.length > 0) ? t : !$input[0].checked;
  103. $input.attr("checked", toggle).trigger("change");
  104. };
  105. // disable/enable the control
  106. this.disable = function (t){
  107. var toggle = (arguments.length > 0) ? t : !disabled;
  108. // mark the control disabled
  109. disabled = toggle;
  110. // mark the input disabled
  111. $input.attr("disabled", toggle);
  112. // set the diabled styles
  113. $container[toggle ? "addClass" : "removeClass"](options.classDisabled);
  114. // run callback
  115. if( $.isFunction(options.disable) ) options.disable.apply(self, [disabled, $input, options]);
  116. };
  117. // repaint the button
  118. this.repaint = function (){
  119. positionHandle();
  120. };
  121. // this will destroy the iButton style
  122. this.destroy = function (){
  123. // remove behaviors
  124. $([$input[0], $container[0]]).unbind(".iButton");
  125. $(document).unbind(".iButton_" + id);
  126. // move the checkbox to it's original location
  127. $container.after($input).remove();
  128. // kill the reference
  129. $.data($input[0], "iButton", null);
  130. // run callback
  131. if( $.isFunction(options.destroy) ) options.destroy.apply(self, [$input, options]);
  132. };
  133. $input
  134. // create the wrapper code
  135. .wrap('<div class="' + $.trim(options.classContainer + ' ' + options.className) + '" />')
  136. .after(
  137. '<div class="' + options.classHandle + '"><div class="' + options.classHandleRight + '"><div class="' + options.classHandleMiddle + '" /></div></div>'
  138. + '<div class="' + options.classLabelOff + '"><span><label>'+ options.labelOff + '</label></span></div>'
  139. + '<div class="' + options.classLabelOn + '"><span><label>' + options.labelOn + '</label></span></div>'
  140. + '<div class="' + options.classPaddingLeft + '"></div><div class="' + options.classPaddingRight + '"></div>'
  141. );
  142. var $container = $input.parent()
  143. , $handle = $input.siblings("." + options.classHandle)
  144. , $offlabel = $input.siblings("." + options.classLabelOff)
  145. , $offspan = $offlabel.children("span")
  146. , $onlabel = $input.siblings("." + options.classLabelOn)
  147. , $onspan = $onlabel.children("span");
  148. // if we need to do some resizing, get the widths only once
  149. if( options.resizeHandle || options.resizeContainer ){
  150. width.onspan = $onspan.outerWidth();
  151. width.offspan = $offspan.outerWidth();
  152. }
  153. // automatically resize the handle
  154. if( options.resizeHandle ){
  155. width.handle = Math.min(width.onspan, width.offspan);
  156. $handle.css("width", width.handle);
  157. } else {
  158. width.handle = $handle.width();
  159. }
  160. // automatically resize the control
  161. if( options.resizeContainer ){
  162. width.container = (Math.max(width.onspan, width.offspan) + width.handle + 20);
  163. $container.css("width", width.container);
  164. // adjust the off label to match the new container size
  165. $offlabel.css("width", width.container);
  166. } else {
  167. width.container = $container.width();
  168. }
  169. var handleRight = width.container - width.handle;
  170. var positionHandle = function (animate){
  171. var checked = $input[0].checked
  172. , x = (checked) ? handleRight : 0
  173. , animate = (arguments.length > 0) ? arguments[0] : true;
  174. if( animate && options.enableFx ){
  175. $handle.stop().animate({left: x}, options.duration, options.easing);
  176. $onlabel.stop().animate({width: x+30}, options.duration, options.easing);
  177. $onspan.stop().animate({marginLeft: x - handleRight}, options.duration, options.easing);
  178. $offspan.stop().animate({marginRight: -x}, options.duration, options.easing);
  179. } else {
  180. $handle.css("left", x);
  181. $onlabel.css("width", x+30);
  182. $onspan.css("marginLeft", x - handleRight);
  183. $offspan.css("marginRight", -x);
  184. }
  185. };
  186. // place the buttons in their default location
  187. positionHandle(false);
  188. var getDragPos = function(e){
  189. return e.pageX || ((e.originalEvent.changedTouches) ? e.originalEvent.changedTouches[0].pageX : 0);
  190. };
  191. // monitor mouse clicks in the container
  192. $container.bind("mousedown.iButton touchstart.iButton", function(e) {
  193. // abort if disabled or allow clicking the input to toggle the status (if input is visible)
  194. if( $(e.target).is(allow) || disabled || (!options.allowRadioUncheck && $input.is(":radio:checked")) ) return;
  195. e.preventDefault();
  196. mouse.clicked = $handle;
  197. dragStart.position = getDragPos(e);
  198. dragStart.offset = dragStart.position - (parseInt($handle.css("left"), 10) || 0);
  199. dragStart.time = (new Date()).getTime();
  200. return false;
  201. });
  202. // make sure dragging support is enabled
  203. if( options.enableDrag ){
  204. // monitor mouse movement on the page
  205. $(document).bind("mousemove.iButton_" + id + " touchmove.iButton_" + id, function(e) {
  206. // if we haven't clicked on the container, cancel event
  207. if( mouse.clicked != $handle ){ return }
  208. e.preventDefault();
  209. var x = getDragPos(e);
  210. if( x != dragStart.offset ){
  211. mouse.dragging = true;
  212. $container.addClass(options.classHandleActive);
  213. }
  214. // make sure number is between 0 and 1
  215. var pct = Math.min(1, Math.max(0, (x - dragStart.offset) / handleRight));
  216. $handle.css("left", pct * handleRight);
  217. $onlabel.css("width", pct * handleRight);
  218. $offspan.css("marginRight", -pct * handleRight);
  219. $onspan.css("marginLeft", -(1 - pct) * handleRight);
  220. return false;
  221. });
  222. }
  223. // monitor when the mouse button is released
  224. $(document).bind("mouseup.iButton_" + id + " touchend.iButton_" + id, function(e) {
  225. if( mouse.clicked != $handle ){ return false }
  226. e.preventDefault();
  227. // track if the value has changed
  228. var changed = true;
  229. // if not dragging or click time under a certain millisecond, then just toggle
  230. if( !mouse.dragging || (((new Date()).getTime() - dragStart.time) < options.clickOffset ) ){
  231. var checked = $input[0].checked;
  232. $input.attr("checked", !checked);
  233. // run callback
  234. if( $.isFunction(options.click) ) options.click.apply(self, [!checked, $input, options]);
  235. } else {
  236. var x = getDragPos(e);
  237. var pct = (x - dragStart.offset) / handleRight;
  238. var checked = (pct >= 0.5);
  239. // if the value is the same, don't run change event
  240. if( $input[0].checked == checked ) changed = false;
  241. $input.attr("checked", checked);
  242. }
  243. // remove the active handler class
  244. $container.removeClass(options.classHandleActive);
  245. mouse.clicked = null;
  246. mouse.dragging = null;
  247. // run any change event for the element
  248. if( changed ) $input.trigger("change");
  249. // if the value didn't change, just reset the handle
  250. else positionHandle();
  251. return false;
  252. });
  253. // animate when we get a change event
  254. $input
  255. .bind("change.iButton", function (){
  256. // move handle
  257. positionHandle();
  258. // if a radio element, then we must repaint the other elements in it's group to show them as not selected
  259. if( $input.is(":radio") ){
  260. var el = $input[0];
  261. // try to use the DOM to get the grouped elements, but if not in a form get by name attr
  262. var $radio = $(el.form ? el.form[el.name] : ":radio[name=" + el.name + "]");
  263. // repaint the radio elements that are not checked
  264. $radio.filter(":not(:checked)").iButton("repaint");
  265. }
  266. // run callback
  267. if( $.isFunction(options.change) ) options.change.apply(self, [$input, options]);
  268. })
  269. // if the element has focus, we need to highlight the container
  270. .bind("focus.iButton", function (){
  271. $container.addClass(options.classFocus);
  272. })
  273. // if the element has focus, we need to highlight the container
  274. .bind("blur.iButton", function (){
  275. $container.removeClass(options.classFocus);
  276. });
  277. // if a click event is registered, we must register on the checkbox so it's fired if triggered on the checkbox itself
  278. if( $.isFunction(options.click) ){
  279. $input.bind("click.iButton", function (){
  280. options.click.apply(self, [$input[0].checked, $input, options]);
  281. });
  282. }
  283. // if the field is disabled, mark it as such
  284. if( $input.is(":disabled") ) this.disable(true);
  285. // special behaviors for IE
  286. if( $.browser.msie ){
  287. // disable text selection in IE, other browsers are controlled via CSS
  288. $container.find("*").andSelf().attr("unselectable", "on");
  289. // IE needs to register to the "click" event to make changes immediately (the change event only occurs on blur)
  290. $input.bind("click.iButton", function (){ $input.triggerHandler("change.iButton"); });
  291. }
  292. // run the init callback
  293. if( $.isFunction(options.init) ) options.init.apply(self, [$input, options]);
  294. };
  295. var defaults = {
  296. duration: 200 // the speed of the animation
  297. , easing: "swing" // the easing animation to use
  298. , labelOn: "ON" // the text to show when toggled on
  299. , labelOff: "OFF" // the text to show when toggled off
  300. , resizeHandle: "auto" // determines if handle should be resized
  301. , resizeContainer: "auto" // determines if container should be resized
  302. , enableDrag: true // determines if we allow dragging
  303. , enableFx: true // determines if we show animation
  304. , allowRadioUncheck: false // determine if a radio button should be able to be unchecked
  305. , clickOffset: 120 // if millseconds between a mousedown & mouseup event this value, then considered a mouse click
  306. // define the class statements
  307. , className: ""
  308. , classContainer: "ibutton-container"
  309. , classDisabled: "ibutton-disabled"
  310. , classFocus: "ibutton-focus"
  311. , classLabelOn: "ibutton-label-on"
  312. , classLabelOff: "ibutton-label-off"
  313. , classHandle: "ibutton-handle"
  314. , classHandleMiddle: "ibutton-handle-middle"
  315. , classHandleRight: "ibutton-handle-right"
  316. , classHandleActive: "ibutton-active-handle"
  317. , classPaddingLeft: "ibutton-padding-left"
  318. , classPaddingRight: "ibutton-padding-right"
  319. // event handlers
  320. , init: null // callback that occurs when a iButton is initialized
  321. , change: null // callback that occurs when the button state is changed
  322. , click: null // callback that occurs when the button is clicked
  323. , disable: null // callback that occurs when the button is disabled/enabled
  324. , destroy: null // callback that occurs when the button is destroyed
  325. }, ON = defaults.labelOn, OFF = defaults.labelOff;
  326. })(jQuery);