Project

General

Profile

1
/*
2
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
3
$Id: WidgetBase.js 3880 2008-02-27 15:37:08Z gjvoosten $
4
*/
5

    
6
/**
7
 * Base Class for widgets. All widgets must extend this base class.
8
 * Defines the default prePaint() and postPaint() methods for all widgets.
9
 *
10
 * @constructor
11
 * @author Mike Adair 
12
 * @param widget      Pointer to the widget instance being created
13
 * @param widgetNode  The widget's XML object node from the configuration document.
14
 * @param model       The model object that this widget belongs to.
15
 */
16
function WidgetBase(widgetNode,model) {
17
  this.model = model;
18
  this.widgetNode = widgetNode;
19
//alert(widgetNode.nodeName);
20
	var templatedWidget = false;
21
	if(model.modelNode.attributes.getNamedItem("createByTemplate") && model.modelNode.attributes.getNamedItem("createByTemplate").nodeValue=='true'){
22
		widgetNode.setAttribute("id","MbWidget_" + mbIds.getId());
23
	  templatedWidget = true;
24
  }
25

    
26
  /** Widget's Id defined in the Config (required) */
27
  if (widgetNode.attributes.getNamedItem("id")) {
28
    this.id = widgetNode.attributes.getNamedItem("id").nodeValue;
29
  } else {
30
    alert(mbGetMessage("idRequired", widgetNode.nodeName));
31
  }
32
  
33
  /**
34
   * Convenient access to Mapbuilder.getProperty
35
   * @param property property to get
36
   * @param default value to use if property is not set
37
   * @return the value for the property
38
   */
39
  this.getProperty = function(property, defaultValue) {
40
    return Mapbuilder.getProperty(widgetNode, property, defaultValue);
41
  }
42

    
43
  //allow the widget output to be replaced on each paint call
44
  if(templatedWidget){
45
    this.outputNodeId = this.id;
46
  }else {
47
    this.outputNodeId = this.getProperty("mb:outputNodeId", "MbWidget_" + mbIds.getId());
48
  }
49

    
50
  //until htmlTagNode becomes required allow setting of it by widget id
51
  if (!this.htmlTagId) {
52
    this.htmlTagId = this.getProperty("mb:htmlTagId", this.id);
53
  }
54

    
55
  this.getNode = function() {
56
    // Node in main HTML to attach widget to.
57
    var node = document.getElementById(this.htmlTagId);
58
    if(!node) {
59
      //alert("htmlTagId: "+this.htmlTagId+" for widget "+widgetNode.nodeName+" not found in config");
60
    }
61
    return node;
62
  }
63

    
64
  //allow widgets to not automatically update themseleves in certain circumstances (see layerControl for example)
65
  this.autoRefresh = Mapbuilder.parseBoolean(this.getProperty("mb:autoRefresh", true));
66

    
67
  //set a debug property in config to see inputs and outputs of stylehseet
68
  this.debug = Mapbuilder.parseBoolean(this.getProperty("mb:debug", false));
69

    
70
  /**
71
   * Initialize dynamic properties.set the target model
72
   * @param toolRef Pointer to this object.
73
   */
74
  this.initTargetModel = function(objRef) {
75
    //set the target model
76
    var targetModel = objRef.getProperty("mb:targetModel");
77
    if (targetModel) {
78
      objRef.targetModel = window.config.objects[targetModel];
79
      if ( !objRef.targetModel ) {
80
        alert(mbGetMessage("noTargetModelWidget", targetModel, objRef.id));
81
      }
82
    } else {
83
      objRef.targetModel = objRef.model;
84
    }
85
  }
86
  this.model.addListener("init", this.initTargetModel, this);
87

    
88
  /**
89
   * Called before paint(), can be used to set up a widget's paint parameters,
90
   * or modify model using this.resultDoc().
91
   * @param objRef Pointer to this object.
92
   */
93
  this.prePaint = function(objRef) {
94
    //no-op by default
95
  }
96

    
97
  /**
98
   * Called after paint(), can be used to initialize things that depend on the
99
   * the widget output being presetn, eg. form and form elements
100
   * @param objRef Pointer to this object.
101
   */
102
  this.postPaint = function(objRef) {
103
    //no-op by default
104
  }
105

    
106
  /**
107
   * Remove widget from display.
108
   * @param objRef Pointer to this object.
109
   */ 
110
  this.clearWidget = function(objRef) {
111
    //with node remove child
112
    var outputNode = document.getElementById( objRef.outputNodeId );
113
    var node = objRef.getNode();
114
    if (node && outputNode) node.removeChild(outputNode);
115
  }
116
  this.model.addListener("newModel",this.clearWidget, this);
117
}
(134-134/145)