App.vue 8.4 KB

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