d9af51d940dfb42bb34d1429de18068b33090442
[nit.git] / contrib / opportunity / src / templates / meetup.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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
14
15 # Shows a meetup and allows to modify its participants
16 module meetup
17
18 import opportunity_model
19 import boilerplate
20 import welcome
21 import template
22
23 # Shows a meetup and allows to modify its participants
24 class OpportunityMeetupPage
25 super OpportunityPage
26
27 # Meetup the page is supposed to show
28 var meetup: nullable Meetup = null
29
30 init from_id(id: String) do
31 var db = new OpportunityDB.open("opportunity")
32 meetup = db.find_meetup_by_id(id)
33 db.close
34 end
35
36 init do
37 header.page_js = """
38 function change_answer(ele){
39 var e = document.getElementById(ele.id);
40 var i = e.innerHTML;
41 var ans = true;
42 if(i === "<center>✔</center>"){
43 ans = false;
44 e.innerHTML = "<center>✘</center>"
45 e.style.color = "red";
46 }else{
47 e.innerHTML = "<center>✔</center>";
48 e.style.color = "green";
49 }
50 var a = ele.id.split('_')
51 var pid = a[1]
52 var aid = a[2]
53 $.ajax({
54 type: "POST",
55 url: "/rest/answer",
56 data: {
57 answer_id: aid,
58 pers_id: pid,
59 answer: ans
60 }
61 });
62 }
63 function change_temp_answer(ele){
64 var e = document.getElementById(ele.id);
65 var i = e.innerHTML;
66 var ans = true;
67 if(i === "<center>✔</center>"){
68 ans = false;
69 e.innerHTML = "<center>✘</center>";
70 e.style.color = "red";
71 }else{
72 e.innerHTML = "<center>✔</center>";
73 e.style.color = "green";
74 }
75 }
76 function add_part(ele){
77 var e = document.getElementById(ele.id);
78 var pname = document.getElementById("new_name").value;
79 var arr = e.id.split("_");
80 var mid = arr[1];
81 var ans = $('#' + ele.id).parent().parent().parent().children(".answer");
82 ansmap = {};
83 for(i=0;i<ans.length;i++){
84 var curr = ans.eq(i)
85 if(curr[0].innerHTML === "✔"){
86 ansmap[curr.attr('id')] = true
87 }else{
88 ansmap[curr.attr('id')] = false
89 }
90 }
91 $.ajax({
92 type: "POST",
93 url: "/rest/meetup/new_pers",
94 data: {
95 meetup_id: mid,
96 persname: pname,
97 answers: $.param(ansmap)
98 }
99 })
100 .done(function(data){
101 location.reload();
102 })
103 .fail(function(data){
104 //TODO: Notify of failure
105 });
106 }
107 function remove_people(ele){
108 var arr = ele.id.split("_")
109 var pid = arr[1]
110 $('#' + ele.id).parent().remove();
111 $.ajax({
112 type: "POST",
113 url: "/rest/people",
114 data: {
115 method: "DELETE",
116 p_id: pid
117 }
118 });
119 }
120 """
121 end
122
123
124 redef fun rendering do
125 if meetup == null then
126 add((new OpportunityHomePage).write_to_string)
127 return
128 end
129 add header
130 var db = new OpportunityDB.open("opportunity")
131 add meetup.to_html(db)
132 db.close
133 add footer
134 end
135 end
136
137 redef class Meetup
138 # Build the HTML for `self`
139 fun to_html(db: OpportunityDB): Streamable do
140 var t = new Template
141 t.add """
142 <div class="page-header">
143 <center><h1>{{{name}}}</h1></center>
144 <center><h4>When : {{{date}}}</h4></center>
145 <center><h4>Where : {{{place}}}</h4></center>
146 </div>
147 <table class="table">
148 """
149 t.add "<th></th>"
150 t.add "<th>Participating</th>"
151 for i in answers(db) do
152 t.add "<th>"
153 t.add i.to_s
154 t.add "</th>"
155 end
156 t.add "</tr>"
157 for i in participants(db) do
158 t.add "<tr>"
159 t.add """<td class="opportunity-action" style="color: red;" onclick="remove_people(this)" id="remove_{{{i.id}}}"><center>❌</center></td>"""
160 i.load_answers(db, self)
161 t.add "<td>"
162 t.add i.to_s
163 t.add "</td>"
164 for j,k in i.answers do
165 t.add """<td class="answer" onclick="change_answer(this)" id="answer_{{{j.id}}}_{{{i.id}}}""""
166 if k then
167 t.add " style=\"color:green;\""
168 else
169 t.add " style=\"color:red;\""
170 end
171 t.add"><center>"
172 if k then
173 t.add ""
174 else
175 t.add ""
176 end
177 t.add "</center></td>"
178 end
179 t.add "</tr>"
180 end
181 t.add """
182 <tr id="newrow">
183 <td><center><span id="add_{{{id}}}" onclick="add_part(this)" style="color:green;" class="action"><strong>+</strong></span></center></td>
184 <td><input id="new_name" type="text" placeholder="Your name" class="input-large"></td>
185 """
186 for i in answers(db) do
187 t.add "<td class=\"answer\" id=\"newans_{i.id}\" onclick=\"change_temp_answer(this)\" style=\"color:red;\"><center></center></td>"
188 end
189 t.add "</tr>"
190 t.add "</table>"
191 return t
192 end
193 end