Project

General

Profile

1
/*
2
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
3
$Id: WidgetBaseXSL.js 3892 2008-02-29 18:21:12Z ahocevar $
4
*/
5

    
6
mapbuilder.loadScript(baseDir+"/widget/WidgetBase.js");
7

    
8
/**
9
 * Base Class for widgets.  Associates a node on the page with a stylesheet and
10
 * model.  All widgets must extend this base class.
11
 * Defines the default paint() method for all widgets which is where the 
12
 * stylesheet is applied to the model XML document.
13
 * To override widget.paint(), define it before calling this constructor.
14
 * The stylesheet URL defaults to "widget/<widgetName>.xsl" if it is not defined
15
 * in config file.  Set a stylesheet property containing an XSL URL in config
16
 * to customize the stylesheet used.
17
 * All stylesheets will have "modelId" and "widgetId" parameters set when called.
18
 *
19
 * @constructor
20
 * @base WidgetBase
21
 * @author Mike Adair 
22
 * @param widget      Pointer to the widget instance being created
23
 * @param widgetNode  The widget's XML object node from the configuration document.
24
 * @param model       The model object that this widget belongs to.
25
 */
26
function WidgetBaseXSL(widgetNode,model) {
27
  // Extend WidgetBase
28
  WidgetBase.apply(this, new Array(widgetNode, model));
29

    
30
  // Set this.stylesheet
31
  // Defaults to "widget/<widgetName>.xsl" if not defined in config file.
32
  if ( !this.stylesheet ) {
33
    this.stylesheet = new XslProcessor(this.getProperty("mb:stylesheet", baseDir+"/widget/"+widgetNode.nodeName+".xsl", model.namespace));
34
  }
35

    
36
  // Do we allow to parse HTML nodes?
37
  this.parseHTMLNodes = Mapbuilder.parseBoolean(this.getProperty("mb:parseHTMLNodes", false));
38

    
39
  // Set widget text values as parameters 
40
  if (config.widgetText) {
41
    var textNodeXpath = "/mb:WidgetText/mb:widgets/mb:" + widgetNode.nodeName;
42
    var textParams = config.widgetText.selectNodes(textNodeXpath+"/*");
43
    for (var j=0;j<textParams.length;j++) {
44
      this.stylesheet.setParameter(textParams[j].nodeName,getNodeValue(textParams[j]));
45
    }
46
  }
47

    
48
  // Set stylesheet parameters for all the child nodes from the config file
49
  for (var j=0;j<widgetNode.childNodes.length;j++) {
50
    if (widgetNode.childNodes[j].firstChild)
51
    {
52
      this.stylesheet.setParameter(
53
        widgetNode.childNodes[j].nodeName, widgetNode.childNodes[j].firstChild.nodeValue);
54
    }
55
  }
56

    
57
  //all stylesheets will have these properties available
58
  this.stylesheet.setParameter("modelId", this.model.id );
59
  this.stylesheet.setParameter("modelTitle", this.model.title );
60
  this.stylesheet.setParameter("widgetId", this.id );
61
  this.stylesheet.setParameter("skinDir", config.skinDir );
62
  this.stylesheet.setParameter("lang", config.lang );
63

    
64
  /**
65
   * Render the widget.
66
   * @param objRef Pointer to widget object.
67
   */
68
  this.paint = function(objRef, refreshId) {
69

    
70
    //pass in a widget ID to refresh only that widget
71
    if (refreshId && (refreshId!=objRef.id)) return;
72

    
73
    if (objRef.model.doc && objRef.getNode()) {
74
      objRef.stylesheet.setParameter("modelUrl", objRef.model.url);
75
      objRef.stylesheet.setParameter("targetModelId", objRef.targetModel.id );
76

    
77
      //if (objRef.debug) mbDebugMessage(objRef, "source:"+(new XMLSerializer()).serializeToString(objRef.model.doc));
78
      objRef.resultDoc = objRef.model.doc; // resultDoc sometimes modified by prePaint()
79
      objRef.model.setParam("prePaint", objRef);
80
      objRef.prePaint(objRef);
81

    
82
      //confirm inputs
83
      if (objRef.debug) mbDebugMessage(objRef, "prepaint:"+(new XMLSerializer()).serializeToString(objRef.resultDoc));
84
      if (objRef.debug) mbDebugMessage(objRef, "stylesheet:"+(new XMLSerializer()).serializeToString(objRef.stylesheet.xslDom));
85

    
86
      //set to output to a temporary node
87
      //hack to get by doc parsing problem in IE
88
      //the firstChild of tempNode will be the root element output by the stylesheet
89
      var outputNode = document.getElementById( objRef.outputNodeId );
90
      var tempNode = document.createElement("DIV");
91

    
92
      //process the doc with the stylesheet
93
      var s = objRef.stylesheet.transformNodeToString(objRef.resultDoc);
94
      if (config.serializeUrl && objRef.debug) postLoad(config.serializeUrl, s);
95
      if (objRef.debug) mbDebugMessage(objRef, "painting:"+objRef.id+":"+s);
96
      tempNode.innerHTML = objRef.parseHTMLNodes ? Sarissa.unescape(s) : s;
97
      if( tempNode.firstChild != null ) { //Could be null!
98
        tempNode.firstChild.setAttribute("id", objRef.outputNodeId);
99

    
100
  	    //look for this widgets output and replace if found,
101
  	    //otherwise append it
102
  	    if (outputNode) {
103
  	      objRef.getNode().replaceChild(tempNode.firstChild,outputNode);
104
  	    } else {
105
  	      objRef.getNode().appendChild(tempNode.firstChild);
106
  	    }
107
      }
108
      objRef.postPaint(objRef);
109
      objRef.model.setParam("postPaint", objRef);
110
    }
111
  }
112
  // Call paint when model changes
113
  this.model.addListener("refresh",this.paint, this);
114

    
115
}
(137-137/145)