App.vue 8.6 KB

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