From 3079df6d8c497ef293823ef2316674dc6c98d659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?agentzh=20=28=E7=AB=A0=E4=BA=A6=E6=98=A5=29?= Date: Mon, 25 Jan 2010 03:03:00 +0800 Subject: [PATCH] the blog demo works now. --- clients/js/openresty.js | 445 ++++++++++++++++++++++++++++ demo/Blog/js/blog.js | 628 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1073 insertions(+) create mode 100644 clients/js/openresty.js create mode 100644 demo/Blog/js/blog.js diff --git a/clients/js/openresty.js b/clients/js/openresty.js new file mode 100644 index 0000000..0b9e36e --- /dev/null +++ b/clients/js/openresty.js @@ -0,0 +1,445 @@ +var console, _log; +(function(){ +if(typeof console == 'undefined'){ + _log = function(a, b, c){/*alert('' + a + b + c)*/}; +}else{ + _log = function(){ + console.log.apply(console, arguments) + }; +} +})(); + + +if (typeof window.OpenResty == "undefined") { +(function() { +var undefined; +var browser = (function(){ + var u = navigator.userAgent.toLowerCase(), a = /(msie|firefox|camino|chrome|opera|safari|netscape|konqueror|lynx)/, b = /(msie|firefox|chrome|opera|safari|camino|netscape|netscape6|konqueror|lynx|version)(\/|\s)([a-z0-9\.\+]*?)(\;|dev|rel|\s|$)/; + var un = 'unknown', x = 'X', r = { + name: (a.exec(u) || [un, un])[1] + }; + r[r.name] = true; + r.version = (b.exec(u) || [x, x, x, x])[3]; + return r; +})(); + +var OpenResty = { + callbackMap: {}, + isDone: {}, + counter: 0 +}; + +OpenResty.Client = function (params) { + if (params == undefined) params = {}; + this.callback = params.callback; + var server = params.server; + if (!/^https?:\/\//.test(server)) { + server = 'http://' + server; + } + this.server = server; + this.user = params.user; + //this.password = params.password; +}; + +var $ClientP = OpenResty.Client.prototype; +$ClientP.isSuccess = function (res) { + // alert(JSON.stringify(res)); + return !(typeof res == 'object' && res.errcode); +}; + +$ClientP.logout = function () { + this.get('/=/logout'); +}; + +$ClientP.login = function (user, password) { + this.user = user; + var userCallback = this.callback; + if (typeof userCallback == 'string') { + userCallback = eval(userCallback); + } + + var self = this; + this.callback = function (data) { + //alert(data.session); + self.session = data.session; + userCallback(data); + }; + if (password == null) + password = ''; + else + password = hex_md5(password); + //this.callback = 'save_session'; + this.get('/=/login/' + user + '/' + password); +}; + +$ClientP.postByGet = function (url) { + var args, content; + if (arguments.length == 3) { + args = arguments[1]; + content = arguments[2]; + } else { + content = arguments[1]; + } + if (!args) args = {}; + url = url.replace(/^\/=\//, '/=/post/'); + content = JSON.stringify(content); + //alert("type of content: " + typeof(content)); + //alert("content: " + content); + args._data = content; + this.get(url, args); +}; + +$ClientP.genId = (function (n) { + //return ( Math.random() * 1000000 ); + return function() { return n++ }; + //return this.counter++; +})(new Date().getTime()); + +/* + post: for large data transform + crossdomain ~ y/n + + _post_by_form({ + form: form, + url: fullURL, + content: { data: content }, + preventCache: true, + method: "post", + handleAs: 'html', + handle: function () { + self.get('/=/last/response/' + args._last_response); + } + }); +*/ + + +var _post_forms_div, + _post_forms = {}; + +OpenResty.Client._post_forms = _post_forms; + +$ClientP._post_by_form = function (url, content, args) { + if (!_post_forms_div) { + _post_forms_div = document.createElement('div'); + var _s = _post_forms_div.style; + _s.position = 'absolute'; + _s.left = '-1000px'; + _s.width = '500px'; + _s.height = '1px'; + _s.overflow = 'hidden'; + _s = null; + document.body.appendChild(_post_forms_div); + } + + var reqId = args._last_response; + + if (reqId in _post_forms) { + throw "the post already exist!!"; + } + + var self = this; + var obj = { + reqId: reqId + }; + _post_forms[reqId] = obj; + var submited = false; + // var onloadCount = 0; + obj.onload = function() { + if (!submited) return; + + // alert('contentLoad ' + (++onloadCount) + ' times\n' + this); + + var _iframe = obj.iframe; + + // maybe we can process response under same domain + + delete obj.onload; + + _post_forms_div.removeChild(_iframe); + var _form = obj.form; + _post_forms_div.removeChild(obj.form); + + _iframe = null; + _form = null; + + delete obj.iframe; + delete obj.form; + + delete _post_forms[reqId]; + + obj = null; + + self.get('/=/last/response/' + reqId); + }; + + var form_id = 'OpenResty_form_' + reqId, + iframe_id = 'OpenResty_iframe_' + reqId; + + var _5 = 'http://ajax.googleapis.com/ajax/libs/dojo/1.3.2/dojo/resources/blank.html'; + + var _iframe; + var _6 = browser.msie ? '