-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcode2.js
359 lines (296 loc) · 8.89 KB
/
code2.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* Adds or deletes values for each variable based on checkboxes. */
var selectCheckbox = function(e) {
var el = $(e.target);
var aid = $(".article").attr("id").split("_")[1];
var sid = el.attr("id").split("_");
var val = $("label[for='" + el.attr('id') + "']").text();
var eid = $(e.target).closest(".event-block").attr("id").split("_")[1];
var is_checked = el.is(':checked');
var action = ''
// add this checkbox item
if (is_checked == true) {
action = 'add';
} else { // delete it
action = 'del';
}
req = $.ajax({
type: "POST",
url: $SCRIPT_ROOT + '/_' + action + '_code/2',
data: {
article: aid,
variable: sid[1],
value: val,
event: eid
}
});
req.success(function(e) {
$('#flash-error').hide();
});
req.fail(function(e) {
$("#flash-error").text("Error changing checkbox.");
$("#flash-error").show();
});
}
/* Add or edit an event. Controls the event list. */
var modifyEvent = function(e) {
var aid = $(".article").attr("id").split("_")[1];
var eid = e.target.parentElement.id.split("_")[1];
req = $.ajax({
type: "GET",
url: $SCRIPT_ROOT + '/_load_event_block',
data: {
'article_id': aid,
'event_id': eid
}
});
req.success(function(ev) {
$("#flash-error").hide();
// add the event block to the HTML
$("#event-blocks").append(req.responseText);
// add listeners for options
$(".event-block :radio").each(function() {
$(this).change(function(e) {
var variable = $(e.target).attr("id").split("_")[1];
// hide everything
$(".options").each( function() { $(this).hide() });
// except options for currently selected variable
$("#options_" + variable).show();
});
// set eid here for adding new events
eid = $(".event-block").attr("id").split("_")[1];
});
// listeners for adding or deleting checkboxes
$(':checkbox').change(selectCheckbox);
// listener to add code
$('.event-block label a').click(addCode);
// listener for submit and save
$("#submit").click(function(e) {
var event_id = $(e.target).parent().attr("id").split("_")[1];
// save comments
var comments = $('#eventblock_' + event_id + ' #comments').val();
req = $.ajax({
type: "POST",
url: $SCRIPT_ROOT + '/_add_code/2',
data: {
article: aid,
variable: "comments",
value: comments,
event: event_id
}
});
req.success(function(e) {
$('#flash-error').hide();
});
req.fail(function(e) {
$("#flash-error").text("Error adding comments.");
$("#flash-error").show();
});
// remove all the event blocks
$(".event-block").each(function() {
$(this).remove();
});
getEvents(aid);
});
});
req.fail(function(e) {
$("#flash-error").text("Error loading event block.");
$("#flash-error").show();
});
}
/* Deletes an event on clicking the remove button. */
var deleteEvent = function(e) {
var aid = $(".article").attr("id").split("_")[1];
var eid = e.target.id.split("_")[1];
var r = confirm("Are you sure you want to delete this item?");
if (r == false) {
return
}
req = $.ajax({
type: "POST",
url: $SCRIPT_ROOT + '/_del_event',
data: {
event: eid
}
});
// on success, remove HTML element
// and the edit block if it exists
req.success(function() {
$('#flash-error').hide();
$(e.target).parent().remove();
$("#eventblock_" + eid).remove();
});
req.fail(function() {
$("#flash-error").text("Error deleting item.");
$("#flash-error").show();
});
}
/* Loads the list of events upload page load and adding a new event. */
var getEvents = function(aid) {
var req = $.ajax({
type: "GET",
url: $SCRIPT_ROOT + '/_get_events',
data: {
article_id: aid
}
});
// on success -- add items to list
req.success(function(ev) {
events = ev['events'];
// empty list
$('#list-events').empty();
// add existing events
for(i = 0; i < events.length; i++) {
var event_id = events[i]['id'];
var event_repr = events[i]['repr'];
var date = new Date();
// create a datetime hashes
var edit_hash = ["edit", event_id, date.getTime()].join("_");
var del_hash = ["del", event_id, date.getTime()].join("_");
// create elements
var event = "<p id='listevent_" + event_id + "'>" + event_id + ": " + event_repr + " <a id='" + edit_hash + "' class='glyphicon glyphicon-pencil'></a> <a id='" + del_hash + "' class='glyphicon glyphicon-remove'></a></p>";
// add new events
$('#list-events').append(event);
// create a listener for the edit and delete buttons
$('#' + edit_hash).click(modifyEvent);
$('#' + del_hash).click(deleteEvent);
}
});
req.fail(function(e) {
$("#flash-error").text("Error loading stored events.");
$("#flash-error").show();
});
}
/* Adds a value for a variable if not available in the current list. */
var addCode = function(e) {
var oText = '';
var aid = $(".article").attr("id").split("_")[1];
var eid = $(e.target).closest(".event-block").attr("id").split("_")[1];
var variable = e.target.parentElement.id.split("_")[2];
// the various methods of getting the text object
if (window.getSelection) {
oText = window.getSelection();
} else if (document.selection) {
oText = document.selection.createRange().text;
} else if (document.getSelection) {
oText = document.getSelection();
}
text = oText.toString().trim();
// skip when there is nothing selected
if (text.length <= 0) {
return;
}
req = $.ajax({
type: "POST",
url: $SCRIPT_ROOT + '/_add_code/2',
data: {
article: aid,
variable: variable,
value: text,
event: eid
}
});
// add element or change tab color on success
req.success(function() {
$("#flash-error").hide();
// create element
var li = '<input type="checkbox" id="dd_' + variable + '_' + text + '" checked /> <label for="dd_' + variable + '_' + text + '">' + text + '</label><br />';
// add to the list
$('#options_' + variable).append(li);
// create a listener for the checkbox
$('#dd_' + variable + '_' + text).click(selectCheckbox);
});
// on failure
req.fail(function(e) {
$("#flash-error").text = "Error adding item.";
$("#flash-error").show();
});
}
$(function(){ // document ready
var aid = $(".article").attr("id").split("_")[1];
getEvents(aid);
// add event listener
$('#add-event').click(modifyEvent);
// mark done
$('.mark-done').each(function() {
$(this).click(function() {
var req = $.ajax({
type: "POST",
url: $SCRIPT_ROOT + '/_mark_sp_done',
data: {
article_id: aid
}
});
req.success(function(ev) {
$("#flash-error").hide();
// go to next
window.location.assign($SCRIPT_ROOT + '/code2');
});
req.fail(function(e) {
$("#flash-error").text("Error going to next article.");
$("#flash-error").show();
});
});
});
// Add listener for ignore button
$('a#ignore').click(function(e) {
var r = confirm("Are you sure you want to ignore this article?");
if (r == false) {
return
}
req = $.ajax({
type: "POST",
url: $SCRIPT_ROOT + '/_add_code/2',
data: {
article: aid,
variable: "ignore",
value: 0
}
});
req.success(function(e) {
$('#flash-error').hide();
var req = $.ajax({
type: "POST",
url: $SCRIPT_ROOT + '/_mark_sp_done',
data: {
article_id: aid
}
});
req.success(function(ev) {
// go to next
window.location.assign($SCRIPT_ROOT + '/code2');
});
req.fail(function(e) {
$("#flash-error").text("Error ignoring article and moving to next.");
$("#flash-error").show();
});
});
req.fail(function(e) {
$("#flash-error").text("Error ignoring article.");
$("#flash-error").show();
});
});
// on radio button change, highlight text
$('#secondpassinfo :radio').change(function() {
// get coder and variable
var variable = $("input[name=var]:checked").val()
req = $.ajax({
type: "GET",
url: $SCRIPT_ROOT + '/_highlight_var',
data: {
article: aid,
variable: variable
}
});
req.fail(function(e) {
$("#flash-error").text("Error selecting variable.");
$("#flash-error").show();
return;
});
// on success, show text + highlighting
req.success(function(e) {
$('#flash-error').hide();
$('#meta').html(req['responseJSON']['result']['meta']);
$('#bodytext').html(req['responseJSON']['result']['body']);
});
});
});