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 // Remember the participant's name client-side
216 set_cookie("opportunity_participant_name", pname);
219 function remove_people(ele){
220 var arr = ele.id.split("_")
222 $('#' + ele.id).parent().parent().parent().remove();
226 url: "./rest/people",
234 // ID of line currently open for modification
235 var in_modification_id = null;
236 function modify_people(ele, id){
237 if (in_modification_id != null) {
238 // reset to normal values
239 $('#modify_'+in_modification_id).text("{{{"Modify or delete"}}}");
240 $('#modify_'+in_modification_id).attr("class", "btn btn-xs btn-warning");
241 $('#line_'+in_modification_id).css("background-color", "");
242 $('#delete_'+in_modification_id).css("display", "none");
244 if (in_modification_id != id) {
245 // activate modifiable mode
246 $('#modify_'+id).text("{{{"Done"}}}");
247 $('#modify_'+id).attr("class", "btn btn-xs btn-success");
248 $('#line_'+id).css("background-color", "LightYellow");
249 $('#delete_'+id).show();
251 in_modification_id = id;
253 in_modification_id = null;
257 function get_cookie(cookie_name) {
258 var name = cookie_name + "=";
259 var ca = document.cookie.split(';');
260 for(var i = 0; i < ca.length; i ++) {
262 while (c.charAt(0) == ' ') c = c.substring(1);
263 if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
268 function set_cookie(cookie_name, value) {
269 var date = new Date();
270 date.setTime(date.getTime() + (365*24*60*60*1000));
271 var expires = "expires="+date.toUTCString();
272 document.cookie = cookie_name + "=" + value + "; " + expires;
275 // Retrieve the last client-side participant's name
276 window.onload = function () {
277 var name_field = document.getElementById("new_name");
278 name_field.value = get_cookie("opportunity_participant_name");
283 redef fun rendering
do
284 if meetup
== null then
285 add
((new OpportunityHomePage).write_to_string
)
289 var db
= new OpportunityDB.open
("opportunity")
290 add meetup
.to_html
(db
)
297 # Build the HTML for `self`
298 fun to_html
(db
: OpportunityDB): Writable do
301 <div class="container">
302 <div class="page-header">
303 <center><h1>{{{name}}}</h1></center>
305 if not date
.is_empty
then t
.add
"""
306 <center><h4>{{{"When:"}}} {{{date}}}</h4></center>"""
308 if not place
.is_empty
then t
.add
"""
309 <center><h4>{{{"Where:"}}} {{{place}}}</h4></center>"""
313 <table class="table">
315 t
.add
"<th>{"Participant name"}</th>"
316 for i
in answers
(db
) do
317 t
.add
"<th class=\"text-center\
">"
323 for i
in participants
(db
) do
324 i
.load_answers
(db
, self)
325 t
.add
"<tr id=\"line_
{i.id}\
">"
329 for j
, k
in i
.answers
do
331 if answer_mode
== 0 then
346 t
.add
"""<td class="answer" onclick="change_answer(this, {{{i.id}}})" id="answer_{{{j.id}}}_{{{i.id}}}" style="color:{{{color}}}">"""
348 if answer_mode
== 0 then
363 t
.add
"</center></td>"
365 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> """
366 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>"""
370 <tr id="newrow" style="background-color: LightYellow">
371 <td><input id="new_name" type="text" placeholder="{{{"Your name"}}}" class="input-large"></td>
373 for i
in answers
(db
) do
374 t
.add
"<td class=\"answer\
" id=\"newans_
{i.id}\
" onclick=\"change_temp_answer
(this
)\
" style=\"color
:red
;\
"><center>✘</center></td>"
377 <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>"""
379 # Compute score for each answer
380 var scores
= new HashMap[Int, Int]
382 for i
in answers
(db
) do
383 scores
[i
.id
] = i
.score
(db
)
384 if scores
[i
.id
] > maxsc
then maxsc
= scores
[i
.id
]
388 <th>Total ({{{participants(db).length}}})</th>
390 for i
in answers
(db
) do
391 t
.add
"""<th id="total{{{i.id}}}"><center>{{{i.count(db)}}}"""
392 if scores
.has_key
(i
.id
) and scores
[i
.id
] >= maxsc
then
393 t
.add
"""<br/><span style="color:blue">★</span>"""
395 t
.add
"</center></th>"