LibsPage.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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-5" 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. const popupCenter = ({url, title, w, h}) => {
  53. // Fixes dual-screen position Most browsers Firefox
  54. const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : window.screenX;
  55. const dualScreenTop = window.screenTop !== undefined ? window.screenTop : window.screenY;
  56. const width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
  57. const height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
  58. const systemZoom = width / window.screen.availWidth;
  59. const left = (width - w) / 2 / systemZoom + dualScreenLeft
  60. const top = (height - h) / 2 / systemZoom + dualScreenTop
  61. const newWindow = window.open(url, title,
  62. `
  63. scrollbars=yes,
  64. width=${w / systemZoom},
  65. height=${h / systemZoom},
  66. top=${top},
  67. left=${left}
  68. `
  69. )
  70. if (window.focus) newWindow.focus();
  71. //newWindow.close();
  72. }
  73. export default @Component({
  74. components: {
  75. Window
  76. },
  77. watch: {
  78. libs: function() {
  79. this.loadLibs();
  80. },
  81. rootLink: function() {
  82. this.updateSelectedLink();
  83. this.updateStartLink();
  84. },
  85. selectedLink: function() {
  86. this.updateStartLink();
  87. }
  88. }
  89. })
  90. class LibsPage extends Vue {
  91. frameVisible = false;
  92. startLink = '';
  93. rootLink = '';
  94. selectedLink = '';
  95. frameSrc = '';
  96. bookUrl = '';
  97. created() {
  98. this.commit = this.$store.commit;
  99. this.loadLibs();
  100. //this.commit('reader/setLibs', rstore.libsDefaults);
  101. }
  102. init() {
  103. this.$refs.window.init();
  104. if (!this.frameSrc)
  105. this.frameSrc = this.libs.startLink;
  106. this.frameVisible = false;
  107. this.frameVisible = true;
  108. }
  109. get libs() {
  110. return this.$store.state.reader.libs;
  111. }
  112. loadLibs() {
  113. const libs = this.libs;
  114. this.startLink = (libs.comment ? libs.comment + ' ': '') + this.removeProtocol(libs.startLink);
  115. this.rootLink = this.getOrigin(libs.startLink);
  116. this.updateSelectedLink();
  117. }
  118. updateSelectedLink() {
  119. const index = this.getRootIndexByUrl(this.rootLink);
  120. if (index >= 0)
  121. this.selectedLink = this.libs.groups[index].s;
  122. }
  123. updateStartLink() {
  124. const index = this.getRootIndexByUrl(this.rootLink);
  125. if (index >= 0) {
  126. let libs = _.cloneDeep(this.libs);
  127. libs.groups[index].s = this.selectedLink;
  128. libs.startLink = this.selectedLink;
  129. libs.comment = this.getCommentByLink(libs.groups[index].list, this.selectedLink);
  130. this.frameSrc = this.selectedLink;
  131. this.commit('reader/setLibs', libs);
  132. }
  133. }
  134. get rootLinkOptions() {
  135. let result = [];
  136. this.libs.groups.forEach(group => {
  137. result.push({label: this.removeProtocol(group.r), value: group.r});
  138. });
  139. return result;
  140. }
  141. get selectedLinkOptions() {
  142. let result = [];
  143. const index = this.getRootIndexByUrl(this.rootLink);
  144. if (index >= 0) {
  145. this.libs.groups[index].list.forEach(link => {
  146. result.push({label: (link.c ? link.c + ' ': '') + this.removeOrigin(link.l), value: link.l});
  147. });
  148. }
  149. return result;
  150. }
  151. openBookUrlInFrame() {
  152. if (this.bookUrl)
  153. this.frameSrc = this.addProtocol(this.bookUrl);
  154. }
  155. goToStartLink() {
  156. this.frameSrc = this.libs.startLink;
  157. this.frameVisible = false;
  158. this.$nextTick(() => {
  159. this.frameVisible = true;
  160. });
  161. popupCenter({url: this.libs.startLink, w: 900, h: 500});
  162. }
  163. addProtocol(url) {
  164. if ((url.indexOf('http://') != 0) && (url.indexOf('https://') != 0))
  165. return 'http://' + url;
  166. return url;
  167. }
  168. removeProtocol(url) {
  169. return url.replace(/(^\w+:|^)\/\//, '');
  170. }
  171. getOrigin(url) {
  172. const parsed = new URL(url);
  173. return parsed.origin;
  174. }
  175. removeOrigin(url) {
  176. const parsed = new URL(url);
  177. const result = url.substring(parsed.origin.length);
  178. return (result ? result : '/');
  179. }
  180. getRootIndexByUrl(url) {
  181. const origin = this.getOrigin(url);
  182. const groups = this.libs.groups;
  183. for (let i = 0; i < groups.length; i++) {
  184. if (groups[i].r == origin)
  185. return i;
  186. }
  187. return -1;
  188. }
  189. getCommentByLink(list, link) {
  190. for (const item of list) {
  191. if (item.l == link)
  192. return item.c;
  193. }
  194. return '';
  195. }
  196. onInputFocus() {
  197. this.$refs.input.select();
  198. }
  199. submitUrl() {
  200. if (this.bookUrl) {
  201. this.$emit('load-book', {url: this.addProtocol(this.bookUrl), force: true});
  202. this.bookUrl = '';
  203. }
  204. }
  205. close() {
  206. this.$emit('do-action', {action: 'libs'});
  207. }
  208. keyHook() {
  209. //недостатки сторонних ui
  210. const input = this.$refs.input.$refs.input;
  211. if (document.activeElement === input && event.type == 'keydown' && event.code == 'Enter') {
  212. this.submitUrl();
  213. return true;
  214. }
  215. if (event.type == 'keydown' && (event.code == 'Escape')) {
  216. this.close();
  217. }
  218. return true;
  219. }
  220. }
  221. //-----------------------------------------------------------------------------
  222. </script>
  223. <style scoped>
  224. .separator {
  225. height: 1px;
  226. background-color: #A0A0A0;
  227. }
  228. </style>