forked from lsuits/lsu-gradereport_gradebook_builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
297 lines (230 loc) · 9.35 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
$(document).ready(function() {
var category_tmpl = $('div#grade-category-tmpl').find('table'),
item_tmpl = $('div#grade-item-tmpl').find('tr'),
weight_tmpl = $('div#category-weight-tmpl').find('div:first'),
target_weight = 100;
function checkElemWeight(elem, weight) {
if (isNaN(weight)) {
elem.css('border', '1px solid red');
return false;
} else {
elem.removeAttr('style');
return true;
}
}
function get_selected(selector) {
return $(selector).children().filter(':selected').html();
}
function get_selected_value(selector) {
return $(selector).children().filter(':selected').val();
}
function add_category(name, weight) {
var category = category_tmpl.clone(),
weights = weight_tmpl.clone(),
id = new Date().getTime() + '';
category.attr('name', name).attr('id', id);
weights.attr('name', name).attr('data-categoryid', id);
if (name === '') {
return;
}
category.find('h3').replaceWith('<h3><span>' + name + '</span> <span class="label label-important remove-category-label">X</span></h3>');
weights.find('span:first').replaceWith('<span>' + name + '</span>');
$('div#grade-categories').append(category);
$('select#add-item-category')
.append('<option value="' + id + '">' + name + '</option>');
// $('div#howto').replaceWith('<div class="help"><a href=http://grok.lsu.edu/Article.aspx?articleid=16094 target="_blank" class="help">help</a></div>');
readjustWeight(function(tw) {
tw.append(weights);
if (weight) {
$('.control-group[data-categoryid="' + id + '"]')
.find('input').val(weight * 100);
}
});
}
function readjustWeight(callback) {
var tw = $('#category-weights fieldset'),
old_sep = Math.round(target_weight / tw.children().length);
// manipulate size
callback(tw);
var sep = Math.round(target_weight / tw.children().length);
tw.find('.input-tiny').each(function(i, elem) {
var val = $(elem).val();
console.log(parseFloat(val));
console.log(old_sep);
console.log(sep);
if (val === "0" || isNaN(val) || parseFloat(val) == old_sep) {
$(elem).val(sep);
}
});
}
// Add grade category
$('button#add-category').click(function(e) {
e.preventDefault();
var name = $('input#category-name').val();
add_category(name);
});
function add_item(category_name, name, points, itemtype) {
var category = $('table[name="' + category_name + '"]').find('tbody'),
to_add = $('input#grade-item-num-add').val(),
i = 0;
for (; i < to_add; i++) {
var item = item_tmpl.clone();
if (!name) {
var num = category.children().length + 1,
itemname = category_name + ' ' + num;
} else {
var itemname = name;
}
if (!itemtype) {
var itemtype = get_selected_value('select#grade-itemtype');
}
item.find('span:first').replaceWith('<span data-itemtype="' + itemtype + '">' + itemname + ' <span class="label label-important remove-item-label">X</span></span>');
if (points) {
item.find('input.input-tiny').val(points);
}
category.append(item);
}
}
// Add grade item
$('button#add-item').click(function(e) {
e.preventDefault();
var category_name = get_selected('select#add-item-category');
add_item(category_name, '');
});
// Save item point value
$('div.point-blank input').live('focusout', function(e) {
var elem = $(e.currentTarget);
var val = elem.val();
// TODO: some kind of numeric evaluation?
});
// Category remove button
$('span.remove-category-label').live('click', function(e) {
var elem = $(e.currentTarget),
table = elem.parent().parent().parent().parent().parent(),
id = table.attr('id'),
name = elem.parent().find('span:first').html();
table.remove();
readjustWeight(function() {
$('div.control-group[data-categoryid="' + id + '"]').remove();
});
$('#add-item-category').children('option[value=' + id + ']').remove();
});
// Template name
$("#template-toggle-input").on('click', function() {
var s = $(this);
s.html('<input value="' + s.text() + '"/>')
.children("input").focus()
.on('focusout', function() {
var name = $(this).val().trim() == '' ? 'New Template' : $(this).val();
s.html(name);
$('#builder').children("input[name=name]").val(name);
});
return false;
});
// Item remove button
$('span.remove-item-label').live('click', function(e) {
$(e.currentTarget).parent().parent().parent().remove();
});
/*
// Hover to show remove button for categories
$('tr').live({
mouseenter: function(e) {
$(e.currentTarget).children().find('span.remove-category-label').show();
},
mouseleave: function(e) {
$(e.currentTarget).children().find('span.remove-category-label').hide();
}
});
// Hover to show remove button for items
$('tr').live({
mouseenter: function(e) {
$(e.currentTarget).children().find('span.remove-item-label').show();
},
mouseleave: function(e) {
$(e.currentTarget).children().find('span.remove-item-label').hide();
}
});
*/
// Show or hide category weights
$('select#grading-method').change(function(e) {
var val = $('select#grading-method').val();
if (val === "10") {
$('form#category-weights').show();
$('div#builder-start.container').addClass('taller');
} else {
$('form#category-weights').hide();
$('div#builder-start.container').removeClass('taller');
}
});
// Convert form to JSON and Submit on save button click
$('form#builder').submit(function() {
var gb = {},
total_weight = 0,
tw = $('#category-weights fieldset'),
errors = [];
gb['name'] = $(this).children('input[name=name]').val();
gb['aggregation'] = $('select#grading-method').val();
gb['categories'] = [];
if (gb['aggregation'] === "10") {
tw.find('.input-tiny').each(function() {
total_weight += isNaN($(this).val()) ? 0 : parseFloat($(this).val());
});
$('#category-weights h3').siblings('.error').remove();
if (total_weight != 100) {
$('#category-weights h3')
.after('<div class="error">Does not total 100%</div>');
errors.push('total');
}
}
$('div#builder-start').find('table').each(function() {
var cat_obj = {},
parent = $(this),
id = parent.attr('id'),
input = $('.control-group[data-categoryid=' + id + ']').find('input'),
weight = input.val(),
items = [];
cat_obj['name'] = $(this).find('span:first').html();
if (!checkElemWeight(input, weight)) {
errors.push(cat_obj['name'] + ' bad weight');
return;
}
cat_obj['weight'] = gb['aggregation'] === "10" ?
parseFloat(weight) / 100.0 : 0;
parent.find('td').each(function() {
var gi_name = $(this).find('span:first').clone().children().remove().end().text().trim();
var gi_itemtype = $(this).find('span:first').clone().attr('data-itemtype');
var gi_points = $(this).find('input.input-tiny').val();
if (!checkElemWeight($(this).find('input.input-tiny'), gi_points)) {
errors.push(gi_name + ' bad number');
return;
}
// Gather itemtype and itemmodule
items.push({
'name': gi_name,
'grademax': gi_points,
'weight': gb['aggregation'] === "10" ? 1 : 0,
'itemtype': gi_itemtype == 'manual' ? 'manual' : 'mod',
'itemmodule': gi_itemtype == 'manual' ? '' : gi_itemtype,
});
});
cat_obj['items'] = items;
gb['categories'].push(cat_obj);
});
$('input[name="data"]').val(JSON.stringify(gb));
return errors.length == 0;
});
// Launch change for wight mean
$('select#grading-method').change();
var gb_json = $('input[name="data"]').val();
if (gb_json.length > 2) {
var gb_obj = JSON.parse(gb_json);
$('select#grading-method').val(gb_obj['aggregation']).change();
$.each(gb_obj['categories'], function() {
var cat_node = this;
add_category(cat_node.name, cat_node.weight);
$.each(cat_node['items'], function() {
add_item(cat_node.name, this.name, this.grademax, this.itemmodule);
});
});
}
});