| 1 |
<!DOCTYPE html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<title>JSDoc: Source: context_menu.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: context_menu.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 Class of context menus. |
| 47 |
* |
| 48 |
* @author [email protected] (Volker Sorge) |
| 49 |
*/ |
| 50 |
/// <reference path="abstract_menu.ts" /> |
| 51 |
/// <reference path="html_classes.ts" /> |
| 52 |
/// <reference path="item.ts" /> |
| 53 |
/// <reference path="menu_store.ts" /> |
| 54 |
var ContextMenu; |
| 55 |
(function (ContextMenu_1) { |
| 56 |
class ContextMenu extends ContextMenu_1.AbstractMenu { |
| 57 |
/** |
| 58 |
* @constructor |
| 59 |
* @extends {AbstractMenu} |
| 60 |
*/ |
| 61 |
constructor() { |
| 62 |
super(); |
| 63 |
/** |
| 64 |
* Flag to avoid redoing taborder if we are between elements. |
| 65 |
* @type {boolean} |
| 66 |
*/ |
| 67 |
this.moving = false; |
| 68 |
/** |
| 69 |
* A store the menu belongs to. |
| 70 |
* @type {MenuStore} |
| 71 |
*/ |
| 72 |
this.store_ = new ContextMenu_1.MenuStore(this); |
| 73 |
/** |
| 74 |
* Registry of currently open widgets. |
| 75 |
* @type {Array.<Postable>} |
| 76 |
*/ |
| 77 |
this.widgets = []; |
| 78 |
this.variablePool = new ContextMenu_1.VariablePool(); |
| 79 |
} |
| 80 |
/** |
| 81 |
* Parses a JSON respresentation of a variable pool. |
| 82 |
* @param {JSON} json The JSON object to parse. |
| 83 |
* @return {ContextMenu} The new context menu object. |
| 84 |
*/ |
| 85 |
static parse({ menu: menu }) { |
| 86 |
if (!menu) { |
| 87 |
ContextMenu_1.MenuUtil.error(null, 'Wrong JSON format for menu.'); |
| 88 |
return; |
| 89 |
} |
| 90 |
// The variable id is currently ignored! |
| 91 |
let { pool: pool, items: items, id: id } = menu; |
| 92 |
let ctxtMenu = new ContextMenu(); |
| 93 |
// TODO: Try and catch with error |
| 94 |
pool.forEach(ctxtMenu.parseVariable.bind(ctxtMenu)); |
| 95 |
ctxtMenu.parseItems(items); |
| 96 |
return ctxtMenu; |
| 97 |
} |
| 98 |
/** |
| 99 |
* @override |
| 100 |
*/ |
| 101 |
generateHtml() { |
| 102 |
super.generateHtml(); |
| 103 |
this.frame = document.createElement('div'); |
| 104 |
this.frame.classList.add(ContextMenu_1.HtmlClasses['MENUFRAME']); |
| 105 |
//// TODO: Adapt to other browsers. |
| 106 |
let styleString = 'left: 0px; top: 0px; z-index: 200; width: 100%; ' + |
| 107 |
'height: 100%; border: 0px; padding: 0px; margin: 0px;'; |
| 108 |
this.frame.setAttribute('style', 'position: absolute; ' + styleString); |
| 109 |
let innerDiv = document.createElement('div'); |
| 110 |
innerDiv.setAttribute('style', 'position: fixed; ' + styleString); |
| 111 |
this.frame.appendChild(innerDiv); |
| 112 |
innerDiv.addEventListener('mousedown', function (event) { |
| 113 |
this.unpost(); |
| 114 |
this.unpostWidgets(); |
| 115 |
this.stop(event); |
| 116 |
}.bind(this)); |
| 117 |
} |
| 118 |
/** |
| 119 |
* @override |
| 120 |
*/ |
| 121 |
display() { |
| 122 |
document.body.appendChild(this.frame); |
| 123 |
this.frame.appendChild(this.getHtml()); |
| 124 |
this.focus(); |
| 125 |
} |
| 126 |
/** |
| 127 |
* @override |
| 128 |
*/ |
| 129 |
escape(event) { |
| 130 |
this.unpost(); |
| 131 |
this.unpostWidgets(); |
| 132 |
} |
| 133 |
/** |
| 134 |
* @override |
| 135 |
*/ |
| 136 |
unpost() { |
| 137 |
super.unpost(); |
| 138 |
if (this.widgets.length > 0) { |
| 139 |
return; |
| 140 |
} |
| 141 |
this.frame.parentNode.removeChild(this.frame); |
| 142 |
let store = this.getStore(); |
| 143 |
if (!this.moving) { |
| 144 |
store.insertTaborder(); |
| 145 |
} |
| 146 |
store.getActive().focus(); |
| 147 |
} |
| 148 |
/** |
| 149 |
* @override |
| 150 |
*/ |
| 151 |
left(event) { |
| 152 |
this.move_(this.store_.previous()); |
| 153 |
} |
| 154 |
/** |
| 155 |
* @override |
| 156 |
*/ |
| 157 |
right(event) { |
| 158 |
this.move_(this.store_.next()); |
| 159 |
} |
| 160 |
/** |
| 161 |
* @return {HTMLElement} The frame element wrapping all the elements of the |
| 162 |
* menu. |
| 163 |
*/ |
| 164 |
getFrame() { |
| 165 |
return this.frame; |
| 166 |
} |
| 167 |
/** |
| 168 |
* @return {MenuStore} The store of this menu. |
| 169 |
*/ |
| 170 |
getStore() { |
| 171 |
return this.store_; |
| 172 |
} |
| 173 |
/** |
| 174 |
* @param {Event|number|HTMLElement} numberOrEvent The overloaded first |
| 175 |
* argument. |
| 176 |
* @param {number} isY The y coordinate. |
| 177 |
*/ |
| 178 |
post(numberOrEvent, isY) { |
| 179 |
if (typeof (isY) !== 'undefined') { |
| 180 |
if (!this.moving) { |
| 181 |
this.getStore().removeTaborder(); |
| 182 |
} |
| 183 |
super.post(numberOrEvent, isY); |
| 184 |
return; |
| 185 |
} |
| 186 |
let node; |
| 187 |
let x; |
| 188 |
let y; |
| 189 |
let keydown = false; |
| 190 |
if (numberOrEvent instanceof Event) { |
| 191 |
let event = numberOrEvent; |
| 192 |
x = event['pageX'], y = event['pageY']; |
| 193 |
if (!x && !y && event['clientX']) { |
| 194 |
x = event.clientX + document.body.scrollLeft + |
| 195 |
document.documentElement.scrollLeft; |
| 196 |
y = event.clientY + document.body.scrollTop + |
| 197 |
document.documentElement.scrollTop; |
| 198 |
} |
| 199 |
node = event.target; |
| 200 |
keydown = event.type === 'keydown'; |
| 201 |
this.stop(event); |
| 202 |
} |
| 203 |
else { |
| 204 |
node = numberOrEvent; |
| 205 |
} |
| 206 |
this.getStore().setActive(node); |
| 207 |
this.anchor = this.getStore().getActive(); |
| 208 |
if ((keydown || (!x && !y)) && node) { |
| 209 |
let offsetX = window.pageXOffset || document.documentElement.scrollLeft; |
| 210 |
let offsetY = window.pageYOffset || document.documentElement.scrollTop; |
| 211 |
let rect = node.getBoundingClientRect(); |
| 212 |
x = (rect.right + rect.left) / 2 + offsetX; |
| 213 |
y = (rect.bottom + rect.top) / 2 + offsetY; |
| 214 |
} |
| 215 |
let rect = node.getBoundingClientRect(); |
| 216 |
let menu = this.getHtml(); |
| 217 |
let margin = 5; |
| 218 |
if (x + menu.offsetWidth > document.body.offsetWidth - margin) { |
| 219 |
x = document.body.offsetWidth - menu.offsetWidth - margin; |
| 220 |
} |
| 221 |
// Not sure what these do! |
| 222 |
// |
| 223 |
// // if (MENU.isMobile) { |
| 224 |
// // x = Math.max(5,x-Math.floor(menu.offsetWidth/2)); y -= 20 |
| 225 |
// // } |
| 226 |
// // MENU.skipUp = event.isContextMenu; |
| 227 |
this.post(x, y); |
| 228 |
} |
| 229 |
/** |
| 230 |
* Registers widgets that are opened by the menu. |
| 231 |
* @param {Postable} widget The open widget. |
| 232 |
*/ |
| 233 |
registerWidget(widget) { |
| 234 |
this.widgets.push(widget); |
| 235 |
} |
| 236 |
/** |
| 237 |
* Removes an opened widgets. |
| 238 |
* @param {Postable} widget The closed widget. |
| 239 |
*/ |
| 240 |
unregisterWidget(widget) { |
| 241 |
let index = this.widgets.indexOf(widget); |
| 242 |
if (index > -1) { |
| 243 |
this.widgets.splice(index, 1); |
| 244 |
} |
| 245 |
if (this.widgets.length === 0) { |
| 246 |
this.unpost(); |
| 247 |
} |
| 248 |
} |
| 249 |
/** |
| 250 |
* Closes all widgets that were opened from this menu. |
| 251 |
*/ |
| 252 |
unpostWidgets() { |
| 253 |
this.widgets.forEach(x => x.unpost()); |
| 254 |
} |
| 255 |
/** |
| 256 |
* Moves to the given next element. |
| 257 |
* @param {HTMLELement} next The next element in the sequence. |
| 258 |
* @private |
| 259 |
*/ |
| 260 |
move_(next) { |
| 261 |
if (this.anchor && next !== this.anchor) { |
| 262 |
this.moving = true; |
| 263 |
this.unpost(); |
| 264 |
this.post(next); |
| 265 |
this.moving = false; |
| 266 |
} |
| 267 |
} |
| 268 |
/** |
| 269 |
* Parses a JSON respresentation of a variable and inserts it into the |
| 270 |
* variable pool of the context menu. |
| 271 |
* @param {JSON} json The JSON object to parse. |
| 272 |
*/ |
| 273 |
parseVariable({ name: name, value: value, action: action }) { |
| 274 |
this.getPool().insert(new ContextMenu_1.Variable(name, value, action)); |
| 275 |
} |
| 276 |
} |
| 277 |
ContextMenu_1.ContextMenu = ContextMenu; |
| 278 |
})(ContextMenu || (ContextMenu = {})); |
| 279 |
</code></pre> |
| 280 |
</article> |
| 281 |
</section> |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
</div> |
| 287 |
|
| 288 |
<nav> |
| 289 |
<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> |
| 290 |
</nav> |
| 291 |
|
| 292 |
<br class="clear"> |
| 293 |
|
| 294 |
<footer> |
| 295 |
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) |
| 296 |
</footer> |
| 297 |
|
| 298 |
<script> prettyPrint(); </script> |
| 299 |
<script src="scripts/linenumber.js"> </script> |
| 300 |
</body> |
| 301 |
</html> |