/** =============================================================== XML PAGE GENERATOR v1.0 Copyright (c) 2009 Jason Fries, Iowa Electronic health Markets, University of Iowa description: Takes XMLdoc input and renders a html page. The XML input is stored as an external document under the editable pages home directory in the default Newsfutures installation {root}/raw/editable/{document_name} XML Format Example:
Since 2000, there has been a dramatic increase in the number of syphilis cases across the United States.
dependencies: IEhM_Utils.js revisions: 2009.07.14 IE6 compatible 2009.06.25 Code cleanup, header style support 2009.03.27 Rendering handled via callbacks now (event driven rendering caused issues) 2009.03.20 first release TODO: none Tested on Safari/WebKit 4.0+, Chrome, IE6+, FF3.5+ =============================================================== **/ function XMLPageLoader() { //XMLDoc - page content this.xml = null; this.element = document.createElement("DIV"); //parsed elements this.content = new Array(); //Ajax HttpService this.webservice = new HttpService(); //XMLHttpRequest Object this.request = false; //parse loaded XML this.XMLParser = this.parseXML; //once file is parsed call this function this.loadCompleteCallback; //page rendering function this.rendererCallback; //create Group child section function this.sectionCreator = XMLPageLoader.createSectionObject; //browser specific flags this.bFloat = "styleFloat"; this.bClass = "className"; this.opacity = "filter"; this.browserFixes(); }; XMLPageLoader.IMG_ROOT = NFSiteConfig.IMG_ROOT; /** ====================================== PUBLIC ====================================== **/ /** * Initalize XMLPageLoader * @description */ XMLPageLoader.prototype.load = function( filename ) { this.webservice.url = filename; //setup params to pass to event handler methods var parent = this; var success = this.resultHandler; var fault = this.faultHandler; this.webservice.addEventListener( HttpService.RESULT, function(response) { success(response, parent); } ); this.webservice.addEventListener( HttpService.FAULT, function(response) { fault(response, parent); } ); this.webservice.send(); }; /** * Render HTML * @description Create DOM page objects */ XMLPageLoader.prototype.render = function( targetElement, headerStyle ) { this.rendererCallback( targetElement, headerStyle ); }; /** ====================================== INTERNAL ====================================== **/ //IE and Firefox and Safari all reference style elements differnetly, necessitiating these tests XMLPageLoader.prototype.browserFixes = function() { if (document.all){ // very basic browser detection this.bFloat = "styleFloat"; //ie this.bClass = "className"; this.opacity = "filter"; }else{ this.bFloat = "cssFloat"; //firefox this.bClass = "class"; this.opacity = "opacity"; } }; /** * EVENT HANDLING **/ /** * Result OK Handler * @description Ajax response returns correcty */ XMLPageLoader.prototype.resultHandler = function( response, parent ) { parent.XMLParser( response ); }; /** * Result OK Handler * @description Ajax response returns correcty */ XMLPageLoader.prototype.faultHandler = function( response, parent ) { alert("XMLPageLoader Ajax ERROR! " + response); }; /** * Parse XML Document * * @description Parse default InformationXML format item * @param * @return */ XMLPageLoader.prototype.parseXML = function( xmlDoc ) { //root var root = xmlDoc.getElementsByTagName("information").item(0); //child groups var groups = root.getElementsByTagName("group"); for(var i=0; i < groups.length; i++){ var groupName = "", groupOrientation = ""; if(groups.item(i).attributes.getNamedItem("name")) groupName = groups.item(i).attributes.getNamedItem("name").value; if(groups.item(i).attributes.getNamedItem("orientation")) groupOrientation = groups.item(i).attributes.getNamedItem("orientation").value; //each group can consist of multiple link items var gSections = groups.item(i).getElementsByTagName("section"); var sections = new Array(); for(var j=0; j < gSections.length; j++){ var s = gSections.item(j); //group link descriptors var title = "",icon = "",body = "",url = ""; var width = -1; if(s.attributes.getNamedItem("name")) title = s.attributes.getNamedItem("name").value; if(s.attributes.getNamedItem("width")) width = parseInt(s.attributes.getNamedItem("width").value); if(s.getElementsByTagName("icon").item(0) && s.getElementsByTagName("icon").item(0).childNodes[0]){ icon = s.getElementsByTagName("icon").item(0).childNodes[0].data; icon = XMLPageLoader.IMG_ROOT + icon; //add NF search root } if(s.getElementsByTagName("body").item(0) && s.getElementsByTagName("body").item(0).childNodes[0]) body = s.getElementsByTagName("body").item(0).childNodes[0].data; if(s.getElementsByTagName("url").item(0) && s.getElementsByTagName("url").item(0).childNodes[0]) url = s.getElementsByTagName("url").item(0).childNodes[0].data; sections.push( {width:width, name:title, body:body, icon:icon, url:url} ); } this.content.push( {name:groupName, orientation:groupOrientation, sections:sections} ); } if(this.loadCompleteCallback) this.loadCompleteCallback(); }; /** * Parse XML Document * * @description * @param * @return */ XMLPageLoader.prototype.parseNFNewsXML = function( xmlDoc ) { //root var root = xmlDoc.getElementsByTagName("nfnews").item(0); //child groups var newsItems = root.getElementsByTagName("news"); for(var i=0; i < newsItems.length; i++){ var title = "", pubDateStr ="", source = "", url = ""; if(newsItems.item(i).attributes.getNamedItem("title")) title = newsItems.item(i).attributes.getNamedItem("title").value; if(newsItems.item(i).getElementsByTagName("source")){ source = newsItems.item(i).getElementsByTagName("source").item(0).childNodes[0].data; } if(newsItems.item(i).attributes.getNamedItem("url")) url = newsItems.item(i).attributes.getNamedItem("url").value; if(newsItems.item(i).attributes.getNamedItem("pubDateStr")) pubDateStr = newsItems.item(i).attributes.getNamedItem("pubDateStr").value; this.content.push( {title:title, pubDateStr:pubDateStr, source:source, url:url} ); } if(this.loadCompleteCallback) this.loadCompleteCallback(); }; /** ====================================== Page Specific Renderer Callbacks ====================================== **/ /** * NF Market News Page Renderer * */ XMLPageLoader.NFNewsPageRenderer = function( targetElement ) { var e = targetElement; this.element.setAttribute(this.bClass, "marketNews"); e.appendChild( this.element); for(var i=0; i 0) columns = 2; var section = new NFTable("",1,columns); section.element.setAttribute("class","infoSection"); section.element.setAttribute("className","infoSection"); section.element.style.width = "100%"; var img = document.createElement("IMG"); img.setAttribute("src", icon); //PNG files only. IE scales PNG's differnently (bug) than other img files //the img to it's real dimensions //img.setAttribute("width", dim.width + "px"); //img.setAttribute("height", dim.height +"px"); img.style.verticalAlign = "middle"; //don't include img if link is empty if(icon.length > 0) section.cell(0,0).appendChild(img); //title var title = document.createElement("A"); title.setAttribute("class", "linkTitle"); title.setAttribute("className", "linkTitle"); title.setAttribute("href", url); title.setAttribute("target", "_blank"); //open link in new window/tab title.appendChild( document.createTextNode(name) ); //description var body; //does description contain html tags? if(description.indexOf("<") != -1){ body = document.createElement("DIV"); body.innerHTML = description; }else{ body = document.createElement("P"); body.appendChild( document.createTextNode(description) ); } var link = document.createElement("A"); link.setAttribute("class", "linkURL"); link.setAttribute("className", "linkURL"); link.setAttribute("href", url); link.setAttribute("target", "_blank"); //open link in new window/tab link.appendChild( document.createTextNode(url) ); //append to last column section.cell(0,columns-1).appendChild(title); section.cell(0,columns-1).appendChild(body); section.cell(0,columns-1).appendChild(link); return section.element; }; /** * Information Page DOM Element Renderer * * @description Create DOM HTML object corresponding to a link group * @param name of section * @param description of section * @param [OPTIONAL] link to icon (if not provided this column is removed from the table) * @param url link to article * @return */ XMLPageLoader.createMediaSectionObject = function( name, description, icon, url ) { var rows = 1; //if no icon is present, remove columnn from table if(icon.length > 0) rows = 2; var section = new NFTable("",rows,1); section.element.setAttribute("class","infoSection"); section.element.setAttribute("className","infoSection"); section.element.style.width = "100%"; //don't include img if link is empty if(icon.length > 0){ var img = document.createElement("IMG"); img.setAttribute("src", icon); img.style.verticalAlign = "middle"; img.style.padding = "0px"; img.style.margin = "0px"; section.cell(0,0).appendChild(img); } //title var title = document.createElement("A"); title.setAttribute("class", "linkTitle"); title.setAttribute("className", "linkTitle"); title.setAttribute("href", url); title.setAttribute("target", "_blank"); //open link in new window/tab title.appendChild( document.createTextNode(name) ); //description var body = document.createElement("P"); body.appendChild( document.createTextNode(description) ); var link = document.createElement("A"); link.setAttribute("class", "linkURL"); link.setAttribute("className", "linkURL"); link.setAttribute("href", url); link.setAttribute("target", "_blank"); //open link in new window/tab // link.appendChild( document.createTextNode(url) ); //append to last column section.cell(rows-1,0).appendChild(title); section.cell(rows-1,0).appendChild(body); section.cell(rows-1,0).appendChild(link); return section.element; }; /** * Tournament Description Page Renderer * @description Create DOM HTML object corresponding to tournaments * description page. * @param target Element for rendering content * @return */ XMLPageLoader.trnmtPageRenderer = function( e ) { var padding = 10; //column pixel padding var currentHeight = 0; for(var i=0; i