0358f33da1aa20a1be23abf4703a9cbd8c164ce0
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License
15 # Shows a meetup and allows to modify its participants
18 import opportunity_model
23 # Shows a meetup and allows to modify its participants
24 class OpportunityMeetupPage
27 # Meetup the page is supposed to show
28 var meetup
: nullable Meetup = null
29 # Answer mode for the meetup
32 init from_id
(id
: String) do
33 var db
= new OpportunityDB.open
("opportunity")
34 meetup
= db
.find_meetup_by_id
(id
)
36 if meetup
!= null then mode
= meetup
.answer_mode
41 header
.page_js
= "mode = {mode};\n"
43 function update_scores(){
44 var anss = $('.answer');
49 for(i=0; i < anss.length; i++){
52 var idparts = anss[i].id.split("_");
53 var ansid = idparts[1];
54 var html = anss[i].innerHTML;
55 if(html === "<center>✔</center>"){
58 }else if(html === "<center>❓</center>"){
61 var intansid = parseInt(ansid)
62 if(answers.indexOf(intansid) == -1){
63 answers.push(intansid);
66 count[ansid] += inccount;
68 count[ansid] = inccount;
71 scores[ansid] += incscore;
73 scores[ansid] = incscore;
75 if(scores[ansid] > maxscore){
76 maxscore = scores[ansid];
79 for(i=0; i < answers.length; i++){
80 var ansid = answers[i].toString();
81 var el = $('#total'+ansid)[0];
82 var ins = "<center>"+count[ansid];
83 if(scores[ansid] >= maxscore){
84 ins += "<br/><span style=\\"color:blue\\">★</span>";
90 function change_answer(ele, id){
91 // modify only the currently selected entry
92 if (in_modification_id != id) return;
94 var e = document.getElementById(ele.id);
99 if(i === "<center>✔</center>"){
101 e.innerHTML = "<center>✘</center>"
102 e.style.color = "red";
105 e.innerHTML = "<center>✔</center>";
106 e.style.color = "green";
110 header
.page_js
+= """
111 if(i === "<center>✔</center>"){
113 e.innerHTML = "<center>❓</center>"
114 e.style.color = "#B8860B";
115 }else if(i === "<center>❓</center>"){
117 e.innerHTML = "<center>✘</center>"
118 e.style.color = "red";
121 e.innerHTML = "<center>✔</center>";
122 e.style.color = "green";
125 header
.page_js
+= """
126 var a = ele.id.split('_')
132 url: "./rest/answer",
140 function change_temp_answer(ele){
141 var e = document.getElementById(ele.id);
142 var i = e.innerHTML;"""
144 header
.page_js
+= """
145 if(i === "<center>✔</center>"){
146 e.innerHTML = "<center>✘</center>"
147 e.style.color = "red";
149 e.innerHTML = "<center>✔</center>";
150 e.style.color = "green";
154 header
.page_js
+= """
155 if(i === "<center>✔</center>"){
156 e.innerHTML = "<center>❓</center>";
157 e.style.color = "#B8860B";
158 }else if(i === "<center>❓</center>"){
159 e.innerHTML = "<center>✘</center>"
160 e.style.color = "red";
162 e.innerHTML = "<center>✔</center>";
163 e.style.color = "green";
167 header
.page_js
+= """
170 function add_part(ele){
171 var e = document.getElementById(ele.id);
172 var pname = document.getElementById("new_name").value;
173 var arr = e.id.split("_");
175 var ans = $('#' + ele.id).parent().parent().parent().children(".answer");
177 for(i=0;i<ans.length;i++){
181 header
.page_js
+= """
182 if(curr[0].innerHTML === "<center>✔</center>"){
183 ansmap[curr.attr('id')] = 1
185 ansmap[curr.attr('id')] = 0
188 header
.page_js
+= """
189 if(curr[0].innerHTML === "<center>✔</center>"){
190 ansmap[curr.attr('id')] = 2
191 }else if(curr[0].innerHTML === "<center>❓</center>"){
192 ansmap[curr.attr('id')] = 1
194 ansmap[curr.attr('id')] = 0
197 header
.page_js
+= """
201 url: "./rest/meetup/new_pers",
205 answers: $.param(ansmap)
208 .done(function(data){
211 .fail(function(data){
212 //TODO: Notify of failure
215 function remove_people(ele){
216 var arr = ele.id.split("_")
218 $('#' + ele.id).parent().parent().parent().remove();
222 url: "./rest/people",
229 // ID of line currently open for modification
230 var in_modification_id = null;
231 function modify_people(ele, id){
232 if (in_modification_id != null) {
233 // reset to normal values
234 $('#modify_'+in_modification_id).text("{{{"Modify or delete"}}}");
235 $('#modify_'+in_modification_id).attr("class", "btn btn-xs btn-warning");
236 $('#line_'+in_modification_id).css("background-color", "");
237 $('#delete_'+in_modification_id).css("display", "none");
239 if (in_modification_id != id) {
240 // activate modifiable mode
241 $('#modify_'+id).text("{{{"Done"}}}");
242 $('#modify_'+id).attr("class", "btn btn-xs btn-success");
243 $('#line_'+id).css("background-color", "LightYellow");
244 $('#delete_'+id).show();
246 in_modification_id = id;
248 in_modification_id = null;
254 redef fun rendering
do
255 if meetup
== null then
256 add
((new OpportunityHomePage).write_to_string
)
260 var db
= new OpportunityDB.open
("opportunity")
261 add meetup
.to_html
(db
)
268 # Build the HTML for `self`
269 fun to_html
(db
: OpportunityDB): Writable do
272 <div class="container">
273 <div class="page-header">
274 <center><h1>{{{name}}}</h1></center>
276 if not date
.is_empty
then t
.add
"""
277 <center><h4>{{{"When:"}}} {{{date}}}</h4></center>"""
279 if not place
.is_empty
then t
.add
"""
280 <center><h4>{{{"Where:"}}} {{{place}}}</h4></center>"""
284 <table class="table">
286 t
.add
"<th>{"Participant name"}</th>"
287 for i
in answers
(db
) do
288 t
.add
"<th class=\"text-center\
">"
294 for i
in participants
(db
) do
295 i
.load_answers
(db
, self)
296 t
.add
"<tr id=\"line_
{i.id}\
">"
300 for j
, k
in i
.answers
do
302 if answer_mode
== 0 then
317 t
.add
"""<td class="answer" onclick="change_answer(this, {{{i.id}}})" id="answer_{{{j.id}}}_{{{i.id}}}" style="color:{{{color}}}">"""
319 if answer_mode
== 0 then
334 t
.add
"</center></td>"
336 t
.add
"""<td class="opportunity-action"><center><button class="btn btn-xs btn-warning" type="button" onclick="modify_people(this, {{{i.id}}})" id="modify_{{{i.id}}}">{{{"Modify or delete"}}}</button> """
337 t
.add
"""<button class="btn btn-xs btn-danger" type="button" onclick="remove_people(this)" id="delete_{{{i.id}}}" style="display: none;">{{{"Delete"}}}</button></center></td>"""
341 <tr id="newrow" style="background-color: LightYellow">
342 <td><input id="new_name" type="text" placeholder="{{{"Your name"}}}" class="input-large"></td>
344 for i
in answers
(db
) do
345 t
.add
"<td class=\"answer\
" id=\"newans_
{i.id}\
" onclick=\"change_temp_answer
(this
)\
" style=\"color
:red
;\
"><center>✘</center></td>"
348 <td><center><span id="add_{{{id}}}" onclick="add_part(this)" style="color:green;" class="action"><button class="btn btn-xs btn-success" type="button">{{{"Done"}}}</button></span></center></td>"""
350 # Compute score for each answer
351 var scores
= new HashMap[Int, Int]
353 for i
in answers
(db
) do
354 scores
[i
.id
] = i
.score
(db
)
355 if scores
[i
.id
] > maxsc
then maxsc
= scores
[i
.id
]
359 <th>Total ({{{participants(db).length}}})</th>
361 for i
in answers
(db
) do
362 t
.add
"""<th id="total{{{i.id}}}"><center>{{{i.count(db)}}}"""
363 if scores
.has_key
(i
.id
) and scores
[i
.id
] >= maxsc
then
364 t
.add
"""<br/><span style="color:blue">★</span>"""
366 t
.add
"</center></th>"