1 | var E={};
|
2 |
|
3 | E.Server = {};
|
4 |
|
5 | E.isLogin = function() {
|
6 | if (localStorage.wd_login !== "true") {
|
7 |
|
8 | alert('You can not save & edit before login. Please login now !');
|
9 | window.location.href="login.html"
|
10 | return false;
|
11 | }
|
12 | return true;
|
13 | }
|
14 |
|
15 | E.Server.save=function(path, text) {
|
16 | if (!path.startsWith("/")) path="/"+path;
|
17 | console.log("save");
|
18 | $.ajax({
|
19 | type: "POST",
|
20 | url: "file"+path,
|
21 | timeout: this.timeout,
|
22 | data: { text: text },
|
23 | statusCode: {
|
24 | 401: function() {
|
25 | localStorage.wd_login = "false";
|
26 | E.isLogin();
|
27 | }
|
28 | }
|
29 | })
|
30 | .done(function(data) {
|
31 | console.log("save success");
|
32 | alert("Save success!");
|
33 | })
|
34 | .fail(function() {
|
35 | alert("Save fail!");
|
36 | });
|
37 | }
|
38 |
|
39 | E.Server.login=function() {
|
40 | $.ajax({
|
41 | type: "POST",
|
42 | url: "/login",
|
43 | timeout: this.timeout,
|
44 | data: { user:$('#loginUser').val(), password:$('#loginPassword').val() },
|
45 | })
|
46 | .done(function(data) {
|
47 | localStorage.wd_login = "true";
|
48 | $('#loginMessage').html("Login success!");
|
49 | })
|
50 | .fail(function() {
|
51 | localStorage.wd_login = "false";
|
52 | $('#loginMessage').html("Login fail! Please try again.");
|
53 | });
|
54 | }
|
55 |
|
56 | E.Server.logout=function() {
|
57 | $.ajax({
|
58 | type: "POST",
|
59 | url: "/logout",
|
60 | timeout: this.timeout,
|
61 | data: {},
|
62 | })
|
63 | .done(function(data) {
|
64 | localStorage.wd_login = "false";
|
65 | alert( "Logout success!");
|
66 | E.switchPanel('panelShow');
|
67 | })
|
68 | .fail(function() {
|
69 | alert( "Logout fail!" );
|
70 | });
|
71 | }
|
72 |
|
73 | E.loginBoxHtml = '<div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="loginModalLabel">\
|
74 | <div class="modal-dialog modal-sm" role="document">\
|
75 | <div class="modal-content">\
|
76 | <div class="modal-header">\
|
77 | <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>\
|
78 | <h4 class="modal-title" id="loginModalLabel">Login</h4>\
|
79 | </div>\
|
80 | <div id="loginBox" class="modal-body">\
|
81 | <form class="form-signin" role="form">\
|
82 | <input type="text" id="loginUser" class="form-control" required autofocus data-mt="User" placeholder="Account"/>\
|
83 | <input type="password" id="loginPassword" class="form-control" required data-mt="Password" placeholder="Password"/>\
|
84 | <br/>\
|
85 | <label id="loginMessage"></label>\
|
86 | </form>\
|
87 | </div>\
|
88 | <div class="modal-footer">\
|
89 | <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>\
|
90 | <button type="button" class="btn btn-primary" onclick="E.Server.login()">Login</button>\
|
91 | </div>\
|
92 | </div>\
|
93 | </div>\
|
94 | </div>';
|