forked from Link2Twenty/l2t-context-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjas-context-menu.html
336 lines (327 loc) · 10.5 KB
/
jas-context-menu.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-listbox/paper-listbox.html" />
<link rel="import" href="../paper-item/paper-item.html" />
<link rel="import" href="../paper-item/paper-icon-item.html" />
<link rel="import" href="../iron-icon/iron-icon.html" />
<link rel="import" href="../iron-iconset-svg/iron-iconset-svg.html">
<!--
Polymer element to replace right click functionality.
### Styling
The following custom properties are available for styling:
Custom property | Description | Default
----------------|-------------|----------
`--app-primary-color` | Background color of the menu when focused. | `#fff`
`--context-text-color` | Text color within the menu. | `#333`
`--context-link-text-color` | Text color and on hover background color for links<br>For text within 'A' tags. | `#0066aa`
`--context-horizontal-rule-color` | Color of 'HR' tags. | `#bcbcbc`
@element l2t-context-menu
@demo demo/index.html
-->
<dom-module id="jas-context-menu">
<template>
<style>
.context-menu {
display: none;
position: absolute;
z-index: 1000;
padding: 6px;
background-color: var(--context-background-color, #fff);
-moz-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, .4);
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, .4);
}
.context-menu--active {
display: block;
}
paper-icon-item {
cursor: pointer;
color: var(--app-primary-color, #0066aa);
}
paper-icon-item:hover {
background-color: var(--app-primary-color, #0066aa);
color: var(--app-primary-text-color, #fff);
}
paper-item {
cursor: pointer;
color: var(--app-primary-color, #0066aa);
}
paper-item:hover {
background-color: var(--app-primary-color, #0066aa);
color: var(--app-primary-text-color, #fff);
}
hr {
background-color: var(--app-primary-color, #0066aa);
border-color: var(--app-primary-color, #0066aa);
height: 1px;
border: 0;
}
</style>
<iron-iconset-svg name="contextmenu" size="24">
<svg>
<defs>
<g id="arrow-drop-down">
<path d="M7 10l5 5 5-5z" />
</g>
</defs>
</svg>
</iron-iconset-svg>
<div id="node" class="content">
<paper-listbox id="contextMenu" class="context-menu drag-drop-ignore" allowOutsideScroll selected="-1">
<template id="resultList" is="dom-repeat" items="{{menu}}" as="m">
<template is="dom-if" if="[[m.isDivider]]">
<hr>
</template>
<template is="dom-if" if="[[m.submenu]]">
<paper-icon-item class="menu-trigger" role="menuitem" submenu="[[m.submenu]]" on-tap="_onSubMenuSelect">
<iron-icon class="menu-trigger" on-tap="_onSubMenuSelect" submenu="[[m.submenu]]" icon="contextmenu:arrow-drop-down" slot="item-icon">
</iron-icon>[[m.description]]</paper-icon-item>
</template>
<template is="dom-if" if="[[m.menu]]">
<paper-icon-item role="menuitem" command="[[m.command]]" subcommand="[[m.subcommand]]" on-tap="_onMenuSelect">
<iron-icon class="menu-trigger" on-tap="_onMenuSelect" command="[[m.command]]" subcommand="[[m.subcommand]]" icon="[[m.icon]]"
slot="item-icon">
</iron-icon>[[m.description]]</paper-icon-item>
</template>
</template>
</paper-listbox>
</div>
</template>
</dom-module>
<script>
var contextMenu;
class JasContextMenu extends Polymer.Element {
static get is() { return 'jas-context-menu'; }
static get properties() {
return {
/**
* Boolean for storing value
* for menu state
*
* @attribute _menuState
* @type Boolean
* @default "false"
*/
_menuState: {
type: Boolean,
value: 'false',
notify: true
},
/**
* Pass Menu To Build On Fly
*
* @attribute menu
* @type Object
* @default none
*/
menu: {
type: Object,
notify: true
}
}
}
constructor(mainMenu) {
super();
this.mainMenu = mainMenu;
}
/**
* Method to show the menu
* updates class and vibrates on phones
*
*/
_toggleMenuOn() {
1 !== this._menuState && (this._menuState = 1,
this.$.contextMenu.classList.add('context-menu--active'));
// Error catcher for IE and Edge
if (typeof navigator.vibrate === "function") {
navigator.vibrate([25])
}
}
/**
* Method to hide the menu
* updates class
*
*/
_toggleMenuOff() {
0 !== this._menuState && (this._menuState = 0, this.$.contextMenu.classList.remove('context-menu--active'))
}
/**
* Method to work out to place
* click event and return boolean
*
*/
_clickInsideElement(e, t) {
var n = e.path[0];
if (n.classList.contains(t)) return n;
}
/**
* Method to listen for clicks
* and pass data on to _clickInsideElement
* hides menu if click is not allowed
*
*/
_clickListener() {
var $this = this;
document.addEventListener("click", function (e) {
if ($this.ignoreNextClick == true) {
$this.ignoreNextClick = false;
return;
}
var t = $this._clickInsideElement(e, 'menu-trigger');
if (!t)
var n = e.which || e.button;
1 === n && $this._toggleMenuOff()
})
}
/**
* Method to listen for keyup
* hides menu if ESC is pressed
*
*/
_keyupListener() {
var $this = this;
window.onkeyup = function (e) {
27 === e.keyCode && $this._toggleMenuOff()
}
}
/**
* Method to listen for page resize
* hides menu when actioned
*
*/
_resizeListener() {
var $this = this;
window.onresize = function () {
$this._toggleMenuOff()
}
}
/**
* Method to get mouse position
* and return x and y values
*
*/
_getPosition(e) {
var t, n;
return e.pageX || e.pageY ? (t = e.pageX, n = e.pageY) :
(e.clientX || e.clientY) && (t = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft,
n = e.clientY + document.body.scrollTop + document.documentElement.scrollTop),
{ x: t, y: n }
}
/**
* Method to start all listeners
* called when the dom is ready
*
*/
_listenerInit() {
this._clickListener();
this._keyupListener();
this._resizeListener();
}
/**
* Method to move the menu
* passes information to _getPosition
* to get x and y cords, then restyles
*
*/
_positionMenu(e) {
var clickCoords = this._getPosition(e);
var menuSelector = this.$.contextMenu;
var menuWidth = menuSelector.offsetWidth + 4;
var menuHeight = menuSelector.offsetHeight + 4;
var windowWidth = window.innerWidth + (clickCoords.x - e.clientX);
var windowHeight = window.innerHeight + (clickCoords.y - e.clientY);
var menuCoordsX = menuWidth > windowWidth - clickCoords.x ? clickCoords.x - menuWidth + "px" : clickCoords.x + "px";
var menuCoordsY = menuHeight > windowHeight - clickCoords.y ? clickCoords.y - menuHeight + "px" : clickCoords.y + "px";
menuSelector.style.left = menuCoordsX;
menuSelector.style.top = menuCoordsY;
}
// Position on Document Clicked. This needs to be translated by the click
_positionClicked(e) {
var clickCoords = this._getPosition(e);
this.menuCoordsX = clickCoords.x;
this.menuCoordsY = clickCoords.y;
}
/**
* Method the scripts
* domReady for when the dom is connected
*
*/
connectedCallback() {
super.connectedCallback();
this._listenerInit();
// Main Menu - Sub Menu a copy is not linked as the main context menu
if (contextMenu == undefined) {
contextMenu = this;
}
}
showMenu(menuData, e, menu, node, newHandleSelect) {
// Build Menu
var aMenu;
var buildMenu = [];
var i = 0;
// Convert menu into array if it isn't. Supports multi menu options
if (!Array.isArray(menu)) {
var tempObject = menu;
menu = [];
if (tempObject != null) {
menu[0] = tempObject;
}
}
// Check if option is on menu
while (aMenu = menuData[i++]) {
if (menu.indexOf(aMenu.type) > -1) {
if (aMenu.description == "-") {
aMenu.isDivider = true;
buildMenu.push(aMenu);
} else {
if (aMenu.submenu == null) {
aMenu.menu = true;
}
buildMenu.push(aMenu);
}
}
}
this.menu = buildMenu;
this.menuType = menu;
this.ownerNode = e;
this.handleSelect = newHandleSelect;
this._openContextMenu(node);
}
_openContextMenu(e) {
var $this = this;
// Stored incase required by Add Object here for example. Unusual case but needs to be handled.
var element = app.Poly.page.shadowRoot.querySelector('#samsPageItems');
if (!this.isSubMenu) {
// Get Position
this._positionClicked(e);
this.coordsClicked = { clientX: this.menuCoordsX, clientY: this.menuCoordsY };
}
// Trick to wait for component just about to be drawn (rendered),
// so that we can measure width and move around (near bottom open upwards etc) in the right spot!
Polymer.RenderStatus.beforeNextRender(this, function () {
// Now menu updated. Run open context menu and position
$this._toggleMenuOn();
$this._positionMenu(e);
});
}
_onSubMenuSelect(data) {
data.stopPropagation();
if (this.submenu == undefined) {
this.submenu = new JasContextMenu(this);
this.submenu.setAttribute('id', "jassubmenu");
this.parentNode.appendChild(this.submenu);
this.submenu.isSubMenu = true;
}
this.submenu.coordsClicked = this.coordsClicked;
data.clientX = data.detail.x;
data.clientY = data.detail.y;
this.submenu.showMenu(data.target.submenu, this, this.menuType, data, this.handleSelect);
return false;
}
_onMenuSelect(data) {
data.stopPropagation();
this._toggleMenuOff();
this.handleSelect(data.target.command, this.ownerNode, data.target.subcommand, this.coordsClicked);
return false;
}
}
customElements.define(JasContextMenu.is, JasContextMenu);
</script>