@@ -20,8 +20,8 @@ window.$ = $;
2020Backbone . $ = $ ;
2121
2222_ . templateSettings = {
23- interpolate : / \{ \{ = ( .+ ?) \} \} / g,
24- evaluate : / \{ \{ ( .+ ?) \} \} / g
23+ evaluate : / \{ \{ ( .+ ?) \} \} / g,
24+ interpolate : / \{ \{ = ( .+ ?) \} \} / g
2525} ;
2626
2727function CSRFToken ( ) {
@@ -35,25 +35,17 @@ function requestHeaders() {
3535}
3636
3737var Story = Backbone . Model . extend ( {
38+ close : function ( ) {
39+ this . set ( "open" , false ) ;
40+ } ,
41+
3842 defaults : function ( ) {
3943 return {
4044 "open" : false ,
4145 "selected" : false
4246 }
4347 } ,
4448
45- toggle : function ( ) {
46- if ( this . get ( "open" ) ) {
47- this . close ( ) ;
48- } else {
49- this . open ( ) ;
50- }
51- } ,
52-
53- shouldSave : function ( ) {
54- return this . changedAttributes ( ) && this . get ( "id" ) > 0 ;
55- } ,
56-
5749 open : function ( ) {
5850 if ( ! this . get ( "keep_unread" ) ) this . set ( "is_read" , true ) ;
5951 if ( this . shouldSave ( ) ) this . save ( null , { headers : requestHeaders ( ) } ) ;
@@ -68,6 +60,27 @@ var Story = Backbone.Model.extend({
6860 this . set ( "selected" , true ) ;
6961 } ,
7062
63+ openInTab : function ( ) {
64+ window . open ( this . get ( "permalink" ) , '_blank' ) ;
65+ } ,
66+
67+ select : function ( ) {
68+ if ( this . collection ) this . collection . unselectAll ( ) ;
69+ this . set ( "selected" , true ) ;
70+ } ,
71+
72+ shouldSave : function ( ) {
73+ return this . changedAttributes ( ) && this . get ( "id" ) > 0 ;
74+ } ,
75+
76+ toggle : function ( ) {
77+ if ( this . get ( "open" ) ) {
78+ this . close ( ) ;
79+ } else {
80+ this . open ( ) ;
81+ }
82+ } ,
83+
7184 toggleKeepUnread : function ( ) {
7285 if ( this . get ( "keep_unread" ) ) {
7386 this . set ( "keep_unread" , false ) ;
@@ -80,30 +93,13 @@ var Story = Backbone.Model.extend({
8093 if ( this . shouldSave ( ) ) this . save ( null , { headers : requestHeaders ( ) } ) ;
8194 } ,
8295
83- close : function ( ) {
84- this . set ( "open" , false ) ;
85- } ,
86-
87- select : function ( ) {
88- if ( this . collection ) this . collection . unselectAll ( ) ;
89- this . set ( "selected" , true ) ;
90- } ,
91-
9296 unselect : function ( ) {
9397 this . set ( "selected" , false ) ;
94- } ,
95-
96- openInTab : function ( ) {
97- window . open ( this . get ( "permalink" ) , '_blank' ) ;
9898 }
9999} ) ;
100100
101101var StoryView = Backbone . View . extend ( {
102- tagName : "li" ,
103102 className : "story" ,
104-
105- template : "#story-template" ,
106-
107103 events : {
108104 "click .story-preview" : "storyClicked"
109105 } ,
@@ -116,11 +112,31 @@ var StoryView = Backbone.View.extend({
116112 this . listenTo ( this . model , 'change:is_read' , this . itemRead ) ;
117113 this . el . addEventListener ( 'keep-unread-toggle:toggled' , ( e ) => {
118114 var detail = e . detail ;
119- this . model . set ( { keep_unread : detail . keepUnread , is_read : detail . isRead } , { silent : true } ) ;
115+ this . model . set ( { is_read : detail . isRead , keep_unread : detail . keepUnread } , { silent : true } ) ;
120116 this . model . trigger ( 'change:is_read' ) ;
121117 } ) ;
122118 } ,
123119
120+ itemOpened : function ( ) {
121+ if ( this . model . get ( "open" ) ) {
122+ this . $el . addClass ( "open" ) ;
123+ $ ( ".story-lead" , this . $el ) . fadeOut ( 1000 ) ;
124+ window . scrollTo ( 0 , this . $el . offset ( ) . top ) ;
125+ } else {
126+ this . $el . removeClass ( "open" ) ;
127+ $ ( ".story-lead" , this . $el ) . show ( ) ;
128+ }
129+ } ,
130+
131+ itemRead : function ( ) {
132+ this . $el . toggleClass ( "read" , this . model . get ( "is_read" ) ) ;
133+ } ,
134+
135+ itemSelected : function ( ) {
136+ this . $el . toggleClass ( "cursor" , this . model . get ( "selected" ) ) ;
137+ if ( ! this . $el . visible ( ) ) window . scrollTo ( 0 , this . $el . offset ( ) . top ) ;
138+ } ,
139+
124140 render : function ( ) {
125141 var jsonModel = this . model . toJSON ( ) ;
126142 this . $el . html ( this . template ( jsonModel ) ) ;
@@ -141,26 +157,6 @@ var StoryView = Backbone.View.extend({
141157 return this ;
142158 } ,
143159
144- itemRead : function ( ) {
145- this . $el . toggleClass ( "read" , this . model . get ( "is_read" ) ) ;
146- } ,
147-
148- itemOpened : function ( ) {
149- if ( this . model . get ( "open" ) ) {
150- this . $el . addClass ( "open" ) ;
151- $ ( ".story-lead" , this . $el ) . fadeOut ( 1000 ) ;
152- window . scrollTo ( 0 , this . $el . offset ( ) . top ) ;
153- } else {
154- this . $el . removeClass ( "open" ) ;
155- $ ( ".story-lead" , this . $el ) . show ( ) ;
156- }
157- } ,
158-
159- itemSelected : function ( ) {
160- this . $el . toggleClass ( "cursor" , this . model . get ( "selected" ) ) ;
161- if ( ! this . $el . visible ( ) ) window . scrollTo ( 0 , this . $el . offset ( ) . top ) ;
162- } ,
163-
164160 storyClicked : function ( e ) {
165161 if ( e . metaKey || e . ctrlKey || e . which == 2 ) {
166162 var backgroundTab = window . open ( this . model . get ( "permalink" ) ) ;
@@ -174,48 +170,29 @@ var StoryView = Backbone.View.extend({
174170 }
175171 } ,
176172
177- } ) ;
178-
179- var StoryList = Backbone . Collection . extend ( {
180- model : Story ,
181- url : "/stories" ,
182-
183- initialize : function ( ) {
184- this . cursorPosition = - 1 ;
185- } ,
173+ tagName : "li" ,
186174
187- max_position : function ( ) {
188- return this . length - 1 ;
189- } ,
175+ template : "#story-template" ,
190176
191- unreadCount : function ( ) {
192- return this . where ( { is_read : false } ) . length ;
193- } ,
177+ } ) ;
194178
179+ var StoryList = Backbone . Collection . extend ( {
195180 closeOthers : function ( modelToSkip ) {
196181 this . each ( function ( model ) {
197182 if ( model . id != modelToSkip . id ) {
198183 model . close ( ) ;
199184 }
200185 } ) ;
201186 } ,
202-
203- selected : function ( ) {
204- return this . where ( { selected : true } ) ;
205- } ,
206-
207- unselectAll : function ( ) {
208- _ . invoke ( this . selected ( ) , "unselect" ) ;
187+ initialize : function ( ) {
188+ this . cursorPosition = - 1 ;
209189 } ,
210190
211- selectedStoryId : function ( ) {
212- var selectedStory = this . at ( this . cursorPosition ) ;
213- return selectedStory ? selectedStory . id : - 1 ;
191+ max_position : function ( ) {
192+ return this . length - 1 ;
214193 } ,
215194
216- setSelection : function ( model ) {
217- this . cursorPosition = this . indexOf ( model ) ;
218- } ,
195+ model : Story ,
219196
220197 moveCursorDown : function ( ) {
221198 if ( this . cursorPosition < this . max_position ( ) ) {
@@ -241,23 +218,55 @@ var StoryList = Backbone.Collection.extend({
241218 this . at ( this . cursorPosition ) . open ( ) ;
242219 } ,
243220
221+ selected : function ( ) {
222+ return this . where ( { selected : true } ) ;
223+ } ,
224+
225+ selectedStoryId : function ( ) {
226+ var selectedStory = this . at ( this . cursorPosition ) ;
227+ return selectedStory ? selectedStory . id : - 1 ;
228+ } ,
229+
230+ setSelection : function ( model ) {
231+ this . cursorPosition = this . indexOf ( model ) ;
232+ } ,
233+
244234 toggleCurrent : function ( ) {
245235 if ( this . cursorPosition < 0 ) this . cursorPosition = 0 ;
246236 this . at ( this . cursorPosition ) . toggle ( ) ;
247237 } ,
248238
249- viewCurrentInTab : function ( ) {
239+ toggleCurrentKeepUnread : function ( ) {
250240 if ( this . cursorPosition < 0 ) this . cursorPosition = 0 ;
251- this . at ( this . cursorPosition ) . openInTab ( ) ;
241+ this . at ( this . cursorPosition ) . toggleKeepUnread ( ) ;
252242 } ,
253243
254- toggleCurrentKeepUnread : function ( ) {
244+ unreadCount : function ( ) {
245+ return this . where ( { is_read : false } ) . length ;
246+ } ,
247+
248+ unselectAll : function ( ) {
249+ _ . invoke ( this . selected ( ) , "unselect" ) ;
250+ } ,
251+
252+ url : "/stories" ,
253+
254+ viewCurrentInTab : function ( ) {
255255 if ( this . cursorPosition < 0 ) this . cursorPosition = 0 ;
256- this . at ( this . cursorPosition ) . toggleKeepUnread ( ) ;
256+ this . at ( this . cursorPosition ) . openInTab ( ) ;
257257 }
258258} ) ;
259259
260260var AppView = Backbone . View . extend ( {
261+ addAll : function ( ) {
262+ this . stories . each ( this . addOne , this ) ;
263+ } ,
264+
265+ addOne : function ( story ) {
266+ var view = new StoryView ( { model : story } ) ;
267+ this . $ ( "#story-list" ) . append ( view . render ( ) . el ) ;
268+ } ,
269+
261270 el : "#stories" ,
262271
263272 initialize : function ( collection ) {
@@ -273,25 +282,6 @@ var AppView = Backbone.View.extend({
273282 this . stories . reset ( data ) ;
274283 } ,
275284
276- render : function ( ) {
277- var unreadCount = this . stories . unreadCount ( ) ;
278-
279- if ( unreadCount === 0 ) {
280- document . title = window . i18n . titleName ;
281- } else {
282- document . title = "(" + unreadCount + ") " + window . i18n . titleName ;
283- }
284- } ,
285-
286- addOne : function ( story ) {
287- var view = new StoryView ( { model : story } ) ;
288- this . $ ( "#story-list" ) . append ( view . render ( ) . el ) ;
289- } ,
290-
291- addAll : function ( ) {
292- this . stories . each ( this . addOne , this ) ;
293- } ,
294-
295285 moveCursorDown : function ( ) {
296286 this . stories . moveCursorDown ( ) ;
297287 } ,
@@ -304,16 +294,26 @@ var AppView = Backbone.View.extend({
304294 this . stories . openCurrentSelection ( ) ;
305295 } ,
306296
307- toggleCurrent : function ( ) {
308- this . stories . toggleCurrent ( ) ;
297+ render : function ( ) {
298+ var unreadCount = this . stories . unreadCount ( ) ;
299+
300+ if ( unreadCount === 0 ) {
301+ document . title = window . i18n . titleName ;
302+ } else {
303+ document . title = "(" + unreadCount + ") " + window . i18n . titleName ;
304+ }
309305 } ,
310306
311- viewCurrentInTab : function ( ) {
312- this . stories . viewCurrentInTab ( ) ;
307+ toggleCurrent : function ( ) {
308+ this . stories . toggleCurrent ( ) ;
313309 } ,
314310
315311 toggleCurrentKeepUnread : function ( ) {
316312 this . stories . toggleCurrentKeepUnread ( ) ;
313+ } ,
314+
315+ viewCurrentInTab : function ( ) {
316+ this . stories . viewCurrentInTab ( ) ;
317317 }
318318} ) ;
319319
0 commit comments