App.vue 9.3 KB

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