| 1 | define([ |
| 2 | "dojo/_base/array", // array.forEach array.some |
| 3 | "dojo/aspect", |
| 4 | "dojo/_base/declare", // declare |
| 5 | "dojo/dom", // dom.isDescendant |
| 6 | "dojo/dom-attr", // domAttr.set |
| 7 | "dojo/dom-construct", // domConstruct.create domConstruct.destroy |
| 8 | "dojo/dom-geometry", // domGeometry.isBodyLtr |
| 9 | "dojo/dom-style", // domStyle.set |
| 10 | "dojo/has", // has("config-bgIframe") |
| 11 | "dojo/keys", |
| 12 | "dojo/_base/lang", // lang.hitch |
| 13 | "dojo/on", |
| 14 | "./place", |
| 15 | "./BackgroundIframe", |
| 16 | "./Viewport", |
| 17 | "./main", // dijit (defining dijit.popup to match API doc) |
| 18 | "dojo/touch" // use of dojoClick |
| 19 | ], function(array, aspect, declare, dom, domAttr, domConstruct, domGeometry, domStyle, has, keys, lang, on, |
| 20 | place, BackgroundIframe, Viewport, dijit){ |
| 21 | |
| 22 | // module: |
| 23 | // dijit/popup |
| 24 | |
| 25 | /*===== |
| 26 | var __OpenArgs = { |
| 27 | // popup: Widget |
| 28 | // widget to display |
| 29 | // parent: Widget |
| 30 | // the button etc. that is displaying this popup |
| 31 | // around: DomNode |
| 32 | // DOM node (typically a button); place popup relative to this node. (Specify this *or* "x" and "y" parameters.) |
| 33 | // x: Integer |
| 34 | // Absolute horizontal position (in pixels) to place node at. (Specify this *or* "around" parameter.) |
| 35 | // y: Integer |
| 36 | // Absolute vertical position (in pixels) to place node at. (Specify this *or* "around" parameter.) |
| 37 | // orient: Object|String |
| 38 | // When the around parameter is specified, orient should be a list of positions to try, ex: |
| 39 | // | [ "below", "above" ] |
| 40 | // For backwards compatibility it can also be an (ordered) hash of tuples of the form |
| 41 | // (around-node-corner, popup-node-corner), ex: |
| 42 | // | { "BL": "TL", "TL": "BL" } |
| 43 | // where BL means "bottom left" and "TL" means "top left", etc. |
| 44 | // |
| 45 | // dijit/popup.open() tries to position the popup according to each specified position, in order, |
| 46 | // until the popup appears fully within the viewport. |
| 47 | // |
| 48 | // The default value is ["below", "above"] |
| 49 | // |
| 50 | // When an (x,y) position is specified rather than an around node, orient is either |
| 51 | // "R" or "L". R (for right) means that it tries to put the popup to the right of the mouse, |
| 52 | // specifically positioning the popup's top-right corner at the mouse position, and if that doesn't |
| 53 | // fit in the viewport, then it tries, in order, the bottom-right corner, the top left corner, |
| 54 | // and the top-right corner. |
| 55 | // onCancel: Function |
| 56 | // callback when user has canceled the popup by: |
| 57 | // |
| 58 | // 1. hitting ESC or |
| 59 | // 2. by using the popup widget's proprietary cancel mechanism (like a cancel button in a dialog); |
| 60 | // i.e. whenever popupWidget.onCancel() is called, args.onCancel is called |
| 61 | // onClose: Function |
| 62 | // callback whenever this popup is closed |
| 63 | // onExecute: Function |
| 64 | // callback when user "executed" on the popup/sub-popup by selecting a menu choice, etc. (top menu only) |
| 65 | // padding: place.__Position |
| 66 | // adding a buffer around the opening position. This is only useful when around is not set. |
| 67 | // maxHeight: Integer |
| 68 | // The max height for the popup. Any popup taller than this will have scrollbars. |
| 69 | // Set to Infinity for no max height. Default is to limit height to available space in viewport, |
| 70 | // above or below the aroundNode or specified x/y position. |
| 71 | }; |
| 72 | =====*/ |
| 73 | |
| 74 | function destroyWrapper(){ |
| 75 | // summary: |
| 76 | // Function to destroy wrapper when popup widget is destroyed. |
| 77 | // Left in this scope to avoid memory leak on IE8 on refresh page, see #15206. |
| 78 | if(this._popupWrapper){ |
| 79 | domConstruct.destroy(this._popupWrapper); |
| 80 | delete this._popupWrapper; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | var PopupManager = declare(null, { |
| 85 | // summary: |
| 86 | // Used to show drop downs (ex: the select list of a ComboBox) |
| 87 | // or popups (ex: right-click context menus). |
| 88 | |
| 89 | // _stack: dijit/_WidgetBase[] |
| 90 | // Stack of currently popped up widgets. |
| 91 | // (someone opened _stack[0], and then it opened _stack[1], etc.) |
| 92 | _stack: [], |
| 93 | |
| 94 | // _beginZIndex: Number |
| 95 | // Z-index of the first popup. (If first popup opens other |
| 96 | // popups they get a higher z-index.) |
| 97 | _beginZIndex: 1000, |
| 98 | |
| 99 | _idGen: 1, |
| 100 | |
| 101 | _repositionAll: function(){ |
| 102 | // summary: |
| 103 | // If screen has been scrolled, reposition all the popups in the stack. |
| 104 | // Then set timer to check again later. |
| 105 | |
| 106 | if(this._firstAroundNode){ // guard for when clearTimeout() on IE doesn't work |
| 107 | var oldPos = this._firstAroundPosition, |
| 108 | newPos = domGeometry.position(this._firstAroundNode, true), |
| 109 | dx = newPos.x - oldPos.x, |
| 110 | dy = newPos.y - oldPos.y; |
| 111 | |
| 112 | if(dx || dy){ |
| 113 | this._firstAroundPosition = newPos; |
| 114 | for(var i = 0; i < this._stack.length; i++){ |
| 115 | var style = this._stack[i].wrapper.style; |
| 116 | style.top = (parseFloat(style.top) + dy) + "px"; |
| 117 | if(style.right == "auto"){ |
| 118 | style.left = (parseFloat(style.left) + dx) + "px"; |
| 119 | }else{ |
| 120 | style.right = (parseFloat(style.right) - dx) + "px"; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | this._aroundMoveListener = setTimeout(lang.hitch(this, "_repositionAll"), dx || dy ? 10 : 50); |
| 126 | } |
| 127 | }, |
| 128 | |
| 129 | _createWrapper: function(/*Widget*/ widget){ |
| 130 | // summary: |
| 131 | // Initialization for widgets that will be used as popups. |
| 132 | // Puts widget inside a wrapper DIV (if not already in one), |
| 133 | // and returns pointer to that wrapper DIV. |
| 134 | |
| 135 | var wrapper = widget._popupWrapper, |
| 136 | node = widget.domNode; |
| 137 | |
| 138 | if(!wrapper){ |
| 139 | // Create wrapper <div> for when this widget [in the future] will be used as a popup. |
| 140 | // This is done early because of IE bugs where creating/moving DOM nodes causes focus |
| 141 | // to go wonky, see tests/robot/Toolbar.html to reproduce |
| 142 | wrapper = domConstruct.create("div", { |
| 143 | "class": "dijitPopup", |
| 144 | style: { display: "none"}, |
| 145 | role: "region", |
| 146 | "aria-label": widget["aria-label"] || widget.label || widget.name || widget.id |
| 147 | }, widget.ownerDocumentBody); |
| 148 | wrapper.appendChild(node); |
| 149 | |
| 150 | var s = node.style; |
| 151 | s.display = ""; |
| 152 | s.visibility = ""; |
| 153 | s.position = ""; |
| 154 | s.top = "0px"; |
| 155 | |
| 156 | widget._popupWrapper = wrapper; |
| 157 | aspect.after(widget, "destroy", destroyWrapper, true); |
| 158 | |
| 159 | // Workaround iOS problem where clicking a Menu can focus an <input> (or click a button) behind it. |
| 160 | // Need to be careful though that you can still focus <input>'s and click <button>'s in a TooltipDialog. |
| 161 | // Also, be careful not to break (native) scrolling of dropdown like ComboBox's options list. |
| 162 | if("ontouchend" in document) { |
| 163 | on(wrapper, "touchend", function (evt){ |
| 164 | if(!/^(input|button|textarea)$/i.test(evt.target.tagName)) { |
| 165 | evt.preventDefault(); |
| 166 | } |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | // Calling evt.preventDefault() suppresses the native click event on most browsers. However, it doesn't |
| 171 | // suppress the synthetic click event emitted by dojo/touch. In order for clicks in popups to work |
| 172 | // consistently, always use dojo/touch in popups. See #18150. |
| 173 | wrapper.dojoClick = true; |
| 174 | } |
| 175 | |
| 176 | return wrapper; |
| 177 | }, |
| 178 | |
| 179 | moveOffScreen: function(/*Widget*/ widget){ |
| 180 | // summary: |
| 181 | // Moves the popup widget off-screen. |
| 182 | // Do not use this method to hide popups when not in use, because |
| 183 | // that will create an accessibility issue: the offscreen popup is |
| 184 | // still in the tabbing order. |
| 185 | |
| 186 | // Create wrapper if not already there |
| 187 | var wrapper = this._createWrapper(widget); |
| 188 | |
| 189 | // Besides setting visibility:hidden, move it out of the viewport, see #5776, #10111, #13604 |
| 190 | var ltr = domGeometry.isBodyLtr(widget.ownerDocument), |
| 191 | style = { |
| 192 | visibility: "hidden", |
| 193 | top: "-9999px", |
| 194 | display: "" |
| 195 | }; |
| 196 | style[ltr ? "left" : "right"] = "-9999px"; |
| 197 | style[ltr ? "right" : "left"] = "auto"; |
| 198 | domStyle.set(wrapper, style); |
| 199 | |
| 200 | return wrapper; |
| 201 | }, |
| 202 | |
| 203 | hide: function(/*Widget*/ widget){ |
| 204 | // summary: |
| 205 | // Hide this popup widget (until it is ready to be shown). |
| 206 | // Initialization for widgets that will be used as popups |
| 207 | // |
| 208 | // Also puts widget inside a wrapper DIV (if not already in one) |
| 209 | // |
| 210 | // If popup widget needs to layout it should |
| 211 | // do so when it is made visible, and popup._onShow() is called. |
| 212 | |
| 213 | // Create wrapper if not already there |
| 214 | var wrapper = this._createWrapper(widget); |
| 215 | |
| 216 | domStyle.set(wrapper, { |
| 217 | display: "none", |
| 218 | height: "auto", // Open() may have limited the height to fit in the viewport, |
| 219 | overflowY: "visible", // and set overflowY to "auto". |
| 220 | border: "" // Open() may have moved border from popup to wrapper. |
| 221 | }); |
| 222 | |
| 223 | // Open() may have moved border from popup to wrapper. Move it back. |
| 224 | var node = widget.domNode; |
| 225 | if("_originalStyle" in node){ |
| 226 | node.style.cssText = node._originalStyle; |
| 227 | } |
| 228 | }, |
| 229 | |
| 230 | getTopPopup: function(){ |
| 231 | // summary: |
| 232 | // Compute the closest ancestor popup that's *not* a child of another popup. |
| 233 | // Ex: For a TooltipDialog with a button that spawns a tree of menus, find the popup of the button. |
| 234 | var stack = this._stack; |
| 235 | for(var pi = stack.length - 1; pi > 0 && stack[pi].parent === stack[pi - 1].widget; pi--){ |
| 236 | /* do nothing, just trying to get right value for pi */ |
| 237 | } |
| 238 | return stack[pi]; |
| 239 | }, |
| 240 | |
| 241 | open: function(/*__OpenArgs*/ args){ |
| 242 | // summary: |
| 243 | // Popup the widget at the specified position |
| 244 | // |
| 245 | // example: |
| 246 | // opening at the mouse position |
| 247 | // | popup.open({popup: menuWidget, x: evt.pageX, y: evt.pageY}); |
| 248 | // |
| 249 | // example: |
| 250 | // opening the widget as a dropdown |
| 251 | // | popup.open({parent: this, popup: menuWidget, around: this.domNode, onClose: function(){...}}); |
| 252 | // |
| 253 | // Note that whatever widget called dijit/popup.open() should also listen to its own _onBlur callback |
| 254 | // (fired from _base/focus.js) to know that focus has moved somewhere else and thus the popup should be closed. |
| 255 | |
| 256 | var stack = this._stack, |
| 257 | widget = args.popup, |
| 258 | node = widget.domNode, |
| 259 | orient = args.orient || ["below", "below-alt", "above", "above-alt"], |
| 260 | ltr = args.parent ? args.parent.isLeftToRight() : domGeometry.isBodyLtr(widget.ownerDocument), |
| 261 | around = args.around, |
| 262 | id = (args.around && args.around.id) ? (args.around.id + "_dropdown") : ("popup_" + this._idGen++); |
| 263 | |
| 264 | // If we are opening a new popup that isn't a child of a currently opened popup, then |
| 265 | // close currently opened popup(s). This should happen automatically when the old popups |
| 266 | // gets the _onBlur() event, except that the _onBlur() event isn't reliable on IE, see [22198]. |
| 267 | while(stack.length && (!args.parent || !dom.isDescendant(args.parent.domNode, stack[stack.length - 1].widget.domNode))){ |
| 268 | this.close(stack[stack.length - 1].widget); |
| 269 | } |
| 270 | |
| 271 | // Get pointer to popup wrapper, and create wrapper if it doesn't exist. Remove display:none (but keep |
| 272 | // off screen) so we can do sizing calculations. |
| 273 | var wrapper = this.moveOffScreen(widget); |
| 274 | |
| 275 | if(widget.startup && !widget._started){ |
| 276 | widget.startup(); // this has to be done after being added to the DOM |
| 277 | } |
| 278 | |
| 279 | // Limit height to space available in viewport either above or below aroundNode (whichever side has more |
| 280 | // room), adding scrollbar if necessary. Can't add scrollbar to widget because it may be a <table> (ex: |
| 281 | // dijit/Menu), so add to wrapper, and then move popup's border to wrapper so scroll bar inside border. |
| 282 | var maxHeight, popupSize = domGeometry.position(node); |
| 283 | if("maxHeight" in args && args.maxHeight != -1){ |
| 284 | maxHeight = args.maxHeight || Infinity; // map 0 --> infinity for back-compat of _HasDropDown.maxHeight |
| 285 | }else{ |
| 286 | var viewport = Viewport.getEffectiveBox(this.ownerDocument), |
| 287 | aroundPos = around ? domGeometry.position(around, false) : {y: args.y - (args.padding||0), h: (args.padding||0) * 2}; |
| 288 | maxHeight = Math.floor(Math.max(aroundPos.y, viewport.h - (aroundPos.y + aroundPos.h))); |
| 289 | } |
| 290 | if(popupSize.h > maxHeight){ |
| 291 | // Get style of popup's border. Unfortunately domStyle.get(node, "border") doesn't work on FF or IE, |
| 292 | // and domStyle.get(node, "borderColor") etc. doesn't work on FF, so need to use fully qualified names. |
| 293 | var cs = domStyle.getComputedStyle(node), |
| 294 | borderStyle = cs.borderLeftWidth + " " + cs.borderLeftStyle + " " + cs.borderLeftColor; |
| 295 | domStyle.set(wrapper, { |
| 296 | overflowY: "scroll", |
| 297 | height: maxHeight + "px", |
| 298 | border: borderStyle // so scrollbar is inside border |
| 299 | }); |
| 300 | node._originalStyle = node.style.cssText; |
| 301 | node.style.border = "none"; |
| 302 | } |
| 303 | |
| 304 | domAttr.set(wrapper, { |
| 305 | id: id, |
| 306 | style: { |
| 307 | zIndex: this._beginZIndex + stack.length |
| 308 | }, |
| 309 | "class": "dijitPopup " + (widget.baseClass || widget["class"] || "").split(" ")[0] + "Popup", |
| 310 | dijitPopupParent: args.parent ? args.parent.id : "" |
| 311 | }); |
| 312 | |
| 313 | if(stack.length == 0 && around){ |
| 314 | // First element on stack. Save position of aroundNode and setup listener for changes to that position. |
| 315 | this._firstAroundNode = around; |
| 316 | this._firstAroundPosition = domGeometry.position(around, true); |
| 317 | this._aroundMoveListener = setTimeout(lang.hitch(this, "_repositionAll"), 50); |
| 318 | } |
| 319 | |
| 320 | if(has("config-bgIframe") && !widget.bgIframe){ |
| 321 | // setting widget.bgIframe triggers cleanup in _WidgetBase.destroyRendering() |
| 322 | widget.bgIframe = new BackgroundIframe(wrapper); |
| 323 | } |
| 324 | |
| 325 | // position the wrapper node and make it visible |
| 326 | var layoutFunc = widget.orient ? lang.hitch(widget, "orient") : null, |
| 327 | best = around ? |
| 328 | place.around(wrapper, around, orient, ltr, layoutFunc) : |
| 329 | place.at(wrapper, args, orient == 'R' ? ['TR', 'BR', 'TL', 'BL'] : ['TL', 'BL', 'TR', 'BR'], args.padding, |
| 330 | layoutFunc); |
| 331 | |
| 332 | wrapper.style.visibility = "visible"; |
| 333 | node.style.visibility = "visible"; // counteract effects from _HasDropDown |
| 334 | |
| 335 | var handlers = []; |
| 336 | |
| 337 | // provide default escape and tab key handling |
| 338 | // (this will work for any widget, not just menu) |
| 339 | handlers.push(on(wrapper, "keydown", lang.hitch(this, function(evt){ |
| 340 | if(evt.keyCode == keys.ESCAPE && args.onCancel){ |
| 341 | evt.stopPropagation(); |
| 342 | evt.preventDefault(); |
| 343 | args.onCancel(); |
| 344 | }else if(evt.keyCode == keys.TAB){ |
| 345 | evt.stopPropagation(); |
| 346 | evt.preventDefault(); |
| 347 | var topPopup = this.getTopPopup(); |
| 348 | if(topPopup && topPopup.onCancel){ |
| 349 | topPopup.onCancel(); |
| 350 | } |
| 351 | } |
| 352 | }))); |
| 353 | |
| 354 | // watch for cancel/execute events on the popup and notify the caller |
| 355 | // (for a menu, "execute" means clicking an item) |
| 356 | if(widget.onCancel && args.onCancel){ |
| 357 | handlers.push(widget.on("cancel", args.onCancel)); |
| 358 | } |
| 359 | |
| 360 | handlers.push(widget.on(widget.onExecute ? "execute" : "change", lang.hitch(this, function(){ |
| 361 | var topPopup = this.getTopPopup(); |
| 362 | if(topPopup && topPopup.onExecute){ |
| 363 | topPopup.onExecute(); |
| 364 | } |
| 365 | }))); |
| 366 | |
| 367 | stack.push({ |
| 368 | widget: widget, |
| 369 | wrapper: wrapper, |
| 370 | parent: args.parent, |
| 371 | onExecute: args.onExecute, |
| 372 | onCancel: args.onCancel, |
| 373 | onClose: args.onClose, |
| 374 | handlers: handlers |
| 375 | }); |
| 376 | |
| 377 | if(widget.onOpen){ |
| 378 | // TODO: in 2.0 standardize onShow() (used by StackContainer) and onOpen() (used here) |
| 379 | widget.onOpen(best); |
| 380 | } |
| 381 | |
| 382 | return best; |
| 383 | }, |
| 384 | |
| 385 | close: function(/*Widget?*/ popup){ |
| 386 | // summary: |
| 387 | // Close specified popup and any popups that it parented. |
| 388 | // If no popup is specified, closes all popups. |
| 389 | |
| 390 | var stack = this._stack; |
| 391 | |
| 392 | // Basically work backwards from the top of the stack closing popups |
| 393 | // until we hit the specified popup, but IIRC there was some issue where closing |
| 394 | // a popup would cause others to close too. Thus if we are trying to close B in [A,B,C] |
| 395 | // closing C might close B indirectly and then the while() condition will run where stack==[A]... |
| 396 | // so the while condition is constructed defensively. |
| 397 | while((popup && array.some(stack, function(elem){ |
| 398 | return elem.widget == popup; |
| 399 | })) || |
| 400 | (!popup && stack.length)){ |
| 401 | var top = stack.pop(), |
| 402 | widget = top.widget, |
| 403 | onClose = top.onClose; |
| 404 | |
| 405 | if (widget.bgIframe) { |
| 406 | // push the iframe back onto the stack. |
| 407 | widget.bgIframe.destroy(); |
| 408 | delete widget.bgIframe; |
| 409 | } |
| 410 | |
| 411 | if(widget.onClose){ |
| 412 | // TODO: in 2.0 standardize onHide() (used by StackContainer) and onClose() (used here). |
| 413 | // Actually, StackContainer also calls onClose(), but to mean that the pane is being deleted |
| 414 | // (i.e. that the TabContainer's tab's [x] icon was clicked) |
| 415 | widget.onClose(); |
| 416 | } |
| 417 | |
| 418 | var h; |
| 419 | while(h = top.handlers.pop()){ |
| 420 | h.remove(); |
| 421 | } |
| 422 | |
| 423 | // Hide the widget and it's wrapper unless it has already been destroyed in above onClose() etc. |
| 424 | if(widget && widget.domNode){ |
| 425 | this.hide(widget); |
| 426 | } |
| 427 | |
| 428 | if(onClose){ |
| 429 | onClose(); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | if(stack.length == 0 && this._aroundMoveListener){ |
| 434 | clearTimeout(this._aroundMoveListener); |
| 435 | this._firstAroundNode = this._firstAroundPosition = this._aroundMoveListener = null; |
| 436 | } |
| 437 | } |
| 438 | }); |
| 439 | |
| 440 | return (dijit.popup = new PopupManager()); |
| 441 | }); |