Работа над LibsPage

This commit is contained in:
Book Pauk
2020-10-29 14:45:10 +07:00
parent ac6ebb9e8d
commit 57e521e2ff
2 changed files with 106 additions and 29 deletions

View File

@@ -1,10 +1,10 @@
<template> <template>
<Window ref="window" @close="close"> <Window ref="window" @close="close">
<template slot="header"> <template slot="header">
Библиотека <span v-show="startLink">(выбрано {{ startLink }})</span> {{ header }}
</template> </template>
<div class="col column" style="min-width: 600px"> <div v-show="ready" class="col column" style="min-width: 600px">
<div class="row items-center q-px-sm" style="height: 50px"> <div class="row items-center q-px-sm" style="height: 50px">
<q-select class="q-mr-sm" v-model="rootLink" :options="rootLinkOptions" <q-select class="q-mr-sm" v-model="rootLink" :options="rootLinkOptions"
style="width: 230px" style="width: 230px"
@@ -51,6 +51,7 @@ import Component from 'vue-class-component';
import _ from 'lodash'; import _ from 'lodash';
import Window from '../share/Window.vue'; import Window from '../share/Window.vue';
import * as utils from '../../share/utils';
//import rstore from '../../store/modules/reader'; //import rstore from '../../store/modules/reader';
export default @Component({ export default @Component({
@@ -71,23 +72,23 @@ export default @Component({
} }
}) })
class ExternalLibs extends Vue { class ExternalLibs extends Vue {
ready = false;
frameVisible = false; frameVisible = false;
startLink = ''; startLink = '';
rootLink = ''; rootLink = '';
selectedLink = ''; selectedLink = '';
frameSrc = ''; frameSrc = '';
bookUrl = ''; bookUrl = '';
libs = {};
created() { created() {
this.commit = this.$store.commit; this.$root.addKeyHook(this.keyHook);
this.loadLibs(); //this.commit = this.$store.commit;
//this.commit('reader/setLibs', rstore.libsDefaults); //this.commit('reader/setLibs', rstore.libsDefaults);
} }
mounted() { mounted() {
this.$refs.window.init(); this.$refs.window.init();
if (!this.frameSrc)
this.goToStartLink();
this.opener = null; this.opener = null;
const host = window.location.host; const host = window.location.host;
@@ -106,10 +107,31 @@ class ExternalLibs extends Vue {
this.opener = event.source; this.opener = event.source;
this.openerOrigin = event.origin; this.openerOrigin = event.origin;
console.log(event); //console.log(event);
this.recvMessage(event.data); this.recvMessage(event.data);
}, false); });
//Проверка закрытия родительского окна
(async() => {
let i = 0;
while(!this.opener && i < 10) {
await utils.sleep(1000);
i++;
}
if (i >= 10) {
await this.$root.stdDialog.alert('Нет связи с читалкой. Окно будет закрыто', 'Ошибка');
window.close();
}
while(this.opener) {
if (this.opener.closed) {
await this.$root.stdDialog.alert('Потеряна связь с читалкой. Окно будет закрыто', 'Ошибка');
window.close();
}
await utils.sleep(1000);
}
})();
} }
recvMessage(d) { recvMessage(d) {
@@ -117,8 +139,10 @@ console.log(event);
switch(d.data) { switch(d.data) {
case 'hello': this.sendMessage({type: 'mes', data: 'ready'}); break; case 'hello': this.sendMessage({type: 'mes', data: 'ready'}); break;
} }
} else if (d.type == 'obj') { } else if (d.type == 'libs') {
// this.ready = true;
this.libs = _.cloneDeep(d.data);
this.goToStartLink();
} }
} }
@@ -127,8 +151,8 @@ console.log(event);
this.opener.postMessage(Object.assign({}, {from: 'ExternalLibs'}, d), this.openerOrigin); this.opener.postMessage(Object.assign({}, {from: 'ExternalLibs'}, d), this.openerOrigin);
} }
get libs() { commitLibs(libs) {
return this.$store.state.reader.libs; this.sendMessage({type: 'libs', data: libs});
} }
loadLibs() { loadLibs() {
@@ -138,13 +162,26 @@ console.log(event);
this.updateSelectedLink(); this.updateSelectedLink();
} }
get header() {
let result = (this.ready ? 'Библиотека' : 'Загрузка...');
if (this.ready && this.startLink) {
result += ` | ${this.startLink}`;
}
this.$root.$emit('set-app-title', result);
return result;
}
updateSelectedLink() { updateSelectedLink() {
if (!this.ready)
return;
const index = this.getRootIndexByUrl(this.rootLink); const index = this.getRootIndexByUrl(this.rootLink);
if (index >= 0) if (index >= 0)
this.selectedLink = this.libs.groups[index].s; this.selectedLink = this.libs.groups[index].s;
} }
updateStartLink() { updateStartLink() {
if (!this.ready)
return;
const index = this.getRootIndexByUrl(this.rootLink); const index = this.getRootIndexByUrl(this.rootLink);
if (index >= 0) { if (index >= 0) {
let libs = _.cloneDeep(this.libs); let libs = _.cloneDeep(this.libs);
@@ -152,12 +189,15 @@ console.log(event);
libs.startLink = this.selectedLink; libs.startLink = this.selectedLink;
libs.comment = this.getCommentByLink(libs.groups[index].list, this.selectedLink); libs.comment = this.getCommentByLink(libs.groups[index].list, this.selectedLink);
this.frameSrc = this.selectedLink; this.frameSrc = this.selectedLink;
this.commit('reader/setLibs', libs); this.commitLibs(libs);
} }
} }
get rootLinkOptions() { get rootLinkOptions() {
let result = []; let result = [];
if (!this.ready)
return result;
this.libs.groups.forEach(group => { this.libs.groups.forEach(group => {
result.push({label: this.removeProtocol(group.r), value: group.r}); result.push({label: this.removeProtocol(group.r), value: group.r});
}); });
@@ -167,6 +207,9 @@ console.log(event);
get selectedLinkOptions() { get selectedLinkOptions() {
let result = []; let result = [];
if (!this.ready)
return result;
const index = this.getRootIndexByUrl(this.rootLink); const index = this.getRootIndexByUrl(this.rootLink);
if (index >= 0) { if (index >= 0) {
this.libs.groups[index].list.forEach(link => { this.libs.groups[index].list.forEach(link => {
@@ -183,6 +226,9 @@ console.log(event);
} }
goToStartLink() { goToStartLink() {
if (!this.ready)
return;
this.frameSrc = this.libs.startLink; this.frameSrc = this.libs.startLink;
this.frameVisible = false; this.frameVisible = false;
this.$nextTick(() => { this.$nextTick(() => {
@@ -212,6 +258,8 @@ console.log(event);
} }
getRootIndexByUrl(url) { getRootIndexByUrl(url) {
if (!this.ready)
return -1;
const origin = this.getOrigin(url); const origin = this.getOrigin(url);
const groups = this.libs.groups; const groups = this.libs.groups;
for (let i = 0; i < groups.length; i++) { for (let i = 0; i < groups.length; i++) {
@@ -241,7 +289,7 @@ console.log(event);
} }
close() { close() {
this.$emit('do-action', {action: 'libs'}); this.sendMessage({type: 'close'});
} }
keyHook() { keyHook() {

View File

@@ -17,7 +17,7 @@ export default @Component({
}, },
watch: { watch: {
libs: function() { libs: function() {
this.loadLibs(); this.sendLibs();
}, },
} }
}) })
@@ -29,13 +29,25 @@ class LibsPage extends Vue {
} }
init() { init() {
this.childReady = false;
const subdomain = (window.location.protocol != 'http:' ? 'b.' : ''); const subdomain = (window.location.protocol != 'http:' ? 'b.' : '');
const origin = `http://${subdomain}${window.location.host}`; this.origin = `http://${subdomain}${window.location.host}`;
this.popupWindow = window.open(`${origin}/#/external-libs`); this.messageListener = (event) => {
if (event.origin !== this.origin)
return;
//console.log(event.data);
this.recvMessage(event.data);
};
this.popupWindow = window.open(`${this.origin}/#/external-libs`);
if (this.popupWindow) { if (this.popupWindow) {
window.addEventListener('message', this.messageListener);
//Проверка закрытия окна //Проверка закрытия окна
(async() => { (async() => {
while(this.popupWindow) { while(this.popupWindow) {
@@ -45,24 +57,40 @@ class LibsPage extends Vue {
} }
})(); })();
window.addEventListener('message', (event) => { //Установление связи с окном
if (event.origin !== origin)
return;
console.log(event.data);
}, false);
(async() => { (async() => {
while(this.popupWindow) { let i = 0;
this.popupWindow.postMessage({from: 'LibsPage', type: 'mes', data: 'hello'}, origin); while(!this.childReady && this.popupWindow && i < 100) {
await utils.sleep(1000); this.sendMessage({type: 'mes', data: 'hello'});
await utils.sleep(100);
i++;
} }
this.sendLibs();
})(); })();
this.loadLibs();
} }
} }
recvMessage(d) {
if (d.type == 'mes') {
switch(d.data) {
case 'ready':
this.childReady = true;
break;
}
} else if (d.type == 'libs') {
this.commit('reader/setLibs', d.data);
} else if (d.type == 'close') {
this.close();
}
}
sendMessage(d) {
if (this.popupWindow)
this.popupWindow.postMessage(Object.assign({}, {from: 'LibsPage'}, d), this.origin);
}
done() { done() {
window.removeEventListener('message', this.messageListener);
if (this.popupWindow) { if (this.popupWindow) {
this.popupWindow.close(); this.popupWindow.close();
this.popupWindow = null; this.popupWindow = null;
@@ -73,7 +101,8 @@ class LibsPage extends Vue {
return this.$store.state.reader.libs; return this.$store.state.reader.libs;
} }
loadLibs() { sendLibs() {
this.sendMessage({type: 'libs', data: this.libs});
} }
/* submitUrl() { /* submitUrl() {