11//var db = new PouchDB('teletext');
22
33var justify = function ( str , len ) {
4-
54 var re = RegExp ( "(?:\\s|^)(.{1," + len + "})(?=\\s|$)" , "g" ) ;
65 var res = [ ] ;
76 var finalResult = [ ] ;
8-
97 while ( ( m = re . exec ( str ) ) !== null ) {
108 res . push ( m [ 1 ] ) ;
119 }
12-
1310 for ( var i = 0 ; i < res . length - 1 ; i ++ ) {
1411 if ( res [ i ] . indexOf ( ' ' ) != - 1 ) {
1512 while ( res [ i ] . length < len ) {
@@ -24,9 +21,7 @@ var justify = function(str, len) {
2421 }
2522 finalResult . push ( res [ i ] ) ;
2623 }
27-
2824 finalResult . push ( res [ res . length - 1 ] ) ;
29-
3025 let first = true
3126 finalResult = finalResult . map ( ( l ) => {
3227 if ( first ) {
@@ -36,9 +31,7 @@ var justify = function(str, len) {
3631 return ' ' + l
3732 }
3833 } )
39-
4034 return finalResult . join ( '\n' ) ;
41-
4235}
4336
4437var app = new Vue ( {
@@ -49,9 +42,7 @@ var app = new Vue({
4942 date : '' ,
5043 time : '' ,
5144 progress : 0 ,
52- stories : { } ,
53- subsetOfStories : { } ,
54- story : { }
45+ stories : [ ]
5546 } ,
5647 methods : {
5748 next : ( ) => {
@@ -98,34 +89,28 @@ var app = new Vue({
9889 computed : {
9990 paddedPageStr : function ( ) {
10091 return this . pageStr . padEnd ( 3 , '_' )
92+ } ,
93+ subsetOfStories : function ( ) {
94+ if ( this . page < 101 || this . page >= 200 ) {
95+ return [ ]
96+ }
97+ const start = 10 * ( this . page - 101 )
98+ return this . stories . slice ( start , start + 10 )
99+ } ,
100+ story : function ( ) {
101+ if ( this . page . toString ( ) . length !== 3 ) {
102+ return null
103+ }
104+ if ( this . page >= 200 ) {
105+ return this . stories [ this . page - 200 ]
106+ }
107+ return null
101108 }
102109 } ,
103110 created ( ) {
104111 window . addEventListener ( 'keydown' , async ( e ) => {
105112 app . key ( e )
106113 } ) ;
107- } ,
108- watch : {
109- page : function ( ) {
110- if ( this . page . toString ( ) . length !== 3 ) {
111- return
112- }
113- this . subsetOfStories = { }
114- this . story = { }
115- if ( this . page == 100 ) {
116- return
117- }
118- if ( this . page >= 200 ) {
119- this . story = this . stories [ this . page ]
120- } else {
121- const start = 200 + ( this . page - 101 ) * 10
122- for ( var i = start ; i < start + 10 ; i ++ ) {
123- this . subsetOfStories [ i ] = this . stories [ i ]
124- this . subsetOfStories [ i ] . justified = justify ( this . subsetOfStories [ i ] . description , 39 )
125- this . subsetOfStories [ i ] . justifiedTitle = justify ( this . subsetOfStories [ i ] . title , 39 )
126- }
127- }
128- }
129114 }
130115} )
131116
@@ -160,7 +145,10 @@ const startup = async function() {
160145 story . url = story . link
161146 story . shorturl = story . link . substr ( 0 , 20 )
162147 story . time = story . pubDate
163- app . stories [ id ] = story
148+ story . justified = justify ( story . description , 39 )
149+ story . justifiedTitle = justify ( story . title , 39 )
150+ story . id = id
151+ app . stories . push ( story )
164152 app . progress ++
165153 id ++
166154 if ( id >= 300 ) {
0 commit comments