/**
* apply method »ç¿ë
* À̺¥Æ®¹ß»ý½Ã ÇÔ¼ö¿¡ Àü´ÞÇÒ¶§ Ŭ·¡½º¾ÈÀÇ this ¸¦ Æ÷ÇÔÇÑ´Ù.
*/
Function.prototype.bind = function()
{
for ( var i = 0, method = this , args = [] , len = arguments.length ; i < len ; i++ )
{
args.push( arguments[ i ] );
}
return function()
{
return method.apply( args[0] , args.slice(1) );
}
}
function debug(o, options)
{
// check
if( o == null ) return;
if( typeof options == 'undefined' ) options = {pos: false};
// process
if( typeof o == 'string' || typeof o == 'number' )
{
var debugStr = o;
}
else
{
// process #2
var debugStr = '
';
for( i in o )
{
//if( o[i] == '[object HTMLFormElement]' ) continue;
debugStr += '| ' + i + ' | ' + o[i] + ' | ' + typeof o[i] + ' |
';
if( options.optSub == 1 )
{
if( typeof o[i] == 'object' )
{
for( j in o[i] )
{
debugStr += '======>' + j + '=' + o[i][j] + '
';
}
}
}
}
debugStr += '
';
if( options.cookie == true ) debugStr += 'document.cookie:' + document.cookie + '
';
}
var divObj = document.createElement('div');
divObj.id = 'debug';
if( options.pos == true )
{
divObj.style.position = 'absolute';
divObj.style.top = ( options.top || 0 ) + 'px';
divObj.style.left = ( options.left || 500 ) + 'px';
divObj.style.zIndex = options.zIndex || 10000;
}
divObj.innerHTML = debugStr;
document.body.appendChild(divObj);
}
var xhr = {
instance: false,
init: function()
{
if( typeof XMLHttpRequest != 'undefined' )
{
this.instance = new XMLHttpRequest();
}
if( !this.instance )
{
try
{
this.instance = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
this.instance = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
this.instance = false;
}
}
}
return this.instance;
}
}
/**
* ¿ÀºêÁ§Æ® È®Àå
* @param {Object} destination
* @param {Object} source
* @return {Object}
* @author ¹Ú³ÇÏ nanhap@gmail.com
* @date 2007-03-09
*/
Object.extend = function(destination, source)
{
for(var property in source)
{
destination[property] = source[property];
}
return destination;
}
/**
* Ajax
* @author ¹Ú³ÇÏ
* @date 2007-02-22 ¿ÀÈÄ 5:46
*/
var Ajax = {
Events: ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'], // readyState ¿¡ ´ëÇÑ À̺¥Æ®¸í
setOptions: {
method: 'post',
asynchronous: true,
contentType: 'application/x-www-form-urlencoded',
encoding: 'UTF-8',
parameters: ''
}, // ±âº» ¿É¼Ç
/**
* ½ÇÇàÇÔ¼ö
* @param {String} url
* @param {Object object} options ¿É¼Ç JSON
*/
init: function(url, options)
{
this.url = url;
this.xhrObj = xhr.init();
Object.extend(this.setOptions, options || {});
this.request();
},
/**
* ¿äû
*/
request: function()
{
// when GET, append parameters to URL
//var params = encodeURIComponent(this.setOptions.parameters);
var params = this.setOptions.parameters;
if( this.setOptions.method == 'get' && params )
this.url += (this.url.indexOf('?') > -1 ? '&' : '?') + params;
try {
if(typeof this.onCreate == 'function') this.onCreate(); // ÃÖÃÊÇÔ¼ö ½ÇÇà
this.xhrObj.open(this.setOptions.method, this.url, this.setOptions.asynchronous);
this.xhrObj.onreadystatechange = this.onStateChange.bind(this);
this.header();
var body = this.setOptions.method == 'post' ? (this.setOptions.postBody || params) : null;
this.xhrObj.send(body);
/* Force Firefox to handle ready state for synchronous requests */
if( !this.setOptions.asynchronous && this.xhrObj.overrideMimeType )
this.onStateChange();
}
catch(e) {
debug(e);
}
},
/**
* Çì´õ¼±¾ð
*/
header: function()
{
var headers = {};
headers['X-Requested-With'] = 'XMLHttpRequest'; //ajax ¿äû¿©ºÎ heade°ª Ãß°¡
if(this.setOptions.method == 'post')
{
headers['Content-Type'] = this.setOptions.contentType + ( this.setOptions.encoding ? '; charset=' + this.setOptions.encoding : '' );
}
for(var name in headers)
this.xhrObj.setRequestHeader(name, headers[name]);
},
/**
* monitor
*/
onStateChange: function()
{
//if( this.xhrObj.readyState > 1 && !( this.xhrObj.readyState == 4 && this._complete ) )
if( this.xhrObj.readyState > 1 )
this.respond(this.xhrObj.readyState);
},
/**
* ¿äû ¿Ï·á
*/
success: function() {
return !this.xhrObj.status
|| (this.xhrObj.status >= 200 && this.xhrObj.status < 300);
},
/**
* ¿äû»óź° üũ
* @param {String} readyState
*/
respond: function(readyState)
{
var state = this.Events[readyState];
if( state == 'Complete' )
{
this._complete = true;
var json = this.evalJSON();
if( typeof this.setOptions.onComplete == 'function' )
{
this.setOptions.onComplete(this.xhrObj, json);
}
}
},
/**
* ¿äûÇÑ URL ¿¡ ´ëÇÑ Çì´õ¾òÀ½
* @param {String} name Çì´õ ±¸ºÐ°ª
* @return {Boolen} true/false
*/
getHeader: function(name) {
try {
return this.xhrObj.getResponseHeader(name);
}
catch(e) { debug(e); }
return true;
},
/**
* JSON °ªÀ» °¡Á®¿È
*/
// return json type
evalJSON: function() {
try {
var json = this.getHeader('X-JSON');
return json ? eval('(' + json + ')') : null;
} catch (e) { return null }
}
}