LibsPage.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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" size="12px"/>
  29. <q-btn round dense color="blue" icon="la la-angle-double-down" @click="openBookUrlInFrame" size="12px"/>
  30. </template>
  31. </q-input>
  32. <q-btn rounded color="green-5" no-caps size="14px" @click="submitUrl">Открыть</q-btn>
  33. </div>
  34. <div class="separator"></div>
  35. <iframe class="col fit" :src="frameSrc" frameborder="0"></iframe>
  36. </div>
  37. </Window>
  38. </template>
  39. <script>
  40. //-----------------------------------------------------------------------------
  41. import Vue from 'vue';
  42. import Component from 'vue-class-component';
  43. import _ from 'lodash';
  44. import Window from '../../share/Window.vue';
  45. //import rstore from '../../../store/modules/reader';
  46. export default @Component({
  47. components: {
  48. Window
  49. },
  50. watch: {
  51. libs: function() {
  52. this.loadLibs();
  53. },
  54. rootLink: function() {
  55. this.updateSelectedLink();
  56. this.updateStartLink();
  57. },
  58. selectedLink: function() {
  59. this.updateStartLink();
  60. }
  61. }
  62. })
  63. class LibsPage extends Vue {
  64. startLink = '';
  65. rootLink = '';
  66. selectedLink = '';
  67. frameSrc = '';
  68. bookUrl = '';
  69. created() {
  70. this.commit = this.$store.commit;
  71. this.loadLibs();
  72. //this.commit('reader/setLibs', rstore.libsDefaults);
  73. }
  74. init() {
  75. this.$refs.window.init();
  76. if (!this.frameSrc)
  77. this.frameSrc = this.libs.startLink;
  78. }
  79. get libs() {
  80. return this.$store.state.reader.libs;
  81. }
  82. loadLibs() {
  83. const libs = this.libs;
  84. console.log(libs.comment);
  85. this.startLink = (libs.comment ? libs.comment + ' ': '') + this.removeProtocol(libs.startLink);
  86. this.rootLink = this.getOrigin(libs.startLink);
  87. this.updateSelectedLink();
  88. }
  89. updateSelectedLink() {
  90. const index = this.getRootIndexByUrl(this.rootLink);
  91. if (index >= 0)
  92. this.selectedLink = this.libs.groups[index].s;
  93. }
  94. updateStartLink() {
  95. const index = this.getRootIndexByUrl(this.rootLink);
  96. if (index >= 0) {
  97. let libs = _.cloneDeep(this.libs);
  98. libs.groups[index].s = this.selectedLink;
  99. libs.startLink = this.selectedLink;
  100. libs.comment = this.getCommentByLink(libs.groups[index].list, this.selectedLink);
  101. this.frameSrc = this.selectedLink;
  102. this.commit('reader/setLibs', libs);
  103. }
  104. }
  105. get rootLinkOptions() {
  106. let result = [];
  107. this.libs.groups.forEach(group => {
  108. result.push({label: this.removeProtocol(group.r), value: group.r});
  109. });
  110. return result;
  111. }
  112. get selectedLinkOptions() {
  113. let result = [];
  114. const index = this.getRootIndexByUrl(this.rootLink);
  115. if (index >= 0) {
  116. this.libs.groups[index].list.forEach(link => {
  117. result.push({label: (link.c ? link.c + ' ': '') + this.removeOrigin(link.l), value: link.l});
  118. });
  119. }
  120. return result;
  121. }
  122. openBookUrlInFrame() {
  123. if (this.bookUrl)
  124. this.frameSrc = this.addProtocol(this.bookUrl);
  125. }
  126. addProtocol(url) {
  127. if ((url.indexOf('http://') != 0) && (url.indexOf('https://') != 0))
  128. return 'http://' + url;
  129. return url;
  130. }
  131. removeProtocol(url) {
  132. return url.replace(/(^\w+:|^)\/\//, '');
  133. }
  134. getOrigin(url) {
  135. const parsed = new URL(url);
  136. return parsed.origin;
  137. }
  138. removeOrigin(url) {
  139. const parsed = new URL(url);
  140. const result = url.substring(parsed.origin.length);
  141. return (result ? result : '/');
  142. }
  143. getRootIndexByUrl(url) {
  144. const origin = this.getOrigin(url);
  145. const groups = this.libs.groups;
  146. for (let i = 0; i < groups.length; i++) {
  147. if (groups[i].r == origin)
  148. return i;
  149. }
  150. return -1;
  151. }
  152. getCommentByLink(list, link) {
  153. for (const item of list) {
  154. if (item.l == link)
  155. return item.c;
  156. }
  157. return '';
  158. }
  159. onInputFocus() {
  160. this.$refs.input.select();
  161. }
  162. submitUrl() {
  163. if (this.bookUrl) {
  164. this.$emit('load-book', {url: this.bookUrl, force: true});
  165. this.bookUrl = '';
  166. }
  167. }
  168. close() {
  169. this.$emit('do-action', {action: 'libs'});
  170. }
  171. keyHook() {
  172. //недостатки сторонних ui
  173. const input = this.$refs.input.$refs.input;
  174. if (document.activeElement === input && event.type == 'keydown' && event.code == 'Enter') {
  175. this.submitUrl();
  176. return true;
  177. }
  178. if (event.type == 'keydown' && (event.code == 'Escape')) {
  179. this.close();
  180. }
  181. return true;
  182. }
  183. }
  184. //-----------------------------------------------------------------------------
  185. </script>
  186. <style scoped>
  187. .separator {
  188. height: 1px;
  189. background-color: #A0A0A0;
  190. }
  191. </style>