ExternalLibs.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <Window ref="window" @close="close">
  3. <template slot="header">
  4. Библиотека <span v-show="startLink">(выбрано {{ startLink }})</span>
  5. </template>
  6. <div 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 rstore from '../../store/modules/reader';
  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. frameVisible = false;
  71. startLink = '';
  72. rootLink = '';
  73. selectedLink = '';
  74. frameSrc = '';
  75. bookUrl = '';
  76. created() {
  77. this.commit = this.$store.commit;
  78. this.loadLibs();
  79. //this.commit('reader/setLibs', rstore.libsDefaults);
  80. }
  81. mounted() {
  82. this.$refs.window.init();
  83. if (!this.frameSrc)
  84. this.goToStartLink();
  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. }, false);
  103. }
  104. recvMessage(d) {
  105. if (d.type == 'mes') {
  106. switch(d.data) {
  107. case 'hello': this.sendMessage({type: 'mes', data: 'ready'}); break;
  108. }
  109. } else if (d.type == 'obj') {
  110. //
  111. }
  112. }
  113. sendMessage(d) {
  114. if (this.opener && this.openerOrigin)
  115. this.opener.postMessage(Object.assign({}, {from: 'ExternalLibs'}, d), this.openerOrigin);
  116. }
  117. get libs() {
  118. return this.$store.state.reader.libs;
  119. }
  120. loadLibs() {
  121. const libs = this.libs;
  122. this.startLink = (libs.comment ? libs.comment + ' ': '') + this.removeProtocol(libs.startLink);
  123. this.rootLink = this.getOrigin(libs.startLink);
  124. this.updateSelectedLink();
  125. }
  126. updateSelectedLink() {
  127. const index = this.getRootIndexByUrl(this.rootLink);
  128. if (index >= 0)
  129. this.selectedLink = this.libs.groups[index].s;
  130. }
  131. updateStartLink() {
  132. const index = this.getRootIndexByUrl(this.rootLink);
  133. if (index >= 0) {
  134. let libs = _.cloneDeep(this.libs);
  135. libs.groups[index].s = this.selectedLink;
  136. libs.startLink = this.selectedLink;
  137. libs.comment = this.getCommentByLink(libs.groups[index].list, this.selectedLink);
  138. this.frameSrc = this.selectedLink;
  139. this.commit('reader/setLibs', libs);
  140. }
  141. }
  142. get rootLinkOptions() {
  143. let result = [];
  144. this.libs.groups.forEach(group => {
  145. result.push({label: this.removeProtocol(group.r), value: group.r});
  146. });
  147. return result;
  148. }
  149. get selectedLinkOptions() {
  150. let result = [];
  151. const index = this.getRootIndexByUrl(this.rootLink);
  152. if (index >= 0) {
  153. this.libs.groups[index].list.forEach(link => {
  154. result.push({label: (link.c ? link.c + ' ': '') + this.removeOrigin(link.l), value: link.l});
  155. });
  156. }
  157. return result;
  158. }
  159. openBookUrlInFrame() {
  160. if (this.bookUrl)
  161. this.frameSrc = this.addProtocol(this.bookUrl);
  162. }
  163. goToStartLink() {
  164. this.frameSrc = this.libs.startLink;
  165. this.frameVisible = false;
  166. this.$nextTick(() => {
  167. this.frameVisible = true;
  168. });
  169. }
  170. addProtocol(url) {
  171. if ((url.indexOf('http://') != 0) && (url.indexOf('https://') != 0))
  172. return 'http://' + url;
  173. return url;
  174. }
  175. removeProtocol(url) {
  176. return url.replace(/(^\w+:|^)\/\//, '');
  177. }
  178. getOrigin(url) {
  179. const parsed = new URL(url);
  180. return parsed.origin;
  181. }
  182. removeOrigin(url) {
  183. const parsed = new URL(url);
  184. const result = url.substring(parsed.origin.length);
  185. return (result ? result : '/');
  186. }
  187. getRootIndexByUrl(url) {
  188. const origin = this.getOrigin(url);
  189. const groups = this.libs.groups;
  190. for (let i = 0; i < groups.length; i++) {
  191. if (groups[i].r == origin)
  192. return i;
  193. }
  194. return -1;
  195. }
  196. getCommentByLink(list, link) {
  197. for (const item of list) {
  198. if (item.l == link)
  199. return item.c;
  200. }
  201. return '';
  202. }
  203. onInputFocus() {
  204. this.$refs.input.select();
  205. }
  206. submitUrl() {
  207. if (this.bookUrl) {
  208. this.$emit('load-book', {url: this.addProtocol(this.bookUrl), force: true});
  209. this.bookUrl = '';
  210. }
  211. }
  212. close() {
  213. this.$emit('do-action', {action: 'libs'});
  214. }
  215. keyHook() {
  216. //недостатки сторонних ui
  217. const input = this.$refs.input.$refs.input;
  218. if (document.activeElement === input && event.type == 'keydown' && event.code == 'Enter') {
  219. this.submitUrl();
  220. return true;
  221. }
  222. if (event.type == 'keydown' && (event.code == 'Escape')) {
  223. this.close();
  224. }
  225. return true;
  226. }
  227. }
  228. //-----------------------------------------------------------------------------
  229. </script>
  230. <style scoped>
  231. .separator {
  232. height: 1px;
  233. background-color: #A0A0A0;
  234. }
  235. </style>