-
Notifications
You must be signed in to change notification settings - Fork 19
/
addEdit.js
125 lines (104 loc) · 3.05 KB
/
addEdit.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
function getNumQuestions() {
var count = 0;
$('.key').each(function(element) {
if ($(this).val().length > 0) {
count++;
}
});
return count;
}
$('.saveButton').click(function() {
if (getNumQuestions() < 2) {
alert('You must provide at least two questions.');
return;
}
var categoryName = $('.categoryName').val();
if (categoryName.length === 0) {
$('.categoryName').focus();
alert('Please provide a category title');
return;
}
localStorage.setItem(categoryName, serializeQuestions());
var categoryList = localStorage.getItem('categories');
if (categoryList) {
categoryList = JSON.parse(categoryList);
}
else {
categoryList = [];
}
// See if this list already existed
var alreadyExists = categoryList.some(function(list) {
return list === categoryName;
});
// Only add it if it didn't exist
if (!alreadyExists) {
categoryList.push(categoryName);
localStorage.setItem('categories', JSON.stringify(categoryList));
}
document.location = 'index.html';
});
$('.deleteButton').click(function() {
var categoryName = $('.categoryName').val();
localStorage.removeItem(categoryName);
var categoryList = localStorage.getItem('categories');
if (categoryList) {
categoryList = JSON.parse(categoryList);
categoryList = categoryList.filter(function(theName) {
return theName !== categoryName;
});
localStorage.setItem('categories', JSON.stringify(categoryList));
}
document.location = 'addEditList.html';
});
function checkForNewRowNeeded() {
// is there an empty row still?
var lastQuestion = $('.question:last');
if (lastQuestion.find('.key').val().length > 0 || lastQuestion.find('.value').val().length > 0) {
// add one
var newRow = Handlebars.templates['question.html']([{
key: '',
value: ''
}
]);
$('.questions').append(newRow);
$('.question:last > .key, .question:last > .value').focus(checkForNewRowNeeded);
}
}
function serializeQuestions() {
var questionSet = [];
$('.question').each(function(index, question) {
var questionNode = $(question);
var key = questionNode.find('.key').val();
var value = questionNode.find('.value').val();
if (key && value && key.trim().length > 0 && value.trim().length > 0) {
questionSet.push({
key: key,
value: value
});
}
});
return JSON.stringify(questionSet);
}
function getListNameFromQueryString() {
return decodeURI(document.location.search).replace('?category=', '');
}
function categoryClickedCallback() {
document.location = 'addEditList.html?category=' + this.innerHTML;
}
var excludeBuiltinCategories = true;
common.renderCategories(excludeBuiltinCategories, categoryClickedCallback);
var listToEdit = getListNameFromQueryString();
if (listToEdit.length > 0) {
$('.deleteButton').show();
$('.categoryName').val(listToEdit);
var loadedList = localStorage.getItem(listToEdit);
if (loadedList) {
var loadedListAsObject = JSON.parse(loadedList);
var nodesToAdd = Handlebars.templates['question.html'](loadedListAsObject);
$('.header').after(nodesToAdd);
}
}
else {
$('.newLink').hide();
}
$('.key, .value').focus(checkForNewRowNeeded);