-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
74 lines (61 loc) · 1.6 KB
/
app.js
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
$(function() {
var Menu = function(element) {
this.items = $(element).find("li");
this.index = 0;
this.size = this.items.length;
}
Menu.prototype = {
'select': function() {
var link = this.items.eq(this.index).find("a");
selectStory.apply(link);
},
'previous': function() {
this.highlight( (this.index == 0 ? this.size : this.index) - 1 );
},
'next': function() {
this.highlight( (this.index == this.size - 1 ? -1 : this.index) + 1 );
},
'highlight': function(index) {
this.items.eq(this.index).removeClass("highlight");
this.items.eq(this.index = index).addClass("highlight");
}
}
var Controller = function(menu) {
this.menu = menu;
}
Controller.prototype = {
'up': function() {
this.menu.previous();
},
'down': function() {
this.menu.next();
},
'select' : function() {
this.menu.select();
}
}
$.getJSON('/stories/top', function(stories) {
buildUI();
$.each(stories, addStory);
window.controller = new Controller(new Menu(".menu"));
});
function buildUI() {
$('<ol class="menu"></ol><h1> </h1><p> </p>').appendTo("body");
}
function addStory() {
$("<a />").appendTo($("<li />").appendTo(".menu"))
.attr('href', this.url)
.html(this.title)
}
function selectStory() {
var storyLink = $(this);
storyLink.addClass('selected');
var url = storyLink.attr('href');
$.getJSON(url, showStory);
return false;
}
function showStory(story) {
$("h1").html(story.title);
$("p").html(story.body);
}
});