Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // xhr request object
- var XHR = {
- indicator: 'load_indicator', // the element id of the load indicator
- active: false, // the state of the request system
- pending: 0, // the current number of uncomplete requests
- current: {}, // a place to keep track open requests
- create: function() {
- if (XHR.active == false) if ($(XHR.indicator)) $(XHR.indicator).className = 'loading_active';
- XHR.pending++; // increment the pending requests counter
- XHR.active = true; // set the request handler state to active
- },
- complete: function(key) {
- XHR.pending--; // reduce the number of pending requests by 1
- // if there are no more pending requests
- if (XHR.pending == 0)
- {
- XHR.active = false; // set the request handler state to inactive
- if ($(XHR.indicator)) $(XHR.indicator).className = 'loading_inactive'; // hide the load indicator
- }
- this.current[ key ]['stop'] = parseInt(new Date().getTime().toString().substring(0, 10));
- setTimeout('XHR.cleanUp()', 30000);
- },
- init: function(url) {
- var parts = url.split("?"); // split the url into address / parameter data
- var url = parts[0]; // the url is in part 0
- var params = {}; // set params to nothing
- if (parts[1]) // if there appears to be a parameter string, handle it
- {
- params = parts[1].toQueryParams(); // convert the parameter string to an object
- if (params['f'] != undefined) // if there appears to be a form id
- {
- if ($(params['f'])) // if the form actually exists
- {
- var form_params = $(params['f']).serialize(true); // serialize the form
- for (i in form_params) params[i] = form_params[i]; // add the form params to the other params
- }
- }
- }
- params['requested_via_ajax'] = true; // add a value to signify that the request if being handled using ajax
- var start = parseInt(new Date().getTime().toString().substring(0, 10));
- var key = md5(start+url); // handle for this request
- // add the request to the current object
- this.current[ key ] = { start:start, stop:0, url:url, params:params, ajax:null, response:null };
- return key;
- },
- // perform an ajax request
- load: function(url, flag, function) {
- var key = this.init(url); // parse the url and create a new record
- var flag = flag || 'update'; // default action is update
- XHR.current[ key ]['ajax'] = new Ajax.Request(XHR.current[ key ]['url'], {
- parameters: XHR.current[ key ]['params'],
- onCreate: function() { XHR.create(); },
- onSuccess: function(transport) {
- XHR.current[ key ]['response'] = transport.responseText;
- if (flag == 'json')
- return transport.responseText;
- else
- XHR.update(key); // otherwise use the response to update the document
- },
- onFailure: function() { alert('failed!') },
- onComplete: function() {
- XHR.complete(key);
- return XHR.current[ key ]['response'];
- }
- });
- },
- update: function(key) {
- var element = 'viewport'; // set the default element to update to the viewport div
- var content = false; // set the content to false
- var data = false; // set the data to false
- var js = false; // set the js to false
- // check to see if the element to update was provided in the parameter string
- if (XHR.current[ key ] != undefined)
- if (XHR.current[ key ]['params']['e'] != undefined) element = XHR.current[ key ]['params']['e'];
- // if the request returns JSON data
- if (XHR.current[ key ]['response'].isJSON())
- {
- // evaluate the json response
- var json = XHR.current[ key ]['response'].evalJSON();
- // if an element is supplied in the response it supercedes the element from the parameter string
- if (json['e'] != undefined) element = json['e'];
- // if there is content in the response update the appropriate element with the content
- if (json['content'] != undefined) if ($(element)) $(element).update(json['content']).show();
- // if there is data in
- if (json['data'] != undefined) data = jason['data'];
- // if there is any javascript, supply it with any response data and evaluate it
- if (json['js'] != undefined) eval("var data = "+data+";"+json['js']);
- }
- // the response is not JSON
- else
- {
- // update the contents of the element, then show it
- if ($(element)) $(element).update(XHR.current[ key ]['response']).show();
- }
- return true;
- },
- // clear completed requests from XHR.current
- cleanUp: function() {
- var now = parseInt(new Date().getTime().toString().substring(0, 10));
- for (key in XHR.current)
- {
- if (now - XHR.current[ key ]['stop'] > 10) delete XHR.current[ key ];
- }
- }
- }
Add Comment
Please, Sign In to add comment