-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance_ui.js
134 lines (97 loc) · 4.63 KB
/
performance_ui.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
/*----------------------------------------------------------------------------------------------------------------------------------*/
function performance_ui_new(performance) {
var table = document.createElement("table");
table.tr = document.createElement("tr");
table.appendChild(table.tr);
table.border = "1";
var div = document.createElement("div");
div.id = makeid(999);
var a = create_link_to_change_name(div.id, performance);
performance.poet.notify_name.add_notify(update_link_on_name_change, "link_change_name_" + div.id);
div.appendChild(a);
insert_into_table(table, div);
table.tr = document.createElement("tr");
table.appendChild(table.tr);
insert_into_table(table, judges_ui_new(performance));
insert_into_table(table, time_ui_new(performance));
insert_into_table(table, penalty_ui_new(performance));
insert_into_table(table, score_ui_new(performance));
insert_into_table(table, subscore_ui_new(performance));
insert_into_table(table, cum_score_ui_new(performance));
insert_into_table(table, rank_ui_new(performance));
table.tr = document.createElement("tr");
table.appendChild(table.tr);
insert_into_table(table, document.createTextNode("Enter scores multiplied by 10."));
var p = document.createElement("p");
insert_into_table(table, p);
// Another field to show time penalty.
var data = {p: p, performance: performance};
performance.add_notify_score(time_penalty_update, data);
time_penalty_update(data);
return table;
}
/*----------------------------------------------------------------------------------------------------------------------------------*/
update_link_on_name_change = function(id) {
var a = document.getElementById(id);
if (a != null)
a.innerHTML = a.performance.poet.name;
}
/*----------------------------------------------------------------------------------------------------------------------------------*/
function create_link_to_change_name(id, performance) {
var a = document.createElement("a");
a.id = "link_change_name_" + id;
a.innerHTML = performance.name;
a.setAttribute("href", "javascript:create_input_to_change_name('" + id + "'" + ", '" + performance.name + "')");
a.performance = performance;
return a;
}
/*----------------------------------------------------------------------------------------------------------------------------------*/
// Sometimes an input doesn't lose focus even though it should.
// So if a new input is created, we'll remove the other ones.
function remove_inputs_that_should_have_lost_focus() {
var inputs = document.getElementsByClassName("input_change_name");
for (var i=0; i< inputs.length; i++)
change_input_to_link(inputs[i].parent_id);
}
/*----------------------------------------------------------------------------------------------------------------------------------*/
function create_input_to_change_name(id, name) {
remove_inputs_that_should_have_lost_focus();
var performance = document.getElementById("link_change_name_" + id).performance;
$("#link_change_name_" + id).remove();
var input = document.createElement("input");
input.id = "input_change_name_" + id;
input.parent_id = id;
input.setAttribute("class", "input_change_name");
input.value = performance.name;
input.performance = performance;
input.setAttribute("onchange", "change_input_to_link('" + id + "')");
input.setAttribute("onblur", "change_input_to_link('" + id + "')");
input.setAttribute("onfocusout", "change_input_to_link('" + id + "')");
$("#" + id).append(input);
}
/*----------------------------------------------------------------------------------------------------------------------------------*/
// From: http://stackoverflow.com/a/3261380
function isBlank(str) {
return (!str || /^\s*$/.test(str));
}
/*----------------------------------------------------------------------------------------------------------------------------------*/
function change_input_to_link(id) {
var input = document.getElementById("input_change_name_" + id);
var name = input.value;
var performance = input.performance;
$("#input_change_name_" + id).remove();
if (!isBlank(name))
performance.poet.set_name(name);
var a = create_link_to_change_name(id, performance);
$("#" + id).append(a);
}
/*----------------------------------------------------------------------------------------------------------------------------------*/
// From: http://stackoverflow.com/a/1349426
function makeid(length)
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < length; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}