2014-05-05 19:56:14 +02:00
|
|
|
/**
|
|
|
|
* @provides phuix-dropdown-menu
|
|
|
|
* @requires javelin-install
|
|
|
|
* javelin-util
|
|
|
|
* javelin-dom
|
|
|
|
* javelin-vector
|
|
|
|
* javelin-stratcom
|
|
|
|
* @javelin
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic interaction for a dropdown menu.
|
|
|
|
*
|
|
|
|
* The menu is unaware of the content inside it, so it can not close itself
|
|
|
|
* when an item is selected. Callers must make a call to @{method:close} after
|
|
|
|
* an item is chosen in order to close the menu.
|
|
|
|
*/
|
|
|
|
JX.install('PHUIXDropdownMenu', {
|
|
|
|
|
|
|
|
construct : function(node) {
|
|
|
|
this._node = node;
|
|
|
|
|
2020-05-13 20:09:44 +02:00
|
|
|
if (node) {
|
|
|
|
JX.DOM.listen(
|
|
|
|
this._node,
|
|
|
|
'click',
|
|
|
|
null,
|
|
|
|
JX.bind(this, this._onclick));
|
|
|
|
}
|
2014-05-05 19:56:14 +02:00
|
|
|
|
|
|
|
JX.Stratcom.listen(
|
|
|
|
'mousedown',
|
|
|
|
null,
|
|
|
|
JX.bind(this, this._onanyclick));
|
|
|
|
|
|
|
|
JX.Stratcom.listen(
|
|
|
|
'resize',
|
|
|
|
null,
|
|
|
|
JX.bind(this, this._adjustposition));
|
|
|
|
|
|
|
|
JX.Stratcom.listen('phuix.dropdown.open', null, JX.bind(this, this.close));
|
2014-05-05 19:57:23 +02:00
|
|
|
|
|
|
|
JX.Stratcom.listen('keydown', null, JX.bind(this, this._onkey));
|
2017-01-18 20:17:11 +01:00
|
|
|
|
|
|
|
JX.DOM.listen(
|
|
|
|
this._getMenuNode(),
|
|
|
|
'click',
|
|
|
|
'tag:a',
|
|
|
|
JX.bind(this, this._onlink));
|
2014-05-05 19:56:14 +02:00
|
|
|
},
|
|
|
|
|
2016-06-21 02:49:38 +02:00
|
|
|
events: ['open', 'close'],
|
2014-05-05 19:56:14 +02:00
|
|
|
|
|
|
|
properties: {
|
|
|
|
width: null,
|
2014-05-05 19:56:38 +02:00
|
|
|
align: 'right',
|
|
|
|
offsetX: 0,
|
2020-05-13 20:09:44 +02:00
|
|
|
offsetY: 0,
|
|
|
|
disableAutofocus: false
|
2014-05-05 19:56:14 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
members: {
|
|
|
|
_node: null,
|
|
|
|
_menu: null,
|
|
|
|
_open: false,
|
|
|
|
_content: null,
|
2020-05-13 20:09:44 +02:00
|
|
|
_position: null,
|
|
|
|
_visible: false,
|
2014-05-05 19:56:14 +02:00
|
|
|
|
|
|
|
setContent: function(content) {
|
|
|
|
JX.DOM.setContent(this._getMenuNode(), content);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
open: function() {
|
|
|
|
if (this._open) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.invoke('open');
|
|
|
|
JX.Stratcom.invoke('phuix.dropdown.open');
|
|
|
|
|
|
|
|
this._open = true;
|
|
|
|
this._show();
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
close: function() {
|
|
|
|
if (!this._open) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._open = false;
|
|
|
|
this._hide();
|
|
|
|
|
2016-06-21 02:49:38 +02:00
|
|
|
this.invoke('close');
|
|
|
|
|
2014-05-05 19:56:14 +02:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2020-05-13 20:09:44 +02:00
|
|
|
setPosition: function(pos) {
|
|
|
|
this._position = pos;
|
|
|
|
this._setMenuNodePosition(pos);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2014-05-05 19:56:14 +02:00
|
|
|
_getMenuNode: function() {
|
|
|
|
if (!this._menu) {
|
|
|
|
var attrs = {
|
|
|
|
className: 'phuix-dropdown-menu',
|
|
|
|
role: 'button'
|
|
|
|
};
|
|
|
|
|
|
|
|
var menu = JX.$N('div', attrs);
|
|
|
|
|
|
|
|
this._menu = menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._menu;
|
|
|
|
},
|
|
|
|
|
|
|
|
_onclick : function(e) {
|
|
|
|
if (this._open) {
|
|
|
|
this.close();
|
|
|
|
} else {
|
|
|
|
this.open();
|
|
|
|
}
|
|
|
|
e.prevent();
|
|
|
|
},
|
|
|
|
|
2017-01-18 20:17:11 +01:00
|
|
|
_onlink: function(e) {
|
|
|
|
if (!e.isNormalClick()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this action was built dynamically with PHUIXActionView, don't
|
2017-10-09 19:48:01 +02:00
|
|
|
// do anything by default. The caller is responsible for installing a
|
2017-01-18 20:17:11 +01:00
|
|
|
// handler if they want to react to clicks.
|
|
|
|
if (e.getNode('phuix-action-view')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this item opens a submenu, we don't want to close the current
|
|
|
|
// menu. One submenu is "Edit Related Objects..." on mobile.
|
|
|
|
var link = e.getNode('tag:a');
|
|
|
|
if (JX.Stratcom.hasSigil(link, 'keep-open')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.close();
|
|
|
|
},
|
|
|
|
|
2014-05-05 19:56:14 +02:00
|
|
|
_onanyclick : function(e) {
|
|
|
|
if (!this._open) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JX.Stratcom.pass(e)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var t = e.getTarget();
|
|
|
|
while (t) {
|
|
|
|
if (t == this._menu || t == this._node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
t = t.parentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
_show : function() {
|
2020-05-13 20:09:44 +02:00
|
|
|
if (!this._visible) {
|
|
|
|
this._visible = true;
|
|
|
|
document.body.appendChild(this._menu);
|
|
|
|
}
|
2014-05-05 19:56:14 +02:00
|
|
|
|
|
|
|
if (this.getWidth()) {
|
|
|
|
new JX.Vector(this.getWidth(), null).setDim(this._menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._adjustposition();
|
|
|
|
|
2020-05-13 20:09:44 +02:00
|
|
|
if (this._node) {
|
|
|
|
JX.DOM.alterClass(this._node, 'phuix-dropdown-open', true);
|
|
|
|
this._node.setAttribute('aria-expanded', 'true');
|
|
|
|
}
|
2014-05-05 19:57:23 +02:00
|
|
|
|
|
|
|
// Try to highlight the first link in the menu for assistive technologies.
|
2020-05-13 20:09:44 +02:00
|
|
|
if (!this.getDisableAutofocus()) {
|
|
|
|
var links = JX.DOM.scry(this._menu, 'a');
|
|
|
|
if (links[0]) {
|
|
|
|
JX.DOM.focus(links[0]);
|
|
|
|
}
|
2014-05-05 19:57:23 +02:00
|
|
|
}
|
2014-05-05 19:56:14 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
_hide : function() {
|
2020-05-13 20:09:44 +02:00
|
|
|
this._visible = false;
|
2014-05-05 19:56:14 +02:00
|
|
|
JX.DOM.remove(this._menu);
|
|
|
|
|
2020-05-13 20:09:44 +02:00
|
|
|
if (this._node) {
|
|
|
|
JX.DOM.alterClass(this._node, 'phuix-dropdown-open', false);
|
|
|
|
this._node.setAttribute('aria-expanded', 'false');
|
|
|
|
}
|
2014-05-05 19:56:14 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
_adjustposition : function() {
|
|
|
|
if (!this._open) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:09:44 +02:00
|
|
|
if (this._position) {
|
|
|
|
this._setMenuNodePosition(this._position);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-05 19:56:14 +02:00
|
|
|
var m = JX.Vector.getDim(this._menu);
|
|
|
|
|
|
|
|
var v = JX.$V(this._node);
|
|
|
|
var d = JX.Vector.getDim(this._node);
|
|
|
|
|
2019-04-08 19:29:05 +02:00
|
|
|
var alignments = ['right', 'left'];
|
|
|
|
var disallow = {};
|
|
|
|
var margin = 8;
|
|
|
|
|
|
|
|
// If "right" alignment would leave us with the dropdown near or off the
|
|
|
|
// left side of the screen, disallow it.
|
|
|
|
var x_min = ((v.x + d.x) - m.x);
|
|
|
|
if (x_min < margin) {
|
|
|
|
disallow.right = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var align = this.getAlign();
|
|
|
|
|
|
|
|
// If the position disallows the configured alignment, try the next
|
|
|
|
// best alignment instead.
|
|
|
|
|
|
|
|
// If no alignment is allowed, we'll stick with the original alignment
|
|
|
|
// and accept that it isn't going to render very nicely. This can happen
|
|
|
|
// if the browser window is very, very small.
|
|
|
|
if (align in disallow) {
|
|
|
|
for (var ii = 0; ii < alignments.length; ii++) {
|
|
|
|
if (!(alignments[ii] in disallow)) {
|
|
|
|
align = alignments[ii];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (align) {
|
2014-05-05 19:56:14 +02:00
|
|
|
case 'right':
|
|
|
|
v = v.add(d)
|
|
|
|
.add(JX.$V(-m.x, 0));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
v = v.add(0, d.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:09:44 +02:00
|
|
|
this._setMenuNodePosition(v);
|
|
|
|
},
|
2014-05-05 19:56:38 +02:00
|
|
|
|
2020-05-13 20:09:44 +02:00
|
|
|
_setMenuNodePosition: function(v) {
|
|
|
|
v = v.add(this.getOffsetX(), this.getOffsetY());
|
2014-05-05 19:56:14 +02:00
|
|
|
v.setPos(this._menu);
|
2014-05-05 19:57:23 +02:00
|
|
|
},
|
|
|
|
|
2020-05-13 20:09:44 +02:00
|
|
|
getMenuNodeDimensions: function() {
|
|
|
|
if (!this._visible) {
|
|
|
|
document.body.appendChild(this._menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
var dim = JX.Vector.getDim(this._menu);
|
|
|
|
|
|
|
|
if (!this._visible) {
|
|
|
|
JX.DOM.remove(this._menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dim;
|
|
|
|
},
|
|
|
|
|
2014-05-05 19:57:23 +02:00
|
|
|
_onkey: function(e) {
|
|
|
|
// When the user presses escape with a menu open, close the menu and
|
|
|
|
// refocus the button which activates the menu. In particular, this makes
|
|
|
|
// popups more usable with assistive technologies.
|
|
|
|
|
|
|
|
if (!this._open) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.getSpecialKey() != 'esc') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.close();
|
2020-05-13 20:09:44 +02:00
|
|
|
|
|
|
|
if (this._node) {
|
|
|
|
JX.DOM.focus(this._node);
|
|
|
|
}
|
2014-05-05 19:57:23 +02:00
|
|
|
|
|
|
|
e.prevent();
|
2014-05-05 19:56:14 +02:00
|
|
|
}
|
2014-05-05 19:57:23 +02:00
|
|
|
|
2014-05-05 19:56:14 +02:00
|
|
|
}
|
|
|
|
});
|