Skip to content

Commit e052ca8

Browse files
author
Matt Swanson
committed
Move clientside code to backbone.
1 parent 95cd1ae commit e052ca8

15 files changed

Lines changed: 372 additions & 181 deletions

File tree

‎app.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Stringer < Sinatra::Base
2828
register Sinatra::Flash
2929
register Sinatra::Contrib
3030
register Sinatra::AssetPack
31+
32+
ActiveRecord::Base.include_root_in_json = false
3133
end
3234

3335
assets {
@@ -61,6 +63,14 @@ def render_partial(name, locals = {})
6163
erb "partials/_#{name}".to_sym, layout: false, locals: locals
6264
end
6365

66+
def render_js_template(name)
67+
erb "js/templates/_#{name}.js".to_sym, layout: false
68+
end
69+
70+
def render_js(name, locals = {})
71+
erb "js/#{name}.js".to_sym, layout: false, locals: locals
72+
end
73+
6474
def t(*args)
6575
I18n.t(*args)
6676
end

‎app/controllers/stories_controller.rb‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ class Stringer < Sinatra::Base
1414
erb :archive
1515
end
1616

17-
post "/mark_as_read" do
18-
story = StoryRepository.fetch(params[:story_id])
19-
story.is_read = true
17+
put "/stories/:id" do
18+
json_params = JSON.parse(request.body.read, symbolize_names: true)
19+
20+
story = StoryRepository.fetch(params[:id])
21+
story.is_read = json_params[:is_read]
2022
StoryRepository.save(story)
2123
end
2224

‎app/models/story.rb‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ def lead
1818
def source
1919
self.feed.name
2020
end
21+
22+
def pretty_date
23+
self.published.strftime("%A, %B %d")
24+
end
25+
26+
def as_json(options = {})
27+
super(methods: [:headline, :lead, :source, :pretty_date])
28+
end
2129
end

‎app/public/css/styles.css‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,14 @@ li.story.open .story-preview {
166166
margin-left: 20px;
167167
}
168168

169+
.story-keep-unread {
170+
margin-right: 25px;
171+
}
172+
169173
.story-body {
170174
margin-left: 20px;
171175
margin-right: 20px;
176+
margin-bottom: 10px;
172177
text-align: left;
173178
}
174179

@@ -197,6 +202,14 @@ li.story.cursor {
197202
border: 3px solid #484948;
198203
}
199204

205+
li.story .story-body-container {
206+
display: none;
207+
}
208+
209+
li.story.open .story-body-container {
210+
display: block;
211+
}
212+
200213
p.story-details {
201214
margin-right: 5px;
202215
overflow: hidden;

‎app/public/js/app.js‎

Lines changed: 6 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,9 @@
1-
$(document).ready(function() {
2-
var previous_req_id = 0;
3-
$(".story-preview").click(function(e){
4-
e.preventDefault();
5-
6-
var $this = $(this);
7-
8-
var openStories = $("li.story.open");
9-
10-
if(!$this.hasClass("open")) {
11-
$this.trigger("openStory");
12-
}
13-
14-
openStories.trigger("closeStory");
15-
});
16-
17-
$("li.story").on("closeStory", function(e) {
18-
e.preventDefault();
19-
var $this = $(this);
20-
21-
if($this.hasClass("open")) {
22-
$this.removeClass("open");
23-
$this.addClass("read");
24-
25-
$(".story-lead", this).show();
26-
$(".story-body-container", this).stop().hide();
27-
}
28-
});
29-
30-
$("li.story").on("openStory", function(e) {
31-
e.preventDefault();
32-
var $this = $(this);
33-
34-
$this.addClass("open");
35-
36-
$(".story-lead", this).fadeOut(1000);
37-
$(".story-body-container", this).stop().show();
38-
39-
var storyId = $this.data("id");
40-
41-
if (storyId > 0) {
42-
$.post("/mark_as_read", { story_id: storyId })
43-
.done(function() {
44-
if(previous_req_id != storyId ){
45-
var title = $("head > title")[0];
46-
var stories_count = parseInt(title.text.match(/\d+/));
47-
if (stories_count > 1){
48-
title.text = title.text.replace(/\d+/,stories_count - 1);
49-
}else{
50-
title.text = title.text.replace("(1)","")
51-
}
52-
previous_req_id = storyId;
53-
}
54-
})
55-
.fail(function() { alert("something broke!"); });
56-
}
57-
});
58-
59-
$("li.story").on("toggleStory", function(e) {
60-
e.preventDefault();
61-
var $this = $(this);
62-
63-
var openStories = $("li.story.open");
64-
65-
if($this.hasClass("open")) {
66-
$this.trigger("closeStory");
67-
} else {
68-
$this.trigger("openStory");
69-
}
70-
71-
openStories.trigger("closeStory");
72-
73-
window.scrollTo(0, $this.offset().top);
74-
});
75-
76-
$("#mark-all").click(function(e) {
77-
e.preventDefault();
78-
79-
$("form#mark-all-as-read").submit();
80-
});
1+
_.templateSettings = {
2+
interpolate: /\{\{\=(.+?)\}\}/g,
3+
evaluate: /\{\{(.+?)\}\}/g
4+
};
815

6+
$(document).ready(function() {
827
$(".remove-feed").click(function(e) {
838
e.preventDefault();
849
var $this = $(this);
@@ -99,64 +24,4 @@ $(document).ready(function() {
9924
Mousetrap.bind("?", function() {
10025
$("#shortcuts").modal('toggle');
10126
});
102-
103-
var cursorPosition = -1;
104-
var MAX_POSITION = $("li.story").size();
105-
106-
var Stringer = {
107-
setCursorPosition: function(position) {
108-
if (position < 0) position = 0;
109-
110-
var stories = $("li.story");
111-
stories.removeClass("cursor");
112-
stories.eq(position).addClass("cursor");
113-
},
114-
115-
currentlySelectedStory: function() {
116-
position = cursorPosition;
117-
if (position < 0) position = 0;
118-
119-
return $("li.story").eq(position);
120-
}
121-
};
122-
123-
Mousetrap.bind("j", function() {
124-
if (cursorPosition < MAX_POSITION - 1) {
125-
Stringer.setCursorPosition(++cursorPosition)
126-
Stringer.currentlySelectedStory().trigger("toggleStory");
127-
}
128-
});
129-
130-
Mousetrap.bind("k", function() {
131-
if (cursorPosition > 0) {
132-
Stringer.setCursorPosition(--cursorPosition);
133-
Stringer.currentlySelectedStory().trigger("toggleStory");
134-
} else {
135-
cursorPosition = 0;
136-
137-
Stringer.setCursorPosition(cursorPosition);
138-
Stringer.currentlySelectedStory().trigger("openStory");
139-
}
140-
});
141-
142-
Mousetrap.bind(["o", "enter"], function() {
143-
Stringer.currentlySelectedStory().trigger("toggleStory");
144-
Stringer.setCursorPosition(cursorPosition);
145-
});
146-
147-
Mousetrap.bind("r", function() {
148-
var refresh = $("a#refresh")[0];
149-
if (refresh) refresh.click();
150-
});
151-
152-
Mousetrap.bind("shift+a", function() {
153-
$("form#mark-all-as-read").submit();
154-
});
155-
156-
Mousetrap.bind(["b","v"], function() {
157-
var currentStory = Stringer.currentlySelectedStory();
158-
var permalink = currentStory.find("a.story-permalink")[0];
159-
160-
if (permalink) window.open(permalink.href, '_blank');
161-
});
162-
});
27+
});

‎app/public/js/backbone-min.js‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎app/public/js/underscore-min.js‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎app/views/archive.erb‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
</div>
44

55
<% unless @read_stories.empty? %>
6-
<div id="stories">
7-
<ul id="story-list">
8-
<% @read_stories.each do |story| %>
9-
<%= render_partial :story, { story: story } %>
10-
<% end %>
11-
</ul>
12-
</div>
6+
<%= render_js :stories, { stories: @read_stories } %>
7+
8+
<div id="stories">
9+
<ul id="story-list">
10+
</ul>
11+
</div>
1312

14-
<div id="pagination">
13+
<div id="pagination">
1514
<% if @read_stories.previous_page %>
1615
<a href="?page=<%= @read_stories.previous_page %>"><%= t('archive.previous') %></a>
1716
<% end %>
@@ -23,7 +22,7 @@
2322
<% if @read_stories.next_page %>
2423
<a href="?page=<%= @read_stories.next_page %>"><%= t('archive.next') %></a>
2524
<% end %>
26-
</div>
25+
</div>
2726
<% else %>
2827
<div id="sorry">
2928
<p><%= t('archive.sorry') %></p>

0 commit comments

Comments
 (0)