-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclauses.js
More file actions
474 lines (434 loc) · 12.3 KB
/
clauses.js
File metadata and controls
474 lines (434 loc) · 12.3 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
var state = {
'verses': [],
'hidden': [],
'selected': null
};
function hide_verse(verse) {
state.hidden.push(verse);
state.hidden = state.hidden.sort(function(a,b) {return a-b;});
}
function unhide_range(from, to) {
state.hidden = state.hidden.filter(function(x) {
return x < from || x > to;
});
}
// Toggle a special annotation for a word on a location
function toggle_word_special(loc, special) {
var words = state.verses[loc.verse].clauses[loc.clause].words[loc.word].specials;
var i = words.indexOf(special);
if (i == -1) {
state.verses[loc.verse].clauses[loc.clause].words[loc.word].specials.push(special);
} else {
state.verses[loc.verse].clauses[loc.clause].words[loc.word].specials.splice(i, 1);
}
}
// Toggle a special annotation for a clause on a location
function toggle_clause_special(loc, special) {
var specials = state.verses[loc.verse].clauses[loc.clause].specials;
var i = specials.indexOf(special);
if (i == -1) {
state.verses[loc.verse].clauses[loc.clause].specials.push(special);
} else {
state.verses[loc.verse].clauses[loc.clause].specials.splice(i, 1);
}
}
// Insert a clause break on the specified location. loc should have the keys
// verse, clause and word.
function insert_clause_break(loc) {
if (loc.word != 0) {
var org_clause = state.verses[loc.verse].clauses[loc.clause];
state.verses[loc.verse].clauses.splice(loc.clause + 1, 0, {
'indent': org_clause.indent,
'words': org_clause.words.slice(loc.word),
'specials': []
});
state.verses[loc.verse].clauses[loc.clause].words
= org_clause.words.slice(0, loc.word);
}
}
// Remove a clause break before the specified location (merge the specified
// clause with the preceeding clause). Fails if loc.clause == 0, since merging
// verses is not (yet?) supported.
function remove_clause_break(loc) {
if (loc.clause == 0) {
window.alert('Cannot merge verses');
return;
}
state.verses[loc.verse].clauses[loc.clause - 1].words =
state.verses[loc.verse].clauses[loc.clause - 1].words.concat(
state.verses[loc.verse].clauses[loc.clause].words);
state.verses[loc.verse].clauses.splice(loc.clause, 1);
}
// Increase the indent of the clause specified by loc; give a negative
// add_indent to decrease indent.
function increase_clause_indent(loc, add_indent) {
state.verses[loc.verse].clauses[loc.clause].indent += add_indent;
}
// Toggle the deletion state of a word
function toggle_word_deletion(loc) {
state.verses[loc.verse].clauses[loc.clause].words[loc.word].deleted =
!state.verses[loc.verse].clauses[loc.clause].words[loc.word].deleted;
}
// Gets text for a reference; the reference is an object with keys start_book,
// start_chapter, start_verse and their corresponding end_ alternatives.
function getText(reference, callback) {
$.ajax('getText.php', {
'type': 'GET',
'data': reference,
'dataType': 'json',
'success': function(data, textStatus, jqXHR) {
callback(data);
}
});
}
// Make a Hebrew word searchable: remove accents and vowels
function makeSearchable(word) {
return word
.replace(/\ufb2c/g, '\ufb2a') // shin
.replace(/\ufb2d/g, '\ufb2b') // sin
.replace(/[^\u05be\u05c1\u05c2\u05d0-\u05ea\ufb2a\ufb2b]+/g, '') // only keep letters
.replace(/^\u05d5?\u05d4?/, ''); // initial waw and/or he
}
// From a clauses.php verse object (with keys verse and text), make a
// clauses.js verse object (with keys ref and an array clauses)
// Initially this is just one clause
// A clause has keys indent and words
function make_verse(verse) {
var make_word = function(word) {
return {
'text': word,
'unvocalised': makeSearchable(word),
'deleted': false,
'specials': []
};
}
return {
'ref': verse.verse,
'clauses': [{
'indent': 0,
'words': verse.text.split(' ').map(make_word),
'specials': []
}]
};
}
// Check if a word should be highlighted
function highlight(word) {
return state.selected != null &&
word == state
.verses[state.selected.verse]
.clauses[state.selected.clause]
.words[state.selected.word]
.unvocalised;
}
// From a clause object, make a DOM element to represent it
function make_editable_clause(clause) {
var div = $('<div></div>');
div.addClass('clause');
for (var i in clause.specials) {
div.addClass('special-' + clause.specials[i]);
}
div.css('margin-right', (clause.indent * 1.5) + 'em');
for (var i in clause.words) {
var word = $('<div></div>')
.addClass('word')
.data('wordid', i)
.attr('onclick', 'select_word(this)')
.text(clause.words[i].text);
if (highlight(clause.words[i].unvocalised)) {
word.addClass('highlighted');
}
if ('translation' in clause.words[i]) {
var translation = $('<span></span>')
.addClass('translation')
.text(clause.words[i].translation);
word.append(translation);
}
if ('note' in clause.words[i]) {
var note = $('<span></span>')
.addClass('note')
.text(clause.words[i].note);
word.append(note);
}
if (clause.words[i].deleted) {
word.addClass('deleted');
}
for (var j in clause.words[i].specials) {
word.addClass('special-' + clause.words[i].specials[j]);
}
div.append(word);
}
return div;
}
// From a verse object, make a DOM element to represent it
function make_verse_elem(i) {
var tohide = i;
var verse = state.verses[i];
var div = $('<div></div>');
div.addClass('verse');
div.append(
$('<span class="ref"></span>')
.text(verse.ref)
.attr('title', 'Hide')
.click(function(){
hide_verse(tohide);
update_document();
}));
for (var i in verse.clauses) {
clause = verse.clauses[i];
div.append(make_editable_clause(clause).data('clauseid', i));
}
return div;
}
// Make an expandable verse list for hidden verses
function make_expandable_elem(start, end) {
var div = $('<div></div>');
div.addClass('expandable-verse')
.text(start == end
? state.verses[start].ref
: (state.verses[start].ref + ' - ' + state.verses[end].ref))
.attr('title', 'Show')
.click(function(){
unhide_range(start, end);
update_document();
});
return div;
}
// Update all DOM elements under our control
function update_document() {
var elem = $('#text');
elem.html('');
var h = 0;
for (var i = 0; i < state.verses.length; i++) {
if (h >= state.hidden.length || i != state.hidden[h]) {
var div = make_verse_elem(i);
div.data('verseid', i);
elem.append(div);
} else {
var start = i;
do { i++; h++; } while (h < state.hidden.length && state.hidden[h] == i);
i--;
var end = i;
elem.append(make_expandable_elem(start, end));
}
}
if (state.selected) {
var loc = state.selected;
$($($(elem.find('.verse')[loc.verse])
.find('.clause')[loc.clause])
.find('.word')[loc.word])
.addClass('selected');
}
}
// Find the new location (verse, clause, word) after changes based on only the
// verse and wordcount of the old location
function find_location(loc) {
var clauses = state.verses[loc.verse].clauses;
var wordcount = 0;
for (ci in clauses) {
var words = clauses[ci].words;
for (wi in words) {
if (wordcount >= loc.wordcount) {
loc.clause = parseInt(ci);
loc.word = parseInt(wi);
return loc;
}
wordcount++;
}
}
loc.clause = parseInt(ci);
loc.word = parseInt(wi);
loc.wordcount = wordcount - 1;
return loc;
}
function select_word(elem) {
var verse = parseInt($(elem).parent().parent().data('verseid'));
var clause = parseInt($(elem).parent().data('clauseid'));
var word = parseInt($(elem).data('wordid'));
var get_word_count = function(v, c, w) {
var clauses = state.verses[v].clauses;
var wordcount = 0;
for (ci in clauses) {
if (ci == c) {
return wordcount + w;
}
wordcount += clauses[ci].words.length;
}
}
state.selected = {
'verse': verse,
'clause': clause,
'word': word,
'wordcount': get_word_count(verse, clause, word)
};
update_document();
}
$('#get_text_form').submit(function(){
reference = {
'book': $('#book').val(),
'start_chapter': $('#start_chapter').val(),
'start_verse': $('#start_verse').val(),
'end_chapter': $('#end_chapter').val(),
'end_verse': $('#end_verse').val()
};
getText(reference, function(verses) {
state.verses = verses.map(make_verse);
update_document();
});
return false;
});
$('#make_pdf').submit(function(){
document.forms.make_pdf.elements.verses.value = JSON.stringify(state.verses);
});
$('#make_zip').submit(function(){
document.forms.make_zip.elements.verses.value = JSON.stringify(state.verses);
});
$('body').keydown(function(event){
if (typeof event.target.form != 'undefined' ||
event.altKey || event.ctrlKey || event.shiftKey) {
return true;
}
console.log(event);
switch (event.keyCode) {
case 27: // Escape
state.selected = null;
break;
case 13: // Return
if (state.selected) {
insert_clause_break(state.selected);
}
break;
case 8: // Backspace
if (state.selected) {
loc = state.selected;
if (state.verses[loc.verse].clauses[loc.clause].indent > 0) {
increase_clause_indent(state.selected, -1);
} else {
remove_clause_break(state.selected);
}
}
break;
case 9: // Tab
if (state.selected) {
increase_clause_indent(state.selected, 1);
}
break;
case 46: // Delete
if (state.selected) {
toggle_word_deletion(state.selected);
}
break;
case 39: // Right
if (state.selected) {
if (state.selected.wordcount > 0)
state.selected.wordcount--;
}
break;
case 37: // Left
if (state.selected) {
state.selected.wordcount++;
}
break;
case 38: // Up
if (state.selected) {
if (state.selected.clause > 0) {
state.selected.wordcount -= Math.max(
state.verses[state.selected.verse]
.clauses[state.selected.clause - 1].words.length,
state.selected.word + 1);
} else if (state.selected.verse > 0) {
state.selected.verse--;
state.selected.wordcount =
state.verses[state.selected.verse].clauses.reduce(
function(a,b){return a+b.words.length;}, 0) -
state.verses[state.selected.verse].clauses
[state.verses[state.selected.verse].clauses.length - 1]
.words.length +
state.selected.word;
}
}
break;
case 40: // Down
if (state.selected) {
if (state.selected.clause + 1 <
state.verses[state.selected.verse].clauses.length) {
state.selected.wordcount += state.verses[state.selected.verse]
.clauses[state.selected.clause].words.length;
} else if (state.selected.verse + 1 < state.verses.length) {
state.selected.verse++;
state.selected.wordcount = state.selected.word;
}
}
break;
case 68: // D (diacritical sign)
if (state.selected) {
toggle_clause_special(state.selected, 'diacritical');
}
break;
case 78: // N (Note)
if (state.selected) {
var word = state.verses[state.selected.verse]
.clauses[state.selected.clause]
.words[state.selected.word];
var note = 'note' in word ? word['note'] : '';
note = prompt("Enter note for " + word['text'] + ": ", note);
if (!note)
delete word['note'];
else
word['note'] = note;
}
break;
case 80: // P (predicate)
if (state.selected) {
toggle_word_special(state.selected, 'predicate');
}
break;
case 83: // S (subject)
if (state.selected) {
toggle_word_special(state.selected, 'subject');
}
break;
case 84: // T (translation)
if (state.selected) {
var word = state.verses[state.selected.verse]
.clauses[state.selected.clause]
.words[state.selected.word];
var trans = 'translation' in word ? word['translation'] : '';
trans = prompt("Enter translation for " + word['text'] + ": ", trans);
if (!trans)
delete word['translation'];
else
word['translation'] = trans;
}
break;
default:
return true;
}
if (state.selected) {
state.selected = find_location(state.selected);
}
update_document();
return false;
});
$('#TeXModal').on('shown.bs.modal', function(){
$.ajax('makeTeX.php', {
'type': 'POST',
'data': {
'verses': JSON.stringify(state.verses)
},
'success': function(data, textStatus, jqXHR) {
$('#TeXTextarea').val(data).focus().select();
}
});
});
$('#saveModal').on('shown.bs.modal', function(){
$('#saveTextarea').val(JSON.stringify(state.verses)).focus().select();
});
$('#restore').click(function(){
state.verses = JSON.parse($('#saveTextarea').val());
state.selected = null;
update_document();
$('#saveModal').modal('hide');
});
$('#show_help').click(function(){
$('#help').slideToggle();
});