ExternalLibs.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <template>
  2. <Window ref="window" @close="close">
  3. <template slot="header">
  4. {{ header }}
  5. </template>
  6. <div v-show="ready" class="col column" style="min-width: 600px">
  7. <div class="row items-center q-px-sm" style="height: 50px">
  8. <q-select class="q-mr-sm" v-model="rootLink" :options="rootLinkOptions"
  9. style="width: 230px"
  10. dropdown-icon="la la-angle-down la-sm"
  11. rounded outlined dense emit-value map-options display-value-sanitize options-sanitize
  12. >
  13. <template v-slot:prepend>
  14. <q-btn class="q-mr-xs" round dense color="blue" icon="la la-plus" size="12px"/>
  15. <q-btn round dense color="blue" icon="la la-bars" size="12px"/>
  16. </template>
  17. <template v-slot:selected>
  18. <div style="overflow: hidden; white-space: nowrap;">{{ removeProtocol(rootLink) }}</div>
  19. </template>
  20. </q-select>
  21. <q-select class="q-mr-sm" v-model="selectedLink" :options="selectedLinkOptions" style="width: 50px"
  22. dropdown-icon="la la-angle-down la-sm"
  23. rounded outlined dense emit-value map-options hide-selected display-value-sanitize options-sanitize
  24. >
  25. </q-select>
  26. <q-input class="col q-mr-sm" ref="input" rounded outlined dense bg-color="white" v-model="bookUrl" placeholder="Скопируйте сюда URL книги" @focus="onInputFocus">
  27. <template v-slot:prepend>
  28. <q-btn class="q-mr-xs" round dense color="blue" icon="la la-home" @click="goToStartLink" size="12px">
  29. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">Вернуться на стартовую страницу</q-tooltip>
  30. </q-btn>
  31. <q-btn round dense color="blue" icon="la la-angle-double-down" @click="openBookUrlInFrame" size="12px">
  32. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">Загрузить URL во фрейм</q-tooltip>
  33. </q-btn>
  34. </template>
  35. </q-input>
  36. <q-btn rounded color="green-7" no-caps size="14px" @click="submitUrl">Открыть
  37. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">Открыть в читалке</q-tooltip>
  38. </q-btn>
  39. </div>
  40. <div class="separator"></div>
  41. <iframe v-if="frameVisible" class="col fit" ref="frame" :src="frameSrc" sandbox="allow-same-origin" frameborder="0"></iframe>
  42. </div>
  43. </Window>
  44. </template>
  45. <script>
  46. //-----------------------------------------------------------------------------
  47. import Vue from 'vue';
  48. import Component from 'vue-class-component';
  49. import _ from 'lodash';
  50. import Window from '../share/Window.vue';
  51. import * as utils from '../../share/utils';
  52. //import rstore from '../../store/modules/reader';
  53. export default @Component({
  54. components: {
  55. Window
  56. },
  57. watch: {
  58. libs: function() {
  59. this.loadLibs();
  60. },
  61. rootLink: function() {
  62. this.updateSelectedLink();
  63. this.updateStartLink();
  64. },
  65. selectedLink: function() {
  66. this.updateStartLink();
  67. }
  68. }
  69. })
  70. class ExternalLibs extends Vue {
  71. ready = false;
  72. frameVisible = false;
  73. startLink = '';
  74. rootLink = '';
  75. selectedLink = '';
  76. frameSrc = '';
  77. bookUrl = '';
  78. libs = {};
  79. created() {
  80. this.$root.addKeyHook(this.keyHook);
  81. //this.commit = this.$store.commit;
  82. //this.commit('reader/setLibs', rstore.libsDefaults);
  83. }
  84. mounted() {
  85. this.$refs.window.init();
  86. this.opener = null;
  87. const host = window.location.host;
  88. const openerHost = (host.indexOf('b.') == 0 ? host.substring(2) : host);
  89. const openerOrigin1 = `http://${openerHost}`;
  90. const openerOrigin2 = `https://${openerHost}`;
  91. window.addEventListener('message', (event) => {
  92. if (event.origin !== openerOrigin1 && event.origin !== openerOrigin2)
  93. return;
  94. if (!_.isObject(event.data) || event.data.from != 'LibsPage')
  95. return;
  96. if (event.origin == openerOrigin1)
  97. this.opener = window.opener;
  98. else
  99. this.opener = event.source;
  100. this.openerOrigin = event.origin;
  101. //console.log(event);
  102. this.recvMessage(event.data);
  103. });
  104. //Проверка закрытия родительского окна
  105. (async() => {
  106. let i = 0;
  107. while(!this.opener && i < 10) {
  108. await utils.sleep(1000);
  109. i++;
  110. }
  111. if (i >= 10) {
  112. await this.$root.stdDialog.alert('Нет связи с читалкой. Окно будет закрыто', 'Ошибка');
  113. window.close();
  114. }
  115. while(this.opener) {
  116. await this.checkOpener();
  117. await utils.sleep(1000);
  118. }
  119. })();
  120. }
  121. recvMessage(d) {
  122. if (d.type == 'mes') {
  123. switch(d.data) {
  124. case 'hello': this.sendMessage({type: 'mes', data: 'ready'}); break;
  125. }
  126. } else if (d.type == 'libs') {
  127. this.ready = true;
  128. this.libs = _.cloneDeep(d.data);
  129. this.goToStartLink();
  130. } else if (d.type == 'notify') {
  131. this.$root.notify.success(d.data);
  132. }
  133. }
  134. sendMessage(d) {
  135. (async() => {
  136. await this.checkOpener();
  137. if (this.opener && this.openerOrigin)
  138. this.opener.postMessage(Object.assign({}, {from: 'ExternalLibs'}, d), this.openerOrigin);
  139. })();
  140. }
  141. async checkOpener() {
  142. if (this.opener.closed) {
  143. await this.$root.stdDialog.alert('Потеряна связь с читалкой. Окно будет закрыто', 'Ошибка');
  144. window.close();
  145. }
  146. }
  147. commitLibs(libs) {
  148. this.sendMessage({type: 'libs', data: libs});
  149. }
  150. loadLibs() {
  151. const libs = this.libs;
  152. this.startLink = (libs.comment ? libs.comment + ' ': '') + this.removeProtocol(libs.startLink);
  153. this.rootLink = this.getOrigin(libs.startLink);
  154. this.updateSelectedLink();
  155. }
  156. get header() {
  157. let result = (this.ready ? 'Библиотека' : 'Загрузка...');
  158. if (this.ready && this.startLink) {
  159. result += ` | ${this.startLink}`;
  160. }
  161. this.$root.$emit('set-app-title', result);
  162. return result;
  163. }
  164. updateSelectedLink() {
  165. if (!this.ready)
  166. return;
  167. const index = this.getRootIndexByUrl(this.rootLink);
  168. if (index >= 0)
  169. this.selectedLink = this.libs.groups[index].s;
  170. }
  171. updateStartLink() {
  172. if (!this.ready)
  173. return;
  174. const index = this.getRootIndexByUrl(this.rootLink);
  175. if (index >= 0) {
  176. let libs = _.cloneDeep(this.libs);
  177. libs.groups[index].s = this.selectedLink;
  178. libs.startLink = this.selectedLink;
  179. libs.comment = this.getCommentByLink(libs.groups[index].list, this.selectedLink);
  180. this.frameSrc = this.selectedLink;
  181. this.commitLibs(libs);
  182. }
  183. }
  184. get rootLinkOptions() {
  185. let result = [];
  186. if (!this.ready)
  187. return result;
  188. this.libs.groups.forEach(group => {
  189. result.push({label: this.removeProtocol(group.r), value: group.r});
  190. });
  191. return result;
  192. }
  193. get selectedLinkOptions() {
  194. let result = [];
  195. if (!this.ready)
  196. return result;
  197. const index = this.getRootIndexByUrl(this.rootLink);
  198. if (index >= 0) {
  199. this.libs.groups[index].list.forEach(link => {
  200. result.push({label: (link.c ? link.c + ' ': '') + this.removeOrigin(link.l), value: link.l});
  201. });
  202. }
  203. return result;
  204. }
  205. openBookUrlInFrame() {
  206. if (this.bookUrl)
  207. this.frameSrc = this.addProtocol(this.bookUrl);
  208. }
  209. goToStartLink() {
  210. if (!this.ready)
  211. return;
  212. this.frameSrc = this.libs.startLink;
  213. this.frameVisible = false;
  214. this.$nextTick(() => {
  215. this.frameVisible = true;
  216. });
  217. }
  218. addProtocol(url) {
  219. if ((url.indexOf('http://') != 0) && (url.indexOf('https://') != 0))
  220. return 'http://' + url;
  221. return url;
  222. }
  223. removeProtocol(url) {
  224. return url.replace(/(^\w+:|^)\/\//, '');
  225. }
  226. getOrigin(url) {
  227. const parsed = new URL(url);
  228. return parsed.origin;
  229. }
  230. removeOrigin(url) {
  231. const parsed = new URL(url);
  232. const result = url.substring(parsed.origin.length);
  233. return (result ? result : '/');
  234. }
  235. getRootIndexByUrl(url) {
  236. if (!this.ready)
  237. return -1;
  238. const origin = this.getOrigin(url);
  239. const groups = this.libs.groups;
  240. for (let i = 0; i < groups.length; i++) {
  241. if (groups[i].r == origin)
  242. return i;
  243. }
  244. return -1;
  245. }
  246. getCommentByLink(list, link) {
  247. for (const item of list) {
  248. if (item.l == link)
  249. return item.c;
  250. }
  251. return '';
  252. }
  253. onInputFocus() {
  254. this.$refs.input.select();
  255. }
  256. submitUrl() {
  257. if (this.bookUrl) {
  258. this.sendMessage({type: 'submitUrl', data: {url: this.addProtocol(this.bookUrl), force: true}});
  259. this.bookUrl = '';
  260. if (this.libs.closeAfterSubmit)
  261. this.close();
  262. }
  263. }
  264. close() {
  265. this.sendMessage({type: 'close'});
  266. }
  267. keyHook() {
  268. //недостатки сторонних ui
  269. const input = this.$refs.input.$refs.input;
  270. if (document.activeElement === input && event.type == 'keydown' && event.code == 'Enter') {
  271. this.submitUrl();
  272. return true;
  273. }
  274. if (event.type == 'keydown' && (event.code == 'Escape')) {
  275. this.close();
  276. }
  277. return true;
  278. }
  279. }
  280. //-----------------------------------------------------------------------------
  281. </script>
  282. <style scoped>
  283. .separator {
  284. height: 1px;
  285. background-color: #A0A0A0;
  286. }
  287. </style>