
if (typeof XDocRender == "undefined" || !XDocRender) {
    /**
     * The XDocRender global namespace object.  If XDocRender is already defined, the
     * existing XDocRender object will not be overwritten so that defined
     * namespaces are preserved.
     */
    var XDocRender = {};
}
/***
* miscellaneous data
***/
XDocRender.env = { 
"COMPONENTS_DB_NAME": "WebSite/Components.nsf",
"RENDER_DB_NAME": "Website/Renderer.nsf",
"LIBRARY_DB_NAME": "Website/library.nsf",
"SEARCH_DB_NAME": "Website/sessions.nsf",
"IMG_PATH": "/WebSite/Components.nsf/",
"LANGUAGE": "en",
"CACHEID_PARAM": ""
}
if (typeof(xdoc_cacheid)!="undefined"){
XDocRender.env.CACHEID_PARAM = "&cacheID="+xdoc_cacheid;
}
/***
* component prototypes
***/
XDocRender.components = [];
/***
* component images
***/
XDocRender.imagesBySrc = [];
/***
* component instances object
***/
XDocRender.componentInstances = {
/***
* available component list
***/
instancesList: [],
/***
* created component list by id
***/
instancesById: [],
/***
* component properties list by id
***/
instancePropertiesById: [],
/***
* component events by Id
***/
instanceEventsById: []
};
XDocRender.allDependencies = [];
XDocRender.events = [];
XDocRender.events["onComponentListRetrieved"] = new YAHOO.util.CustomEvent("onComponentListRetrieved");
/***
* XDocRender core functions
***/
XDocRender.core = {
componentParts: {
core: {
"partLabel" : "core",
"partType" : "JS",
"isMandatory" : true
},
defaultDisplayFunctions: {
"partLabel" : "display_functions",
"partType" : "JS",
"isMandatory" : false,
"configVar" : "useDefaultDisplayFunctions"
},
properties: {
"partLabel" : "properties_"+XDocRender.env.LANGUAGE,
"partType" : "JS",
"isMandatory" : false,
"configVar" : "useInternationalization"
},
defaultCSS: {
"partLabel" : "css",
"partType" : "CSS",
"isMandatory" : false,
"configVar" : "useDefaultCss"
}
},
customComponentParts: {
customCSS: {
"partLabel" : "customCss",
"partType" : "CSS",
"url" : "/"+XDocRender.env.RENDER_DB_NAME+"/(LookupCSSByName)/@@File@@?OpenDocument"+XDocRender.env.CACHEID_PARAM
},
customJsLibrary: {
"partLabel" : "customJsLibrary",
"partType" : "JS",
"url" : "/"+XDocRender.env.RENDER_DB_NAME+"/(LookupJSLibrariesByName)/@@File@@?OpenDocument"+XDocRender.env.CACHEID_PARAM
}
},
urls: {
componentListUrl: "/"+XDocRender.env.COMPONENTS_DB_NAME+"/JSONComponentsList?ReadForm"+XDocRender.env.CACHEID_PARAM,
componentConfigurationUrl: "/"+XDocRender.env.COMPONENTS_DB_NAME+"/ComponentsByID/@@ComponentId@@?OpenDocument&ComponentPart=configuration"+XDocRender.env.CACHEID_PARAM,
JSComponentPartUrl: "/"+XDocRender.env.COMPONENTS_DB_NAME+"/ComponentProvider?ReadForm&ComponentType=@@ComponentType@@&ComponentPart=@@ComponentPart@@"+XDocRender.env.CACHEID_PARAM,
CSSComponentPartUrl: "/"+XDocRender.env.COMPONENTS_DB_NAME+"/ComponentCSS?ReadForm&ComponentType=@@ComponentType@@&ComponentPart=@@ComponentPart@@"+XDocRender.env.CACHEID_PARAM,
YUILibrariesUrl: xdoc_yuilibpath+"@@YUI_FILE@@?OpenFileResource"+XDocRender.env.CACHEID_PARAM
},
YUIextensions: {
"calendar": {
"name" : "xdoc_calendar",
"url" : "/"+XDocRender.env.COMPONENTS_DB_NAME+"/XDocCalendar.js?OpenPage"+XDocRender.env.CACHEID_PARAM
}
},
/***
* create all components of the current page
***/
creation: function(){
if (XDocRender.componentInstances.instancesList.length>0) return;
XDocRender.core.getComponentListAndCreateThem();
},
createCompo: function(args){
XDocRender.core.getConfigurationAndImportComponent(args["id"],args["type"]);
},
/***
* get components configuration and create them
***/
getComponentListAndCreateThem: function(){
var responseSuccess = function(o){
//instances list
XDocRender.componentInstances.instancesList = [];
try {
XDocRender.componentInstances.instancesList = YAHOO.lang.JSON.parse(o.responseText);
}
catch (e) {
//alert("JSON Parse failed!");
return;
}
//for each component, get configuration and create it
for (var i=0;i<XDocRender.componentInstances.instancesList.length;i++){
var instance = XDocRender.componentInstances.instancesList[i];
YAHOO.util.Event.onAvailable(instance["id"], XDocRender.core.createCompo, instance);
//create custom events for each instances
XDocRender.componentInstances.instanceEventsById[instance["id"]] = [];
XDocRender.componentInstances.instanceEventsById[instance["id"]]["onContentReady"] = new YAHOO.util.CustomEvent("onContentReady");
XDocRender.componentInstances.instanceEventsById[instance["id"]]["onRendererReady"] = new YAHOO.util.CustomEvent("onRendererReady");
}
XDocRender.events["onComponentListRetrieved"].fire();
}
var responseFailure = function(o){}
var callback = {   
success: responseSuccess,   
failure: responseFailure
}
YAHOO.util.Connect.asyncRequest('GET', this.urls["componentListUrl"], callback, null);
},
/***
* Get component configuration and import necessary files
* @param componentId : component Id
* @param componentType : component type
***/
getConfigurationAndImportComponent: function(componentId, componentType){
//Success
var responseSuccess = function(o){
//JSON response 
var config = {};
try {
config = YAHOO.lang.JSON.parse(o.responseText);
}
catch (e) {
//alert("JSON Parse failed!");
return;
}
var componentId = o.argument[0];
var componentType = o.argument[1];
XDocRender.core.importComponent(componentId, componentType, config);
}
//Failure
var responseFailure = function(o){   
//alert("No parameter file for component "+o.argument[0]);
};
var callback = {
success: responseSuccess,   
failure: responseFailure,
argument:[componentId,componentType]
}
YAHOO.util.Connect.asyncRequest('GET', this.urls["componentConfigurationUrl"].replace("@@ComponentId@@",componentId), callback, null);
},
/***
* import component
* @param componentId : component Id
* @param componentType : component type
* @param config : component config
***/
importComponent: function(componentId, componentType, config){
if (componentId=="filter") debugger;
if (XDocRender.componentInstances.instancePropertiesById[componentId]){
//componenant has already been created
return;
}
XDocRender.componentInstances.instancePropertiesById[componentId] = {};
var instance = XDocRender.componentInstances.instancePropertiesById[componentId];
if (!XDocRender.components[componentType]){
XDocRender.components[componentType] = function(){
};
}
//set 'class' attribute
document.getElementById(componentId).className = componentType;
//set component instance properties
instance["type"] = componentType;
instance["config"] = config;
instance["loaded"] = false;
instance["dependencies"] = [];
var jsFilesToImport = [];
var cssFilesToImport = [];
//component parts
for(var cmpntPart in this.componentParts){
//get component part properties
var partLabel = this.componentParts[cmpntPart]["partLabel"];
var partType = this.componentParts[cmpntPart]["partType"];
var isMandatory = this.componentParts[cmpntPart]["isMandatory"];
var configVar = this.componentParts[cmpntPart]["configVar"];
//is component part is mandatory, it has to be loaded
//otherwise check config
var toLoad = isMandatory;
if (!isMandatory){
toLoad = config["common"][configVar];
}
//if component part has to be loaded
if (toLoad){
var name = componentType+"_"+partLabel;
var file = this.urls[partType+"ComponentPartUrl"].replace("@@ComponentType@@",componentType);
file = file.replace("@@ComponentPart@@",partLabel);
if (partType=="CSS"){
cssFilesToImport.push(file);
}else{
//if we already tried to load dependency, don't need to retry
if (XDocRender.allDependencies[name]){
instance["dependencies"][name] = {"loaded": XDocRender.allDependencies[name]["loaded"]};
for(var requiredDep in XDocRender.allDependencies[name]["requirements"]){
instance["dependencies"][requiredDep] = {"loaded": XDocRender.allDependencies[requiredDep]["loaded"]};
}
}else{
instance["dependencies"][name] = {"loaded": false};
jsFilesToImport.push({"name":name, "file":file, "cmpntPart": partLabel});
}
}
}
}
//custom component parts
for(var cmpntPart in this.customComponentParts){
//get component part properties
var partLabel = this.customComponentParts[cmpntPart]["partLabel"];
var partType = this.customComponentParts[cmpntPart]["partType"];
var url = this.customComponentParts[cmpntPart]["url"];
//if custom part is set in config
if (config["common"][partLabel]!=""){
var name = componentType+"_"+partLabel+"_"+config["common"][partLabel];
var file = url.replace("@@File@@",config["common"][partLabel]);
if (partType=="CSS"){
cssFilesToImport.push(file);
}else{
//if we already tried to load dependency, don't need to retry
if (XDocRender.allDependencies[name]){
instance["dependencies"][name] = {"loaded": XDocRender.allDependencies[name]["loaded"]};
for(var requiredDep in XDocRender.allDependencies[name]["requirements"]){
instance["dependencies"][requiredDep] = {"loaded": XDocRender.allDependencies[requiredDep]["loaded"]};
}
}else{
instance["dependencies"][name] = {"loaded": false};
jsFilesToImport.push({"name":name, "file":file, "cmpntPart": partLabel});
}
}
}
}
//import files
for (var i=0; i<jsFilesToImport.length; i++){
if (!XDocRender.allDependencies[jsFilesToImport[i]["name"]]){
//add dependency in allDependencies array
XDocRender.allDependencies[jsFilesToImport[i]["name"]] = {"loaded": false, "requirements": []};
XDocRender.util.File.importJSFile(jsFilesToImport[i]["file"]);
}
}
for (var i=0; i<cssFilesToImport.length; i++){
XDocRender.util.File.importCSSFile(cssFilesToImport[i]);
}
var isReady = true;
for(var dep in instance["dependencies"]){
if (!instance["dependencies"][dep]["loaded"]){
isReady=false;
break;
}
}
if (isReady){
XDocRender.componentInstances.instancePropertiesById[componentId]["loaded"] = true;
if (!XDocRender.componentInstances.instancesById[componentId]){
XDocRender.componentInstances.instancesById[componentId] = new XDocRender.components[componentType]();
XDocRender.componentInstances.instancesById[componentId].addComponent(componentId, config);
XDocRender.componentInstances.instancesById[componentId]["type"] = componentType;
}
}
},
/***
* test the existence of a component
* @param componentId : component Id
***/
exist: function(componentId){
return (XDocRender.componentInstances.instancesById[componentId]!=null);
},
/***
* get component by its id
* @param componentId : component Id
* return component
***/
getComponentById: function(componentId){
return XDocRender.componentInstances.instancesById[componentId];
},
/***
* fires when a component part is loaded
* @param cmpntPart : component part
***/
onXDocComponentLoad: function(cmpntPart){
if (XDocRender.allDependencies[cmpntPart.name]){
//if dependency already loaded
if (XDocRender.allDependencies[cmpntPart.name]["loaded"]) return;
XDocRender.allDependencies[cmpntPart.name]["loaded"] = true;
//for each component, check if dependency is needed and update "loaded" properties
for(var componentName in XDocRender.componentInstances.instancePropertiesById){
var cmpntProperties = XDocRender.componentInstances.instancePropertiesById[componentName];
var loaded = true;
if (!cmpntProperties["loaded"]){
if (cmpntProperties["dependencies"][cmpntPart.name]){
cmpntProperties["dependencies"][cmpntPart.name]["loaded"] = true;
for(var part in cmpntProperties["dependencies"]){
if (!cmpntProperties["dependencies"][part]["loaded"]){
loaded = false;
break;
}
}
//if all parts of components are loaded, we can create component
if (loaded){
cmpntProperties["loaded"] = true;
if (!XDocRender.componentInstances.instancesById[componentName]){
XDocRender.componentInstances.instancesById[componentName] = new XDocRender.components[cmpntProperties["type"]]();
XDocRender.componentInstances.instancesById[componentName].addComponent(componentName, cmpntProperties["config"]);
XDocRender.componentInstances.instancesById[componentName]["type"] = cmpntProperties["type"];
}
}
}
}
}
}
},
/***
* add dependency to XDoc
* @param label : dependency label
* @param url : dependency url
* @param componentType : component which need to load the dependency
* @param componentPart : component part in which we use the dependency
* @param extension : [optional] xdoc extension
***/
addDependency: function(label, url, componentType, componentPart, extension){
var depName = componentType+"_"+componentPart;
//if we have already tried to load this dependency, we update its loaded status in each component which use it
if (XDocRender.allDependencies[label]){
XDocRender.allDependencies[label]["requiredBy"][depName] = "";
XDocRender.allDependencies[depName]["requirements"][label] = "";
for(var requiredBy in XDocRender.allDependencies[label]["requiredBy"]){
for(var componentName in XDocRender.componentInstances.instancePropertiesById){
var cmpntProperties = XDocRender.componentInstances.instancePropertiesById[componentName];
if (cmpntProperties["dependencies"][requiredBy]){
cmpntProperties["dependencies"][label] = {"loaded": XDocRender.allDependencies[label]["loaded"]};
}
}
}
if (extension!=null) this.addExtension(extension, componentType ,componentPart);
}else{
//initialize loaded status
XDocRender.allDependencies[label] = {"loaded": false, "requiredBy":[]};
XDocRender.allDependencies[label]["requiredBy"][depName] = "";
XDocRender.allDependencies[depName]["requirements"][label] = "";
for(var componentName in XDocRender.componentInstances.instancePropertiesById){
var cmpntProperties = XDocRender.componentInstances.instancePropertiesById[componentName];
if (cmpntProperties["dependencies"][depName]){
//initialize loaded status in component
cmpntProperties["dependencies"][label] = {"loaded": false};
}
}
if (extension!=null) this.addExtension(extension, componentType ,componentPart);
//import dependency file (if not an extension)
XDocRender.util.File.importJSFile(url);
}
},
/***
* add extension
* @param label : dependency label
* @param componentPart : component part in which we use the dependency
***/
addExtension: function(label, componentType, componentPart){
var depName = componentType+"_"+componentPart;
//if we have already tried to load this dependency, we update its loaded status in each component which use it
if (XDocRender.allDependencies[label]){
for(var componentName in XDocRender.componentInstances.instancePropertiesById){
var cmpntProperties = XDocRender.componentInstances.instancePropertiesById[componentName];
if (cmpntProperties["type"] == componentType){
if (cmpntProperties["dependencies"][componentType+"_"+componentPart]){
cmpntProperties["dependencies"][label] = {"loaded": XDocRender.allDependencies[label]["loaded"]};
}
}
}
}else{
//initialize loaded status
XDocRender.allDependencies[label] = {"loaded": false};
XDocRender.allDependencies[depName]["requirements"][label] = "";
for(var componentName in XDocRender.componentInstances.instancePropertiesById){
var cmpntProperties = XDocRender.componentInstances.instancePropertiesById[componentName];
if (cmpntProperties["type"] == componentType){
if (cmpntProperties["dependencies"][componentType+"_"+componentPart]){
//initialize loaded status in component
cmpntProperties["dependencies"][label] = {"loaded": false};
}
}
}
}
},
/***
* import XDoc extension of YUI component. Fires when yui components are loaded.
* @param yui_cmpnt : yui component label
***/
importXDocExtension: function(yui_cmpnt){
var ext = this.YUIextensions[yui_cmpnt];
if (!ext) return;
if (XDocRender.allDependencies[ext["name"]]){
if (!XDocRender.allDependencies[ext["name"]]["loaded"]){
XDocRender.util.File.importJSFile(ext["url"]);
}
}
}
};
