rebuild GUI structure(only changed modules' name)
[vnfsdk/refrepo.git] / auth / src / main / webapp / user / js / user.js
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 $(document).ready(function() {
17     var USER_SERVICE = "/openoapi/auth/v1/users";
18     var deleteEditOpt = "<img class='edit' title='edit' src='/openoui/user/images/edit.png'><img class='changePsd' title='Change Password' src='/openoui/user/images/reset.png'><img class='delete' title='delete' src='/openoui/user/images/delete.png'>";
19     var editOpt = "<img class='edit' title='edit' src='/openoui/user/images/edit.png'><img class='changePsd' title='Change Password' src='/openoui/user/images/reset.png'>";
20
21     var userListHeader = [
22         { title: "User", data: "User",width: "20%"},
23         { title: "Description", data: "Description",width: "60%"},
24         { title: "Operations", data: "Operations",width: "20%"}
25     ];
26     function initialPage() {
27         /*get the user list data;*/
28         getUserList().done(function(data) {
29             var data = formatUsers(data);
30             Table.create(data, "table_id", userListHeader);
31             $(".hw_body").css("visibility", "visible");
32         }).error(function(data) {
33             if (data.status == 403) {
34                 $(".hw_body").html("<span style='font-size:20px;'>" + JSON.parse(data.responseText).error.message + "</span>");
35             }
36         });
37
38         /*add the listener*/
39         $("#table_id tbody").on("click", "td", function(e) {
40             var classname = e.target.className;
41             var id = $("#table_id").DataTable().row(this).data().rowid;
42             if (classname == "delete") {
43                 top.bootbox.confirm("Are you sure to delete this user?", function(result) {
44                     if (result) {
45                         deleteUser(id).done(function() {
46                             getUserList().done(function(data) {
47                                 var data = formatUsers(data);
48                                 var datatable = $("#table_id").dataTable().api();
49                                 datatable.clear();
50                                 datatable.rows.add(data);
51                                 datatable.draw();
52                             })
53                         })
54                     }
55                 })
56             } else if (classname == "edit") {
57                 window.document.location = "/openoui/user/modifyUser.html" + "?id=" + id;
58             } else if (classname == "changePsd") {
59                 window.document.location = "/openoui/user/changePassword.html" + "?id=" + id;
60             }
61         })
62
63         $("#create").click(function(e) {
64             window.document.location = "/openoui/user/createUser.html";
65         })
66     }
67
68     function getUserList() {
69         return Rest.http({
70             url: USER_SERVICE + "?=" + new Date().getTime(),
71             type: "GET",
72             async: false,
73             contentType: 'application/json',
74             dataType: "json"
75         })
76     }
77
78     function deleteUser(id) {
79         return Rest.http({
80             url: USER_SERVICE + "/" + id + "?=" + new Date().getTime(),
81             type: "DELETE",
82             async: false,
83             contentType: 'application/json',
84             dataType: "json"
85         })
86     }
87
88     function formatUsers(data) {
89         var tableData = [];
90         for (var i = 0; i < data.length; i++) {
91             var temp = {};
92             temp.rowid = data[i].id;
93             temp.User = data[i].name;
94             temp.Description = data[i].description;
95             if (data[i].name == "admin") {
96                 temp.Operations = editOpt;
97             } else {
98                 temp.Operations = deleteEditOpt;
99             }
100             tableData.push(temp);
101         }
102         return tableData;
103     }
104     initialPage();
105
106     setTimeout(function() {
107         Table.enableToolTips("table_id");
108     }, 0)
109 });