automatic reload on lists; new django decorator for ajax request
[osm/LW-UI.git] / projecthandler / template / project / projectlist.html
1 {% extends "base.html" %}
2 {% load get %}
3 {% load date_tag %}
4 {% load staticfiles %}
5
6 {% block head_block %}
7 {{ block.super }}
8 <link rel="stylesheet" href="/static/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css">
9
10 {% endblock %}
11 {% block title_header_big %}
12 {{ block.super }}
13 {% endblock %}
14 {% block left_sidebar %}
15
16 {% include 'osm/osm_project_left_sidebar.html' %}
17
18 {% endblock %}
19
20
21 {% block breadcrumb_body %}
22 {{ block.super }}
23 <li><a href="{% url 'projects:projects_list' %}">Projects</a></li>
24 {% endblock %}
25
26 {% block content_body %}
27 {{ block.super }}
28 {% csrf_token %}
29 <div class="row">
30 <div class="col-md-12">
31
32 <div class="box">
33 <div class="box-header with-border">
34 <h3 class="box-title">Projects</h3>
35 <div class="box-tools">
36 <button type="button" class="btn btn-default" data-container="body"
37 data-toggle="tooltip" data-placement="top" title="New Project"
38 onclick="javascript:showModalNewProject()">
39 <i class="fa fa-plus"></i> New Project</button>
40 </div>
41 </div>
42 <div class="box-body">
43 <table id="projects_table" class="table table-bordered table-striped">
44 <thead>
45 <tr>
46
47 <th>Name</th>
48
49 <th>Modification Date</th>
50 <th>Creation Date</th>
51 <th>Actions</th>
52 </tr>
53 </thead>
54 <tbody>
55
56 </tbody>
57 </table>
58 </div>
59 </div>
60 </div>
61
62 </div>
63 {% include "modal/project_new.html" %}
64 {% include "modal/project_edit.html" %}
65 {% endblock %}
66
67 {% block resource_block %}
68 {{ block.super }}
69 <script src="/static/bower_components/datatables.net/js/jquery.dataTables.min.js"></script>
70 <script src="/static/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
71
72 <script>
73 $(document).ready( function () {
74 var table = $('#projects_table').DataTable({
75 responsive: true,
76 "ajax": {
77 "url": "/projects/list",
78 "dataSrc": function (json) {
79 return json['projects'];
80 },
81 statusCode: {
82 401: function () {
83 console.log("no auth");
84 moveToLogin(window.location.pathname);
85 }
86 },
87 "error": function (hxr, error, thrown) {
88 console.log(hxr)
89 console.log(thrown)
90 console.log(error);
91 }
92
93 },
94 "columns": [
95 {
96 "render": function (data, type, row) {
97 return '<a href="/projects/switch/'+row['name']+'" >'+row['name']+'</a>'
98 },
99 "targets": 0
100 },
101 {
102 "render": function (data, type, row) {
103 return moment.unix(row["_admin"]['modified']).format('YYYY-MM-DD hh:mm:ss a');
104 },
105 "targets": 1
106 },
107 {
108 "render": function (data, type, row) {
109 return moment.unix(row["_admin"]['created']).format('YYYY-MM-DD hh:mm:ss a');
110 },
111 "targets": 2
112 },
113 {
114 "render": function (data, type, row) {
115 return '<div class="btn-group">\n' +
116 ' <button type="button" class="btn btn-default dropdown-toggle"\n' +
117 ' data-toggle="dropdown" aria-expanded="false">Actions\n' +
118 ' <span class="fa fa-caret-down"></span></button>\n' +
119 ' <ul class="dropdown-menu">\n' +
120 ' <li><a href="#"\n' +
121 ' onclick="javascript:editProject(\''+ row['name']+'\')">\n' +
122 ' <i class="fa fa-edit"></i> Rename</a></li>\n' +
123 ' <li>\n' +
124 ' <a href="#" onclick="javascript:deleteProject(\''+ row['name']+'\')" style="color:red">\n' +
125 ' <i class="fa fa-trash" ></i> Delete</a>\n' +
126 ' </li>\n' +
127 ' </ul>\n' +
128 ' </div>';
129 },
130 "targets": 3
131 }
132 ]
133 });
134
135 setInterval(function () {
136 table.ajax.reload();
137 }, 10000);
138 });
139 </script>
140 <script>
141 function showModalNewProject(){
142 $('#modal_new_project').modal('show');
143 }
144
145 function editProject(project_id){
146 var url = "/projects/" + project_id+"/edit";
147 $("#formEditProject").attr("action", url);
148 $('#modal_edit_project').modal('show');
149 }
150
151 function deleteProject(project_id) {
152 var url = "/projects/" + project_id+"/delete";
153 bootbox.confirm("Are you sure want to delete?", function (result) {
154 if (result) {
155 $.ajax({
156 url: url,
157 type: 'GET',
158 headers: {
159 "Accept": 'application/json'
160 },
161 contentType: false,
162 processData: false
163 }).done(function (response,textStatus, jqXHR) {
164
165 bootbox.alert({
166 title: "Result",
167 message: "Project deleted.",
168 callback: function () {
169 location.reload();
170 }
171 });
172 }).fail(function(result){
173 var data = result.responseJSON;
174 var title = "Error " + (data.code ? data.code: 'unknown');
175 var message = data.detail ? data.detail: 'No detail available.';
176 bootbox.alert({
177 title: title,
178 message: message
179 });
180 });
181 }
182 })
183 }
184
185 $(document).ready(function () {
186 $("#formNewProject").submit(function (event) {
187 event.preventDefault(); //prevent default action
188 var post_url = $(this).attr("action"); //get form action url
189 var request_method = $(this).attr("method"); //get form GET/POST method
190 var form_data = new FormData(this); //Encode form elements for submission
191 console.log(post_url);
192 $.ajax({
193 url: post_url,
194 type: request_method,
195 data: form_data,
196 headers: {
197 "Accept": 'application/json'
198 },
199 contentType: false,
200 processData: false
201 }).done(function (response,textStatus, jqXHR) {
202 bootbox.alert({
203 title: "Result",
204 message: "Project created.",
205 callback: function () {
206 location.reload();
207 }
208 });
209 }).fail(function(result){
210 var data = result.responseJSON;
211 var title = "Error " + (data.code ? data.code: 'unknown');
212 var message = data.detail ? data.detail: 'No detail available.';
213 bootbox.alert({
214 title: title,
215 message: message
216 });
217 });
218 });
219 });
220
221
222
223
224 </script>
225
226
227 {% endblock %}