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),
|
|
|
|
show: ({id, elementRect, options}) => this.show(id, elementRect, options),
|
|
|
|
showOrphaned: ({id, elementRect, options}) => this.show(id, elementRect, options),
|
|
|
|
hide: ({id}) => this.hide(id),
|
|
|
|
setVisible: ({id, visible}) => this.setVisible(id, visible),
|
2019-09-14 18:27:25 +00:00
|
|
|
containsPoint: ({id, x, y}) => this.containsPoint(id, x, y),
|
2019-09-12 01:29:08 +00:00
|
|
|
termsShow: ({id, elementRect, writingMode, definitions, options, context}) => this.termsShow(id, elementRect, writingMode, definitions, options, context),
|
|
|
|
kanjiShow: ({id, elementRect, writingMode, definitions, options, context}) => this.kanjiShow(id, elementRect, writingMode, definitions, options, context),
|
2019-08-17 22:50:48 +00:00
|
|
|
clearAutoPlayTimer: ({id}) => this.clearAutoPlayTimer(id)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createPopup(parentId) {
|
|
|
|
const parent = (typeof parentId === 'string' && this.popups.hasOwnProperty(parentId) ? this.popups[parentId] : null);
|
2019-08-17 23:32:58 +00:00
|
|
|
const depth = (parent !== null ? parent.depth + 1 : 0);
|
2019-08-17 22:50:48 +00:00
|
|
|
const id = `${this.nextId}`;
|
|
|
|
++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) {
|
|
|
|
return this.createPopup(parentId).id;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPopup(id) {
|
|
|
|
if (!this.popups.hasOwnProperty(id)) {
|
|
|
|
throw 'Invalid popup ID';
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
async show(id, elementRect, options) {
|
|
|
|
const popup = this.getPopup(id);
|
|
|
|
elementRect = this.jsonRectToDOMRect(popup, elementRect);
|
|
|
|
return await popup.show(elementRect, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async showOrphaned(id, elementRect, options) {
|
|
|
|
const popup = this.getPopup(id);
|
|
|
|
elementRect = this.jsonRectToDOMRect(popup, elementRect);
|
|
|
|
return await popup.showOrphaned(elementRect, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async hide(id) {
|
|
|
|
const popup = this.getPopup(id);
|
|
|
|
return popup.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
async setVisible(id, visible) {
|
|
|
|
const popup = this.getPopup(id);
|
|
|
|
return popup.setVisible(visible);
|
|
|
|
}
|
|
|
|
|
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-09-12 01:29:08 +00:00
|
|
|
async termsShow(id, elementRect, writingMode, definitions, options, context) {
|
2019-08-17 22:50:48 +00:00
|
|
|
const popup = this.getPopup(id);
|
|
|
|
elementRect = this.jsonRectToDOMRect(popup, elementRect);
|
2019-09-12 01:32:27 +00:00
|
|
|
if (!PopupProxyHost.popupCanShow(popup)) { return false; }
|
2019-09-12 01:29:08 +00:00
|
|
|
return await popup.termsShow(elementRect, writingMode, definitions, options, context);
|
2019-08-17 22:50:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 01:29:08 +00:00
|
|
|
async kanjiShow(id, elementRect, writingMode, definitions, options, context) {
|
2019-08-17 22:50:48 +00:00
|
|
|
const popup = this.getPopup(id);
|
|
|
|
elementRect = this.jsonRectToDOMRect(popup, elementRect);
|
2019-09-12 01:32:27 +00:00
|
|
|
if (!PopupProxyHost.popupCanShow(popup)) { return false; }
|
2019-09-12 01:29:08 +00:00
|
|
|
return await popup.kanjiShow(elementRect, writingMode, definitions, options, context);
|
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();
|