wicked-ui.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2013 [email protected]
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. ;(function( $, window, document, undefined ){
  21. var pluginName = 'wickedtoolbar';
  22. var SHIFT = new RegExp(/shift/i);
  23. var ALT = new RegExp(/alt/i);
  24. var CTRL = new RegExp(/ctrl/i);
  25. var ARROW_DOWN = new RegExp(/↓/);
  26. var ARROW_UP = new RegExp(/↑/);
  27. var ARROW_LEFT = new RegExp(/←/);
  28. var ARROW_RIGHT = new RegExp(/→/);
  29. var keys = {
  30. shift: 16,
  31. alt: 17,
  32. ctrl: 18,
  33. meta: 91,
  34. arrow_up: 38,
  35. arrow_down: 40,
  36. arrow_left: 37,
  37. arrow_right: 39
  38. };
  39. var defaults = {
  40. hasIcon: function(id) { },
  41. getIcon: function(id) { }
  42. };
  43. function MenuBase(element, options) {
  44. var self = this;
  45. this.element = $(element);
  46. this.settings = $.extend({}, defaults, options);
  47. this.bindings = [];
  48. // for each menu item, modify and wrap in div
  49. this.element.find('li').each(function () {
  50. var tthis = $(this);
  51. // since the DOM is modifying wrt to icons, it can match the li[data-cion] from
  52. // the HTML definition, or the span.icon from the modified DOM
  53. var icons = tthis.closest('ul').find('li[data-icon], span.icon').length > 0;
  54. var tnode = tthis.contents().filter(function() { return this.nodeType == 3; }).first();
  55. var text = tnode.text().trim();
  56. // remove the text node
  57. tnode.remove();
  58. if (!text || !text.length) return; // not a regular menu item
  59. // change: <li>Text</li>
  60. // to: <li>
  61. // <a>
  62. // <span>Text</span>
  63. // </a>
  64. // </li>
  65. var li = tthis;
  66. var div = $('<a class="menu-item" href="#">');
  67. div.click(function(ev) {
  68. $(this).focus();
  69. // li.id > a
  70. var id = $(this).parent().attr('id');
  71. if (id) {
  72. self.element.trigger('selected', [id]);
  73. $(this).parents('.hover').removeClass('hover');
  74. }
  75. return false;
  76. });
  77. var span;
  78. if (self.settings._type == 'menu') {
  79. span = $('<span>' + text + '</span>');
  80. }
  81. else {
  82. span = $('<span></span>');
  83. }
  84. div.append(span);
  85. if (self.settings._type == 'menu') {
  86. // accesskey
  87. var accesskey = tthis.attr('accesskey');
  88. if (accesskey) {
  89. div.attr('accesskey', accesskey);
  90. tthis.removeAttr('accesskey');
  91. }
  92. // hotkey
  93. var hotkey = tthis.attr('data-hotkey');
  94. if (hotkey) {
  95. tthis.removeAttr('data-hotkey');
  96. div.append('<span class="hotkey">' + hotkey + '</span>');
  97. if (!accesskey) {
  98. // add our own handler
  99. var parts = hotkey.split('+');
  100. var bind = {};
  101. for (var i = 0; i < parts.length; ++i) {
  102. if (SHIFT.test(parts[i])) {
  103. bind.shiftKey = true;
  104. }
  105. else if (ALT.test(parts[i])) {
  106. bind.altKey = true;
  107. }
  108. else if (CTRL.test(parts[i])) {
  109. bind.ctrlKey = true;
  110. }
  111. else if (ARROW_DOWN.test(parts[i])) {
  112. bind.which = keys.arrow_down;
  113. }
  114. else if (ARROW_UP.test(parts[i])) {
  115. bind.which = keys.arrow_up;
  116. }
  117. else if (ARROW_RIGHT.test(parts[i])) {
  118. bind.which = keys.arrow_right;
  119. }
  120. else if (ARROW_LEFT.test(parts[i])) {
  121. bind.which = keys.arrow_left;
  122. }
  123. }
  124. bind.target = div;
  125. self.bindings.push(bind);
  126. }
  127. }
  128. }
  129. // icon
  130. var id = tthis.attr('id'), icon;
  131. if (self.settings.hasIcon(id)) {
  132. span.addClass('icon');
  133. icon = self.settings.getIcon(id);
  134. if (icon) {
  135. span.addClass(icon);
  136. }
  137. }
  138. icon = tthis.attr('data-icon');
  139. if (icon) {
  140. tthis.removeAttr('data-icon');
  141. span.addClass('icon ' + icon);
  142. }
  143. else if (icons) {
  144. span.addClass('icon');
  145. }
  146. li.prepend(div);
  147. });
  148. $(document).on('keydown', function(ev) {
  149. for (var i = 0; i < self.bindings.length; ++i) {
  150. var bind = self.bindings[i];
  151. // handle custom key events
  152. if ((bind.shiftKey === undefined ? true : (bind.shiftKey === ev.shiftKey)) &&
  153. (bind.ctrlKey === undefined ? true : (bind.ctrlKey === ev.ctrlKey)) &&
  154. (bind.altKey === undefined ? true : (bind.altKey === ev.altKey)) &&
  155. bind.which && ev.which && (bind.which === ev.which)) {
  156. bind.target.trigger('click');
  157. ev.preventDefault();
  158. }
  159. }
  160. });
  161. }
  162. MenuBase.prototype.update = function (id) {
  163. var li = this.element.find('#' + id), icon;
  164. var span = li.find('span:first-child');
  165. if (this.settings.hasIcon(id)) {
  166. span.removeClass(); // this could be brutally unfair
  167. span.addClass('icon');
  168. icon = this.settings.getIcon(id);
  169. if (icon) {
  170. span.addClass(icon);
  171. }
  172. }
  173. };
  174. // ------------
  175. // Menu
  176. // ------------
  177. function Menu(element, options) {
  178. options._type = 'menu';
  179. $.extend(this, new MenuBase(element, options)) ;
  180. this.constructor();
  181. }
  182. Menu.prototype.constructor = function () {
  183. this.element.addClass('wicked-ui wicked-menu');
  184. var self = this;
  185. var dohover = function(ev) {
  186. $(this).parent().addClass('hover');
  187. if ($(this).closest('ul').hasClass('wicked-menu')) {
  188. // if the closest ul is a 'menu' (i.e. if this item is a top-level menu), then
  189. // aply focus
  190. $(this).focus();
  191. }
  192. if (!self.accessing) {
  193. // Set 'accessing' to true and one document click to cancel it
  194. self.accessing = true;
  195. $(document).one('click', function() {
  196. if (!self.accessing) return;
  197. self.accessing = false;
  198. self.element.find('.hover').removeClass('hover');
  199. });
  200. }
  201. };
  202. this.element.find('> li > a.menu-item').click(dohover);
  203. this.element.find('> li > ul > li ul.drop-menu').each(function() {
  204. // li > a + ul
  205. $(this).prev('a.menu-item').addClass('icon-arrow-right');
  206. $(this).prev('a.menu-item').hover(dohover);
  207. $(this).prev('a.menu-item').mouseleave(
  208. function() {
  209. $(this).parent().removeClass('hover');
  210. }
  211. );
  212. });
  213. this.element.find('> li > a.menu-item').hover(
  214. function() {
  215. if (!self.accessing) return;
  216. self.element.find('.hover').removeClass('hover');
  217. $.proxy(dohover, this)();
  218. }
  219. );
  220. };
  221. // ------------
  222. // Toolbar
  223. // ------------
  224. function Toolbar(element, options) {
  225. options._type = 'toolbar';
  226. $.extend(this, new MenuBase(element, options)) ;
  227. this.constructor();
  228. }
  229. Toolbar.prototype.constructor = function () {
  230. this.element.addClass('wicked-ui wicked-toolbar');
  231. this.element.find('> li > a.menu-item').hover(
  232. function(){ $(this).parent().addClass('hover'); },
  233. function(){ $(this).parent().removeClass('hover'); }
  234. );
  235. };
  236. var plugins = { wickedmenu: Menu, wickedtoolbar: Toolbar };
  237. for (var key in plugins) {
  238. (function(name, Plugin){
  239. $.fn[name] = function (options) {
  240. var args = arguments;
  241. return this.each(function () {
  242. if (typeof options === 'object' || !options) {
  243. if (!$.data(this, 'plugin_' + name)) {
  244. $.data(this, 'plugin_' + name, new Plugin( this, options ));
  245. }
  246. }
  247. else {
  248. var d = $.data(this, 'plugin_' + name);
  249. if (!d) {
  250. $.error('jQuery.' + name + ' plugin does not exist');
  251. return;
  252. }
  253. return d[options](Array.prototype.slice.call(args, 1));
  254. }
  255. });
  256. };
  257. })(key, plugins[key])
  258. }
  259. })( jQuery, window, document );