/
selection.js
197 lines (160 loc) · 6.3 KB
/
selection.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*global $, Globals, Analytics */
/* Selection: Concerned with the following ideas:
* adding and removing "items" to a list
* updating the hash component of the URL for persistence
* successfully sending users to Results.
*
* Selection useds Globals.TYPE to decide whether to merge Course or ISBN into itself.
* It calls Selection.init() to set up Course/ISBN specifics.
*/
var Selection = (function() {
var addedItems = [],
displayedItems = [],
CURRENTLY_NO_ITEMS_HTML = "<div id='noItems'>Your " + Globals.ITEM_NAME + " list is currently empty.</div>",
publicObject;
function displayItemInformation(id, string) {
var thing = "#" + htmlID(id);
$(thing).html(string)
.removeClass('loading')
.append("<img src='" + Globals.BASE_URL + "/images/cancel.png' class='removeItem'/>");
}
/* Each item in #items has two identifiers.
* 'id' is an html-friendly attribute for DOM manipulation.
* 'data-id' is a pure ID used for display and existence in `items`
* This function generate 'id' from 'data-id'.
*/
function htmlID(id) {
return "item" + id.replace(/[^A-Za-z 0-9]+/g,'');
}
function displayErrorInList(id) {
displayItemInformation(id, "<h2>Invalid " + Globals.TYPE + ": " + id + "</h2>We're sorry. Please try again.");
}
function addItem(id) {
if (itemAlreadyAdded(id)) {
return showDuplicateError(htmlID(id));
}
var html = $("<div class='item loading' id='" + htmlID(id) + "' data-id='" + id + "'><img src='" + Globals.BASE_URL + "/images/ajax-loader.gif'></div>");
if (displayedItems.length === 0) {
$('#items').html(html); // Kills the "you have no <item>" text
} else {
$('#items').prepend(html); // Adds to the top of the list
}
displayedItems.push(id);
// We purposedly suppress the global error handler here.
$.cachedAjax({
suppressErrors: true,
url: removeAmpersandsFromString(publicObject.ADD_ITEM_URL() + id),
dataType: "json",
success: function(json) {
if (publicObject.isItemValid(id, json)) {
// Actually add the item and update state.
addedItems.push(id);
displayItemInformation(id, publicObject.stringifyItem(id, json));
updateWindowHashAndNavigationButton();
} else {
displayErrorInList(id);
}
},
error: function(json) {
displayErrorInList(id);
}
});
}
function removeItem() {
var item_div = $(this).parent(),
item_id = $(item_div).attr('data-id');
// Update internal state
displayedItems = _.filter(displayedItems, function(value) {
return value !== item_id;
});
addedItems = _.filter(addedItems, function(value) {
return value !== item_id;
});
// display
$(item_div).fadeOut(100, function() {
$(this).remove();
});
updateWindowHashAndNavigationButton();
if ($("#items").children().length <= 1) {
// TODO: Unify the display of noItems html with the original setup function (DRY)
$("#items").hide().html(CURRENTLY_NO_ITEMS_HTML).fadeIn();
}
}
function clearItems() {
displayedItems = [];
addedItems = [];
$("#items").hide().html(CURRENTLY_NO_ITEMS_HTML).fadeIn();
updateWindowHashAndNavigationButton();
}
function showDuplicateError(htmlID) {
$("#"+htmlID).tooltip({
side: "bottom",
html: "<h2>Sorry, you've already added this " + Globals.ITEM_NAME + ".</h2>",
width: 450,
type: 'standard'
});
return false;
}
function itemAlreadyAdded(identifier) {
return (_.indexOf(addedItems, identifier) !== -1);
}
// TODO: Instead of using JavaScript to redirect, on each state change, we can update a hyperlink on the "Go" button.
// That way, it can be opened up in a new tab.
function navigateToResults() {
if (addedItems && addedItems.length !== 0) {
// Analytics.trackEvent("Stats","Item Count",currentMode.name.capitalize(),Selection.addedItems.length);
window.location = "../results/" + removeAmpersandsFromString(Selection.buildHash(addedItems).substring(2));
}
}
// TODO: Try the hyperlink thing.
function updateWindowHashAndNavigationButton() {
window.location.replace("#" + Selection.buildHash(addedItems));
if (addedItems.length > 0 ) {
$("#go").fadeIn("fast").show(); // .show fixes an IE bug. Do not remove.
}
else {
$("#go").fadeOut("fast");
}
}
// Is this function necessary?
function isAutocompleteTooltipVisible() {
return $(publicObject.autocomplete.inputSelector).hasClass("hasTooltip");
}
function removeAmpersandsFromString(s) {
return (s.replace(/&/g, "&")).replace(/&/g, "&");
}
// Release a public object.
publicObject = {
addedItems: function () {
return addedItems;
},
displayedItems: displayedItems,
addItem: addItem,
removeItem: removeItem,
clearItems: clearItems,
itemAlreadyAdded: itemAlreadyAdded,
updateWindowHashAndNavigationButton: updateWindowHashAndNavigationButton,
isAutocompleteTooltipVisible: isAutocompleteTooltipVisible,
removeAmpersandsFromString: removeAmpersandsFromString
};
// Extend the publicObject with the right child.
if (Globals.TYPE === "ISBN") {
$.extend(publicObject, ISBN);
} else {
$.extend(publicObject, Course);
}
// Register private click handlers.
$(document).ready(function() {
$("#go").on('click', navigateToResults);
$("#items").on('click', '.removeItem', removeItem);
});
return publicObject;
})();
$(document).ready(function() {
Globals.firstRun = true;
var success = Selection.parseHash(window.location.hash.substring(1));
if (success === false) {
window.location.replace(window.location.href.split('#')[0]);
}
Selection.init();
});