forked from x/ContextOS
145 lines
3.1 KiB
JavaScript
145 lines
3.1 KiB
JavaScript
ctx = {
|
|
Cookie: function(key, value) {//{{{
|
|
if (value == undefined) {
|
|
var pattern = new RegExp(key+"=([^;]*);?");
|
|
var result = pattern.exec(document.cookie);
|
|
if (result && result.length > 0) {
|
|
return result[1];
|
|
}
|
|
return "";
|
|
}
|
|
|
|
document.cookie = key+"="+value;
|
|
return this.Cookie(key);
|
|
},//}}}
|
|
Search: function(key, value) {//{{{
|
|
var args = {};
|
|
var search = location.search.split("?");
|
|
if (search.length > 1) {
|
|
var searchs = search[1].split("&");
|
|
for (var i = 0; i < searchs.length; i++) {
|
|
var keys = searchs[i].split("=");
|
|
args[keys[0]] = decodeURIComponent(keys[1]);
|
|
}
|
|
}
|
|
|
|
if (typeof key == "object") {
|
|
for (var k in key) {
|
|
if (key[k] != undefined) {
|
|
args[k] = key[k];
|
|
}
|
|
}
|
|
} else if (value == undefined) {
|
|
return args[key];
|
|
} else {
|
|
args[key] = value;
|
|
}
|
|
|
|
var arg = [];
|
|
for (var k in args) {
|
|
arg.push(k+"="+encodeURIComponent(args[k]));
|
|
}
|
|
location.search = arg.join("&");
|
|
},//}}}
|
|
POST: function(url, form, cb) {//{{{
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.onreadystatechange = function() {
|
|
switch (xhr.readyState) {
|
|
case 4:
|
|
switch (xhr.status) {
|
|
case 200:
|
|
var msg = JSON.parse(xhr.responseText||'{"result":[]}');
|
|
msg && console.log(msg)
|
|
msg.result && console.log(msg.result.join(""));
|
|
typeof cb == "function" && cb(msg)
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
xhr.open("POST", url);
|
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
|
|
form = form || {}
|
|
form["dir"] = form["dir"] || this.Search("dir") || undefined
|
|
form["module"] = form["module"] || this.Search("module") || undefined
|
|
form["domain"] = form["domain"] || this.Search("domain") || undefined
|
|
|
|
var args = [];
|
|
for (k in form) {
|
|
if (form[k] != undefined) {
|
|
args.push(k+"="+encodeURIComponent(form[k]));
|
|
}
|
|
}
|
|
|
|
var arg = args.join("&");
|
|
console.log("POST: "+url+"?"+arg);
|
|
xhr.send(arg);
|
|
},//}}}
|
|
|
|
Cap: function(cap, cb) {//{{{
|
|
if (typeof cap == "function") {
|
|
cb = cap;
|
|
cap = undefined;
|
|
}
|
|
|
|
var args = {ccc:"cache"};
|
|
if (cap != undefined) {
|
|
args.name = cap;
|
|
}
|
|
|
|
this.POST("", args, function(msg) {
|
|
var value = msg.result.join("");
|
|
typeof cb == "function" && cb(value);
|
|
});
|
|
},//}}}
|
|
Conf: function(conf, value, cb) {//{{{
|
|
if (typeof conf == "function") {
|
|
value = conf;
|
|
conf = undefined;
|
|
}
|
|
if (typeof value == "function") {
|
|
cb = value;
|
|
value = undefined;
|
|
}
|
|
|
|
var args = {ccc:"config"};
|
|
if (name != undefined) {
|
|
args.name = name
|
|
}
|
|
if (value != undefined) {
|
|
args.value = value
|
|
}
|
|
|
|
this.POST("", args, function(msg) {
|
|
var value = msg.result.join("");
|
|
typeof cb == "function" && cb(value);
|
|
});
|
|
},//}}}
|
|
Cmd: function(cmd, value, cb) {//{{{
|
|
if (typeof cmd == "function") {
|
|
value = cmd;
|
|
cmd = undefined;
|
|
}
|
|
if (typeof value == "function") {
|
|
cb = value;
|
|
value = undefined;
|
|
}
|
|
|
|
var args = {ccc:"command"};
|
|
if (cmd != undefined) {
|
|
args.name = cmd
|
|
}
|
|
if (value != undefined) {
|
|
args.value = value
|
|
}
|
|
|
|
this.POST("", args, cb);
|
|
},//}}}
|
|
Module: function(module, domain) {//{{{
|
|
this.Search({module:module, domain:domain})
|
|
},//}}}
|
|
};
|
|
|