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