yomichan/ext/fg/js/popup-proxy-host.js

146 lines
5.1 KiB
JavaScript
Raw Normal View History

/*
* 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;
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}`, {
createNestedPopup: ({parentId}) => this.createNestedPopup(parentId),
2019-10-12 18:34:45 +00:00
setOptions: ({id, options}) => this.setOptions(id, options),
showOrphaned: ({id, elementRect}) => this.showOrphaned(id, elementRect),
hide: ({id, changeFocus}) => this.hide(id, changeFocus),
setVisibleOverride: ({id, visible}) => this.setVisibleOverride(id, visible),
containsPoint: ({id, x, y}) => this.containsPoint(id, x, y),
termsShow: ({id, elementRect, writingMode, definitions, context}) => this.termsShow(id, elementRect, writingMode, definitions, context),
kanjiShow: ({id, elementRect, writingMode, definitions, context}) => this.kanjiShow(id, elementRect, writingMode, definitions, context),
2019-10-12 21:12:34 +00:00
setCustomCss: ({id, css}) => this.setCustomCss(id, css),
clearAutoPlayTimer: ({id}) => this.clearAutoPlayTimer(id)
});
}
createPopup(parentId) {
const parent = (typeof parentId === 'string' && this.popups.hasOwnProperty(parentId) ? this.popups[parentId] : null);
const depth = (parent !== null ? parent.depth + 1 : 0);
const id = `${this.nextId}`;
++this.nextId;
const popup = new Popup(id, depth, this.frameIdPromise);
if (parent !== null) {
popup.parent = parent;
2019-09-01 16:47:33 +00:00
parent.child = popup;
}
this.popups[id] = popup;
return popup;
}
async createNestedPopup(parentId) {
return this.createPopup(parentId).id;
}
getPopup(id) {
if (!this.popups.hasOwnProperty(id)) {
2019-10-08 00:46:02 +00:00
throw new Error('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);
}
2019-10-12 18:34:45 +00:00
async setOptions(id, options) {
const popup = this.getPopup(id);
return await popup.setOptions(options);
}
async showOrphaned(id, elementRect) {
const popup = this.getPopup(id);
elementRect = this.jsonRectToDOMRect(popup, elementRect);
return await popup.showOrphaned(elementRect);
}
async hide(id, changeFocus) {
const popup = this.getPopup(id);
return popup.hide(changeFocus);
}
async setVisibleOverride(id, visible) {
const popup = this.getPopup(id);
return popup.setVisibleOverride(visible);
}
async containsPoint(id, x, y) {
const popup = this.getPopup(id);
return await popup.containsPoint(x, y);
}
async termsShow(id, elementRect, writingMode, definitions, context) {
const popup = this.getPopup(id);
elementRect = this.jsonRectToDOMRect(popup, elementRect);
if (!PopupProxyHost.popupCanShow(popup)) { return false; }
return await popup.termsShow(elementRect, writingMode, definitions, context);
}
async kanjiShow(id, elementRect, writingMode, definitions, context) {
const popup = this.getPopup(id);
elementRect = this.jsonRectToDOMRect(popup, elementRect);
if (!PopupProxyHost.popupCanShow(popup)) { return false; }
return await popup.kanjiShow(elementRect, writingMode, definitions, context);
}
2019-10-12 21:12:34 +00:00
async setCustomCss(id, css) {
const popup = this.getPopup(id);
return popup.setCustomCss(css);
}
async clearAutoPlayTimer(id) {
const popup = this.getPopup(id);
return popup.clearAutoPlayTimer();
}
static popupCanShow(popup) {
return popup.parent === null || popup.parent.isVisible();
}
}
PopupProxyHost.instance = PopupProxyHost.create();