-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtoggle_plural_field.js
More file actions
97 lines (85 loc) · 2.78 KB
/
toggle_plural_field.js
File metadata and controls
97 lines (85 loc) · 2.78 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
if (!$) {
$ = django.jQuery;
}
//Toggle the plural field depending on the chosen word type
$(document).ready(() => {
$("#id_word_type").change((event) =>
$("#id_plural")
.closest("div.row")
.toggle($(event.target).val() == "Nomen"),
);
$("#id_word_type").trigger("change");
});
//Toggle the drop down for grammatical gender depending on the chosen word type
$(document).ready(() => {
$("#id_word_type").change((event) =>
$("#id_grammatical_gender")
.closest("div.row")
.toggle($(event.target).val() == "Nomen"),
);
$("#id_word_type").trigger("change");
});
function removeDuplicationCheckMessage(){
var existingMessage = document.getElementById("result");
if (existingMessage) {
existingMessage.remove();
}
}
function showDuplicates(data, parent) {
removeDuplicationCheckMessage();
result = document.createElement("div");
result.setAttribute("id", "result");
if (data["word"]) {
// If there is a duplicated word, use orange background
result.style.backgroundColor = "#ffbb4a";
result.style.padding = "25px"
// Show alert message
messageBox = document.createElement("div");
message = document.createTextNode(data["message"]);
messageBox.append(message);
result.append(messageBox);
// Show the duplicated word with its word type
wordBox = document.createElement("div");
word = document.createTextNode(data["word"]);
wordBox.style.fontWeight = "bold";
wordBox.append(word);
result.append(wordBox);
// Show its definition too for more detail
definitionBox = document.createElement("div");
definition = document.createTextNode(data["definition"]);
definitionBox.append(definition);
result.append(definitionBox);
// Show related training sets
trainingSetsBox = document.createElement("div");
trainingSets = document.createTextNode(data["training_sets"]);
trainingSetsBox.append(trainingSets);
result.append(trainingSetsBox);
} else {
// If there is no duplicate, use green background
result.style.backgroundColor = "#72f399";
result.style.padding = "25px"
// Show message that no duplicate was found
messageBox = document.createElement("div");
message = document.createTextNode(data["message"]);
messageBox.append(message);
result.append(messageBox);
}
parent.prepend(result);
}
$(document).ready(() => {
$("#id_word").change((event) => {
if ($(event.target).val().length > 0) {
$.ajax({
type: 'GET',
url: '/api/search_duplicate/' + $(event.target).val(),
dataType: "json",
success: function(data) {
showDuplicates(data, $(event.target).closest(".card-body"));
}
})
} else {
removeDuplicationCheckMessage();
}
});
$("#id_word_type").trigger("change");
});