mj-context-menu
Version:
A generic context menu
102 lines • 3.55 kB
JavaScript
import { AbstractPostable } from './abstract_postable.js';
export class Popup extends AbstractPostable {
constructor(title, content) {
super();
this.title = title;
this.window = null;
this.localSettings = {
left: Math.round((screen.width - 400) / 2),
top: Math.round((screen.height - 300) / 3)
};
this.windowList = [];
this.mobileFlag = false;
this.active = null;
this.content =
content ||
function () {
return '';
};
}
attachMenu(menu) {
this.menu = menu;
}
post() {
this.display();
}
display() {
this.active = this.menu.store.active;
const settings = [];
for (const setting in Popup.popupSettings) {
settings.push(setting + '=' + Popup.popupSettings[setting]);
}
for (const setting in this.localSettings) {
settings.push(setting + '=' + this.localSettings[setting]);
}
this.window = window.open('', '_blank', settings.join(','));
this.windowList.push(this.window);
const doc = this.window.document;
if (this.mobileFlag) {
doc.open();
doc.write('<html><head><meta name="viewport" ' +
'content="width=device-width, initial-scale=1.0" /><title>' +
this.title +
'</title></head><body style="font-size:85%">');
doc.write('<pre>' + this.generateContent() + '</pre>');
doc.write('<hr><input type="button" value="' +
'Close' +
'" onclick="window.close()" />');
doc.write('</body></html>');
doc.close();
}
else {
doc.open();
doc.write('<html><head><title>' +
this.title +
'</title></head><body style="font-size:85%">');
doc.write('<table><tr><td><pre>' +
this.generateContent() +
'</pre></td></tr></table>');
doc.write('</body></html>');
doc.close();
setTimeout(this.resize.bind(this), 50);
}
}
unpost() {
this.windowList.forEach((x) => x.close());
this.window = null;
}
generateContent() {
return this.content(this.active);
}
resize() {
const table = this.window.document.body.firstChild;
let H = this.window.outerHeight - this.window.innerHeight || 30;
let W = this.window.outerWidth - this.window.innerWidth || 30;
W = Math.max(140, Math.min(Math.floor(0.5 * this.window.screen.width), table.offsetWidth + W + 25));
H = Math.max(40, Math.min(Math.floor(0.5 * this.window.screen.height), table.offsetHeight + H + 25));
this.window.resizeTo(W, H);
const bb = this.active.getBoundingClientRect();
if (bb) {
const x = Math.max(0, Math.min(bb.right - Math.floor(W / 2), this.window.screen.width - W - 20));
const y = Math.max(0, Math.min(bb.bottom - Math.floor(H / 2), this.window.screen.height - H - 20));
this.window.moveTo(x, y);
}
this.active = null;
}
toJson() {
return { type: '' };
}
}
Popup.popupSettings = {
status: 'no',
toolbar: 'no',
locationbar: 'no',
menubar: 'no',
directories: 'no',
personalbar: 'no',
resizable: 'yes',
scrollbars: 'yes',
width: 400,
height: 300
};
//# sourceMappingURL=popup.js.map