Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
automatic1111
GitHub Repository: automatic1111/stable-diffusion-webui
Path: blob/master/javascript/contextMenus.js
3055 views
1
2
var contextMenuInit = function() {
3
let eventListenerApplied = false;
4
let menuSpecs = new Map();
5
6
const uid = function() {
7
return Date.now().toString(36) + Math.random().toString(36).substring(2);
8
};
9
10
function showContextMenu(event, element, menuEntries) {
11
let oldMenu = gradioApp().querySelector('#context-menu');
12
if (oldMenu) {
13
oldMenu.remove();
14
}
15
16
let baseStyle = window.getComputedStyle(uiCurrentTab);
17
18
const contextMenu = document.createElement('nav');
19
contextMenu.id = "context-menu";
20
contextMenu.style.background = baseStyle.background;
21
contextMenu.style.color = baseStyle.color;
22
contextMenu.style.fontFamily = baseStyle.fontFamily;
23
contextMenu.style.top = event.pageY + 'px';
24
contextMenu.style.left = event.pageX + 'px';
25
26
const contextMenuList = document.createElement('ul');
27
contextMenuList.className = 'context-menu-items';
28
contextMenu.append(contextMenuList);
29
30
menuEntries.forEach(function(entry) {
31
let contextMenuEntry = document.createElement('a');
32
contextMenuEntry.innerHTML = entry['name'];
33
contextMenuEntry.addEventListener("click", function() {
34
entry['func']();
35
});
36
contextMenuList.append(contextMenuEntry);
37
38
});
39
40
gradioApp().appendChild(contextMenu);
41
}
42
43
function appendContextMenuOption(targetElementSelector, entryName, entryFunction) {
44
45
var currentItems = menuSpecs.get(targetElementSelector);
46
47
if (!currentItems) {
48
currentItems = [];
49
menuSpecs.set(targetElementSelector, currentItems);
50
}
51
let newItem = {
52
id: targetElementSelector + '_' + uid(),
53
name: entryName,
54
func: entryFunction,
55
isNew: true
56
};
57
58
currentItems.push(newItem);
59
return newItem['id'];
60
}
61
62
function removeContextMenuOption(uid) {
63
menuSpecs.forEach(function(v) {
64
let index = -1;
65
v.forEach(function(e, ei) {
66
if (e['id'] == uid) {
67
index = ei;
68
}
69
});
70
if (index >= 0) {
71
v.splice(index, 1);
72
}
73
});
74
}
75
76
function addContextMenuEventListener() {
77
if (eventListenerApplied) {
78
return;
79
}
80
gradioApp().addEventListener("click", function(e) {
81
if (!e.isTrusted) {
82
return;
83
}
84
85
let oldMenu = gradioApp().querySelector('#context-menu');
86
if (oldMenu) {
87
oldMenu.remove();
88
}
89
});
90
['contextmenu', 'touchstart'].forEach((eventType) => {
91
gradioApp().addEventListener(eventType, function(e) {
92
let ev = e;
93
if (eventType.startsWith('touch')) {
94
if (e.touches.length !== 2) return;
95
ev = e.touches[0];
96
}
97
let oldMenu = gradioApp().querySelector('#context-menu');
98
if (oldMenu) {
99
oldMenu.remove();
100
}
101
menuSpecs.forEach(function(v, k) {
102
if (e.composedPath()[0].matches(k)) {
103
showContextMenu(ev, e.composedPath()[0], v);
104
e.preventDefault();
105
}
106
});
107
});
108
});
109
eventListenerApplied = true;
110
111
}
112
113
return [appendContextMenuOption, removeContextMenuOption, addContextMenuEventListener];
114
};
115
116
var initResponse = contextMenuInit();
117
var appendContextMenuOption = initResponse[0];
118
var removeContextMenuOption = initResponse[1];
119
var addContextMenuEventListener = initResponse[2];
120
121
(function() {
122
//Start example Context Menu Items
123
let generateOnRepeat = function(genbuttonid, interruptbuttonid) {
124
let genbutton = gradioApp().querySelector(genbuttonid);
125
let interruptbutton = gradioApp().querySelector(interruptbuttonid);
126
if (!interruptbutton.offsetParent) {
127
genbutton.click();
128
}
129
clearInterval(window.generateOnRepeatInterval);
130
window.generateOnRepeatInterval = setInterval(function() {
131
if (!interruptbutton.offsetParent) {
132
genbutton.click();
133
}
134
},
135
500);
136
};
137
138
let generateOnRepeat_txt2img = function() {
139
generateOnRepeat('#txt2img_generate', '#txt2img_interrupt');
140
};
141
142
let generateOnRepeat_img2img = function() {
143
generateOnRepeat('#img2img_generate', '#img2img_interrupt');
144
};
145
146
appendContextMenuOption('#txt2img_generate', 'Generate forever', generateOnRepeat_txt2img);
147
appendContextMenuOption('#txt2img_interrupt', 'Generate forever', generateOnRepeat_txt2img);
148
appendContextMenuOption('#img2img_generate', 'Generate forever', generateOnRepeat_img2img);
149
appendContextMenuOption('#img2img_interrupt', 'Generate forever', generateOnRepeat_img2img);
150
151
let cancelGenerateForever = function() {
152
clearInterval(window.generateOnRepeatInterval);
153
};
154
155
appendContextMenuOption('#txt2img_interrupt', 'Cancel generate forever', cancelGenerateForever);
156
appendContextMenuOption('#txt2img_generate', 'Cancel generate forever', cancelGenerateForever);
157
appendContextMenuOption('#img2img_interrupt', 'Cancel generate forever', cancelGenerateForever);
158
appendContextMenuOption('#img2img_generate', 'Cancel generate forever', cancelGenerateForever);
159
160
})();
161
//End example Context Menu Items
162
163
onAfterUiUpdate(addContextMenuEventListener);
164
165