-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.php
135 lines (125 loc) · 3.9 KB
/
app.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<script type="text/javascript">
App = Ember.Application.create({
LOG_TRANSITIONS: true
});
App.ApplicationView = Ember.View.extend({
didInsertElement: function() {
$("#loader").remove();
}
});
App.WP = Ember.Object.extend({
loadedPosts: false,
page: 1,
pages: 1,
lastPost: undefined,
loadPosts: function(page) {
var wp = this;
return Ember.Deferred.promise(function (p) {
if (wp.get('loadedPosts') && wp.get('page')==page) {
// We've already loaded the posts, let's return them!
p.resolve(wp.get('posts'));
} else {
// If we haven't loaded the posts, load them via JSON
var url = "<?php echo site_url(); ?>/?json=1&page="+page;
p.resolve($.getJSON(url).then(function(response) {
var posts = response.posts.map(function(post) {
post.category = post.categories[0].title;
post.body = post.content;
return post;
});
wp.setProperties({'posts': posts, 'loadedPosts': true, 'page': page, 'pages': response.pages});
return posts;
}).fail(function(jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
}));
}
});
},
findPostByName: function(post_name) {
var wp = this;
if (post_name === undefined) {
return wp.get('lastPost');
}
var curpage = wp.get('page');
if (curpage === undefined) { curpage = 1; }
return this.loadPosts(curpage).then(function (posts) {
var post = posts.findProperty('slug', post_name);
if (post === undefined) {
return Ember.Deferred.promise(function (p) {
p.resolve($.getJSON("<?php echo site_url(); ?>/" + post_name + "?json=1").then(function(response) {
post = response.post;
post.category = post.categories[0].title;
post.body = post.content;
wp.setProperties({'lastPost':post});
return post;
}));
});
} else {
wp.setProperties({'lastPost':post});
return post;
}
});
}
});
var WP = App.WP.create();
App.Router.map(function() {
this.resource('posts', { path: '/' }, function() {;
this.resource('post', { path: '/:year/:month/:post_name' });
});
this.route('page', { path: '/:page' });
});
App.PostsRoute = Ember.Route.extend({
model: function() {
var curpage = WP.get('page');
if (curpage === undefined) { curpage = 1; }
return WP.loadPosts(curpage).then(function(posts) {
return posts;
});
},
renderTemplate: function() {
this.render({outlet: 'sidebar'});
}
});
App.PageRoute = Ember.Route.extend({
model: function(params) {
return WP.loadPosts(params.page).then(function(posts) {
return posts;
});
},
renderTemplate: function() {
this.render({outlet: 'sidebar'});
this.render('post', {controller: this.controllerFor('post')});
}
});
App.PostRoute = Ember.Route.extend({
model: function(params) {
return WP.findPostByName(params.post_name);
},
serialize: function(model) {
return {year: model.date.substring(0,4), month: model.date.substring(5,7), post_name: model.slug};
}
});
Ember.Handlebars.helper('format-date', function(date) {
return moment(date, 'YYYY-MM-DD hh:mm:ss').format('MMMM D, YYYY');
});
Ember.Handlebars.helper('format-category', function(category) {
// Ugly hacky way to get different colors for category labels.
var color = "color-A";
if (category == "Self-Referential") { color = "color-B" }
else if (category == "Guides") { color = "color-C" }
return new Handlebars.SafeString("<span class='label " + color + "'>" + category + "</span>");
});
Ember.Handlebars.helper('page-links', function() {
var prev = Number(WP.page)-1;
var next = Number(WP.page)+1;
var output = "";
if (prev >= 1) {
output += "<a href='/#/" + prev + "' title='previous'>« previous</a>";
}
if (next <= WP.pages) {
output += " <a href='/#/" + next + "' title='next'>next »</a>";
}
return new Handlebars.SafeString(output);
});
</script>