App.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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('keydown', (event) => {
  85. this.keyHook(event);
  86. });
  87. window.addEventListener('resize', () => {
  88. this.$root.$emit('resize');
  89. });
  90. }
  91. routerReady() {
  92. return new Promise ((resolve) => {
  93. this.$router.onReady(() => {
  94. resolve();
  95. });
  96. });
  97. }
  98. mounted() {
  99. this.$root.notify = this.$refs.notify;
  100. this.$root.stdDialog = this.$refs.stdDialog;
  101. this.dispatch('config/loadConfig');
  102. this.$watch('apiError', function(newError) {
  103. if (newError) {
  104. let mes = newError.message;
  105. if (newError.response && newError.response.config)
  106. mes = newError.response.config.url + '<br>' + newError.response.statusText;
  107. this.$root.notify.error(mes, 'Ошибка API');
  108. }
  109. });
  110. this.setAppTitle();
  111. (async() => {
  112. await this.routerReady();
  113. this.redirectIfNeeded();
  114. })();
  115. }
  116. toggleCollapse() {
  117. this.commit('uistate/setAsideBarCollapse', !this.uistate.asideBarCollapse);
  118. this.$root.$emit('resize');
  119. }
  120. get isCollapse() {
  121. return this.uistate.asideBarCollapse;
  122. }
  123. get asideWidth() {
  124. if (this.uistate.asideBarCollapse) {
  125. return 64;
  126. } else {
  127. return 170;
  128. }
  129. }
  130. get buttonCollapseIcon() {
  131. if (this.uistate.asideBarCollapse) {
  132. return 'el-icon-d-arrow-right';
  133. } else {
  134. return 'el-icon-d-arrow-left';
  135. }
  136. }
  137. get appName() {
  138. if (this.isCollapse)
  139. return '<br><br>';
  140. else
  141. return `${this.config.name} <br>v${this.config.version}`;
  142. }
  143. get apiError() {
  144. return this.state.apiError;
  145. }
  146. get rootRoute() {
  147. return this.$root.rootRoute();
  148. }
  149. setAppTitle(title) {
  150. if (!title) {
  151. if (this.mode == 'liberama.top') {
  152. document.title = `Liberama Reader - всегда с вами`;
  153. } else if (this.mode == 'omnireader') {
  154. document.title = `Omni Reader - всегда с вами`;
  155. } else if (this.config && this.mode !== null) {
  156. document.title = `${this.config.name} - ${this.itemRuText[this.$root.rootRoute]}`;
  157. }
  158. } else {
  159. document.title = title;
  160. }
  161. }
  162. itemTitleClass(path) {
  163. return (this.rootRoute == path ? {'bold-font': true} : {});
  164. }
  165. get mode() {
  166. return this.$store.state.config.mode;
  167. }
  168. get showAsideBar() {
  169. return (this.mode !== null && this.mode != 'reader' && this.mode != 'omnireader' && this.mode != 'liberama.top');
  170. }
  171. set showAsideBar(value) {
  172. }
  173. get isReaderActive() {
  174. return (this.rootRoute == '/reader' || this.rootRoute == '/external-libs');
  175. }
  176. redirectIfNeeded() {
  177. if ((this.mode == 'reader' || this.mode == 'omnireader' || this.mode == 'liberama.top')) {
  178. const search = window.location.search.substr(1);
  179. //распознавание параметра url вида "?url=<link>" и редирект при необходимости
  180. if (!this.isReaderActive) {
  181. const s = search.split('url=');
  182. const url = s[1] || '';
  183. const q = utils.parseQuery(s[0] || '');
  184. if (url) {
  185. q.url = decodeURIComponent(url);
  186. }
  187. window.history.replaceState({}, '', '/');
  188. this.$router.replace({ path: '/reader', query: q });
  189. }
  190. }
  191. }
  192. }
  193. //-----------------------------------------------------------------------------
  194. </script>
  195. <style scoped>
  196. .app-name {
  197. margin-left: 10px;
  198. margin-top: 10px;
  199. text-align: center;
  200. line-height: 140%;
  201. font-weight: bold;
  202. }
  203. </style>
  204. <style>
  205. body, html, #app {
  206. margin: 0;
  207. padding: 0;
  208. width: 100%;
  209. height: 100%;
  210. font: normal 12pt ReaderDefault;
  211. }
  212. .dborder {
  213. border: 2px solid yellow !important;
  214. }
  215. .icon-rotate {
  216. vertical-align: middle;
  217. animation: rotating 2s linear infinite;
  218. }
  219. .notify-button-icon {
  220. font-size: 16px !important;
  221. }
  222. @font-face {
  223. font-family: 'ReaderDefault';
  224. src: url('fonts/reader-default.woff') format('woff'),
  225. url('fonts/reader-default.ttf') format('truetype');
  226. }
  227. @font-face {
  228. font-family: 'OpenSans';
  229. src: url('fonts/open-sans.woff') format('woff'),
  230. url('fonts/open-sans.ttf') format('truetype');
  231. }
  232. @font-face {
  233. font-family: 'Roboto';
  234. src: url('fonts/roboto.woff') format('woff'),
  235. url('fonts/roboto.ttf') format('truetype');
  236. }
  237. @font-face {
  238. font-family: 'Rubik';
  239. src: url('fonts/rubik.woff2') format('woff2');
  240. }
  241. @font-face {
  242. font-family: 'Avrile';
  243. src: url('fonts/avrile.woff') format('woff'),
  244. url('fonts/avrile.ttf') format('truetype');
  245. }
  246. @font-face {
  247. font-family: 'Arimo';
  248. src: url('fonts/arimo.woff2') format('woff2');
  249. }
  250. @font-face {
  251. font-family: 'GEO_1';
  252. src: url('fonts/geo_1.woff') format('woff'),
  253. url('fonts/geo_1.ttf') format('truetype');
  254. }
  255. </style>