4a55cec861dd690494bea276b20d7129a7a72d49
[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 === "<center>✔</center>"){
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="container">
143 <div class="page-header">
144 <center><h1>{{{name}}}</h1></center>
145 """
146 if not date.is_empty then t.add """
147 <center><h4>When: {{{date}}}</h4></center>"""
148
149 if not place.is_empty then t.add """
150 <center><h4>Where: {{{place}}}</h4></center>"""
151
152 t.add """
153 </div>
154 <table class="table">
155 """
156 t.add "<th>Participant name</th>"
157 for i in answers(db) do
158 t.add "<th class=\"text-center\">"
159 t.add i.to_s
160 t.add "</th>"
161 end
162 t.add "<th></th>"
163 t.add "</tr>"
164 for i in participants(db) do
165 t.add "<tr>"
166 i.load_answers(db, self)
167 t.add "<td>"
168 t.add i.to_s
169 t.add "</td>"
170 for j,k in i.answers do
171 t.add """<td class="answer" onclick="change_answer(this)" id="answer_{{{j.id}}}_{{{i.id}}}""""
172 if k then
173 t.add " style=\"color:green;\""
174 else
175 t.add " style=\"color:red;\""
176 end
177 t.add"><center>"
178 if k then
179 t.add ""
180 else
181 t.add ""
182 end
183 t.add "</center></td>"
184 end
185 t.add """<td class="opportunity-action" style="color: red;" onclick="remove_people(this)" id="remove_{{{i.id}}}"><center><button class="btn btn-xs btn-danger" type="button">Remove</button></center></td>"""
186 t.add "</tr>"
187 end
188 t.add """
189 <tr id="newrow">
190 <td><input id="new_name" type="text" placeholder="Your name" class="input-large"></td>
191 """
192 for i in answers(db) do
193 t.add "<td class=\"answer\" id=\"newans_{i.id}\" onclick=\"change_temp_answer(this)\" style=\"color:red;\"><center></center></td>"
194 end
195 t.add """
196 <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>"""
197 t.add "</tr>"
198 t.add "</table>"
199 t.add "</div>"
200 return t
201 end
202 end