Skip to content

Commit

Permalink
isLogined move to mouf.js
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidz committed Dec 8, 2009
1 parent f370352 commit 783319a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 67 deletions.
67 changes: 13 additions & 54 deletions main.js
Expand Up @@ -108,6 +108,7 @@ taskpaper.parse = function(str){
return res;
}; // }}}

/*
taskpaper.isLogined = function(conn){
var req = conn.request;
if(conn.isOwner){
Expand All @@ -124,6 +125,7 @@ taskpaper.isLogined = function(conn){
else return mouf.session.check(sessid);
}
};
*/

(function(){
var getPageName = function(req){
Expand All @@ -141,7 +143,7 @@ taskpaper.isLogined = function(conn){

// main
var main = function(conn, req, res){
var logined = taskpaper.isLogined(conn);
var logined = mouf.session.isLogined(conn);//taskpaper.isLogined(conn);
var password = "";
if(conn.isOwner){
password = mouf.get(taskpaper.constant.PASSWORD_KEY, "");
Expand Down Expand Up @@ -173,7 +175,8 @@ taskpaper.isLogined = function(conn){
mouf.addHandler("_index", main);
mouf.addHandler("main", main);
mouf.addHandler("edit", function(conn, req, res){
if(taskpaper.isLogined(conn)){
//if(taskpaper.isLogined(conn)){
if(mouf.session.isLogined(conn)){
taskpaper.page = getPageName(req);
return mouf.render("template/edit.htm", {
page: taskpaper.page,
Expand Down Expand Up @@ -217,18 +220,16 @@ taskpaper.isLogined = function(conn){
mouf.addHandler("toggle_task", function(conn, req, res){
var result = "error";
if(req.method === "POST" && req.bodyItems.id && req.bodyItems.checked
&& req.bodyItems.page && taskpaper.isLogined(conn)){
&& req.bodyItems.page && mouf.session.isLogined(conn)){
//&& req.bodyItems.page && taskpaper.isLogined(conn)){
var id = parseInt(req.bodyItems.id[0]);
var checked = (req.bodyItems.checked[0] === "true") ? true : false;
var page = req.bodyItems.page[0];

taskpaper.page = page;
//var content = mouf.get(taskpaper.makeKeyName(), taskpaper.constant.DEFAULT_CONTENT);
//var newContent = [];
var newLine = null;
var index = -1;
var changed = false;

taskpaper.page = req.bodyItems.page[0];

taskpaper.mapContents(function(line){
if(line.indexOf("-") === 0) ++index;

Expand All @@ -240,24 +241,6 @@ taskpaper.isLogined = function(conn){
}
});

/*
mouf.each(content.split(/[\r\n]+/), function(){
if(mouf.trim(this).indexOf("-") === 0) ++index;
if(index !== id || changed){
newContent.push(this);
} else {
if(checked){
newLine = this + " #done";
} else {
newLine = this.replace("#done", "").replace(" ", " ");
}
newContent.push(newLine);
changed = true;
}
});
mouf.set(taskpaper.makeKeyName(), newContent.join("\n"));
*/
if(changed) result = "success";
}
return result;
Expand All @@ -267,19 +250,16 @@ taskpaper.isLogined = function(conn){
mouf.addHandler("toggle_group", function(conn, req, res){
var result = "error";
if(req.method === "POST" && req.bodyItems.id && req.bodyItems.opened
&& req.bodyItems.page && taskpaper.isLogined(conn)){
&& req.bodyItems.page && mouf.session.isLogined(conn)){
//&& req.bodyItems.page && taskpaper.isLogined(conn)){
var id = parseInt(req.bodyItems.id[0]);
var opened = (req.bodyItems.opened[0] === "true") ? true : false;
var page = req.bodyItems.page[0];

taskpaper.page = page;

//var content = mouf.get(taskpaper.makeKeyName(), taskpaper.constant.DEFAULT_CONTENT);
//var newContent = [];
var newLine = null;
var index = -1;
var changed = false;

taskpaper.page = req.bodyItems.page[0];

taskpaper.mapContents(function(line){
if(/[:;]\s*$/.test(line)) ++index;

Expand All @@ -291,27 +271,6 @@ taskpaper.isLogined = function(conn){
}
});
if(changed) result = "success";

/*
mouf.each(content.split(/[\r\n]+/), function(){
var trimedLine = mouf.trim(this);
if(/[:;]\s*$/.test(trimedLine)) ++index;
if(index !== id || changed){
newContent.push(this);
} else {
if(opened){
newLine = trimedLine.replace(/:\s*$/, ";");
} else {
newLine = trimedLine.replace(/;\s*$/, ":");
}
newContent.push(newLine);
changed = true;
}
});
mouf.set(taskpaper.makeKeyName(), newContent.join("\n"));
if(changed) result = "success";
*/
}
return result;
}); // }}}
Expand Down
97 changes: 84 additions & 13 deletions mouf.js
Expand Up @@ -4,23 +4,45 @@

var mouf = {};
mouf.handlers = [];
mouf.cache = {};

// =service {{{
mouf.service = {
path: opera.io.webserver.currentServicePath.substr(0,
opera.io.webserver.currentServicePath.length - 1),
name: opera.io.webserver.currentServiceName
}; // }}}

// =setting {{{
mouf.setting = {
SESSION_EXPIRE: 1000 * 60 * 10, // 10 min
SESSION_ID: "sessid",
ERROR_PAGE_TEMPLATE: null
};

mouf.service = {
path: opera.io.webserver.currentServicePath.substr(0, opera.io.webserver.currentServicePath.length - 1),
name: opera.io.webserver.currentServiceName
mouf.setting.sessionExpire = function(msec){
return (msec) ? (mouf.setting.SESSION_EXPIRE = msec) : mouf.setting.SESSION_EXPIRE;
};

// =addHandler
mouf.setting.errorPage = function(templateName){
return (templateName) ? (mouf.setting.ERROR_PAGE_TEMPLATE = templateName) : mouf.setting.ERROR_PAGE_TEMPLATE;
};

mouf.setting.sessionID = function(sid){
return (sid) ? (mouf.setting.SESSION_ID = sid) : mouf.setting.SESSION_ID;
};
// }}}

// =addHandler {{{
mouf.addHandler = function(path, fn){
this.handlers.push([path, function(event){
var connection = event.connection;
var request = connection.request;
var response = connection.response;

// use mouf.session.isLogined
mouf.cache.connection = connection;

try {
var result = fn(connection, request, response);
if(result) response.write(result);
Expand All @@ -40,13 +62,9 @@ mouf.addHandler = function(path, fn){
response.close();
}
}]);
};

// =setDefaultErrorPage
mouf.setDefaultErrorPage = function(templateName){
if(templateName) mouf.setting.ERROR_PAGE_TEMPLATE = templateName;
};
}; // }}}

// preference set/get {{{
// =set
mouf.set = function(key, value){
widget.setPreferenceForKey(value, key);
Expand All @@ -56,7 +74,9 @@ mouf.set = function(key, value){
mouf.get = function(key, defaultValue){
return widget.preferenceForKey(key) || (defaultValue ? defaultValue : "");
};
// }}}

// http get/post {{{
// =httpGet
mouf.httpGet = function(url, callback){
var xhr = new XMLHttpRequest();
Expand All @@ -77,6 +97,7 @@ mouf.httpPost = function(url, postData, callback){
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(postData);
};
// }}}

// =render
mouf.render = function(templateName, data){
Expand All @@ -91,7 +112,6 @@ mouf.location = function(response, url){
response.close();
};


// session manage {{{
mouf.session = {};
mouf.session.table = [];
Expand All @@ -114,7 +134,7 @@ mouf.session.makeKey = function(n, b){

// =session.check
mouf.session.check = function(sessionID){
var sess = mouf.session.get(sessionID);
var sess = (sessionID instanceof Array) ? sessionID : mouf.session.get(sessionID);
return (sess !== null
&& ((new Date).getTime() - sess[1].getTime()) < mouf.setting.SESSION_EXPIRE) ? true : false;
};
Expand All @@ -135,10 +155,26 @@ mouf.session.expire = function(sessionID){
return (this[0] !== sessionID);
});
};

// =isLogined
mouf.session.isLogined = function(connection){
var conn = (connection) ? connection : mouf.cache.connection;
var request = conn.request;

if(conn.isOwner){
return true;
} else {
var sessionID = mouf.objectGetValue(req.queryItems, mouf.setting.SESSION_ID);
if(sessionID === null){
sessionID = mouf.objectGetValue(req.bodyItems, mouf.setting.SESSION_ID);
}

return (sessionID === null) ? false : mouf.session.check(sessionID);
}
};
// }}}

// utility {{{

// =each
mouf.each = function(arr, fn){
if(arr instanceof Array){
Expand Down Expand Up @@ -199,6 +235,41 @@ mouf.clone = function(obj){
}
};

// =objectKeyLoop
mouf.objectKeyLoop = function(obj, fn){
if(typeof obj === "object"){
for(var key in obj){
var res = fn.apply(obj[key], [key]);
if(res === true) break;
else if(res === false) continue;
}
}
};

// =objectHasKey
mouf.objectHasKey = function(obj, key){
var res = false;
mouf.objectKeyLoop(obj, function(k){
if(k === key){
return (res = true);
}
});
return res;
};

// =objectGetValue
mouf.objectGetValue = function(obj, key){
var res = null;
mouf.objectKeyLoop(obj, function(k){
if(k == key){
res = this;
return true;
}
});
return res;
};


// =debug
mouf.debug = function(str){
opera.postError(str);
Expand Down

0 comments on commit 783319a

Please sign in to comment.