jquery.ibutton.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. ;(function($) {
  2. if ($.fn.iButton) {
  3. return;
  4. }
  5. $.fn.iButton = function(settings) {
  6. var defaults = {
  7. id: null,
  8. className: null,
  9. title: null,
  10. text: "",
  11. icon: null,
  12. disabled: null
  13. };
  14. // give settings to UI elements
  15. var opts = $.extend(defaults, settings);
  16. // elements used for generate a new jQuery objects
  17. var elements = [];
  18. for (i = 0; i < this.length; i++) {
  19. // if not a button, just backup it.
  20. if (!this.eq(i).is("input[type='button'], button")) {
  21. elements.push(this.eq(i)[0]);
  22. continue;
  23. }
  24. // if a button, do ibutton logic.
  25. var $this = this.eq(i),
  26. id = opts.id || $this.attr("id"),
  27. // "attrClassName" for fixing IE6/7 getAttribute("classname") bug.
  28. attrClassName = $this[0].attributes.classname ? $this[0].attributes.classname.value : null,
  29. cssClassName = $this.attr("class"),
  30. className = (opts.className || attrClassName || "i-button-default") +
  31. (cssClassName ? " " + cssClassName : ""),
  32. title = opts.title || $this.attr("title"),
  33. text = opts.text || ($this.is("button") ? $this.text() : $this.val()),
  34. icon = opts.icon || $this.attr("icon"),
  35. disabled = (opts.disabled === null ? $this.attr("disabled") : opts.disabled);
  36. var $button = $(
  37. '<div tabindex="0"' + (id ? (' id="' + id + '"') : '') +
  38. ' class="i-button ' + className + '"' +
  39. (title ? (' title="' + title + '"') : '') +
  40. (' data-disabled="' + (disabled ? 'true' : 'false') + '">') +
  41. '<div class="i-button-state' + (disabled ? ' i-button-disabled' : '') + '">' +
  42. (icon ? ('<div class="i-button-icon ' + icon +
  43. (disabled ? (' ' + icon + '-disabled') : '') + '"></div>') : '') +
  44. '<div class="i-button-inner">' +
  45. '<div class="i-button-left' + (disabled ? ' i-button-left-disabled' : '') + '">' +
  46. '<div class="i-button-text' + (disabled ? ' i-button-text-disabled' : '') + '">' +
  47. text +
  48. '</div>' +
  49. '</div>' +
  50. '<div class="i-button-right' + (disabled ? ' i-button-right-disabled' : '') + '"></div>' +
  51. '</div>' +
  52. '</div>' +
  53. '</div>'
  54. );
  55. // replace the native button to ibutton object.
  56. $this.replaceWith($button);
  57. elements.push($button[0]);
  58. // save icon setting
  59. if (icon) {
  60. $button.data("ibutton-icon", icon);
  61. }
  62. // bind button interactive effect.
  63. $button
  64. .hover(
  65. function(event) {
  66. var $this = $(this), icon = $this.data("ibutton-icon"),
  67. disabled = $this.attr("data-disabled") == "true";
  68. if (disabled) {
  69. return;
  70. }
  71. $("div.i-button-state", $this).addClass("i-button-hover");
  72. $("div.i-button-left", $this).addClass("i-button-left-hover");
  73. $("div.i-button-right", $this).addClass("i-button-right-hover");
  74. $("div.i-button-text", $this).addClass("i-button-text-hover");
  75. if (icon) {
  76. $("div.i-button-icon", $this).addClass(icon + "-hover");
  77. }
  78. },
  79. function(event) {
  80. var $this = $(this), icon = $this.data("ibutton-icon"),
  81. disabled = $this.attr("data-disabled") == "true";
  82. if (disabled) {
  83. return;
  84. }
  85. $("div.i-button-state", $this).removeClass("i-button-hover");
  86. $("div.i-button-left", $this).removeClass("i-button-left-hover");
  87. $("div.i-button-right", $this).removeClass("i-button-right-hover");
  88. $("div.i-button-text", $this).removeClass("i-button-text-hover");
  89. if (icon) {
  90. $("div.i-button-icon", $this).removeClass(icon + "-hover");
  91. }
  92. }
  93. )
  94. .focus(function(event) {
  95. var $this = $(this), icon = $this.data("ibutton-icon"),
  96. disabled = $this.attr("data-disabled") == "true";
  97. if (disabled) {
  98. return;
  99. }
  100. $("div.i-button-state", $this).addClass("i-button-focus");
  101. $("div.i-button-left", $this).addClass("i-button-left-focus");
  102. $("div.i-button-right", $this).addClass("i-button-right-focus");
  103. $("div.i-button-text", $this).addClass("i-button-text-focus");
  104. if (icon) {
  105. $("div.i-button-icon", $this).addClass(icon + "-focus");
  106. }
  107. })
  108. .bind("blur focusout", function(event) {
  109. var $this = $(this), icon = $this.data("ibutton-icon"),
  110. disabled = $this.attr("data-disabled") == "true";
  111. if (disabled) {
  112. return;
  113. }
  114. $("div.i-button-state", $this).removeClass("i-button-focus");
  115. $("div.i-button-left", $this).removeClass("i-button-left-focus");
  116. $("div.i-button-right", $this).removeClass("i-button-right-focus");
  117. $("div.i-button-text", $this).removeClass("i-button-text-focus");
  118. if (icon) {
  119. $("div.i-button-icon", $this).removeClass(icon + "-focus");
  120. }
  121. })
  122. .mousedown(function(event) {
  123. var $this = $(this), icon = $this.data("ibutton-icon"),
  124. disabled = $this.attr("data-disabled") == "true";
  125. if (disabled) {
  126. return;
  127. }
  128. $("div.i-button-state", $this).addClass("i-button-active");
  129. $("div.i-button-left", $this).addClass("i-button-left-active");
  130. $("div.i-button-right", $this).addClass("i-button-right-active");
  131. $("div.i-button-text", $this).addClass("i-button-text-active");
  132. if (icon) {
  133. $("div.i-button-icon", $this).addClass(icon + "-active");
  134. }
  135. })
  136. .bind("i-button-mouseup", function(event) {
  137. var $this = $(this), icon = $this.data("ibutton-icon"),
  138. disabled = $this.attr("data-disabled") == "true";
  139. if (disabled) {
  140. return;
  141. }
  142. $("div.i-button-state", $this).removeClass("i-button-active");
  143. $("div.i-button-left", $this).removeClass("i-button-left-active");
  144. $("div.i-button-right", $this).removeClass("i-button-right-active");
  145. $("div.i-button-text", $this).removeClass("i-button-text-active");
  146. if (icon) {
  147. $("div.i-button-icon", $this).removeClass(icon + "-active");
  148. }
  149. })
  150. .keypress(function(event) {
  151. var $this = $(this), icon = $this.data("ibutton-icon"),
  152. disabled = $this.attr("data-disabled") == "true";
  153. if (disabled) {
  154. return;
  155. }
  156. if (event.which == "13") {
  157. event.preventDefault();
  158. $this.trigger("click");
  159. }
  160. })
  161. .keydown(function(event) {
  162. var $this = $(this), icon = $this.data("ibutton-icon"),
  163. disabled = $this.attr("data-disabled") == "true";
  164. if (disabled) {
  165. return;
  166. }
  167. if (event.which == "32") {
  168. $("div.i-button-state", $this).addClass("i-button-active");
  169. $("div.i-button-left", $this).addClass("i-button-left-active");
  170. $("div.i-button-right", $this).addClass("i-button-right-active");
  171. $("div.i-button-text", $this).addClass("i-button-text-active");
  172. if (icon) {
  173. $("div.i-button-icon", $this).addClass(icon + "-active");
  174. }
  175. }
  176. })
  177. .keyup(function(event) {
  178. var $this = $(this), icon = $this.data("ibutton-icon"),
  179. disabled = $this.attr("data-disabled") == "true";
  180. if (disabled) {
  181. return;
  182. }
  183. if (event.which == "32") {
  184. $("div.i-button-state", $this).removeClass("i-button-active");
  185. $("div.i-button-left", $this).removeClass("i-button-left-active");
  186. $("div.i-button-right", $this).removeClass("i-button-right-active");
  187. $("div.i-button-text", $this).removeClass("i-button-text-active");
  188. if (icon) {
  189. $("div.i-button-icon", $this).removeClass(icon + "-active");
  190. }
  191. $this.trigger("click");
  192. }
  193. })
  194. // unselected trick
  195. .bind("selectstart", function() {
  196. return false;
  197. })
  198. .find("*").attr("unselectable", "on");
  199. }
  200. // generate a new jQuery objects.
  201. var $elements = $(elements);
  202. // inject jQuery click event to implement disabled logic
  203. var _click = $elements.click;
  204. $elements.click = function(fn) {
  205. _click.call(this, function(event) {
  206. var $this = $(this);
  207. if ($this.attr("data-disabled") == "false") {
  208. fn.call(this, event);
  209. }
  210. });
  211. return this;
  212. };
  213. // iButton text method.
  214. $elements.text = function(value) {
  215. var $iButtons = this.filter("div.i-button");
  216. if ($iButtons.length && typeof value == "undefined") {
  217. return $("div.i-button-text", $iButtons.eq(0)).text();
  218. }
  219. else if ($iButtons.length && typeof value != "undefined") {
  220. $("div.i-button-text", $iButtons).text(value + "");
  221. }
  222. return this;
  223. };
  224. // iButton icon method.
  225. $elements.icon = function(value) {
  226. var $iButtons = this.filter("div.i-button");
  227. if (typeof value == "undefined" && $iButtons.length) {
  228. return $iButtons.eq(0).data("ibutton-icon");
  229. }
  230. else {
  231. for (var i = 0; i < $iButtons.length; i++) {
  232. var $button = $iButtons.eq(i),
  233. $icon = $("div.i-button-icon", $button),
  234. icon = $button.data("ibutton-icon"),
  235. disabled = $button.attr("data-disabled") == "true";
  236. // set or remove icon
  237. if (value) {
  238. if ($icon.length) {
  239. $icon.removeClass(icon + " " + icon + "-hover " + icon + "-focus " + icon + "-active " + icon + "-disabled");
  240. }
  241. else {
  242. $icon = $('<div class="i-button-icon"></div>');
  243. $("div.i-button-inner", $button).before($icon);
  244. }
  245. $icon.addClass(value + (disabled ? (" " + value + "-disabled") : ""));
  246. if (!disabled) {
  247. var $state = $("div.i-button-state", $button),
  248. hovered = $state.hasClass("i-button-hover"),
  249. focused = $state.hasClass("i-button-focus"),
  250. actived = $state.hasClass("i-button-active");
  251. $icon.addClass((hovered ? (value + "-hover") : "") +
  252. (focused ? (" " + value + "-focus") : "") +
  253. (actived ? (" " + value + "-active") : ""));
  254. }
  255. $button.data("ibutton-icon", value + "");
  256. }
  257. else {
  258. if ($icon.length) {
  259. $icon.remove();
  260. $button.removeData("ibutton-icon");
  261. }
  262. }
  263. }
  264. }
  265. return this;
  266. };
  267. // iButton disabled method.
  268. $elements.disabled = function(value) {
  269. var $iButtons = this.filter("div.i-button");
  270. if (typeof value == "undefined" && $iButtons.length) {
  271. return $iButtons.eq(0).attr("data-disabled") == "true";
  272. }
  273. else {
  274. for (var i = 0; i < $iButtons.length; i++) {
  275. var $button = $iButtons.eq(i), icon = $button.data("ibutton-icon");
  276. if (value) {
  277. $("div.i-button-state", $button)
  278. .removeClass("i-button-hover i-button-focus i-button-active")
  279. .addClass("i-button-disabled");
  280. $("div.i-button-left", $button)
  281. .removeClass("i-button-left-hover i-button-left-focus i-button-left-active")
  282. .addClass("i-button-left-disabled");
  283. $("div.i-button-right", $button)
  284. .removeClass("i-button-right-hover i-button-right-focus i-button-right-active")
  285. .addClass("i-button-right-disabled");
  286. $("div.i-button-text", $button)
  287. .removeClass("i-button-text-hover i-button-text-focus i-button-text-active")
  288. .addClass("i-button-text-disabled");
  289. if (icon) {
  290. $("div.i-button-icon", $button)
  291. .removeClass(icon + "-hover " + icon + "-focus " + icon + "-active")
  292. .addClass(icon + "-disabled");
  293. }
  294. $button.attr("data-disabled", "true");
  295. }
  296. else {
  297. $("div.i-button-state", $button).removeClass("i-button-disabled");
  298. $("div.i-button-left", $button).removeClass("i-button-left-disabled");
  299. $("div.i-button-right", $button).removeClass("i-button-right-disabled");
  300. $("div.i-button-text", $button).removeClass("i-button-text-disabled");
  301. if (icon) {
  302. $("div.i-button-icon", $button).removeClass(icon + "-disabled");
  303. }
  304. $button.attr("data-disabled", "false");
  305. }
  306. }
  307. }
  308. return this;
  309. };
  310. // return the generate new jQuery objects.
  311. return $elements;
  312. };
  313. // tricky: document to handle mouse up event
  314. $(document).mouseup(function() {
  315. $("div.i-button").trigger("i-button-mouseup");
  316. });
  317. })(jQuery);