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