Debian Codegraph @

doc/jsdoc/menu_store.js.html

raw
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="utf-8">
5 <title>JSDoc: Source: menu_store.js</title>
6
7 <script src="scripts/prettify/prettify.js"> </script>
8 <script src="scripts/prettify/lang-css.js"> </script>
9 <!--[if lt IE 9]>
10 <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11 <![endif]-->
12 <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
13 <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
14 </head>
15
16 <body>
17
18 <div id="main">
19
20 <h1 class="page-title">Source: menu_store.js</h1>
21
22
23
24
25
26
27 <section>
28 <article>
29 <pre class="prettyprint source linenums"><code>/*************************************************************
30 *
31 * Copyright (c) 2015-2016 The MathJax Consortium
32 *
33 * Licensed under the Apache License, Version 2.0 (the "License");
34 * you may not use this file except in compliance with the License.
35 * You may obtain a copy of the License at
36 *
37 * http://www.apache.org/licenses/LICENSE-2.0
38 *
39 * Unless required by applicable law or agreed to in writing, software
40 * distributed under the License is distributed on an "AS IS" BASIS,
41 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42 * See the License for the specific language governing permissions and
43 * limitations under the License.
44 */
45 /**
46 * @fileoverview A store that maps HTML elements in a document to context menus.
47 *
48 * @author [email protected] (Volker Sorge)
49 */
50 /// &lt;reference path="context_menu.ts" />
51 /// &lt;reference path="menu_util.ts" />
52 var ContextMenu;
53 (function (ContextMenu) {
54 class MenuStore {
55 /**
56 * @constructor
57 * @param {ContextMenu} menu The context menu the store belongs to.
58 */
59 constructor(menu) {
60 this.store = [];
61 this.active = null;
62 this.counter = 0;
63 this.attachedClass = ContextMenu.HtmlClasses['ATTACHED'] + '_' +
64 ContextMenu.MenuUtil.counter();
65 this.taborder = true;
66 this.attrMap = {};
67 this.menu = menu;
68 }
69 /**
70 * Sets the new active store element if it exists in the store.
71 * @param {HTMLElement} element Element to be activated.
72 */
73 setActive(element) {
74 do {
75 if (this.store.indexOf(element) !== -1) {
76 this.active = element;
77 break;
78 }
79 element = element.parentNode;
80 } while (element);
81 }
82 /**
83 * @return {HTMLElement} The currently active store element, if one exists.
84 */
85 getActive() {
86 return this.active;
87 }
88 /**
89 * Returns next active element.
90 * If store is empty returns null and also unsets active element.
91 * If active is not set returns the first element of the store.
92 * @return {HTMLElement} The next element if it exists.
93 */
94 next() {
95 let length = this.store.length;
96 if (length === 0) {
97 this.active = null;
98 return null;
99 }
100 let index = this.store.indexOf(this.active);
101 index = index === -1 ? 0 : (index &lt; length - 1 ? index + 1 : 0);
102 this.active = this.store[index];
103 return this.active;
104 }
105 /**
106 * Returns previous active element.
107 * If store is empty returns null and also unsets active element.
108 * If active is not set returns the last element of the store.
109 * @return {HTMLElement} The previous element if it exists.
110 */
111 previous() {
112 let length = this.store.length;
113 if (length === 0) {
114 this.active = null;
115 return null;
116 }
117 let last = length - 1;
118 let index = this.store.indexOf(this.active);
119 index = index === -1 ? last : (index === 0 ? last : index - 1);
120 this.active = this.store[index];
121 return this.active;
122 }
123 /**
124 * Removes all elements in the store.
125 */
126 clear() {
127 this.remove(this.store);
128 }
129 /**
130 * Inserts DOM elements into the store.
131 * @param {HTMLElement|Array.&lt;HTMLElement>|NodeList} elementOrList Elements
132 * to insert.
133 */
134 insert(elementOrList) {
135 let elements = elementOrList instanceof HTMLElement ?
136 [elementOrList] : elementOrList;
137 for (let i = 0, element; element = elements[i]; i++) {
138 this.insertElement(element);
139 }
140 this.sort();
141 }
142 /**
143 * Removes DOM elements from the store.
144 * @param {HTMLElement|Array.&lt;HTMLElement>|NodeList} elementOrList Elements
145 * to remove.
146 */
147 remove(elementOrList) {
148 let elements = elementOrList instanceof HTMLElement ?
149 [elementOrList] : elementOrList;
150 for (let i = 0, element; element = elements[i]; i++) {
151 this.removeElement(element);
152 }
153 this.sort();
154 }
155 /**
156 * Sets if elements of the store are included in the taborder or not.
157 * @param {boolean} flag If true elements are in taborder, o/w not.
158 */
159 inTaborder(flag) {
160 if (this.taborder &amp;&amp; !flag) {
161 this.removeTaborder();
162 }
163 if (!this.taborder &amp;&amp; flag) {
164 this.insertTaborder();
165 }
166 this.taborder = flag;
167 }
168 /**
169 * Inserts all elements in the store into the tab order.
170 */
171 insertTaborder() {
172 if (this.taborder) {
173 this.insertTaborder_();
174 }
175 }
176 /**
177 * Removes all elements in the store from the tab order.
178 */
179 removeTaborder() {
180 if (this.taborder) {
181 this.removeTaborder_();
182 }
183 }
184 /**
185 * Adds a DOM element to the store.
186 * @param {HTMLElement} element The DOM element.
187 */
188 insertElement(element) {
189 if (element.classList.contains(this.attachedClass)) {
190 return;
191 }
192 element.classList.add(this.attachedClass);
193 if (this.taborder) {
194 this.addTabindex(element);
195 }
196 this.addEvents(element);
197 }
198 /**
199 * Removes a DOM element from the store.
200 * @param {HTMLElement} element The DOM element.
201 */
202 removeElement(element) {
203 if (!element.classList.contains(this.attachedClass)) {
204 return;
205 }
206 element.classList.remove(this.attachedClass);
207 if (this.taborder) {
208 this.removeTabindex(element);
209 }
210 this.removeEvents(element);
211 }
212 /**
213 * Sorts the elements in the store in DOM order.
214 */
215 sort() {
216 let nodes = document.getElementsByClassName(this.attachedClass);
217 this.store = [].slice.call(nodes);
218 }
219 /**
220 * Inserts all elements in the store into the tab order.
221 */
222 insertTaborder_() {
223 this.store.forEach(x => x.setAttribute('tabindex', '0'));
224 }
225 /**
226 * Removes all elements in the store from the tab order.
227 */
228 removeTaborder_() {
229 this.store.forEach(x => x.setAttribute('tabindex', '-1'));
230 }
231 /**
232 * Adds tabindex to an element and possibly safes an existing one.
233 * @param {HTMLElement} element The DOM element.
234 */
235 addTabindex(element) {
236 if (element.hasAttribute('tabindex')) {
237 element.setAttribute(ContextMenu.HtmlAttrs['OLDTAB'], element.getAttribute('tabindex'));
238 }
239 element.setAttribute('tabindex', '0');
240 }
241 /**
242 * Removes tabindex from element or restores an old one.
243 * @param {HTMLElement} element The DOM element.
244 */
245 removeTabindex(element) {
246 if (element.hasAttribute(ContextMenu.HtmlAttrs['OLDTAB'])) {
247 element.setAttribute('tabindex', element.getAttribute(ContextMenu.HtmlAttrs['OLDTAB']));
248 element.removeAttribute(ContextMenu.HtmlAttrs['OLDTAB']);
249 }
250 else {
251 element.removeAttribute('tabindex');
252 }
253 }
254 //// TODO: Need to add touch event.
255 /**
256 * Adds event listeners to activate the context menu to an element.
257 *
258 * To be able to remove event listeners we have to remember exactly which
259 * listeners we have added. We safe them in the attribute mapping attrMap,
260 * as a combination of event handler name and counter, which is unique for
261 * each HTML element. The counter is stored on the HTML element in an
262 * attribute.
263 *
264 * @param {HTMLElement} element The DOM element.
265 */
266 addEvents(element) {
267 if (element.hasAttribute(ContextMenu.HtmlAttrs['COUNTER'])) {
268 return;
269 }
270 this.addEvent(element, 'contextmenu', this.menu.post.bind(this.menu));
271 this.addEvent(element, 'keydown', this.keydown.bind(this));
272 element.setAttribute(ContextMenu.HtmlAttrs['COUNTER'], this.counter.toString());
273 this.counter++;
274 }
275 /**
276 * Adds a single event listeners and stores them in the attribute mapping.
277 * @param {HTMLElement} element The DOM element.
278 * @param {string} name The name of the event handler.
279 * @param {EventListener} func The event listener.
280 */
281 addEvent(element, name, func) {
282 let attrName = ContextMenu.HtmlAttrs[name.toUpperCase() + 'FUNC'];
283 this.attrMap[attrName + this.counter] = func;
284 element.addEventListener(name, func);
285 }
286 /**
287 * Removes event listeners that activate the context menu from an element.
288 * @param {HTMLElement} element The DOM element.
289 */
290 removeEvents(element) {
291 if (!element.hasAttribute(ContextMenu.HtmlAttrs['COUNTER'])) {
292 return;
293 }
294 let counter = element.getAttribute(ContextMenu.HtmlAttrs['COUNTER']);
295 this.removeEvent(element, 'contextmenu', counter);
296 this.removeEvent(element, 'keydown', counter);
297 element.removeAttribute(ContextMenu.HtmlAttrs['COUNTER']);
298 }
299 /**
300 * Removes a single event listeners from an HTML element.
301 * @param {HTMLElement} element The DOM element.
302 * @param {string} name The name of the event handler.
303 * @param {string} counter The unique counter to identify the handler in the
304 * attribute mappings.
305 */
306 removeEvent(element, name, counter) {
307 let attrName = ContextMenu.HtmlAttrs[name.toUpperCase() + 'FUNC'];
308 let menuFunc = this.attrMap[attrName + counter];
309 element.removeEventListener(name, menuFunc);
310 }
311 /**
312 * Deals with key down keyboard events.
313 * @param {KeyboardEvent} event The keyboard event.
314 */
315 keydown(event) {
316 if (event.keyCode === ContextMenu.KEY.SPACE) {
317 this.menu.post(event);
318 }
319 }
320 }
321 ContextMenu.MenuStore = MenuStore;
322 })(ContextMenu || (ContextMenu = {}));
323 </code></pre>
324 </article>
325 </section>
326
327
328
329
330 </div>
331
332 <nav>
333 <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AbstractEntry.html">AbstractEntry</a></li><li><a href="AbstractItem.html">AbstractItem</a></li><li><a href="AbstractMenu.html">AbstractMenu</a></li><li><a href="AbstractNavigatable.html">AbstractNavigatable</a></li><li><a href="AbstractPostable.html">AbstractPostable</a></li><li><a href="Checkbox.html">Checkbox</a></li><li><a href="CloseButton.html">CloseButton</a></li><li><a href="Command.html">Command</a></li><li><a href="ContextMenu.html">ContextMenu</a></li><li><a href="Info.html">Info</a></li><li><a href="Label.html">Label</a></li><li><a href="MenuElement.html">MenuElement</a></li><li><a href="MenuStore.html">MenuStore</a></li><li><a href="Popup.html">Popup</a></li><li><a href="Radio.html">Radio</a></li><li><a href="Rule.html">Rule</a></li><li><a href="SubMenu.html">SubMenu</a></li><li><a href="Submenu_.html">Submenu</a></li><li><a href="Variable.html">Variable</a></li><li><a href="VariablePool.html">VariablePool</a></li></ul><h3>Namespaces</h3><ul><li><a href="CssStyles.html">CssStyles</a></li><li><a href="MenuUtil.html">MenuUtil</a></li></ul><h3>Interfaces</h3><ul><li><a href="Element.html">Element</a></li><li><a href="Entry.html">Entry</a></li><li><a href="Item.html">Item</a></li><li><a href="KeyNavigatable.html">KeyNavigatable</a></li><li><a href="Menu.html">Menu</a></li><li><a href="MouseNavigatable.html">MouseNavigatable</a></li><li><a href="Postable.html">Postable</a></li><li><a href="TouchNavigatable.html">TouchNavigatable</a></li><li><a href="VariableItem.html">VariableItem</a></li></ul><h3>Global</h3><ul><li><a href="global.html#HtmlAttrs">HtmlAttrs</a></li><li><a href="global.html#HtmlClasses">HtmlClasses</a></li><li><a href="global.html#KEY">KEY</a></li><li><a href="global.html#MOUSE">MOUSE</a></li><li><a href="global.html#TOUCH">TOUCH</a></li></ul>
334 </nav>
335
336 <br class="clear">
337
338 <footer>
339 Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Aug 17 2016 12:38:38 GMT+0100 (BST)
340 </footer>
341
342 <script> prettyPrint(); </script>
343 <script src="scripts/linenumber.js"> </script>
344 </body>
345 </html>