LibsPage.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. 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 LibsPage 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. init() {
  82. this.$refs.window.init();
  83. if (!this.frameSrc)
  84. this.frameSrc = this.libs.startLink;
  85. this.frameVisible = false;
  86. this.frameVisible = true;
  87. }
  88. get libs() {
  89. return this.$store.state.reader.libs;
  90. }
  91. loadLibs() {
  92. const libs = this.libs;
  93. this.startLink = (libs.comment ? libs.comment + ' ': '') + this.removeProtocol(libs.startLink);
  94. this.rootLink = this.getOrigin(libs.startLink);
  95. this.updateSelectedLink();
  96. }
  97. updateSelectedLink() {
  98. const index = this.getRootIndexByUrl(this.rootLink);
  99. if (index >= 0)
  100. this.selectedLink = this.libs.groups[index].s;
  101. }
  102. updateStartLink() {
  103. const index = this.getRootIndexByUrl(this.rootLink);
  104. if (index >= 0) {
  105. let libs = _.cloneDeep(this.libs);
  106. libs.groups[index].s = this.selectedLink;
  107. libs.startLink = this.selectedLink;
  108. libs.comment = this.getCommentByLink(libs.groups[index].list, this.selectedLink);
  109. this.frameSrc = this.selectedLink;
  110. this.commit('reader/setLibs', libs);
  111. }
  112. }
  113. get rootLinkOptions() {
  114. let result = [];
  115. this.libs.groups.forEach(group => {
  116. result.push({label: this.removeProtocol(group.r), value: group.r});
  117. });
  118. return result;
  119. }
  120. get selectedLinkOptions() {
  121. let result = [];
  122. const index = this.getRootIndexByUrl(this.rootLink);
  123. if (index >= 0) {
  124. this.libs.groups[index].list.forEach(link => {
  125. result.push({label: (link.c ? link.c + ' ': '') + this.removeOrigin(link.l), value: link.l});
  126. });
  127. }
  128. return result;
  129. }
  130. openBookUrlInFrame() {
  131. if (this.bookUrl)
  132. this.frameSrc = this.addProtocol(this.bookUrl);
  133. }
  134. goToStartLink() {
  135. this.frameSrc = this.libs.startLink;
  136. this.frameVisible = false;
  137. this.$nextTick(() => {
  138. this.frameVisible = true;
  139. });
  140. }
  141. addProtocol(url) {
  142. if ((url.indexOf('http://') != 0) && (url.indexOf('https://') != 0))
  143. return 'http://' + url;
  144. return url;
  145. }
  146. removeProtocol(url) {
  147. return url.replace(/(^\w+:|^)\/\//, '');
  148. }
  149. getOrigin(url) {
  150. const parsed = new URL(url);
  151. return parsed.origin;
  152. }
  153. removeOrigin(url) {
  154. const parsed = new URL(url);
  155. const result = url.substring(parsed.origin.length);
  156. return (result ? result : '/');
  157. }
  158. getRootIndexByUrl(url) {
  159. const origin = this.getOrigin(url);
  160. const groups = this.libs.groups;
  161. for (let i = 0; i < groups.length; i++) {
  162. if (groups[i].r == origin)
  163. return i;
  164. }
  165. return -1;
  166. }
  167. getCommentByLink(list, link) {
  168. for (const item of list) {
  169. if (item.l == link)
  170. return item.c;
  171. }
  172. return '';
  173. }
  174. onInputFocus() {
  175. this.$refs.input.select();
  176. }
  177. submitUrl() {
  178. if (this.bookUrl) {
  179. this.$emit('load-book', {url: this.addProtocol(this.bookUrl), force: true});
  180. this.bookUrl = '';
  181. }
  182. }
  183. close() {
  184. this.$emit('do-action', {action: 'libs'});
  185. }
  186. keyHook() {
  187. //недостатки сторонних ui
  188. const input = this.$refs.input.$refs.input;
  189. if (document.activeElement === input && event.type == 'keydown' && event.code == 'Enter') {
  190. this.submitUrl();
  191. return true;
  192. }
  193. if (event.type == 'keydown' && (event.code == 'Escape')) {
  194. this.close();
  195. }
  196. return true;
  197. }
  198. }
  199. //-----------------------------------------------------------------------------
  200. </script>
  201. <style scoped>
  202. .separator {
  203. height: 1px;
  204. background-color: #A0A0A0;
  205. }
  206. </style>