2019-08-17 22:50:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Alex Yatskov <alex@foosoft.net>
|
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
class PopupProxyHost {
|
|
|
|
constructor() {
|
|
|
|
this.popups = {};
|
|
|
|
this.nextId = 0;
|
2019-08-19 00:51:19 +00:00
|
|
|
this.apiReceiver = null;
|
|
|
|
this.frameIdPromise = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
static create() {
|
|
|
|
const popupProxyHost = new PopupProxyHost();
|
|
|
|
popupProxyHost.prepare();
|
|
|
|
return popupProxyHost;
|
|
|
|
}
|
|
|
|
|
|
|
|
async prepare() {
|
|
|
|
this.frameIdPromise = apiFrameInformationGet();
|
|
|
|
const {frameId} = await this.frameIdPromise;
|
|
|
|
if (typeof frameId !== 'number') { return; }
|
|
|
|
|
|
|
|
this.apiReceiver = new FrontendApiReceiver(`popup-proxy-host#${frameId}`, {
|
2019-08-17 22:50:48 +00:00
|
|
|
createNestedPopup: ({parentId}) => this.createNestedPopup(parentId),
|
2019-10-12 18:34:45 +00:00
|
|
|
setOptions: ({id, options}) => this.setOptions(id, options),
|
2019-09-29 17:00:32 +00:00
|
|
|
hide: ({id, changeFocus}) => this.hide(id, changeFocus),
|
2019-10-17 22:39:12 +00:00
|
|
|
isVisibleAsync: ({id}) => this.isVisibleAsync(id),
|
2019-10-06 01:38:13 +00:00
|
|
|
setVisibleOverride: ({id, visible}) => this.setVisibleOverride(id, visible),
|
2019-09-14 18:27:25 +00:00
|
|
|
containsPoint: ({id, x, y}) => this.containsPoint(id, x, y),
|
2019-10-17 01:36:10 +00:00
|
|
|
showContent: ({id, elementRect, writingMode, type, details}) => this.showContent(id, elementRect, writingMode, type, details),
|
2019-10-12 21:12:34 +00:00
|
|
|
setCustomCss: ({id, css}) => this.setCustomCss(id, css),
|
2019-08-17 22:50:48 +00:00
|
|
|
clearAutoPlayTimer: ({id}) => this.clearAutoPlayTimer(id)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-16 00:23:25 +00:00
|
|
|
createPopup(parentId, depth) {
|
2019-11-25 19:19:18 +00:00
|
|
|
const parent = (typeof parentId === 'string' && hasOwn(this.popups, parentId) ? this.popups[parentId] : null);
|
2019-08-17 22:50:48 +00:00
|
|
|
const id = `${this.nextId}`;
|
2019-10-16 00:23:25 +00:00
|
|
|
if (parent !== null) {
|
|
|
|
depth = parent.depth + 1;
|
|
|
|
}
|
2019-08-17 22:50:48 +00:00
|
|
|
++this.nextId;
|
2019-08-19 00:51:19 +00:00
|
|
|
const popup = new Popup(id, depth, this.frameIdPromise);
|
2019-08-17 22:50:48 +00:00
|
|
|
if (parent !== null) {
|
|
|
|
popup.parent = parent;
|
2019-09-01 16:47:33 +00:00
|
|
|
parent.child = popup;
|
2019-08-17 22:50:48 +00:00
|
|
|
}
|
|
|
|
this.popups[id] = popup;
|
|
|
|
return popup;
|
|
|
|
}
|
|
|
|
|
|
|
|
async createNestedPopup(parentId) {
|
2019-10-16 00:23:25 +00:00
|
|
|
return this.createPopup(parentId, 0).id;
|
2019-08-17 22:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getPopup(id) {
|
2019-11-25 19:19:18 +00:00
|
|
|
if (!hasOwn(this.popups, id)) {
|
2019-10-08 00:46:02 +00:00
|
|
|
throw new Error('Invalid popup ID');
|
2019-08-17 22:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.popups[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonRectToDOMRect(popup, jsonRect) {
|
|
|
|
let x = jsonRect.x;
|
|
|
|
let y = jsonRect.y;
|
|
|
|
if (popup.parent !== null) {
|
|
|
|
const popupRect = popup.parent.container.getBoundingClientRect();
|
|
|
|
x += popupRect.x;
|
|
|
|
y += popupRect.y;
|
|
|
|
}
|
|
|
|
return new DOMRect(x, y, jsonRect.width, jsonRect.height);
|
|
|
|
}
|
|
|
|
|
2019-10-12 18:34:45 +00:00
|
|
|
async setOptions(id, options) {
|
|
|
|
const popup = this.getPopup(id);
|
|
|
|
return await popup.setOptions(options);
|
|
|
|
}
|
|
|
|
|
2019-09-29 17:00:32 +00:00
|
|
|
async hide(id, changeFocus) {
|
2019-08-17 22:50:48 +00:00
|
|
|
const popup = this.getPopup(id);
|
2019-09-29 17:00:32 +00:00
|
|
|
return popup.hide(changeFocus);
|
2019-08-17 22:50:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 22:39:12 +00:00
|
|
|
async isVisibleAsync(id) {
|
|
|
|
const popup = this.getPopup(id);
|
|
|
|
return await popup.isVisibleAsync();
|
|
|
|
}
|
|
|
|
|
2019-10-06 01:38:13 +00:00
|
|
|
async setVisibleOverride(id, visible) {
|
2019-08-17 22:50:48 +00:00
|
|
|
const popup = this.getPopup(id);
|
2019-10-17 22:39:12 +00:00
|
|
|
return await popup.setVisibleOverride(visible);
|
2019-08-17 22:50:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 18:27:25 +00:00
|
|
|
async containsPoint(id, x, y) {
|
2019-08-17 22:50:48 +00:00
|
|
|
const popup = this.getPopup(id);
|
2019-09-14 18:27:25 +00:00
|
|
|
return await popup.containsPoint(x, y);
|
2019-08-17 22:50:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 01:36:10 +00:00
|
|
|
async showContent(id, elementRect, writingMode, type, details) {
|
2019-08-17 22:50:48 +00:00
|
|
|
const popup = this.getPopup(id);
|
|
|
|
elementRect = this.jsonRectToDOMRect(popup, elementRect);
|
2019-10-17 01:36:10 +00:00
|
|
|
if (!PopupProxyHost.popupCanShow(popup)) { return Promise.resolve(false); }
|
|
|
|
return await popup.showContent(elementRect, writingMode, type, details);
|
2019-08-17 22:50:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 21:12:34 +00:00
|
|
|
async setCustomCss(id, css) {
|
|
|
|
const popup = this.getPopup(id);
|
|
|
|
return popup.setCustomCss(css);
|
|
|
|
}
|
|
|
|
|
2019-08-17 22:50:48 +00:00
|
|
|
async clearAutoPlayTimer(id) {
|
|
|
|
const popup = this.getPopup(id);
|
|
|
|
return popup.clearAutoPlayTimer();
|
|
|
|
}
|
2019-09-12 01:32:27 +00:00
|
|
|
|
|
|
|
static popupCanShow(popup) {
|
|
|
|
return popup.parent === null || popup.parent.isVisible();
|
|
|
|
}
|
2019-08-17 22:50:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 00:51:19 +00:00
|
|
|
PopupProxyHost.instance = PopupProxyHost.create();
|