App.vue 8.2 KB

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