Spinner in Management page
[myslice.git] / portal / templates / fed4fire / fed4fire_institution.html
1 {% extends "layout_wide.html" %}
2
3 {% block head %} 
4 <script type="text/javascript" src="{{STATIC_URL}}/js/institution.js"></script>
5 {% endblock head %}
6
7 {% block content %}
8 <div class="container">
9         <div class="row">
10                 <div class="col-md-12">
11                          <div class="breadcrumbs">
12                 {% if project %}
13                                 Management &nbsp;>&nbsp; Project: <span id="authority_name">{{user_details.parent_authority}}</span>
14                 {% else %}
15                                 Management &nbsp;>&nbsp; Institution: <span id="authority_name">{{user_details.parent_authority}}</span>
16                 {% endif %}
17                          </div>
18                 </div>
19         </div>
20 </div>
21 <div class="container">
22         <div class="row">
23                 <div class="col-md-12">
24                         <ul class="nav nav-tabs nav-section">
25                                 <li class="active"><a href="#about">About</a></li>
26                                 <li><a href="#users">Users</a></li>
27                 {% if not project %}
28                                 <li><a href="#projects">Projects</a></li>
29                 {% else %}
30                                 <li><a href="#slices">Slices</a></li>
31                 {% endif %}
32                                 <li><a href="#requests">Requests</a></li>
33                         </ul>
34             </div>
35         </div>
36 </div>
37 <div class="container tab-content">
38         <div class="tab-pane active row" id="about">
39         <div class="col-md-12 el">
40         <h2>{{user_details.parent_authority}}</h2>
41         </div>
42         </div>
43         
44         <div class="tab-pane row" id="users" data-authority="{{user_details.parent_authority}}">
45                 <div class="col-md-12 el">
46                         <div id="user-tab-loading" style="padding-bottom:10px;"><img src="{{ STATIC_URL }}img/loading.gif" alt="Loading Slices" /></div>
47                                 <div id="user-tab-loaded" style="display:none;">
48                                 <table id="user-tab" class="table">
49                                         <tr>
50                                         <th>+/-</th>
51                             {% if not project %}
52                                         <th>Email</th>
53                             {% endif %}
54                                         <th>User hrn</th>
55                             <!--
56                                         <th>First name</th>
57                                         <th>Last name</th>
58                                         <th>Enabled</th>
59                             -->
60                                         </tr>
61                                 </table>
62                                 
63                         </div>
64                         {%if  pi %}     
65                         <div>
66                 {% if project %}
67                                 <button id="removepi" type="button" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Remove from project</button>
68                 {% else %}
69                                 <button id="makepi" type="button" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-up"></span> Upgrade to PI</button>
70                                 <button id="removepi" type="button" class="btn"><span class="glyphicon glyphicon-arrow-down"></span> Downgrade to  user</button>
71                                 <button id="deleteusers" type="button" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Delete selected users</button>
72                 {% endif %}
73                         </div>
74                         {% endif %}
75                 </div>
76         </div>
77
78     {% if not project %}
79         <div class="tab-pane row" id="projects" data-authority="{{user_details.parent_authority}}">
80                 <div class="col-md-12 el">
81                         <div id="project-tab-loading" style="padding-bottom:10px;"><img src="{{ STATIC_URL }}img/loading.gif" alt="Loading Projects" /></div>
82                                 <div id="project-tab-loaded" style="display:none;">
83                                 <table id="project-tab" class="table">
84                                         <tr>
85                                         <th>+/-</th>
86                                         <th>Project hrn</th>
87                                         </tr>
88                                 </table>
89                                 
90                         </div>
91                         <div>
92                 {% if  pi %}
93                         <button id="createproject" type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> Create project</button>
94                         <button id="deleteprojects" type="button" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Delete selected projects</button>
95                         {% else %}
96                         <button id="createproject" type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> Request project</button>
97                         {% endif %}
98                         </div>
99                 </div>
100         </div>
101    {% endif %}
102         <div class="tab-pane row" id="slices">
103                 <div class="col-md-12 el">
104             <div id="slice-tab-loading" style="padding-bottom:10px;"><img src="{{ STATIC_URL }}img/loading.gif" alt="Loading Slices" /></div>
105             <div id="slice-tab-loaded" style="display:none;">
106                 <table id="slice-tab" class="table">
107                     <tr>
108                         <th>+/-</th>
109                         <th>Slice hrn</th>
110                         <th>Users</th>
111                         <th>Url</th>
112                         <!-- <th>nodes</th> -->
113                         <th>Creation</th>
114                     </tr>
115                 </table>                        
116             </div>
117         <div>
118                 {% if pi %}
119                         <button id="createslice" type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> Create slice</button>
120             <button id="renewslices" type="button" class="btn btn-primary"><span class="glyphicon glyphicon-refresh"></span> Renew Slices</button>
121             <button id="deleteslices" type="button" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Delete Slices</button>
122                         {% else %}
123                         <button id="createslice" type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> Request slice</button>
124                         {% endif %}
125         </div>
126            </div>
127         </div>
128         <div class="tab-pane row" id="requests">
129         <div id="spinner" style="padding-top:40px; padding-left:40px;"></div>
130         </div>
131 </div>
132 <script>
133 $(document).ready(function() {
134     {% if person %}
135     {% if user_details.parent_authority %}
136         $('#spinner').spin();
137         
138         $.post("/rest/myslice:slice/",{'fields':['slice_hrn','users','url','slice_date_created'],'filters':{'parent_authority':'{{user_details.parent_authority}}'}}, function( data ) {
139             var list_slices = [];
140             var table_slices = [];
141             /* "slice_hrn", "slice_description", "slice_type", "parent_authority", "created", "nodes", "slice_url", "slice_last_updated", "users", "slice_urn", "slice_expires" */
142             $.each( data, function( key, val ) {
143                 list_slices.push( "<li><a href=\"portal/slice/"+val.slice_hrn+"\">" + val.slice_hrn + "</a></li>" );
144                 if(val.nodes=="undefined" || val.nodes==null){
145                     nodes_length=0;
146                 }else{
147                     nodes_length=val.nodes.length;
148                 }
149                 if(val.users=="undefined" || val.users==null){
150                     users_length=0;
151                 }else{
152                     users_length=val.users.length;
153                 }
154
155                 if(val.url=="undefined" || val.url==null){
156                     slice_url="";
157                 }else{
158                     slice_url="<a href='"+val.url+"' target='_blank'>"+val.url+"</a>";
159                 }
160                 
161                 slice_row = "<tr id='"+val.slice_hrn+"'>";
162                 slice_row += "<td><input type='checkbox' class='slice' id='"+val.slice_hrn+"'><div id='"+val.slice_hrn+"-loading' style='display:none;padding-left:3px;'><img src='{{ STATIC_URL }}img/loading.gif'></div></td>";
163
164                 // filter links to slices, only those that the user has credentials for
165                 s = myslice.user.slices;
166                 if(s.length > 0 && $.inArray(val.slice_hrn, myslice.user.slices)!=-1){
167                     slice_row += "<td><a href='/slice/"+val.slice_hrn+"'>" + val.slice_hrn + "</a></td>";
168                 }else{
169                     slice_row += "<td>" + val.slice_hrn + "</td>";
170                 }
171                 slice_row += "<td>"+users_length+"</td>";
172                 slice_row += "<td>"+slice_url+"</td>";
173                 //slice_row += "<td>"+nodes_length+"</td>";
174                 slice_row += "<td>"+val.slice_date_created+"</td>";
175                 slice_row += "</tr>";
176                 table_slices.push(slice_row);
177                 
178             });
179            
180             /* $("div#slice-list").html($( "<ul/>", { html: list_slices.join( "" ) })); */
181             $("table#slice-tab tr:last").after(table_slices.join( "" ));
182             $("div#slice-tab-loaded").css("display","block");
183             $("div#slice-tab-loading").css("display","none");
184         });
185                 
186         {% if project %}
187                 $.post("/rest/myslice:authority/",{'fields':['pi_users'],'filters':{'authority_hrn':'{{user_details.parent_authority}}'}}, function( data ) {
188            
189             var table_users = [];
190             $.each( data[0].pi_users, function( key, val ) {
191                 user_row = "<tr id='"+val+"'>";
192                 user_row += "<td><input type='checkbox' class='user' id='"+val+"'><div id='"+val+"-loading' style='display:none;padding-left:3px;'><img src='{{ STATIC_URL }}img/loading.gif'></div></td>";
193                 user_row += "<td>"+val+"</td>";
194                 user_row += "</tr>";
195                 table_users.push(user_row);
196             });
197             $("table#user-tab tr:last").after(table_users.join( "" ));
198             $("div#user-tab-loaded").css("display","block");
199             $("div#user-tab-loading").css("display","none");
200
201         });
202         {% else %}
203         $.post("/rest/myslice:user/",{'filters':{'parent_authority':'{{user_details.parent_authority}}'}}, function( data ) {
204             var list_users = [];
205             var table_users = [];
206                     /* Available fields
207                     user_gid, user_enabled, slices, pi_authorities, keys, parent_authority, user_first_name,
208                     user_urn, user_last_name, user_phone, user_hrn, user_email, user_type
209                     */
210             $.each( data, function( key, val ) {
211                 list_users.push( "<li><a href=\"portal/user/"+val.user_email+"\">" + val.user_email + "</a></li>" );
212                 user_row = "<tr id='"+val.user_hrn+"'>";
213                 user_row += "<td><input type='checkbox' class='user' id='"+val.user_hrn+"' data-email='"+val.user_email+"'><div id='"+val.user_hrn+"-loading' style='display:none;padding-left:3px;'><img src='{{ STATIC_URL }}img/loading.gif'></div></td>";
214                 user_row += "<td>"+val.user_email+"</td>";
215                 user_row += "<td>"+val.user_hrn+"</td>";
216                 /*
217                 user_row += "<td>"+val.user_first_name+"</td>";
218                 user_row += "<td>"+val.user_last_name+"</td>";
219                             user_row += "<td>"+val.user_enabled+"</td>";
220                 */
221                 user_row += "</tr>";
222                 table_users.push(user_row);
223             });
224             $("table#user-tab tr:last").after(table_users.join( "" ));
225             $("div#user-tab-loaded").css("display","block");
226             $("div#user-tab-loading").css("display","none");
227         });
228                 $.post("/rest/myslice:authority/",{'fields':['authority_hrn','pi_users'],'filters':{'authority_hrn':'CONTAINS{{user_details.parent_authority}}'}}, function( data ) {
229            
230             var table_projects = [];
231             $.each( data, function( key, val ) {
232                 project_row = "<tr id='"+val.authority_hrn+"'>";
233                 project_row += "<td><input type='checkbox' class='project' id='"+val.authority_hrn+"'><div id='"+val.authority_hrn+"-loading' style='display:none;padding-left:3px;'><img src='{{ STATIC_URL }}img/loading.gif'></div></td>";
234
235                 // filter links to projects, only those that the user has credentials for
236                 p = myslice.projects;
237                 if(p.length > 0 && $.inArray(val.authority_hrn, p)!=-1){
238                     project_row += "<td><a href='/portal/project/"+val.authority_hrn+"'>"+val.authority_hrn+"</a></td>";
239                 }else{
240                     project_row += "<td>"+val.authority_hrn+"</td>";
241                 }
242                 project_row += "</tr>";
243                 table_projects.push(project_row);
244             });
245             $("table#project-tab tr:last").after(table_projects.join( "" ));
246             $("div#project-tab-loaded").css("display","block");
247             $("div#project-tab-loading").css("display","none");
248
249         });
250
251         {% endif %}
252     {% endif %}
253     {% endif %}
254
255 }); // End document.ready
256
257 $(document).ready(function() {
258         $('.nav-tabs a').click(function (e) {
259                 e.preventDefault();
260                 $(this).tab('show');
261         var id = $(this).attr('href').substr(1);
262         if ((id == 'requests')){
263                 $("#" + id).load('/management/' + id);
264         }
265         /*
266         if ((id == 'requests') || (id == 'about'))
267                 $("#" + id).load('/management/' + id);
268         */
269         });
270         var hash = window.location.hash;
271         if (hash) {
272                 $('.nav-tabs a[href='+hash+']').click();
273         } else {
274                 $('.nav-tabs a[href=#about]').click();
275         }
276
277     //upgrade users to PI
278     $('button#makepi').click(function() {
279         spin_all();
280         var flag = false;
281         var pi_users = [];
282         $.post("/rest/myslice:authority/",{'filters':{'authority_hrn': '{{user_details.parent_authority}}'}}, function( data ) {
283             pi_users = data[0]["pi_users"];
284             $('input:checkbox.user').each(function (index) {
285                 if(this.checked){
286                     var record_id = this.id;
287                     pi_users.push(record_id)
288                     $.post("/credentials/clear/",{'emails':[this.dataset['email']]}, function(data) {
289                     }); // post credentials clear
290                     flag = true;                
291                 }
292             });
293             if(flag == true){
294                 $.post("/update/myslice:authority/",{'filters':{'authority_hrn':'{{user_details.parent_authority}}'},'params':{'pi_users':pi_users}}, function(data) {
295                     if(data.success){
296                         mysliceAlert('Success: User rights added to: {{user_details.parent_authority}}','success', true);
297                     }else{
298                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
299                         //alert("Rest Error for "+record_id+": "+data.error);
300                     }
301                     unspin_all();
302                 });
303             }
304         });
305     });
306 //downgrade to user [in projects:remove from project]
307     $('button#removepi').click(function() {
308         spin_all();
309         var flag = false;
310         var pi_users = [];
311         $.post("/rest/myslice:authority/",{'filters':{'authority_hrn': '{{user_details.parent_authority}}'}}, function( data ) {
312             pi_users = data[0]["pi_users"];
313                         // http://stackoverflow.com/questions/9792927/javascript-array-search-and-remove-string
314             $('input:checkbox.user').each(function (index) {
315                 if(this.checked){
316                     var record_id = this.id;
317                                         for (var i=pi_users.length-1; i>=0; i--) {
318                                         if (pi_users[i] === record_id) {
319                                                 pi_users.splice(i, 1);
320                             $.post("/credentials/clear/",{'emails':[this.dataset['email']]}, function(data) {
321                             }); // post credentials clear
322                             flag = true;                
323                                         }
324                                         }
325                 }
326             });
327             if(flag == true){
328                 $.post("/update/myslice:authority/",{'filters':{'authority_hrn':'{{user_details.parent_authority}}'},'params':{'pi_users':pi_users}}, function(data) {
329                     if(data.success){
330                         mysliceAlert('Success: User rights removed from: {{user_details.parent_authority}}','success', true);
331                         {% if project %}
332                         // remove the user from the list of pis of the project displayed
333                         $('input:checkbox.user').each(function (index) {
334                            if(this.checked){
335                                var record_id = this.id;
336                                $('tr[id="'+record_id+'"]').fadeOut("slow");
337                                $('tr[id="'+record_id+'"]').remove();
338                             }
339
340                         });
341                         {% endif %}
342                     }else{
343                         mysliceAlert('Rest Error for: '+data.error,'warning', true);
344                         //alert("Rest Error for "+record_id+": "+data.error);
345                     }
346                     unspin_all();
347                 });
348             }else{
349                 mysliceAlert('No action: User had no rights on: {{user_details.parent_authority}}','success', true);
350                 unspin_all();
351             }
352         });
353     });
354 });
355
356
357
358 </script>
359 {% endblock %}