LibsPage.vue 8.8 KB

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