forked from ReDEnergy/SessionSync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext-menu.js
More file actions
142 lines (115 loc) · 3.71 KB
/
Copy pathcontext-menu.js
File metadata and controls
142 lines (115 loc) · 3.71 KB
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
define(function(require, exports) {
'use strict';
// *****************************************************************************
// Custom Modules
// App
const { AppConfig } = require('../config');
const { HTMLCreator } = require('./dom');
// TODO - remove window/global events
const { WindowEvents, GlobalEvents } = require('./global-events');
// var ID = 0;
// *****************************************************************************
// Bookmark Context Menu
var previousContextMenu = null;
function ContextMenu(options)
{
var DomElem = HTMLCreator();
// ------------------------------------------------------------------------
// Create UI
// var self = this;
var menu = DomElem('div', { class : 'context-menu' });
if (options.width != undefined) {
menu.style.width = options.width + 'px';
}
var context = undefined;
// ------------------------------------------------------------------------
// Events
var openMenu = function openMenu(event)
{
if (previousContextMenu && previousContextMenu != this) {
previousContextMenu.hideMenu();
}
previousContextMenu = this;
menu.setAttribute('data-active', 'true');
var padding = 10;
menu.style.left = Math.min(event.pageX, window.innerWidth - menu.clientWidth - padding) + 'px';
menu.style.top = Math.min(event.pageY, window.innerHeight - menu.clientHeight - padding) + 'px';
document.addEventListener('mousedown', blurMenu);
WindowEvents.emit(document, 'ConfimBox-Hide');
}.bind(this);
var hideMenu = function hideMenu(e)
{
menu.removeAttribute('data-active');
document.removeEventListener('mousedown', blurMenu);
WindowEvents.emit(document, 'OverlaySystem', { state: false });
};
var blurMenu = function hideWhenClickOutside(e) {
var target = e.target;
while (target) {
if (target === menu)
return;
target = target.parentElement;
}
hideMenu();
};
// Actions
// TODO - investigate switch to event delegation
var NewAction = function NewAction(opt) {
if (opt.func) {
return function actionEvent(e) {
context[opt.func](e);
hideMenu();
};
}
if (opt.callback) {
return function actionEvent(e) {
opt.callback(context, e);
hideMenu();
};
}
if (opt.globalEvent) {
return function action() {
GlobalEvents.emit(opt.globalEvent, context);
hideMenu();
};
}
return function action() {
WindowEvents.emit(document, options.name + '-' + opt.event, context);
hideMenu();
};
};
var addMenuEntry = function AddMenuEntry(opt)
{
var event = NewAction(opt);
var entry = DomElem('div', {class : 'button'});
entry.textContent = opt.value;
entry.setAttribute('icon', opt.icon);
menu.appendChild(entry);
var addSeparator = opt.separator;
if (addSeparator != undefined)
{
var separator = DomElem('div', {class : 'separator'});
menu.insertBefore(separator, addSeparator == 'top' ? entry : undefined);
}
entry.addEventListener('click', event);
};
// Register custom events
WindowEvents.on(document, options.name + '-Open', function(options) {
// menu.setAttribute('icons', options.showIcons ? 'yes' : 'no');
openMenu(options.event);
context = options.context;
WindowEvents.emit(document, 'OverlaySystem', { state: true });
});
AppConfig.onChange('context.menu.icons', function(value) {
menu.setAttribute('icons', value ? 'yes' : 'no');
});
// ------------------------------------------------------------------------
// Public properties
this.DOMRoot = menu;
this.hideMenu = hideMenu;
this.addMenuEntry = addMenuEntry;
}
// *****************************************************************************
// Public API
exports.ContextMenu = ContextMenu;
});