Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Menu to choose from for tree views. |
| 3 | */ |
| 4 | define(['menu', 'match/treeitem'], function (menuClass, itemClass) { |
| 5 | "use strict"; |
| 6 | |
| 7 | return { |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 8 | |
| 9 | /** |
| 10 | * Create new menu object. |
| 11 | * Pass the match information object |
| 12 | * and the item parameters. |
| 13 | * |
| 14 | * @param info The match info object |
| 15 | * @param params The match menu items |
| 16 | * as an array of arrays. |
| 17 | */ |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 18 | create : function (list) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 19 | var obj = Object.create(menuClass) |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 20 | .upgradeTo(this) |
Akron | 0988d88 | 2017-11-10 16:13:12 +0100 | [diff] [blame] | 21 | ._init(list, {itemClass : itemClass}); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 22 | obj.limit(6); |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 23 | |
| 24 | var e = obj.element(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 25 | |
| 26 | // This is only domspecific |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 27 | e.addEventListener('blur', function (e) { |
Akron | d67d45b | 2017-05-18 21:47:38 +0200 | [diff] [blame] | 28 | this.menu.hide(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 29 | }); |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 30 | |
| 31 | e.setAttribute('id', 'treeMenu'); |
| 32 | |
| 33 | // Add menu to body |
| 34 | document.getElementsByTagName('body')[0].appendChild(e); |
| 35 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 36 | return obj; |
| 37 | }, |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * The match information object of the menu. |
| 41 | */ |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 42 | info :function (infoVar) { |
| 43 | if (infoVar !== undefined) |
| 44 | this._info = infoVar; |
| 45 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 46 | return this._info; |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 47 | }, |
| 48 | |
| 49 | // Attach menu to |
| 50 | attachTo : function (e) { |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 51 | |
| 52 | // this._attached = e; |
| 53 | this._repos(e); |
| 54 | this.slider().reInit(); |
| 55 | |
| 56 | /* |
| 57 | * This is a suboptimal scrolling solution, see |
| 58 | * see https://developer.mozilla.org/docs/Mozilla/Performance/ScrollLinkedEffects |
| 59 | */ |
| 60 | if (this._onscroll !== undefined) { |
| 61 | window.removeEventListener('scroll', this._onscroll); |
| 62 | }; |
| 63 | |
| 64 | this._onscroll = function () { |
| 65 | this._repos(e); |
| 66 | }.bind(this); |
| 67 | |
| 68 | window.addEventListener('scroll', this._onscroll); |
| 69 | }, |
| 70 | |
| 71 | |
| 72 | // Overwrite onHide method |
| 73 | onHide : function () { |
| 74 | |
| 75 | // Remove listener |
| 76 | if (this._onscroll !== undefined) { |
| 77 | window.removeEventListener('scroll', this._onscroll); |
| 78 | }; |
| 79 | }, |
| 80 | |
| 81 | _repos : function (e) { |
Akron | eaba63e | 2018-01-26 19:49:30 +0100 | [diff] [blame] | 82 | var bounding = e.getBoundingClientRect(); |
| 83 | this._element.style.left = bounding.left + "px"; |
Akron | 257aa85 | 2018-02-06 19:29:51 +0100 | [diff] [blame] | 84 | this._element.style.top = ( |
| 85 | bounding.top + |
| 86 | bounding.height - |
| 87 | this._element.clientHeight |
| 88 | ) + "px"; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 89 | } |
| 90 | }; |
| 91 | }); |