ExternalLibs.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. export default @Component({
  53. components: {
  54. Window
  55. },
  56. watch: {
  57. libs: function() {
  58. this.loadLibs();
  59. },
  60. rootLink: function() {
  61. this.updateSelectedLink();
  62. this.updateStartLink();
  63. },
  64. selectedLink: function() {
  65. this.updateStartLink();
  66. }
  67. }
  68. })
  69. class ExternalLibs extends Vue {
  70. ready = false;
  71. frameVisible = false;
  72. startLink = '';
  73. rootLink = '';
  74. selectedLink = '';
  75. frameSrc = '';
  76. bookUrl = '';
  77. libs = {};
  78. created() {
  79. this.$root.addKeyHook(this.keyHook);
  80. //this.commit = this.$store.commit;
  81. //this.commit('reader/setLibs', rstore.libsDefaults);
  82. }
  83. mounted() {
  84. this.$refs.window.init();
  85. this.opener = null;
  86. const host = window.location.host;
  87. const openerHost = (host.indexOf('b.') == 0 ? host.substring(2) : host);
  88. const openerOrigin1 = `http://${openerHost}`;
  89. const openerOrigin2 = `https://${openerHost}`;
  90. window.addEventListener('message', (event) => {
  91. if (event.origin !== openerOrigin1 && event.origin !== openerOrigin2)
  92. return;
  93. if (!_.isObject(event.data) || event.data.from != 'LibsPage')
  94. return;
  95. if (event.origin == openerOrigin1)
  96. this.opener = window.opener;
  97. else
  98. this.opener = event.source;
  99. this.openerOrigin = event.origin;
  100. //console.log(event);
  101. this.recvMessage(event.data);
  102. });
  103. //Проверка закрытия родительского окна
  104. (async() => {
  105. let i = 0;
  106. while(!this.opener && i < 10) {
  107. await utils.sleep(1000);
  108. i++;
  109. }
  110. if (i >= 10) {
  111. await this.$root.stdDialog.alert('Нет связи с читалкой. Окно будет закрыто', 'Ошибка');
  112. window.close();
  113. }
  114. while(this.opener) {
  115. await this.checkOpener();
  116. await utils.sleep(1000);
  117. }
  118. })();
  119. }
  120. recvMessage(d) {
  121. if (d.type == 'mes') {
  122. switch(d.data) {
  123. case 'hello': this.sendMessage({type: 'mes', data: 'ready'}); break;
  124. }
  125. } else if (d.type == 'libs') {
  126. this.ready = true;
  127. this.libs = _.cloneDeep(d.data);
  128. this.goToStartLink();
  129. } else if (d.type == 'notify') {
  130. this.$root.notify.success(d.data, '', {position: 'bottom-right'});
  131. }
  132. }
  133. sendMessage(d) {
  134. (async() => {
  135. await this.checkOpener();
  136. if (this.opener && this.openerOrigin)
  137. this.opener.postMessage(Object.assign({}, {from: 'ExternalLibs'}, d), this.openerOrigin);
  138. })();
  139. }
  140. async checkOpener() {
  141. if (this.opener.closed) {
  142. await this.$root.stdDialog.alert('Потеряна связь с читалкой. Окно будет закрыто', 'Ошибка');
  143. window.close();
  144. }
  145. }
  146. commitLibs(libs) {
  147. this.sendMessage({type: 'libs', data: libs});
  148. }
  149. loadLibs() {
  150. const libs = this.libs;
  151. this.startLink = (libs.comment ? libs.comment + ' ': '') + this.removeProtocol(libs.startLink);
  152. this.rootLink = this.getOrigin(libs.startLink);
  153. this.updateSelectedLink();
  154. }
  155. get header() {
  156. let result = (this.ready ? 'Библиотека' : 'Загрузка...');
  157. if (this.ready && this.startLink) {
  158. result += ` | ${this.startLink}`;
  159. }
  160. this.$root.$emit('set-app-title', result);
  161. return result;
  162. }
  163. updateSelectedLink() {
  164. if (!this.ready)
  165. return;
  166. const index = this.getRootIndexByUrl(this.rootLink);
  167. if (index >= 0)
  168. this.selectedLink = this.libs.groups[index].s;
  169. }
  170. updateStartLink() {
  171. if (!this.ready)
  172. return;
  173. const index = this.getRootIndexByUrl(this.rootLink);
  174. if (index >= 0) {
  175. let libs = _.cloneDeep(this.libs);
  176. libs.groups[index].s = this.selectedLink;
  177. libs.startLink = this.selectedLink;
  178. libs.comment = this.getCommentByLink(libs.groups[index].list, this.selectedLink);
  179. this.frameSrc = this.selectedLink;
  180. this.commitLibs(libs);
  181. }
  182. }
  183. get rootLinkOptions() {
  184. let result = [];
  185. if (!this.ready)
  186. return result;
  187. this.libs.groups.forEach(group => {
  188. result.push({label: this.removeProtocol(group.r), value: group.r});
  189. });
  190. return result;
  191. }
  192. get selectedLinkOptions() {
  193. let result = [];
  194. if (!this.ready)
  195. return result;
  196. const index = this.getRootIndexByUrl(this.rootLink);
  197. if (index >= 0) {
  198. this.libs.groups[index].list.forEach(link => {
  199. result.push({label: (link.c ? link.c + ' ': '') + this.removeOrigin(link.l), value: link.l});
  200. });
  201. }
  202. return result;
  203. }
  204. openBookUrlInFrame() {
  205. if (this.bookUrl)
  206. this.frameSrc = this.addProtocol(this.bookUrl);
  207. }
  208. goToStartLink() {
  209. if (!this.ready)
  210. return;
  211. this.frameSrc = this.libs.startLink;
  212. this.frameVisible = false;
  213. this.$nextTick(() => {
  214. this.frameVisible = true;
  215. });
  216. }
  217. addProtocol(url) {
  218. if ((url.indexOf('http://') != 0) && (url.indexOf('https://') != 0))
  219. return 'http://' + url;
  220. return url;
  221. }
  222. removeProtocol(url) {
  223. return url.replace(/(^\w+:|^)\/\//, '');
  224. }
  225. getOrigin(url) {
  226. const parsed = new URL(url);
  227. return parsed.origin;
  228. }
  229. removeOrigin(url) {
  230. const parsed = new URL(url);
  231. const result = url.substring(parsed.origin.length);
  232. return (result ? result : '/');
  233. }
  234. getRootIndexByUrl(url) {
  235. if (!this.ready)
  236. return -1;
  237. const origin = this.getOrigin(url);
  238. const groups = this.libs.groups;
  239. for (let i = 0; i < groups.length; i++) {
  240. if (groups[i].r == origin)
  241. return i;
  242. }
  243. return -1;
  244. }
  245. getCommentByLink(list, link) {
  246. for (const item of list) {
  247. if (item.l == link)
  248. return item.c;
  249. }
  250. return '';
  251. }
  252. onInputFocus() {
  253. this.$refs.input.select();
  254. }
  255. submitUrl() {
  256. if (this.bookUrl) {
  257. this.sendMessage({type: 'submitUrl', data: {url: this.addProtocol(this.bookUrl), force: true}});
  258. this.bookUrl = '';
  259. if (this.libs.closeAfterSubmit)
  260. this.close();
  261. }
  262. }
  263. close() {
  264. this.sendMessage({type: 'close'});
  265. }
  266. keyHook() {
  267. //недостатки сторонних ui
  268. const input = this.$refs.input.$refs.input;
  269. if (document.activeElement === input && event.type == 'keydown' && event.code == 'Enter') {
  270. this.submitUrl();
  271. return true;
  272. }
  273. if (event.type == 'keydown' && (event.code == 'Escape')) {
  274. this.close();
  275. }
  276. return true;
  277. }
  278. }
  279. //-----------------------------------------------------------------------------
  280. </script>
  281. <style scoped>
  282. .separator {
  283. height: 1px;
  284. background-color: #A0A0A0;
  285. }
  286. </style>