LibsPage.vue 7.5 KB

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