dojo.provide("dojo.widget.html.loader");
dojo.require("dojo.widget.HtmlWidget");
dojo.require("dojo.io.*");
dojo.require("dojo.lang.extras");
dojo.require("dojo.experimental");
// as this is a singleton dojo.declare doesnt by us anything here
dojo.widget.html.loader = new (function(){
// summary:
// loading stuff moved out of contentpane to make it directly accessible by other widgets
this.toString = function(){ return "dojo.widget.html.loader"; }
var _loader = this;
// back/forward tracking
dojo.addOnLoad(function(){
dojo.experimental(_loader.toString());
var undo = dojo.evalObjPath("dojo.undo.browser");
if(djConfig["preventBackButtonFix"] && undo && !undo.initialState){
undo.setInitialState(new trackerObj);
}
});
var logger = {};
var trackerObj = function(id, data){
this.id = id;
this.data = data
};
trackerObj.prototype.handle = function(type){
if(typeof dojo == 'undefined'){ return; } // wtf? how can dojo become undef?
var wg = dojo.widget.byId(this.id);
if(wg){ wg.setContent(this.data, true); }
};
this._log = function(widget, data){
// if a loader widget B is a child of loader widget A
// we need to destroy all of B's undo if we switch content
if(widget.trackHistory){
if(!logger[widget.widgetId]){
logger[widget.widgetId] = { childrenIds: [], stack:[data] };
}var children = logger[widget.widgetId].childrenIds;
while(children && children.length){
delete logger[children.pop()];
}
for(var child in widget.children){
logger[widget.widgetId].childrenIds = child.widgetId;
}
dojo.undo.browser.addToHistory(new trackerObj(widget.widgetId, dojo.lang.shallowCopy(data, true)));
}
}
// shortCuts
var undef = dojo.lang.isUndefined;
var isFunc = dojo.lang.isFunction;
/************ private needed functions, no need to be part of widget API ***********/
// usefull if user wants to prevent default behaviour ie: _setContent("Error...")
function handleDefaults(e, handler, useAlert){
if(!handler){ handler = "onContentError"; }
if(dojo.lang.isString(e)){ e = {_text: e}; }
if(!e._text){ e._text = e.toString(); }
e.toString = function(){ return this._text; };
if(typeof e.returnValue != "boolean"){
e.returnValue = true;
}
if(typeof e.preventDefault != "function"){
e.preventDefault = function(){ this.returnValue = false; };
}
// call our handler
this[handler](e);
if(e.returnValue){
if(useAlert){
alert(e.toString());
}else{
this.loader.callOnUnLoad.call(this, false);
this.onSetContent(e.toString());
}
}
};
// set up downloader, used by both scripts and content
function downloader(bindArgs) {
for(var x in this.bindArgs){
bindArgs[x] = (undef(bindArgs[x]) ? this.bindArgs[x] : undefined);
}
var cache = this.cacheContent;
if(undef(bindArgs.useCache)){ bindArgs.useCache = cache; }
if(undef(bindArgs.preventCache)){ bindArgs.preventCache = !cache; }
if(undef(bindArgs.mimetype)){ bindArgs.mimetype = "text/html"; }
this.loader.bindObj = dojo.io.bind(bindArgs);
};
// runs addOnLoad/addOnUnLoad functions
function stackRunner(st){
var err = "", func = null;
var scope = this.scriptScope || dojo.global();
while(st.length){
func = st.shift();
try{
func.call(scope);
}catch(e){
err += "\n"+func+" failed: "+e;
}
}
if(err.length){
var name = (st== this.loader.addOnLoads) ? "addOnLoad" : "addOnUnLoad";
handleDefaults.call(this, name+" failure\n "+err, "onExecError", true);
}
};
// push addOnLoad and addOnUnLoad functions onto stack
function stackPusher(st, obj, func){
if(typeof func == 'undefined') {
st.push(obj);
}else{
st.push(function(){ obj[func](); });
}
};
// code saver, collects onLoad, onResized and isLoaded
function refreshed(){
this.onResized();
this.onLoad();
this.isLoaded = true;
};
// runs scripts and starts the content parser
function asyncParse(data){
if(this.executeScripts){
this.onExecScript.call(this, data.scripts);
}
if(this.parseContent){
this.onContentParse.call(this);
}
refreshed.call(this);
};
// run java function
function runHandler(){
//FIXME: current behaviour is to return false if handler is there, is that intended?
if(dojo.lang.isFunction(this.handler)) {
this.handler(this, this.containerNode||this.domNode);
refreshed.call(this);
return false;
}
return true;
};
// divided up splitAndFixPaths in different parts
this.htmlContentBasicFix = function(/*string*/s, /*string||dojo.uri.Uri*/url){
// summary:
// strips out