ExternalLibs.vue 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. <template>
  2. <Window ref="window" margin="2px" @close="close">
  3. <template #header>
  4. {{ header }}
  5. </template>
  6. <template #buttons>
  7. <span class="header-button row justify-center items-center" @mousedown.stop @click="fullScreenToggle">
  8. <q-icon :name="(fullScreenActive ? 'la la-compress-arrows-alt': 'la la-expand-arrows-alt')" size="16px" />
  9. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">На весь экран</q-tooltip>
  10. </span>
  11. <span class="header-button row justify-center items-center" @mousedown.stop @click="changeScale(0.1)">
  12. <q-icon name="la la-plus" size="16px" />
  13. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">Увеличить масштаб</q-tooltip>
  14. </span>
  15. <span class="header-button row justify-center items-center" @mousedown.stop @click="changeScale(-0.1)">
  16. <q-icon name="la la-minus" size="16px" />
  17. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">Уменьшить масштаб</q-tooltip>
  18. </span>
  19. <span class="header-button row justify-center items-center" @mousedown.stop @click="showHelp">
  20. <q-icon name="la la-question-circle" size="16px" />
  21. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">Справка</q-tooltip>
  22. </span>
  23. </template>
  24. <div v-show="ready" class="col column" style="min-width: 600px">
  25. <div class="row items-center q-px-sm" style="height: 50px">
  26. <q-select
  27. ref="rootLink"
  28. v-model="rootLink"
  29. class="q-mr-sm"
  30. bg-color="input"
  31. :options="rootLinkOptions"
  32. style="width: 230px"
  33. dropdown-icon="la la-angle-down la-sm"
  34. outlined dense emit-value map-options display-value-sanitize options-sanitize
  35. @popup-show="onSelectPopupShow" @popup-hide="onSelectPopupHide"
  36. >
  37. <template #prepend>
  38. <q-btn class="q-mr-xs" round dense color="blue" icon="la la-plus" size="12px" @click.stop="addBookmark">
  39. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">
  40. Добавить закладку
  41. </q-tooltip>
  42. </q-btn>
  43. <q-btn round dense color="blue" icon="la la-bars" size="12px" @click.stop="bookmarkSettings">
  44. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">
  45. Настроить закладки
  46. </q-tooltip>
  47. </q-btn>
  48. </template>
  49. <template #selected>
  50. <div style="overflow: hidden; white-space: nowrap;">
  51. {{ rootLinkWithoutProtocol }}
  52. </div>
  53. </template>
  54. </q-select>
  55. <q-select
  56. ref="selectedLink"
  57. v-model="selectedLink"
  58. class="q-mr-sm"
  59. bg-color="input"
  60. :options="selectedLinkOptions"
  61. style="width: 50px"
  62. dropdown-icon="la la-angle-down la-sm"
  63. outlined dense emit-value map-options hide-selected display-value-sanitize options-sanitize
  64. @popup-show="onSelectPopupShow" @popup-hide="onSelectPopupHide"
  65. >
  66. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">
  67. Закладки
  68. </q-tooltip>
  69. </q-select>
  70. <q-input
  71. ref="input"
  72. v-model="bookUrl"
  73. class="col q-mr-sm"
  74. bg-color="input"
  75. outlined dense
  76. placeholder="Скопируйте сюда ссылку на книгу и нажмите 'Открыть'"
  77. @focus="selectAllOnFocus" @keydown="bookUrlKeyDown"
  78. >
  79. <template #prepend>
  80. <q-btn class="q-mr-xs" round dense color="blue" icon="la la-home" size="12px" @click="goToLink(selectedLink)">
  81. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">
  82. Вернуться на стартовую страницу
  83. </q-tooltip>
  84. </q-btn>
  85. <q-btn :disabled="!bookUrl" round dense color="blue" icon="la la-angle-double-down" size="12px" @click="openBookUrlInFrame">
  86. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">
  87. Загрузить URL во фрейм
  88. </q-tooltip>
  89. </q-btn>
  90. </template>
  91. <template #append>
  92. <q-btn round dense color="blue" icon="la la-cog" size="12px" @click.stop="optionsVisible = true">
  93. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">
  94. Опции
  95. </q-tooltip>
  96. </q-btn>
  97. </template>
  98. </q-input>
  99. <q-btn :disabled="!bookUrl" color="green-7" no-caps size="14px" @click="submitUrl">
  100. Открыть
  101. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">
  102. Открыть в читалке
  103. </q-tooltip>
  104. </q-btn>
  105. </div>
  106. <div class="separator"></div>
  107. <div ref="frameBox" class="col fit" style="position: relative; background-color: white">
  108. <div ref="frameWrap" class="overflow-hidden">
  109. <iframe v-if="frameVisible" ref="frame" :src="frameSrc" frameborder="0" allow="clipboard-read; clipboard-write"></iframe>
  110. </div>
  111. <div v-show="transparentLayoutVisible" ref="transparentLayout" class="fit transparent-layout" @click="transparentLayoutClick"></div>
  112. </div>
  113. <Dialog ref="dialogAddBookmark" v-model="addBookmarkVisible">
  114. <template #header>
  115. <div class="row items-center">
  116. <q-icon class="q-mr-sm" name="la la-bookmark" size="28px"></q-icon>
  117. <div v-if="addBookmarkMode == 'edit'">
  118. Редактировать закладку
  119. </div>
  120. <div v-else>
  121. Добавить закладку
  122. </div>
  123. </div>
  124. </template>
  125. <div class="q-mx-md row">
  126. <q-input
  127. ref="bookmarkLink"
  128. v-model="bookmarkLink"
  129. class="col q-mr-sm"
  130. bg-color="input"
  131. outlined dense
  132. placeholder="Ссылка для закладки" maxlength="2000" @focus="selectAllOnFocus" @keydown="bookmarkLinkKeyDown"
  133. >
  134. </q-input>
  135. <q-select
  136. ref="defaultRootLink"
  137. v-model="defaultRootLink"
  138. class="q-mr-sm"
  139. bg-color="input"
  140. :options="defaultRootLinkOptions"
  141. style="width: 50px"
  142. dropdown-icon="la la-angle-down la-sm"
  143. outlined dense emit-value map-options hide-selected display-value-sanitize options-sanitize
  144. >
  145. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">
  146. Предустановленные ссылки
  147. </q-tooltip>
  148. </q-select>
  149. </div>
  150. <div class="q-mx-md q-mt-md">
  151. <q-input
  152. ref="bookmarkDesc"
  153. v-model="bookmarkDesc"
  154. class="col q-mr-sm"
  155. bg-color="input"
  156. outlined dense
  157. placeholder="Описание" style="width: 400px" maxlength="100" @focus="selectAllOnFocus" @keydown="bookmarkDescKeyDown"
  158. >
  159. </q-input>
  160. </div>
  161. <template #footer>
  162. <q-btn v-close-popup class="q-px-md q-ml-sm" dense no-caps>
  163. Отмена
  164. </q-btn>
  165. <q-btn :disabled="!bookmarkLink" class="q-px-md q-ml-sm" color="primary" dense no-caps @click="okAddBookmark">
  166. OK
  167. </q-btn>
  168. </template>
  169. </Dialog>
  170. <Dialog ref="options" v-model="optionsVisible">
  171. <template #header>
  172. <div class="row items-center">
  173. <q-icon class="q-mr-sm" name="la la-cog" size="28px"></q-icon>
  174. Опции
  175. </div>
  176. </template>
  177. <div class="q-mx-md column">
  178. <q-checkbox v-model="closeAfterSubmit" size="36px" label="Закрыть окно при отправке ссылки в читалку" />
  179. <q-checkbox v-model="openInFrameOnEnter" size="36px" label="Открывать ссылку во фрейме при нажатии 'Enter'" />
  180. <q-checkbox v-model="openInFrameOnAdd" size="36px" label="Активировать новую закладку после добавления" />
  181. </div>
  182. <template #footer>
  183. <q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps @click="optionsVisible = false">
  184. OK
  185. </q-btn>
  186. </template>
  187. </Dialog>
  188. </div>
  189. <BookmarkSettings
  190. v-if="bookmarkSettingsActive"
  191. ref="bookmarkSettings"
  192. :libs="libs"
  193. :add-bookmark-visible="addBookmarkVisible"
  194. @do-action="doAction" @close="closeBookmarkSettings"
  195. >
  196. </BookmarkSettings>
  197. </Window>
  198. </template>
  199. <script>
  200. //-----------------------------------------------------------------------------
  201. import vueComponent from '../vueComponent.js';
  202. import _ from 'lodash';
  203. import Window from '../share/Window.vue';
  204. import Dialog from '../share/Dialog.vue';
  205. import BookmarkSettings from './BookmarkSettings/BookmarkSettings.vue';
  206. import rstore from '../../store/modules/reader';
  207. import * as utils from '../../share/utils';
  208. import * as lu from './linkUtils';
  209. const proxySubst = {
  210. 'http://flibusta.is': 'http://b.liberama.top:23480',
  211. 'http://fantasy-worlds.org': 'http://b.liberama.top:23580',
  212. };
  213. const componentOptions = {
  214. components: {
  215. Window,
  216. Dialog,
  217. BookmarkSettings
  218. },
  219. watch: {
  220. libs() {
  221. this.loadLibs();
  222. },
  223. defaultRootLink() {
  224. this.updateBookmarkLink();
  225. },
  226. bookUrl(newValue) {
  227. const value = lu.addProtocol(newValue);
  228. const subst = this.makeProxySubst(value, true);
  229. if (value != subst) {
  230. this.$nextTick(() => {
  231. this.bookUrl = subst;
  232. });
  233. }
  234. },
  235. bookmarkLink(newValue) {
  236. const value = lu.addProtocol(newValue);
  237. const subst = this.makeProxySubst(value, true);
  238. if (value != subst) {
  239. this.$nextTick(() => {
  240. this.bookmarkLink = subst;
  241. });
  242. }
  243. },
  244. closeAfterSubmit(newValue) {
  245. this.commitProp('closeAfterSubmit', newValue);
  246. },
  247. openInFrameOnEnter(newValue) {
  248. this.commitProp('openInFrameOnEnter', newValue);
  249. },
  250. openInFrameOnAdd(newValue) {
  251. this.commitProp('openInFrameOnAdd', newValue);
  252. },
  253. rootLink() {
  254. this.rootLinkInput();
  255. },
  256. selectedLink() {
  257. this.selectedLinkInput();
  258. },
  259. }
  260. };
  261. class ExternalLibs {
  262. _options = componentOptions;
  263. ready = false;
  264. frameVisible = false;
  265. rootLink = '';
  266. selectedLink = '';
  267. frameSrc = '';
  268. bookUrl = '';
  269. libs = {};
  270. fullScreenActive = false;
  271. transparentLayoutVisible = false;
  272. addBookmarkVisible = false;
  273. optionsVisible = false;
  274. addBookmarkMode = '';
  275. bookmarkLink = '';
  276. bookmarkDesc = '';
  277. defaultRootLink = '';
  278. bookmarkSettingsActive = false;
  279. closeAfterSubmit = false;
  280. openInFrameOnEnter = false;
  281. openInFrameOnAdd = false;
  282. frameScale = 1;
  283. inpxReady = false;
  284. inpxTitle = '';
  285. inpxUrl = '';
  286. created() {
  287. this.commit = this.$store.commit;
  288. this.oldStartLink = '';
  289. this.justOpened = true;
  290. this.$root.addEventHook('key', this.keyHook);
  291. this.$root.addEventHook('resize', async() => {
  292. await utils.sleep(200);
  293. this.frameResize();
  294. });
  295. document.addEventListener('fullscreenchange', () => {
  296. this.fullScreenActive = (document.fullscreenElement !== null);
  297. });
  298. this.debouncedGoToLink = _.debounce((link) => {
  299. this.goToLink(link);
  300. }, 100, {'maxWait':200});
  301. }
  302. mounted() {
  303. (async() => {
  304. //подождем this.mode
  305. let i = 0;
  306. while(!this.mode && i < 100) {
  307. await utils.sleep(100);
  308. i++;
  309. }
  310. this.libsDefaults = rstore.getLibsDefaults(this.mode);
  311. this.$refs.window.init();
  312. this.opener = null;
  313. const host = window.location.host;
  314. const openerHost = (host.indexOf('b.') == 0 ? host.substring(2) : host);
  315. const openerOrigin1 = `http://${openerHost}`;
  316. const openerOrigin2 = `https://${openerHost}`;
  317. window.addEventListener('message', (event) => {
  318. //from inpx-web
  319. if (_.isObject(event.data) && event.data.from === 'inpx-web') {
  320. //console.log(event);
  321. this.inpxOrigin = event.origin;
  322. this.recvInpxMessage(event.data);
  323. return;
  324. }
  325. //from parent
  326. if (event.origin !== openerOrigin1 && event.origin !== openerOrigin2)
  327. return;
  328. if (!_.isObject(event.data) || event.data.from != 'LibsPage')
  329. return;
  330. if (event.origin == openerOrigin1)
  331. this.opener = window.opener;
  332. else
  333. this.opener = event.source;
  334. this.openerOrigin = event.origin;
  335. this.recvMessage(event.data);
  336. });
  337. //Ожидаем родителя
  338. i = 0;
  339. while(!this.opener) {
  340. await utils.sleep(1000);
  341. i++;
  342. if (i >= 5) {
  343. await this.$root.stdDialog.alert('Нет связи с читалкой. Окно будет закрыто', 'Ошибка');
  344. window.close();
  345. }
  346. }
  347. //Проверка закрытия родительского окна
  348. while(this.opener) {
  349. await this.checkOpener();
  350. await utils.sleep(1000);
  351. }
  352. })();
  353. }
  354. recvMessage(d) {
  355. if (d.type == 'mes') {
  356. switch(d.data) {
  357. case 'hello': this.sendMessage({type: 'mes', data: 'ready'}); break;
  358. }
  359. } else if (d.type == 'libs') {
  360. this.ready = true;
  361. if (d.data)
  362. this.libs = _.cloneDeep(d.data);
  363. if (d.sets)
  364. this.updateSets(d.sets);
  365. } else if (d.type == 'notify') {
  366. this.$root.notify.success(d.data, '', {position: 'bottom-right'});
  367. }
  368. }
  369. sendMessage(d) {
  370. (async() => {
  371. await this.checkOpener();
  372. if (this.opener && this.openerOrigin)
  373. this.opener.postMessage(Object.assign({}, {from: 'ExternalLibs'}, d), this.openerOrigin);
  374. })();
  375. }
  376. recvInpxMessage(d) {
  377. if (d.type == 'mes') {
  378. switch(d.data) {
  379. case 'hello-from-inpx-web':
  380. this.sendInpxMessage({type: 'mes', data: 'ready'});
  381. break;
  382. case 'ready':
  383. this.inpxReady = true;
  384. break;
  385. }
  386. } else if (d.type == 'submitUrl') {
  387. this.submitUrl(d.data);
  388. } else if (d.type == 'titleChange') {
  389. this.inpxTitle = d.data;
  390. } else if (d.type == 'urlChange') {
  391. this.inpxUrl = d.data;
  392. }
  393. }
  394. sendInpxMessage(d) {
  395. if (this.$refs.frame && this.inpxOrigin)
  396. this.$refs.frame.contentWindow.postMessage(Object.assign({}, {from: 'ExternalLibs'}, d), this.inpxOrigin);
  397. }
  398. async checkOpener() {
  399. if (this.opener.closed) {
  400. await this.$root.stdDialog.alert('Потеряна связь с читалкой. Окно будет закрыто', 'Ошибка');
  401. window.close();
  402. }
  403. }
  404. updateSets(sets) {
  405. if (sets.nightMode !== this.nightMode)
  406. this.commit('reader/nightModeToggle');
  407. }
  408. commitLibs(libs) {
  409. this.sendMessage({type: 'libs', data: libs});
  410. }
  411. commitProp(prop, value) {
  412. let libs = _.cloneDeep(this.libs);
  413. libs[prop] = value;
  414. this.commitLibs(libs);
  415. }
  416. loadLibs() {
  417. const libs = this.libs;
  418. if (!libs.helpShowed) {
  419. this.showHelp();
  420. (async() => {
  421. await utils.sleep(1000);
  422. this.commitProp('helpShowed', true);
  423. })();
  424. }
  425. this.selectedLink = libs.startLink;
  426. this.closeAfterSubmit = libs.closeAfterSubmit || false;
  427. this.openInFrameOnEnter = libs.openInFrameOnEnter || false;
  428. this.openInFrameOnAdd = libs.openInFrameOnAdd || false;
  429. this.frameScale = 1;
  430. const index = lu.getSafeRootIndexByUrl(this.libs.groups, this.selectedLink);
  431. if (index >= 0)
  432. this.frameScale = this.libs.groups[index].frameScale || 1;
  433. this.updateStartLink();
  434. }
  435. doAction(event) {
  436. switch (event.action) {
  437. case 'setLibs': this.commitLibs(event.data); break;
  438. case 'setRootLink': this.rootLink = event.data; this.rootLinkInput(); break;
  439. case 'setSelectedLink': this.selectedLink = event.data; this.selectedLinkInput(); break;
  440. case 'editBookmark': this.addBookmark('edit', event.data.link, event.data.desc); break;
  441. case 'addBookmark': this.addBookmark('add'); break;
  442. }
  443. }
  444. get mode() {
  445. return this.$store.state.config.mode;
  446. }
  447. get nightMode() {
  448. return this.$store.state.reader.settings.nightMode;
  449. }
  450. get header() {
  451. let result = [this.ready ? 'Сетевая библиотека' : 'Загрузка...'];
  452. if (this.ready && this.selectedLink) {
  453. if (this.inpxReady && this.inpxTitle) {
  454. result.push(this.inpxTitle);
  455. result.push(lu.removeProtocol(this.inpxUrl));
  456. } else {
  457. result.push(this.libs.comment);
  458. result.push(lu.removeProtocol(this.libs.startLink));
  459. }
  460. }
  461. result = result.filter(s => s).join(' | ');
  462. this.$root.setAppTitle(result);
  463. return result;
  464. }
  465. get rootLinkWithoutProtocol() {
  466. return lu.removeProtocol(this.rootLink);
  467. }
  468. updateSelectedLinkByRoot() {
  469. if (!this.ready)
  470. return;
  471. const index = lu.getSafeRootIndexByUrl(this.libs.groups, this.rootLink);
  472. if (index >= 0)
  473. this.selectedLink = this.libs.groups[index].s;
  474. else
  475. this.selectedLink = '';
  476. }
  477. updateStartLink(force) {
  478. if (!this.ready)
  479. return;
  480. let index = -1;
  481. try {
  482. this.rootLink = lu.getOrigin(this.selectedLink);
  483. index = lu.getRootIndexByUrl(this.libs.groups, this.rootLink);
  484. } catch(e) {
  485. //
  486. }
  487. if (index >= 0) {
  488. let libs = _.cloneDeep(this.libs);
  489. const com = this.getCommentByLink(libs.groups[index].list, this.selectedLink);
  490. if (libs.groups[index].s != this.selectedLink ||
  491. libs.startLink != this.selectedLink ||
  492. libs.comment != com) {
  493. libs.groups[index].s = this.selectedLink;
  494. libs.startLink = this.selectedLink;
  495. libs.comment = com;
  496. this.commitLibs(libs);
  497. }
  498. if (force || this.oldStartLink != libs.startLink) {
  499. this.oldStartLink = libs.startLink;
  500. this.debouncedGoToLink(this.selectedLink);
  501. }
  502. } else {
  503. this.rootLink = '';
  504. this.selectedLink = '';
  505. this.debouncedGoToLink(this.selectedLink);
  506. }
  507. }
  508. get rootLinkOptions() {
  509. let result = [];
  510. if (!this.ready)
  511. return result;
  512. this.libs.groups.forEach(group => {
  513. result.push({label: lu.removeProtocol(group.r), value: group.r});
  514. });
  515. return result;
  516. }
  517. get defaultRootLinkOptions() {
  518. let result = [];
  519. this.libsDefaults.groups.forEach(group => {
  520. result.push({label: lu.removeProtocol(group.r), value: group.r});
  521. });
  522. return result;
  523. }
  524. get selectedLinkOptions() {
  525. let result = [];
  526. if (!this.ready)
  527. return result;
  528. const index = lu.getSafeRootIndexByUrl(this.libs.groups, this.rootLink);
  529. if (index >= 0) {
  530. this.libs.groups[index].list.forEach(link => {
  531. result.push({label: (link.c ? link.c + ' ': '') + lu.removeOrigin(link.l), value: link.l});
  532. });
  533. }
  534. return result;
  535. }
  536. openBookUrlInFrame() {
  537. if (this.bookUrl) {
  538. this.goToLink(lu.addProtocol(this.bookUrl));
  539. }
  540. }
  541. goToLink(link) {
  542. this.inpxReady = false;
  543. this.inpxTitle = '';
  544. this.inpxUrl = '';
  545. this.inpxOrigin = false;
  546. if (!this.ready || !link)
  547. return;
  548. if (!link) {
  549. this.frameVisible = false;
  550. return;
  551. }
  552. this.frameSrc = this.makeProxySubst(link);
  553. this.frameVisible = false;
  554. this.$nextTick(() => {
  555. this.frameVisible = true;
  556. this.$nextTick(() => {
  557. if (this.$refs.frame) {
  558. this.$refs.frame.contentWindow.location.reload(true);
  559. this.$refs.frame.contentWindow.focus();
  560. this.frameResize();
  561. }
  562. });
  563. });
  564. }
  565. frameResize() {
  566. this.$refs.frameWrap.style = 'width: 1px; height: 1px;';
  567. this.$nextTick(() => {
  568. if (this.$refs.frame) {
  569. const w = this.$refs.frameBox.offsetWidth;
  570. const h = this.$refs.frameBox.offsetHeight;
  571. const normalSize = `width: ${w}px; height: ${h}px;`;
  572. this.$refs.frameWrap.style = normalSize;
  573. if (this.frameScale != 1) {
  574. const s = this.frameScale;
  575. this.$refs.frame.style = `width: ${w/s}px; height: ${h/s}px; transform: scale(${s}); transform-origin: 0 0;`;
  576. } else {
  577. this.$refs.frame.style = normalSize;
  578. }
  579. }
  580. });
  581. }
  582. changeScale(delta) {
  583. if ((this.frameScale > 0.1 && delta <= 0) || (this.frameScale < 5 && delta >= 0)) {
  584. this.frameScale = _.round(this.frameScale + delta, 1);
  585. const index = lu.getSafeRootIndexByUrl(this.libs.groups, this.selectedLink);
  586. if (index >= 0) {
  587. let libs = _.cloneDeep(this.libs);
  588. libs.groups[index].frameScale = this.frameScale;
  589. this.commitLibs(libs);
  590. }
  591. this.frameResize();
  592. this.$root.notify.success(`Масштаб изменен: ${(this.frameScale*100).toFixed(0)}%`, '', {position: 'bottom-right'});
  593. }
  594. }
  595. getCommentByLink(list, link) {
  596. const item = lu.getListItemByLink(list, link);
  597. return (item ? item.c : '');
  598. }
  599. makeProxySubst(url, reverse = false) {
  600. for (const [key, value] of Object.entries(proxySubst)) {
  601. if (reverse && value == url.substring(0, value.length)) {
  602. return key + url.substring(value.length);
  603. } else if (!reverse && key == url.substring(0, key.length)) {
  604. return value + url.substring(key.length);
  605. }
  606. }
  607. return url;
  608. }
  609. selectAllOnFocus(event) {
  610. if (event.target.select)
  611. event.target.select();
  612. }
  613. rootLinkInput() {
  614. this.updateSelectedLinkByRoot();
  615. this.updateStartLink(true);
  616. }
  617. selectedLinkInput() {
  618. this.updateStartLink(true);
  619. }
  620. submitUrl(url) {
  621. if (!url) {
  622. url = this.bookUrl;
  623. this.bookUrl = '';
  624. }
  625. if (url) {
  626. this.sendMessage({type: 'submitUrl', data: {
  627. url,
  628. force: true
  629. }});
  630. if (this.closeAfterSubmit)
  631. this.close();
  632. }
  633. }
  634. addBookmark(mode = 'add', link = '', desc = '') {
  635. if (mode == 'edit') {
  636. this.editBookmarkLink = this.bookmarkLink = link;
  637. this.editBookmarkDesc = this.bookmarkDesc = desc;
  638. } else {
  639. this.bookmarkLink = this.bookUrl;
  640. this.bookmarkDesc = '';
  641. if (!this.bookmarkLink && this.inpxReady && this.inpxUrl) {
  642. this.bookmarkLink = this.inpxUrl;
  643. if (this.inpxTitle)
  644. this.bookmarkDesc = this.inpxTitle;
  645. }
  646. }
  647. this.addBookmarkMode = mode;
  648. this.addBookmarkVisible = true;
  649. this.$nextTick(async() => {
  650. await this.$refs.dialogAddBookmark.waitShown();
  651. this.$refs.bookmarkLink.focus();
  652. });
  653. }
  654. updateBookmarkLink() {
  655. const index = lu.getSafeRootIndexByUrl(this.libsDefaults.groups, this.defaultRootLink);
  656. if (index >= 0) {
  657. this.bookmarkLink = this.libsDefaults.groups[index].s;
  658. this.bookmarkDesc = this.getCommentByLink(this.libsDefaults.groups[index].list, this.bookmarkLink);
  659. } else {
  660. this.bookmarkLink = '';
  661. this.bookmarkDesc = '';
  662. }
  663. }
  664. bookmarkLinkKeyDown(event) {
  665. if (event.key == 'Enter') {
  666. this.$refs.bookmarkDesc.focus();
  667. event.preventDefault();
  668. }
  669. }
  670. bookmarkDescKeyDown(event) {
  671. if (event.key == 'Enter') {
  672. event.stopPropagation();
  673. event.preventDefault();
  674. this.okAddBookmark();
  675. }
  676. }
  677. async okAddBookmark() {
  678. if (!this.bookmarkLink)
  679. return;
  680. const link = (this.addBookmarkMode == 'edit' ? lu.addProtocol(this.editBookmarkLink) : lu.addProtocol(this.bookmarkLink));
  681. let index = -1;
  682. try {
  683. index = lu.getRootIndexByUrl(this.libs.groups, link);
  684. } catch (e) {
  685. await this.$root.stdDialog.alert('Неверный формат ссылки', 'Ошибка');
  686. return;
  687. }
  688. let libs = _.cloneDeep(this.libs);
  689. //добавление
  690. //есть группа в закладках
  691. if (index >= 0) {
  692. const item = lu.getListItemByLink(libs.groups[index].list, link);
  693. //редактирование
  694. if (item && this.addBookmarkMode == 'edit') {
  695. if (item) {
  696. //редактируем
  697. item.l = link;
  698. item.c = this.bookmarkDesc;
  699. this.commitLibs(libs);
  700. } else {
  701. await this.$root.stdDialog.alert('Не удалось отредактировать закладку', 'Ошибка');
  702. }
  703. } else if (!item) {
  704. //добавляем
  705. if (libs.groups[index].list.length >= 100) {
  706. await this.$root.stdDialog.alert('Достигнут предел количества закладок для этого сайта', 'Ошибка');
  707. return;
  708. }
  709. libs.groups[index].list.push({l: link, c: this.bookmarkDesc});
  710. if (this.openInFrameOnAdd) {
  711. libs.startLink = link;
  712. libs.comment = this.bookmarkDesc;
  713. }
  714. this.commitLibs(libs);
  715. } else if (item.c != this.bookmarkDesc) {
  716. if (await this.$root.stdDialog.confirm(`Такая закладка уже существует с другим описанием.<br>` +
  717. `Заменить '${this.$root.sanitize(item.c)}' на '${this.$root.sanitize(this.bookmarkDesc)}'?`, ' ')) {
  718. item.c = this.bookmarkDesc;
  719. this.commitLibs(libs);
  720. } else
  721. return;
  722. } else {
  723. await this.$root.stdDialog.alert('Такая закладка уже существует', ' ');
  724. return;
  725. }
  726. } else {//нет группы в закладках
  727. if (libs.groups.length >= 100) {
  728. await this.$root.stdDialog.alert('Достигнут предел количества различных сайтов в закладках', 'Ошибка');
  729. return;
  730. }
  731. //добавляем сначала группу
  732. libs.groups.push({r: lu.getOrigin(link), s: link, list: []});
  733. index = lu.getSafeRootIndexByUrl(libs.groups, link);
  734. if (index >= 0)
  735. libs.groups[index].list.push({l: link, c: this.bookmarkDesc});
  736. if (this.openInFrameOnAdd) {
  737. libs.startLink = link;
  738. libs.comment = this.bookmarkDesc;
  739. }
  740. this.commitLibs(libs);
  741. }
  742. this.addBookmarkVisible = false;
  743. }
  744. fullScreenToggle() {
  745. this.fullScreenActive = !this.fullScreenActive;
  746. if (this.fullScreenActive) {
  747. this.$q.fullscreen.request();
  748. } else {
  749. this.$q.fullscreen.exit();
  750. }
  751. }
  752. transparentLayoutClick() {
  753. this.transparentLayoutVisible = false;
  754. }
  755. onSelectPopupShow() {
  756. this.transparentLayoutVisible = true;
  757. }
  758. onSelectPopupHide() {
  759. this.transparentLayoutVisible = false;
  760. }
  761. close() {
  762. this.sendMessage({type: 'close'});
  763. }
  764. bookUrlKeyDown(event) {
  765. if (event.key == 'Enter') {
  766. if (!this.openInFrameOnEnter) {
  767. this.submitUrl();
  768. } else {
  769. if (this.bookUrl)
  770. this.goToLink(this.bookUrl);
  771. }
  772. event.preventDefault();
  773. }
  774. }
  775. bookmarkSettings() {
  776. this.bookmarkSettingsActive = true;
  777. this.$nextTick(() => {
  778. this.$refs.bookmarkSettings.init();
  779. });
  780. }
  781. closeBookmarkSettings() {
  782. this.bookmarkSettingsActive = false;
  783. }
  784. showHelp() {
  785. this.$root.stdDialog.alert(`
  786. <p>Окно 'Сетевая библиотека' позволяет открывать ссылки в читалке без переключения между окнами,
  787. что особенно актуально для мобильных устройств. Имеется возможность управлять закладками
  788. на понравившиеся ресурсы, книги или страницы авторов. Открытие ссылок и навигация происходят во фрейме, но,
  789. к сожалению, в нем открываются не все страницы.</p>` +
  790. (this.mode === 'liberama' ?
  791. `<p>Доступ к сайтам <span style="color: blue">http://flibusta.is</span> и <span style="color: blue">http://fantasy-worlds.org</span> работает через прокси.
  792. <br><span style="color: red"><b>ПРЕДУПРЕЖДЕНИЕ!</b></span>
  793. Доступ предназначен только для просмотра и скачивания книг. Авторизоваться на этих сайтах
  794. из фрейма категорически не рекомендуется, т.к. ваше подключение не защищено и данные могут попасть
  795. к третьим лицам.
  796. </p>
  797. `
  798. : '') +
  799. `<p>Из-за проблем с безопасностью, навигация 'вперед-назад' во фрейме осуществляется с помощью контекстного меню правой кнопкой мыши.
  800. На мобильных устройствах для этого служит системная клавиша 'Назад (стрелка влево)' и опция 'Вперед (стрелка вправо)' в меню браузера.
  801. </p>
  802. <p>Приятного пользования ;-)
  803. </p>
  804. `, 'Справка', {iconName: 'la la-info-circle'});
  805. }
  806. keyHook(event) {
  807. if (this.$root.getRootRoute() == '/external-libs') {
  808. if (this.$root.stdDialog.active)
  809. return false;
  810. if (this.bookmarkSettingsActive && this.$refs.bookmarkSettings.keyHook(event))
  811. return true;
  812. if (this.addBookmarkVisible || this.optionsVisible)
  813. return false;
  814. if (event.type == 'keydown' && event.key == 'F4') {
  815. this.addBookmark();
  816. return true;
  817. }
  818. if (event.type == 'keydown' && event.key == 'Escape' &&
  819. (document.activeElement != this.$refs.rootLink.$refs.target || !this.$refs.rootLink.menu) &&
  820. (document.activeElement != this.$refs.selectedLink.$refs.target || !this.$refs.selectedLink.menu)
  821. ) {
  822. this.close();
  823. return true;
  824. }
  825. }
  826. return false;
  827. }
  828. }
  829. export default vueComponent(ExternalLibs);
  830. //-----------------------------------------------------------------------------
  831. </script>
  832. <style scoped>
  833. .separator {
  834. height: 1px;
  835. background-color: #A0A0A0;
  836. }
  837. .header-button {
  838. width: 30px;
  839. height: 30px;
  840. cursor: pointer;
  841. }
  842. .header-button:hover {
  843. color: white;
  844. background-color: #39902F;
  845. }
  846. .transparent-layout {
  847. top: 0;
  848. left: 0;
  849. position: absolute;
  850. }
  851. </style>