forked from Mte90/GlotDict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglotdict.js
71 lines (67 loc) · 2.9 KB
/
glotdict.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
jQuery(document).ready(function () {
select_language();
gd_add_terms();
function select_language() {
jQuery('.filters-toolbar div:first').append('<span class="separator">•</span><span>Pick the glossary: </span><select class="glotdict_language"></select>');
jQuery.each(['it_IT', 'fr_FR', 'nl_NL'], function(key, value) {
var new_option = jQuery('<option></option>').attr('value',value).text(value);
if(localStorage.getItem('gd_language') === value) {
new_option.attr('selected',true);
}
jQuery('.glotdict_language').append(new_option);
});
if(jQuery('.glotdict_language option:selected').length === 0 ) {
localStorage.setItem('gd_language', jQuery('.glotdict_language option:first-child').text());
}
jQuery('.glotdict_language').change(function() {
localStorage.setItem('gd_language', jQuery('.glotdict_language option:selected').text());
location.reload();
});
}
function gd_add_terms() {
jQuery.ajax({
url: glotdict_path + '/' + get_lang() + '.json',
dataType: 'text'
}).done(function (data) {
data = JSON.parse(data);
jQuery('.editor .original').each(function () {
var loop_editor = this;
jQuery.each(data, function (i, item) {
if(i !== '&') {
add_term(i, loop_editor, item.translation, item.pos, item.comment);
}
});
});
jQuery('.editor .original .glossary-word-glotdict').css({'cursor': 'help','border-bottom': '1px dashed'});
jQuery('.editor').tooltip({
items: '.editor .original .glossary-word-glotdict',
content: function () {
var content = jQuery('<ul>');
jQuery.each(jQuery(this).data('translations'), function (i, e) {
var def = jQuery('<li>');
def.append(jQuery('<span>', {text: e.pos}).addClass('pos'));
def.append(jQuery('<span>', {text: e.translation}).addClass('translation'));
def.append(jQuery('<span>', {text: e.comment}).addClass('comment'));
content.append(def);
});
return content;
}
});
}).fail(function (xhr, ajaxOptions, thrownError) {
console.error(thrownError);
console.log('GlotDict: error on loading ' + get_lang() + '.json');
});
}
function add_term(word, element, translation, pos, comment) {
var rgxp = new RegExp('(?!([^<]+)?>)\\b(' + word + ')\\b(?!([^>]+)?>)', 'gi');
var repl = '<span class="glossary-word-glotdict" data-translations=\'[{"translation":"' + translation + '","pos":"' + pos + '","comment":"' + comment + '"}]\'>$2</span>';
jQuery(element).html(jQuery(element).html().replace(rgxp, repl));
}
function get_lang() {
var lang = localStorage.getItem('gd_language');
if(lang === '' || lang === 'null') {
lang = jQuery('.glotdict_language option:first-child').text();
}
return lang;
}
});