App.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <template>
  2. <div class="fit row">
  3. <Notify ref="notify" />
  4. <StdDialog ref="stdDialog" />
  5. <router-view v-slot="{ Component }">
  6. <keep-alive v-if="showPage">
  7. <component :is="Component" class="col" />
  8. </keep-alive>
  9. </router-view>
  10. </div>
  11. </template>
  12. <script>
  13. //-----------------------------------------------------------------------------
  14. import vueComponent from './vueComponent.js';
  15. import Notify from './share/Notify.vue';
  16. import StdDialog from './share/StdDialog.vue';
  17. import sanitizeHtml from 'sanitize-html';
  18. import miscApi from '../api/misc';
  19. import * as utils from '../share/utils';
  20. const componentOptions = {
  21. components: {
  22. Notify,
  23. StdDialog,
  24. },
  25. watch: {
  26. mode: function() {
  27. this.setAppTitle();
  28. this.redirectIfNeeded();
  29. }
  30. },
  31. };
  32. class App {
  33. _options = componentOptions;
  34. showPage = false;
  35. itemRuText = {
  36. '/cardindex': 'Картотека',
  37. '/reader': 'Читалка',
  38. '/forum': 'Форум-чат',
  39. '/income': 'Поступления',
  40. '/sources': 'Источники',
  41. '/settings': 'Параметры',
  42. '/help': 'Справка',
  43. };
  44. created() {
  45. this.commit = this.$store.commit;
  46. this.state = this.$store.state;
  47. this.uistate = this.$store.state.uistate;
  48. this.config = this.$store.state.config;
  49. //root route
  50. let cachedRoute = '';
  51. let cachedPath = '';
  52. this.$root.getRootRoute = () => {
  53. if (this.$route.path != cachedPath) {
  54. cachedPath = this.$route.path;
  55. const m = cachedPath.match(/^(\/[^/]*).*$/i);
  56. cachedRoute = (m ? m[1] : this.$route.path);
  57. }
  58. return cachedRoute;
  59. }
  60. this.$router.beforeEach((to, from, next) => {
  61. //распознавание хоста, если присутствует домен 3-уровня "b.", то разрешена только определенная страница
  62. if (window.location.host.indexOf('b.') == 0 && to.path != '/external-libs' && to.path != '/404') {
  63. next('/404');
  64. } else {
  65. next();
  66. }
  67. });
  68. this.$root.isMobileDevice = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent);
  69. // setAppTitle
  70. this.$root.setAppTitle = this.setAppTitle;
  71. //sanitize
  72. this.$root.sanitize = sanitizeHtml;
  73. //global event hooks
  74. this.eventHooks = {};
  75. this.$root.eventHook = (hookName, event) => {
  76. if (!this.eventHooks[hookName])
  77. return;
  78. for (const hook of this.eventHooks[hookName])
  79. hook(event);
  80. }
  81. this.$root.addEventHook = (hookName, hook) => {
  82. if (!this.eventHooks[hookName])
  83. this.eventHooks[hookName] = [];
  84. if (this.eventHooks[hookName].indexOf(hook) < 0)
  85. this.eventHooks[hookName].push(hook);
  86. }
  87. this.$root.removeEventHook = (hookName, hook) => {
  88. if (!this.eventHooks[hookName])
  89. return;
  90. const i = this.eventHooks[hookName].indexOf(hook);
  91. if (i >= 0)
  92. this.eventHooks[hookName].splice(i, 1);
  93. }
  94. document.addEventListener('keyup', (event) => {
  95. this.$root.eventHook('key', event);
  96. });
  97. document.addEventListener('keypress', (event) => {
  98. this.$root.eventHook('key', event);
  99. });
  100. document.addEventListener('keydown', (event) => {
  101. this.$root.eventHook('key', event);
  102. });
  103. window.addEventListener('resize', (event) => {
  104. this.$root.eventHook('resize', event);
  105. });
  106. }
  107. mounted() {
  108. this.$root.notify = this.$refs.notify;
  109. this.$root.stdDialog = this.$refs.stdDialog;
  110. this.setAppTitle();
  111. (async() => {
  112. //загрузим конфиг сревера
  113. try {
  114. const config = await miscApi.loadConfig();
  115. this.commit('config/setConfig', config);
  116. this.showPage = true;
  117. } catch(e) {
  118. //проверим, не получен ли конфиг ранее
  119. if (!this.mode) {
  120. this.$root.notify.error(e.message, 'Ошибка API');
  121. } else {
  122. //вероятно, работаем в оффлайне
  123. this.showPage = true;
  124. }
  125. console.error(e);
  126. }
  127. //запросим persistent storage
  128. if (navigator.storage && navigator.storage.persist) {
  129. navigator.storage.persist();
  130. }
  131. await this.$router.isReady();
  132. this.redirectIfNeeded();
  133. })();
  134. }
  135. toggleCollapse() {
  136. this.commit('uistate/setAsideBarCollapse', !this.uistate.asideBarCollapse);
  137. this.$root.eventHook('resize');
  138. }
  139. get isCollapse() {
  140. return this.uistate.asideBarCollapse;
  141. }
  142. get asideWidth() {
  143. if (this.uistate.asideBarCollapse) {
  144. return 64;
  145. } else {
  146. return 170;
  147. }
  148. }
  149. get buttonCollapseIcon() {
  150. if (this.uistate.asideBarCollapse) {
  151. return 'el-icon-d-arrow-right';
  152. } else {
  153. return 'el-icon-d-arrow-left';
  154. }
  155. }
  156. get appName() {
  157. if (this.isCollapse)
  158. return '<br><br>';
  159. else
  160. return `${this.config.name} <br>v${this.config.version}`;
  161. }
  162. get apiError() {
  163. return this.state.apiError;
  164. }
  165. get rootRoute() {
  166. return this.$root.getRootRoute();
  167. }
  168. setAppTitle(title) {
  169. if (!title) {
  170. if (this.mode == 'liberama.top') {
  171. document.title = `Liberama Reader - всегда с вами`;
  172. } else if (this.mode == 'omnireader') {
  173. document.title = `Omni Reader - всегда с вами`;
  174. } else if (this.config && this.mode !== null) {
  175. document.title = `${this.config.name} - ${this.itemRuText[this.rootRoute]}`;
  176. }
  177. } else {
  178. document.title = title;
  179. }
  180. }
  181. itemTitleClass(path) {
  182. return (this.rootRoute == path ? {'bold-font': true} : {});
  183. }
  184. get mode() {
  185. return this.$store.state.config.mode;
  186. }
  187. get showAsideBar() {
  188. return (this.mode !== null && this.mode != 'reader' && this.mode != 'omnireader' && this.mode != 'liberama.top');
  189. }
  190. set showAsideBar(value) {
  191. }
  192. get isReaderActive() {
  193. return (this.rootRoute == '/reader' || this.rootRoute == '/external-libs');
  194. }
  195. redirectIfNeeded() {
  196. if ((this.mode == 'reader' || this.mode == 'omnireader' || this.mode == 'liberama.top')) {
  197. const search = window.location.search.substr(1);
  198. //распознавание параметра url вида "?url=<link>" и редирект при необходимости
  199. if (!this.isReaderActive) {
  200. const s = search.split('url=');
  201. const url = s[1] || '';
  202. const q = utils.parseQuery(s[0] || '');
  203. if (url) {
  204. q.url = decodeURIComponent(url);
  205. }
  206. window.history.replaceState({}, '', '/');
  207. this.$router.replace({ path: '/reader', query: q });
  208. }
  209. }
  210. }
  211. }
  212. export default vueComponent(App);
  213. //-----------------------------------------------------------------------------
  214. </script>
  215. <style scoped>
  216. .app-name {
  217. margin-left: 10px;
  218. margin-top: 10px;
  219. text-align: center;
  220. line-height: 140%;
  221. font-weight: bold;
  222. }
  223. </style>
  224. <style>
  225. body, html, #app {
  226. margin: 0;
  227. padding: 0;
  228. width: 100%;
  229. height: 100%;
  230. font: normal 12pt ReaderDefault;
  231. }
  232. .dborder {
  233. border: 2px solid magenta !important;
  234. }
  235. .icon-rotate {
  236. vertical-align: middle;
  237. animation: rotating 2s linear infinite;
  238. }
  239. @keyframes rotating {
  240. from {
  241. transform: rotate(0deg);
  242. } to {
  243. transform: rotate(360deg);
  244. }
  245. }
  246. .notify-button-icon {
  247. font-size: 16px !important;
  248. }
  249. @font-face {
  250. font-family: 'ReaderDefault';
  251. src: url('fonts/reader-default.woff') format('woff'),
  252. url('fonts/reader-default.ttf') format('truetype');
  253. }
  254. @font-face {
  255. font-family: 'OpenSans';
  256. src: url('fonts/open-sans.woff') format('woff'),
  257. url('fonts/open-sans.ttf') format('truetype');
  258. }
  259. @font-face {
  260. font-family: 'Roboto';
  261. src: url('fonts/roboto.woff') format('woff'),
  262. url('fonts/roboto.ttf') format('truetype');
  263. }
  264. @font-face {
  265. font-family: 'Rubik';
  266. src: url('fonts/rubik.woff2') format('woff2');
  267. }
  268. @font-face {
  269. font-family: 'Avrile';
  270. src: url('fonts/avrile.woff') format('woff'),
  271. url('fonts/avrile.ttf') format('truetype');
  272. }
  273. @font-face {
  274. font-family: 'Arimo';
  275. src: url('fonts/arimo.woff2') format('woff2');
  276. }
  277. @font-face {
  278. font-family: 'GEO_1';
  279. src: url('fonts/geo_1.woff') format('woff'),
  280. url('fonts/geo_1.ttf') format('truetype');
  281. }
  282. </style>