| 1 | define([ |
| 2 | "require", |
| 3 | "dojo/_base/array", // array.forEach |
| 4 | "dojo/_base/declare", // declare |
| 5 | "dojo/dom", // dom.byId dom.isDescendant |
| 6 | "dojo/dom-attr", // domAttr.get domAttr.set domAttr.has domAttr.remove |
| 7 | "dojo/dom-geometry", // domStyle.getComputedStyle domGeometry.position |
| 8 | "dojo/dom-style", // domStyle.getComputedStyle |
| 9 | "dojo/keys", // keys.F10 |
| 10 | "dojo/_base/lang", // lang.hitch |
| 11 | "dojo/on", |
| 12 | "dojo/sniff", // has("ie"), has("quirks") |
| 13 | "dojo/_base/window", // win.body |
| 14 | "dojo/window", // winUtils.get |
| 15 | "./popup", |
| 16 | "./DropDownMenu", |
| 17 | "dojo/ready" |
| 18 | ], function(require, array, declare, dom, domAttr, domGeometry, domStyle, keys, lang, on, has, win, winUtils, pm, DropDownMenu, ready){ |
| 19 | |
| 20 | // module: |
| 21 | // dijit/Menu |
| 22 | |
| 23 | // Back compat w/1.6, remove for 2.0 |
| 24 | if(has("dijit-legacy-requires")){ |
| 25 | ready(0, function(){ |
| 26 | var requires = ["dijit/MenuItem", "dijit/PopupMenuItem", "dijit/CheckedMenuItem", "dijit/MenuSeparator"]; |
| 27 | require(requires); // use indirection so modules not rolled into a build |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | return declare("dijit.Menu", DropDownMenu, { |
| 32 | // summary: |
| 33 | // A context menu you can assign to multiple elements |
| 34 | |
| 35 | constructor: function(/*===== params, srcNodeRef =====*/){ |
| 36 | // summary: |
| 37 | // Create the widget. |
| 38 | // params: Object|null |
| 39 | // Hash of initialization parameters for widget, including scalar values (like title, duration etc.) |
| 40 | // and functions, typically callbacks like onClick. |
| 41 | // The hash can contain any of the widget's properties, excluding read-only properties. |
| 42 | // srcNodeRef: DOMNode|String? |
| 43 | // If a srcNodeRef (DOM node) is specified: |
| 44 | // |
| 45 | // - use srcNodeRef.innerHTML as my contents |
| 46 | // - replace srcNodeRef with my generated DOM tree |
| 47 | |
| 48 | this._bindings = []; |
| 49 | }, |
| 50 | |
| 51 | // targetNodeIds: [const] String[] |
| 52 | // Array of dom node ids of nodes to attach to. |
| 53 | // Fill this with nodeIds upon widget creation and it becomes context menu for those nodes. |
| 54 | targetNodeIds: [], |
| 55 | |
| 56 | // selector: String? |
| 57 | // CSS expression to apply this Menu to descendants of targetNodeIds, rather than to |
| 58 | // the nodes specified by targetNodeIds themselves. Useful for applying a Menu to |
| 59 | // a range of rows in a table, tree, etc. |
| 60 | // |
| 61 | // The application must require() an appropriate level of dojo/query to handle the selector. |
| 62 | selector: "", |
| 63 | |
| 64 | // TODO: in 2.0 remove support for multiple targetNodeIds. selector gives the same effect. |
| 65 | // So, change targetNodeIds to a targetNodeId: "", remove bindDomNode()/unBindDomNode(), etc. |
| 66 | |
| 67 | /*===== |
| 68 | // currentTarget: [readonly] DOMNode |
| 69 | // For context menus, set to the current node that the Menu is being displayed for. |
| 70 | // Useful so that the menu actions can be tailored according to the node |
| 71 | currentTarget: null, |
| 72 | =====*/ |
| 73 | |
| 74 | // contextMenuForWindow: [const] Boolean |
| 75 | // If true, right clicking anywhere on the window will cause this context menu to open. |
| 76 | // If false, must specify targetNodeIds. |
| 77 | contextMenuForWindow: false, |
| 78 | |
| 79 | // leftClickToOpen: [const] Boolean |
| 80 | // If true, menu will open on left click instead of right click, similar to a file menu. |
| 81 | leftClickToOpen: false, |
| 82 | // TODO: remove in 2.0, we have better ways of opening a menu with a left click, by extending _HasDropDown. |
| 83 | |
| 84 | // refocus: Boolean |
| 85 | // When this menu closes, re-focus the element which had focus before it was opened. |
| 86 | refocus: true, |
| 87 | |
| 88 | postCreate: function(){ |
| 89 | if(this.contextMenuForWindow){ |
| 90 | this.bindDomNode(this.ownerDocumentBody); |
| 91 | }else{ |
| 92 | array.forEach(this.targetNodeIds, this.bindDomNode, this); |
| 93 | } |
| 94 | this.inherited(arguments); |
| 95 | }, |
| 96 | |
| 97 | // thanks burstlib! |
| 98 | _iframeContentWindow: function(/* HTMLIFrameElement */iframe_el){ |
| 99 | // summary: |
| 100 | // Returns the window reference of the passed iframe |
| 101 | // tags: |
| 102 | // private |
| 103 | return winUtils.get(this._iframeContentDocument(iframe_el)) || |
| 104 | // Moz. TODO: is this available when defaultView isn't? |
| 105 | this._iframeContentDocument(iframe_el)['__parent__'] || |
| 106 | (iframe_el.name && document.frames[iframe_el.name]) || null; // Window |
| 107 | }, |
| 108 | |
| 109 | _iframeContentDocument: function(/* HTMLIFrameElement */iframe_el){ |
| 110 | // summary: |
| 111 | // Returns a reference to the document object inside iframe_el |
| 112 | // tags: |
| 113 | // protected |
| 114 | return iframe_el.contentDocument // W3 |
| 115 | || (iframe_el.contentWindow && iframe_el.contentWindow.document) // IE |
| 116 | || (iframe_el.name && document.frames[iframe_el.name] && document.frames[iframe_el.name].document) |
| 117 | || null; // HTMLDocument |
| 118 | }, |
| 119 | |
| 120 | bindDomNode: function(/*String|DomNode*/ node){ |
| 121 | // summary: |
| 122 | // Attach menu to given node |
| 123 | node = dom.byId(node, this.ownerDocument); |
| 124 | |
| 125 | var cn; // Connect node |
| 126 | |
| 127 | // Support context menus on iframes. Rather than binding to the iframe itself we need |
| 128 | // to bind to the <body> node inside the iframe. |
| 129 | if(node.tagName.toLowerCase() == "iframe"){ |
| 130 | var iframe = node, |
| 131 | window = this._iframeContentWindow(iframe); |
| 132 | cn = win.body(window.document); |
| 133 | }else{ |
| 134 | // To capture these events at the top level, attach to <html>, not <body>. |
| 135 | // Otherwise right-click context menu just doesn't work. |
| 136 | cn = (node == win.body(this.ownerDocument) ? this.ownerDocument.documentElement : node); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | // "binding" is the object to track our connection to the node (ie, the parameter to bindDomNode()) |
| 141 | var binding = { |
| 142 | node: node, |
| 143 | iframe: iframe |
| 144 | }; |
| 145 | |
| 146 | // Save info about binding in _bindings[], and make node itself record index(+1) into |
| 147 | // _bindings[] array. Prefix w/_dijitMenu to avoid setting an attribute that may |
| 148 | // start with a number, which fails on FF/safari. |
| 149 | domAttr.set(node, "_dijitMenu" + this.id, this._bindings.push(binding)); |
| 150 | |
| 151 | // Setup the connections to monitor click etc., unless we are connecting to an iframe which hasn't finished |
| 152 | // loading yet, in which case we need to wait for the onload event first, and then connect |
| 153 | // On linux Shift-F10 produces the oncontextmenu event, but on Windows it doesn't, so |
| 154 | // we need to monitor keyboard events in addition to the oncontextmenu event. |
| 155 | var doConnects = lang.hitch(this, function(cn){ |
| 156 | var selector = this.selector, |
| 157 | delegatedEvent = selector ? |
| 158 | function(eventType){ |
| 159 | return on.selector(selector, eventType); |
| 160 | } : |
| 161 | function(eventType){ |
| 162 | return eventType; |
| 163 | }, |
| 164 | self = this; |
| 165 | return [ |
| 166 | on(cn, delegatedEvent(this.leftClickToOpen ? "click" : "contextmenu"), function(evt){ |
| 167 | evt.stopPropagation(); |
| 168 | evt.preventDefault(); |
| 169 | |
| 170 | if((new Date()).getTime() < self._lastKeyDown + 500){ |
| 171 | // Ignore contextmenu/click events that were already processed in keydown handler below. |
| 172 | // But still call preventDefault() (above) so system context menu doesn't appear. |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | // Schedule context menu to be opened. |
| 177 | // Note that this won't work will if the click was generated by the keyboard, while |
| 178 | // focused on a <button> etc. In that case evt.pageX and evt.pageY are either (0,0) or |
| 179 | // wherever the mouse cursor is. See keydown handler below. |
| 180 | self._scheduleOpen(this, iframe, {x: evt.pageX, y: evt.pageY}, evt.target); |
| 181 | }), |
| 182 | on(cn, delegatedEvent("keydown"), function(evt){ |
| 183 | if(evt.keyCode == 93 || // context menu key |
| 184 | (evt.shiftKey && evt.keyCode == keys.F10) || // shift-F10 |
| 185 | (self.leftClickToOpen && evt.keyCode == keys.SPACE) // space key |
| 186 | ){ |
| 187 | evt.stopPropagation(); |
| 188 | evt.preventDefault(); |
| 189 | |
| 190 | // Open the menu around evt.target. Note that "this" and evt.target |
| 191 | // are likely different, especially for global context menu, where "this" is <body>. |
| 192 | self._scheduleOpen(this, iframe, null, evt.target); // no coords - open near evt.target |
| 193 | |
| 194 | self._lastKeyDown = (new Date()).getTime(); |
| 195 | } |
| 196 | }) |
| 197 | ]; |
| 198 | }); |
| 199 | binding.connects = cn ? doConnects(cn) : []; |
| 200 | |
| 201 | if(iframe){ |
| 202 | // Setup handler to [re]bind to the iframe when the contents are initially loaded, |
| 203 | // and every time the contents change. |
| 204 | // Need to do this b/c we are actually binding to the iframe's <body> node. |
| 205 | // Note: can't use connect.connect(), see #9609. |
| 206 | |
| 207 | binding.onloadHandler = lang.hitch(this, function(){ |
| 208 | // want to remove old connections, but IE throws exceptions when trying to |
| 209 | // access the <body> node because it's already gone, or at least in a state of limbo |
| 210 | |
| 211 | var window = this._iframeContentWindow(iframe), |
| 212 | cn = win.body(window.document); |
| 213 | binding.connects = doConnects(cn); |
| 214 | }); |
| 215 | if(iframe.addEventListener){ |
| 216 | iframe.addEventListener("load", binding.onloadHandler, false); |
| 217 | }else{ |
| 218 | iframe.attachEvent("onload", binding.onloadHandler); |
| 219 | } |
| 220 | } |
| 221 | }, |
| 222 | |
| 223 | unBindDomNode: function(/*String|DomNode*/ nodeName){ |
| 224 | // summary: |
| 225 | // Detach menu from given node |
| 226 | |
| 227 | var node; |
| 228 | try{ |
| 229 | node = dom.byId(nodeName, this.ownerDocument); |
| 230 | }catch(e){ |
| 231 | // On IE the dom.byId() call will get an exception if the attach point was |
| 232 | // the <body> node of an <iframe> that has since been reloaded (and thus the |
| 233 | // <body> node is in a limbo state of destruction. |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | // node["_dijitMenu" + this.id] contains index(+1) into my _bindings[] array |
| 238 | var attrName = "_dijitMenu" + this.id; |
| 239 | if(node && domAttr.has(node, attrName)){ |
| 240 | var bid = domAttr.get(node, attrName) - 1, b = this._bindings[bid], h; |
| 241 | while((h = b.connects.pop())){ |
| 242 | h.remove(); |
| 243 | } |
| 244 | |
| 245 | // Remove listener for iframe onload events |
| 246 | var iframe = b.iframe; |
| 247 | if(iframe){ |
| 248 | if(iframe.removeEventListener){ |
| 249 | iframe.removeEventListener("load", b.onloadHandler, false); |
| 250 | }else{ |
| 251 | iframe.detachEvent("onload", b.onloadHandler); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | domAttr.remove(node, attrName); |
| 256 | delete this._bindings[bid]; |
| 257 | } |
| 258 | }, |
| 259 | |
| 260 | _scheduleOpen: function(delegatedTarget, iframe, coords, target){ |
| 261 | // summary: |
| 262 | // Set timer to display myself. Using a timer rather than displaying immediately solves |
| 263 | // IE problem: without the delay, focus work in "open" causes the system |
| 264 | // context menu to appear in spite of evt.preventDefault(). |
| 265 | // delegatedTarget: Element |
| 266 | // The node specified in targetNodeIds or matching selector that the menu is being opened for. |
| 267 | // iframe: HTMLIframeElement? |
| 268 | // Set if target is inside the specified iframe. |
| 269 | // coords: Object |
| 270 | // x/y position to center the menu around. Undefined if menu was opened via keyboard. |
| 271 | // target: Element |
| 272 | // The actual clicked node, either delegatedTarget or a descendant. |
| 273 | |
| 274 | if(!this._openTimer){ |
| 275 | this._openTimer = this.defer(function(){ |
| 276 | delete this._openTimer; |
| 277 | this._openMyself({ |
| 278 | target: target, |
| 279 | delegatedTarget: delegatedTarget, |
| 280 | iframe: iframe, |
| 281 | coords: coords |
| 282 | }); |
| 283 | }, 1); |
| 284 | } |
| 285 | }, |
| 286 | |
| 287 | _openMyself: function(args){ |
| 288 | // summary: |
| 289 | // Internal function for opening myself when the user does a right-click or something similar. |
| 290 | // args: |
| 291 | // This is an Object containing: |
| 292 | // |
| 293 | // - target: The node that is being clicked. |
| 294 | // - delegatedTarget: The node from this.targetNodeIds or matching this.selector, |
| 295 | // either the same as target or an ancestor of target. |
| 296 | // - iframe: If an `<iframe>` is being clicked, iframe points to that iframe |
| 297 | // - coords: Mouse cursor x/y coordinates. Null when opened via keyboard. |
| 298 | // Put menu at specified position in iframe (if iframe specified) or otherwise in viewport. |
| 299 | // |
| 300 | // _openMyself() formerly took the event object, and since various code references |
| 301 | // evt.target (after connecting to _openMyself()), using an Object for parameters |
| 302 | // (so that old code still works). |
| 303 | |
| 304 | var target = args.target, |
| 305 | iframe = args.iframe, |
| 306 | coords = args.coords, |
| 307 | byKeyboard = !coords; |
| 308 | |
| 309 | // To be used by MenuItem event handlers to tell which node the menu was opened on |
| 310 | this.currentTarget = args.delegatedTarget; |
| 311 | |
| 312 | // Get coordinates to open menu, either at specified (mouse) position or (if triggered via keyboard) |
| 313 | // then near the node the menu is assigned to. |
| 314 | if(coords){ |
| 315 | if(iframe){ |
| 316 | // Specified coordinates are on <body> node of an <iframe>, convert to match main document |
| 317 | var ifc = domGeometry.position(iframe, true), |
| 318 | window = this._iframeContentWindow(iframe), |
| 319 | scroll = domGeometry.docScroll(window.document); |
| 320 | |
| 321 | var cs = domStyle.getComputedStyle(iframe), |
| 322 | tp = domStyle.toPixelValue, |
| 323 | left = (has("ie") && has("quirks") ? 0 : tp(iframe, cs.paddingLeft)) + (has("ie") && has("quirks") ? tp(iframe, cs.borderLeftWidth) : 0), |
| 324 | top = (has("ie") && has("quirks") ? 0 : tp(iframe, cs.paddingTop)) + (has("ie") && has("quirks") ? tp(iframe, cs.borderTopWidth) : 0); |
| 325 | |
| 326 | coords.x += ifc.x + left - scroll.x; |
| 327 | coords.y += ifc.y + top - scroll.y; |
| 328 | } |
| 329 | }else{ |
| 330 | coords = domGeometry.position(target, true); |
| 331 | coords.x += 10; |
| 332 | coords.y += 10; |
| 333 | } |
| 334 | |
| 335 | var self = this; |
| 336 | var prevFocusNode = this._focusManager.get("prevNode"); |
| 337 | var curFocusNode = this._focusManager.get("curNode"); |
| 338 | var savedFocusNode = !curFocusNode || (dom.isDescendant(curFocusNode, this.domNode)) ? prevFocusNode : curFocusNode; |
| 339 | |
| 340 | function closeAndRestoreFocus(){ |
| 341 | // user has clicked on a menu or popup |
| 342 | if(self.refocus && savedFocusNode){ |
| 343 | savedFocusNode.focus(); |
| 344 | } |
| 345 | pm.close(self); |
| 346 | } |
| 347 | |
| 348 | pm.open({ |
| 349 | popup: this, |
| 350 | x: coords.x, |
| 351 | y: coords.y, |
| 352 | onExecute: closeAndRestoreFocus, |
| 353 | onCancel: closeAndRestoreFocus, |
| 354 | orient: this.isLeftToRight() ? 'L' : 'R' |
| 355 | }); |
| 356 | |
| 357 | // Focus the menu even when opened by mouse, so that a click on blank area of screen will close it |
| 358 | this.focus(); |
| 359 | if(!byKeyboard){ |
| 360 | // But then (when opened by mouse), mark Menu as passive, so that the first item isn't highlighted. |
| 361 | // On IE9+ this needs to be on a delay because the focus is asynchronous. |
| 362 | this.defer(function(){ |
| 363 | this._cleanUp(true); |
| 364 | }); |
| 365 | } |
| 366 | |
| 367 | this._onBlur = function(){ |
| 368 | this.inherited('_onBlur', arguments); |
| 369 | // Usually the parent closes the child widget but if this is a context |
| 370 | // menu then there is no parent |
| 371 | pm.close(this); |
| 372 | // don't try to restore focus; user has clicked another part of the screen |
| 373 | // and set focus there |
| 374 | }; |
| 375 | }, |
| 376 | |
| 377 | destroy: function(){ |
| 378 | array.forEach(this._bindings, function(b){ |
| 379 | if(b){ |
| 380 | this.unBindDomNode(b.node); |
| 381 | } |
| 382 | }, this); |
| 383 | this.inherited(arguments); |
| 384 | } |
| 385 | }); |
| 386 | }); |