var m2AjaxParameters = 'm2Parameters'; var m2AjaxMarkerUrlParameter = 'm2MarkerId'; /* ** An m2Ajax allows HTML fragments to be loaded into a DOM Node ** ** @param _containerNode - DOM node to contain loaded fragments ** @param _name - optional descriptive _name to be included in the _name of ** generated tags */ m2Ajax = function(_containerNode, _name) { this._containerNode = _containerNode; this._options = new Object(); this._options.asynchronous = true; this._options.evalScripts = true; this._markerTag = 'div'; this._parameters = new Object(); this.onError = this.defaultErrorAction; this._name = "m2ajax"; if (_name) { this._name = _name; } } /* ** Specifies the type of the Marker and replace element ** Defaults to 'div' ** ** @param tag - _name of HTML tag element, e.g. 'div', 'span' ** ** @return self */ m2Ajax.prototype.setMarkerTagType = function(_tag) { this._markerTag = _tag; return this; } /* ** Sets an action to be performed when the fragment is loaded ** ** @param action - function to call ** ** @return self */ m2Ajax.prototype.setOnComplete = function(_action) { this._options['onComplete'] = _action; return this; } /* ** Sets an action to be performed when the fragment fails to be loaded ** The action should expect 2 parameters ** markerDiv ** transport (an XMLHttpRequest or equivaltent) ** ** The default action puts an error message in the marker div ** ** @param action - function to call ** ** @return self */ m2Ajax.prototype.setOnError = function(_action) { this.onError = _action; return this; } /* ** Specifies a parameter to be passed to fragment ** ** @param _name - _name of parameter ** @param value - value of parameter ** ** @return self */ m2Ajax.prototype.setParameter = function(_name, _value) { this._parameters[_name] = _value; return this; } /* ** Loads the fragment into the container node ** ** @return DOM node that will contain the loaded fragment */ m2Ajax.prototype.setChild = function(_fragmentUrl) { var _markerNode = null; // search for previous marker node and reuse if its avalaible for (var i = 0; i < this._containerNode.childNodes.length; i++) { var node = this._containerNode.childNodes[i]; var id = new String(node.id); if (node.nodeName.toUpperCase() == this._markerTag.toUpperCase() && id.indexOf(this._name) == 0) { _markerNode = node; } } if (!_markerNode) { _markerNode = this._createMarkerNode(); this._containerNode.appendChild(_markerNode); } this.loadMarkerNode(_markerNode, _fragmentUrl, this._options); return _markerNode; } /* ** Appends the content returned by 'fragmentUrl' to the container ** associated with this m2Ajax ** ** @param _fragmentUrl - url of fragment to load ** ** @return DOM node that will contain loading fragment */ m2Ajax.prototype.appendChild = function(_fragmentUrl) { var _markerNode = this._createMarkerNode(); this._containerNode.appendChild(_markerNode); this.loadMarkerNode(_markerNode, _fragmentUrl, this._options); return _markerNode; } /* ** Loads the specified markerNode with 'fragmentUrl' ** Adds parameters to the url to inform the fragment of the marker id ** and indicuate the request is an ajax request - m2ajax=1 ** ** @param fragmentUrl - url of fragment to load ** ** @return DOM node that will contain loading fragment */ m2Ajax.prototype.loadMarkerNode = function(_markerNode, _fragmentUrl) { // attach parameters to div object - nasty, but simple _markerNode[m2AjaxParameters] = this._parameters var seperator = '?'; if (_fragmentUrl.indexOf("?") > 0) { seperator = '&'; } _fragmentUrl = _fragmentUrl + seperator + m2AjaxMarkerUrlParameter + '=' + _markerNode.id; _fragmentUrl = _fragmentUrl + '&m2ajax=1'; this._options['onFailure'] = function(transport) { this.onError(_markerNode, transport); } var _updater = new Ajax.Updater(_markerNode.id, _fragmentUrl, this._options); } /* ** Removes the specified child node */ m2Ajax.prototype.removeChild = function(_markerNode) { this._containerNode.removeChild(_markerNode); } /* ** Removes all child nodes */ m2Ajax.prototype.removeAllChildren = function() { while (this._containerNode.hasChildNodes()) { this._containerNode.removeChild(this._containerNode.firstChild); } } m2Ajax.prototype._createMarkerNode = function() { var _markerNodeId = this._generateMarkerNodeId(); var _markerNode = document.createElement(this._markerTag); _markerNode.setAttribute("id", _markerNodeId); return _markerNode; } m2Ajax.prototype._defaultErrorAction = function(markerDiv, transport) { markerDiv.innerHTML = 'm2Ajax error: ' + transport.status + ' -- ' + transport.statusText + ''; } m2Ajax.prototype._generateMarkerNodeId = function() { return this._name + "-" + (new Date()).getTime() + "-" + Math.floor(Math.random() * 999999); } /* ** Allows an m2AjaxFragment to interact with its loader ** ** @param markerId - the id of the marker, passed as a url parameter ** 'm2MarkerId' to the fragment. ** (it is expected you will use server side processing to get the ** id from the fragment url) */ m2AjaxFragment = function(_markerId) { this.markerDiv = document.getElementById(_markerId); } /* ** @return the marker div for this fragment */ m2AjaxFragment.prototype.getMarkerDiv = function() { return this.markerDiv; } /* ** Deletes the Marker div and all its content associated with this Fragment. */ m2AjaxFragment.prototype.deleteDiv = function() { this.markerDiv.parentNode.removeChild(this.markerDiv); } /* ** @return the value associated with the javascript m2Ajax parameter '_name' ** or null if not defined */ m2AjaxFragment.prototype.getParameter = function(_name) { if (!this.markerDiv || !this.markerDiv[m2AjaxParameters]) { return null; } return this.markerDiv[m2AjaxParameters][_name]; }