Browse Source

Добавил реакцию на роутер

Book Pauk 6 years ago
parent
commit
121d055419
2 changed files with 57 additions and 4 deletions
  1. 20 0
      client/components/Reader/Reader.vue
  2. 37 4
      client/components/Reader/TextPage/TextPage.vue

+ 20 - 0
client/components/Reader/Reader.vue

@@ -82,6 +82,21 @@ class Reader extends Vue {
         /*while (this.lastOpenedBook) {
             this.commit('reader/delOpenedBook', this.lastOpenedBook);
         }*/
+        if (this.$root.rootRoute == '/reader' && this.routeParamUrl && this.routeParamUrl != this.lastOpenedBook.url) {
+            this.commit('reader/setLoaderActive', true);
+            this.loadBook({url: this.routeParamUrl});
+        }        
+    }
+
+    get routeParamUrl() {
+        let result = '';
+        const path = this.$route.fullPath;
+        const i = path.indexOf('url=');
+        if (i >= 0) {
+            result = path.substr(i + 4);
+        }
+        
+        return decodeURIComponent(result);
     }
 
     get loaderActive() {
@@ -130,6 +145,11 @@ class Reader extends Vue {
         if (result != 'TextPage') {
             this.$root.$emit('set-app-title');
         }
+
+        if (result == 'LoaderPage') {
+            this.$router.replace('/reader');
+        }
+        
         return result;
     }
 

+ 37 - 4
client/components/Reader/TextPage/TextPage.vue

@@ -1,6 +1,8 @@
 <template>
     <div class="main">
         <pre>{{ meta }}</pre>
+        <pre>{{ bookPos }}</pre>
+        <pre>{{ $route.query }}</pre>
     </div>
 </template>
 
@@ -13,10 +15,22 @@ import _ from 'lodash';
 import bookManager from '../share/bookManager';
 
 export default @Component({
+    watch: {
+        bookPos: function(newValue) {
+            this.updateRoute(newValue);
+            this.drawPage();
+        },
+        routeParamPos: function(newValue) {
+            if (newValue !== undefined && newValue != this.bookPos) {
+                this.bookPos = newValue;
+            }
+        },
+    },
 })
 class TextPage extends Vue {
     meta = null;
     fb2 = null;
+    bookPos = 0;
 
     created() {
         this.commit = this.$store.commit;
@@ -24,7 +38,7 @@ class TextPage extends Vue {
         this.config = this.$store.state.config;
         this.reader = this.$store.state.reader;
 
-        this.openFailed = false;
+        this.lastOpenTry = '';
     }
 
     activated() {
@@ -32,15 +46,15 @@ class TextPage extends Vue {
         this.meta = null;
         this.fb2 = null;
 
-        const last = this.lastOpenedBook;
+        let last = this.lastOpenedBook;
         if (last) {
             (async() => {
                 const isParsed = await bookManager.hasBookParsed(last);
                 if (!isParsed) {
                     this.$root.$emit('set-app-title');
-                    if (!this.openFailed) {
+                    if (this.lastOpenTry != last) {
                         this.$emit('parse-book', last);
-                        this.openFailed = true;
+                        this.lastOpenTry = last;
                     }
                     return;
                 }
@@ -55,6 +69,8 @@ class TextPage extends Vue {
                     this.fb2.bookTitle
                 ]).join(' '));
 
+                this.bookPos = (this.routeParamPos !== undefined ? this.routeParamPos : last.bookPos || 0);
+                this.updateRoute(this.bookPos);
                 this.drawPage();
             })();
         }
@@ -64,6 +80,23 @@ class TextPage extends Vue {
         return this.$store.getters['reader/lastOpenedBook'];
     }
 
+    get routeParamPos() {
+        let result = undefined;
+        const q = this.$route.query;
+        if (q['__p']) {
+            result = q['__p'];
+            if (Array.isArray(result))
+                result = result[0];
+        }
+        
+        return (result ? parseInt(result, 10) || 0 : result);
+    }
+
+    updateRoute(newPos) {
+        if (this.book)
+            this.$router.replace(`/reader?__p=${newPos}&url=${this.lastOpenedBook.url}`);
+    }
+
     drawPage() {
         const last = this.lastOpenedBook;
         if (!last)