CLIENT GUI Framework
[vnfsdk/refrepo.git] / openo-portal / portal-auth / src / main / webapp / user / js / userTools.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 var Table = {};\r
17 Table.create = function(data, id, columns) {\r
18         $('#' + id).DataTable({\r
19             data: data,\r
20             bSort: false,\r
21             "sDom": "<t<'left'li><'right'p>>",\r
22             columns: columns\r
23         });\r
24     }\r
25     /**\r
26      *  update the table data, the cloumns must be same with the create one.\r
27      *  data: the update data. as the create structure\r
28      *  id: the table id.\r
29      */\r
30 Table.updata = function(data, id) {\r
31     var datatable = $('#' + id).dataTable().api();\r
32     datatable.clear();\r
33     datatable.rows.add(data);\r
34     datatable.draw();\r
35 }\r
36 \r
37 Table.enableToolTips = function(id) {\r
38     $("#" + id + " tr th").each(function(index, sdom){\r
39         sdom.title = sdom.textContent;\r
40     })\r
41     $("#" + id + " tbody tr td").each(function(index, sdom){\r
42         sdom.title = sdom.textContent;\r
43     })\r
44 }\r
45 \r
46 var Rest = {};\r
47 \r
48 Rest.http = function(setting) {\r
49     var ret = $.ajax(setting);\r
50     ret.fail(function(data) {\r
51         try {\r
52             if (data.responseText.indexOf("login") != -1) {\r
53                 top.window.document.location.reload()\r
54             }\r
55             var result = JSON.parse(data.responseText);\r
56             if (result.error && result.error.message) {\r
57                 top.bootbox.alert(result.error.message, function() {});\r
58             }\r
59         } catch (e) {\r
60         }\r
61 \r
62     })\r
63     return ret;\r
64 }\r
65 \r
66 Rest.turn2URI = function(url) {\r
67     var cookies = document.cookie.split(";");\r
68     var cookie = "";\r
69     for (var i = 0; i < cookies.length; i++) {\r
70         if (cookies[i].split("=")[0] == "X-Auth-Token") {\r
71             cookie = cookies[i].split("=")[1];\r
72             break;\r
73         }\r
74     }\r
75     $.ajax({\r
76         url: "/openoapi/auth/v1/tokens",\r
77         type: "HEAD",\r
78         headers: {\r
79             "X-Auth-Token": cookie\r
80         },\r
81         success: function(data) {\r
82             window.document.location = url;\r
83         },\r
84         error: function(data) {\r
85             top.window.document.location.reload();\r
86         }\r
87     })\r
88 }\r