-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpage.js
161 lines (121 loc) · 3.89 KB
/
page.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/** The entire page. */
/** Stores the state of the entire page. */
semanticify.Page = Backbone.Model.extend({
defaults: {
// Key indicates that a tag is selected, value is true by default
'selectedTags': {},
'selectedUrls': {}
},
initialize: function() {
this.urlCollection = new semanticify.UrlCollection();
_.each(data, _.bind(function(rawUrlObj) {
var url = new semanticify.Url({
'id': rawUrlObj.id,
'url': rawUrlObj.url,
'tags': rawUrlObj.tags
});
this.urlCollection.add(url);
}, this));
},
addSelectedTag: function(tagName) {
this.get('selectedTags')[tagName] = true;
this.updateSelectedUrls();
},
removeSelectedTag: function(tagName) {
delete this.get('selectedTags')[tagName];
this.updateSelectedUrls();
},
/** Update which urls are in the selected region. */
updateSelectedUrls: function() {
this.set('selectedUrls', {});
// Initial implementation: Loop over all the urls and cross reference.
_.each(this.urlCollection.models, _.bind(function(url) {
_.each(url.get('tags'), _.bind(function(tagName) {
if (tagName in this.get('selectedTags')) {
this.get('selectedUrls')[url.get('id')] = url;
}
}, this));
}, this));
this.trigger('urls-updated');
},
});
/** Updates the view of the page based on the model. */
semanticify.PageView = Backbone.View.extend({
el: '#container',
initialize: function() {
this.render();
this.model.on('urls-updated', _.bind(function() {
this.render();
}, this));
},
render: function() {
// Wipe anything drawn previously.
$('#selected-tag-container').empty();
$('#selected-urls', this.el).empty();
$('#unselected-urls', this.el).empty();
_.each(this.model.urlCollection.models, _.bind(function(url) {
this.appendUrl(url);
this.updateTags(url);
}, this));
_.each(_.keys(this.model.get('selectedTags')), _.bind(function(tagName) {
this.updateSelectedTagView(tagName);
}, this));
},
events: {
'keyup #search-input': 'handleSearchKeyUp',
'click .selected-tag': 'handleRemoveTag',
'click .original-tag-container-selected': 'handleRemoveTag',
'click .original-tag-container': 'updateSearch',
},
appendUrl: function(url) {
var urlElementHtml =
'<li><a href="' + url.get('url') + '">' + url.get('url') + '</a>' +
'<div id=' + url.get('id') + '></div>' + '</li>';
if (url.get('id') in this.model.get('selectedUrls')) {
$('#selected-urls', this.el).append(urlElementHtml);
} else {
$('#unselected-urls', this.el).append(urlElementHtml);
}
},
updateTags: function(url) {
_.each(url.get('tags'), _.bind(function(tag) {
var tagContainerClass = 'original-tag-container';
if (tag in this.model.get('selectedTags')) {
tagContainerClass = ' original-tag-container-selected';
}
$('#'+url.get('id')).append(
'<span class="' + tagContainerClass +'">'+tag+'</span>'
);
}, this));
},
handleSearchKeyUp: function(e) {
var ENTER_KEY_CODE = 13;
if (e.keyCode == ENTER_KEY_CODE) {
this.addSelectedTag();
}
},
addSelectedTag: function() {
var tagName = $('#search-input').val();
// Update the model.
this.model.addSelectedTag(tagName);
// Clear the input.
$('#search-input').val('');
},
/** Draw the newly selected tag to the view. */
updateSelectedTagView: function(tagName) {
$('#selected-tag-container').append(
'<div class="selected-tag">' + tagName + '</div>'
);
},
updateSearch: function(e) {
var tagName = $(e.target).html()
if (!(this.model.get('selectedTags')[tagName])) {
this.model.addSelectedTag(tagName);
}
},
handleRemoveTag: function(e) {
$(e.target).fadeOut(400);
var tagName = $(e.target).html()
this.model.removeSelectedTag(tagName);
},
});