CLIENT GUI Framework
[vnfsdk/refrepo.git] / openo-portal / portal-common / src / main / webapp / common / js / login.js
1 /*\r
2  * Copyright 2016-2017, CMCC 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 String.prototype.trim = function() {\r
17         return this.replace(/(^\s*)|(\s*$)/g, "");\r
18 };\r
19 \r
20 function loginSubmitHandler() {\r
21         var loginData = {\r
22                 "userName": $("#openo_input_userName").val(),\r
23                 "password": $("#openo_input_password").val()\r
24         }\r
25 \r
26         $.ajax({\r
27                 url : "/openoapi/auth/v1/tokens",\r
28                 type : "POST",\r
29                 contentType : 'application/json; charset=utf-8',\r
30                 data : JSON.stringify(loginData)\r
31         }).done(function(data) {\r
32                 var topURL = top.window.document.location.href;\r
33                 if (topURL.indexOf("?service") != -1) {\r
34                         top.window.document.location.href = decodeURIComponent(topURL.substring(topURL.indexOf("?service") + 9));\r
35                 } else {\r
36                         top.window.document.location.href = "/openoui/common/default.html";\r
37                 }\r
38                 store("loginUserName", $("#openo_input_userName").val());\r
39         }).fail(function(data) {\r
40                 var tipDivId = $("#loginConnError");\r
41                 if (data.status == 401) {\r
42                         tipDivId = $("#nameOrpwdError");\r
43                 }\r
44 \r
45                 tipDivId.addClass('alert-danger');\r
46                 if (tipDivId.attr("tipstatus") == "normal") {\r
47                         tipDivId.show();\r
48                 } else if (tipDivId.attr("tipstatus") == "close") {\r
49                         tipDivId.attr("tipstatus", "normal");\r
50                 }\r
51         });\r
52 \r
53         saveUserInfo();\r
54 };\r
55 \r
56 var Login = function () {\r
57         var handleLogin = function() {\r
58                 $('.login-form').validate({\r
59                         errorElement: 'span', //default input error message container\r
60                         errorClass: 'help-block', // default input error message class\r
61                         focusInvalid: false, // do not focus the last invalid input\r
62                         rules: {\r
63                                 username: {\r
64                                         required: true\r
65                                 },\r
66                                 password: {\r
67                                         required: false\r
68                                 },\r
69                                 remember: {\r
70                                         required: false\r
71                                 }\r
72                         },\r
73                         messages: {\r
74                                 username: {\r
75                                         required: $.i18n.prop('openo_input_userName').replace(/\"/g,'') \r
76                                 },\r
77                                 password: {\r
78                                         required: $.i18n.prop('openo_input_password').replace(/\"/g,'')\r
79                                 }\r
80                         },\r
81                         invalidHandler: function (event, validator) {\r
82                                 $('.alert-danger', $('.login-form')).show();\r
83                         },\r
84                         highlight: function (element) {\r
85                                 $(element).closest('.form-group').addClass('has-error'); // set error class to the control group\r
86                         },\r
87                         success: function (label) {\r
88                                 label.closest('.form-group').removeClass('has-error');\r
89                                 label.remove();\r
90                         },\r
91                         errorPlacement: function (error, element) {\r
92                                 error.insertAfter(element.closest('.input-icon'));\r
93                         },\r
94                         submitHandler: loginSubmitHandler\r
95                 });\r
96 \r
97                 $('.login-form input').keypress(function (e) {\r
98                         $("#nameOrpwdError").hide();\r
99                         $("#loginConnError").hide();\r
100                         if (e.which == 13) {\r
101                                 if ($('.login-form').validate().form()) {\r
102                                         $('.login-form').submit();\r
103                                 }\r
104                                 return false;\r
105                         }\r
106                 });\r
107         }\r
108 \r
109         return {\r
110                 //main function to initiate the module\r
111                 init: function () {\r
112                         handleLogin();\r
113                         $.backstretch([\r
114                                 "image/integration/openo_bg_1.jpg",\r
115                                 "image/integration//openo_bg_2.jpg",\r
116                                 "image/integration//openo_bg_3.jpg"\r
117                         ], {\r
118                                 fade: 500,\r
119                                 duration: 15000\r
120                         });\r
121                 }\r
122         };\r
123 }();\r
124 \r
125 $(document).ready(function() {\r
126         if (store("remember") == "true") {\r
127                 $("input[name='remember']").attr("checked", "checked");\r
128                 $("#openo_input_userName").val(store("openo_input_userName"));\r
129                 $("#openo_input_password").val(store("openo_input_password"));\r
130         }\r
131 });\r
132 \r
133 function saveUserInfo() {\r
134         var rmbcheck = $("input[name='remember']");\r
135         if (rmbcheck.attr("checked") == true || rmbcheck.is(':checked')) {\r
136                 store("remember", "true");\r
137                 store("openo_input_userName", $("#openo_input_userName").val());\r
138                 store("openo_input_password", $("#openo_input_password").val());\r
139         } else {\r
140                 store.remove("remember");\r
141                 store.remove("openo_input_userName");\r
142                 store.remove("openo_input_password");\r
143         }\r
144 }\r
145 \r
146 function logoutSubmit() {\r
147         $.ajax({\r
148                 url: "/openoapi/auth/v1/tokens" + "?=" + new Date().getTime(),\r
149                 type: "DELETE",\r
150                 contentType: "application/json",\r
151                 dataType: "text",\r
152                 success: function() {\r
153                         top.window.location = "/openoui/common/login.html";\r
154                 },\r
155                 error: function() {\r
156                 }\r
157         })\r
158 }\r