Project

General

Profile

1
/*
2
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
3
$Id$
4
*/
5

    
6
// Ensure this object's dependancies are loaded.
7
mapbuilder.loadScript(baseDir+"/tool/Extent.js");
8

    
9
/**
10
 * Base class for a MapContainer.  Widgets extending this class will have their
11
 * output appended to the document in a shared container. 
12
 * The container instance is specified by the mapContainerId property in config. 
13
 * Only one instance of the container can be created and it should have only 
14
 * one model which defines it and set as the widget.containerModel property.  
15
 * Therefore only the first instance of this class with the given id will actually 
16
 * create the container div and containerModel property.  
17
 * Subsequent instances of this class with same mapContainerId will have their
18
 * widget.node value set to the container node and will have access to a 
19
 * widget.containerModel property.
20
 * If the widget has the fixedWidth property set to true, then the width of the
21
 * MapPane will be taken from the width of the HTML element.  Height will be set
22
 * to maintain a constant aspect ratio.
23
 * This widget implements listeners for all mouse event types so that tools can
24
 * define mouse event callbacks.
25
 *
26
 * @constructor
27
 * @author Mike Adair 
28
 * @param widget      Pointer to the widget instance being created
29
 * @param widgetNode  The widget's XML object node from the configuration document.
30
 * @param model       The model object that this widget belongs to.
31
 */
32
function MapContainerBase(widgetNode,model) {
33

    
34
  //make sure that the containerNodeId is set in config
35
  var mapContainerNode = widgetNode.selectSingleNode("mb:mapContainerId");
36
  if (mapContainerNode) {
37
    this.containerNodeId = mapContainerNode.firstChild.nodeValue;
38
  } else {
39
    alert("MapContainerBase: required property mapContainerId missing in config:"+this.id);
40
  }
41

    
42
/**
43
 * Initialize the container.  Only the first widget to attach to this container
44
 * configures the container, all others carry out a much simpler initialization.
45
 * All widgets share the same widget.containerModel, widget.containerModel.extent
46
 * and HTML containerNode.
47
 */
48
  var containerNode = document.getElementById(this.containerNodeId);
49
  if (containerNode) {
50
    this.containerModel = containerNode.containerModel;
51
    model.containerModel = containerNode.containerModel;
52
    //this.containerModel.addListener("bbox",this.paint,this);
53

    
54
    this.setContainerWidth = function(objRef) {
55
      objRef.node.style.width=objRef.containerModel.getWindowWidth()+'px';
56
      objRef.node.style.height=objRef.containerModel.getWindowHeight()+'px';
57
      if (this.stylesheet) {
58
        this.stylesheet.setParameter("width", objRef.containerModel.getWindowWidth() );
59
        this.stylesheet.setParameter("height", objRef.containerModel.getWindowHeight() );
60
      }
61
    }
62

    
63
/**
64
 * the containerModel is initialized here
65
 */
66
  } else {
67
    //create the container node and set it's properties
68
    containerNode = document.createElement("DIV");
69
    containerNode.setAttribute("id",this.containerNodeId);
70
    containerNode.id=this.containerNodeId;
71
    containerNode.style.position="relative";
72
    containerNode.style.overflow="hidden";
73

    
74
    containerNode.containerModel = this.model;
75
    this.containerModel = this.model;
76
    model.containerModel = containerNode.containerModel;   
77

    
78
    /**
79
     * method to adjust the width of the container DIV on startup.  If the 
80
     * <fixedWidth> property is set in config, width and height of the context
81
     * doc will be adjusted.
82
     * @param objRef pointer to this object.
83
     */
84
    this.setContainerWidth = function(objRef) {
85
      //adjust the context width and height if required.
86
      var fixedWidth = widgetNode.selectSingleNode("mb:fixedWidth");
87
      if ( fixedWidth ) {
88
        fixedWidth = fixedWidth.firstChild.nodeValue;
89
        var aspectRatio = objRef.containerModel.getWindowHeight()/objRef.containerModel.getWindowWidth();
90
        var newHeight = Math.round(aspectRatio*fixedWidth);
91
        objRef.containerModel.setWindowWidth( fixedWidth );
92
        objRef.containerModel.setWindowHeight( newHeight );
93
      }
94
      objRef.node.style.width=objRef.containerModel.getWindowWidth() +'px';
95
      objRef.node.style.height=objRef.containerModel.getWindowHeight()+'px';
96
      if (this.stylesheet) {
97
        this.stylesheet.setParameter("width", objRef.containerModel.getWindowWidth() );
98
        this.stylesheet.setParameter("height", objRef.containerModel.getWindowHeight() );
99
      }
100
    }
101

    
102
    //add the extent tool
103
    this.containerModel.extent = new Extent( this.containerModel );
104
    this.containerModel.addListener( "contextLoaded", this.containerModel.extent.firstInit, this.containerModel.extent );
105
    this.containerModel.addListener( "bbox", this.containerModel.extent.init, this.containerModel.extent );
106
    this.containerModel.addListener( "resize", this.containerModel.extent.init, this.containerModel.extent );
107
    //TBD: do an extent history by storing extents every time the aoi changes
108

    
109
    /**
110
     * Called just before paint to set a help message for when the cursor is 
111
     * over the map container.
112
      //TBD: implement some sort of map pane hover mechanism to show the tooltip
113
     * @param objRef pointer to this object.
114
     */
115
    this.setTooltip = function(objRef, tooltip) {
116
      //alert("setting mappane tooltip to:"+tooltip);
117
    }
118
    this.containerModel.addListener( "tooltip", this.setTooltip, this);
119

    
120
  /** Cross-browser mouse event handling.
121
    * This function is the event handler for all MapContainer mouse events.
122
    * Listeners are defined for all the mouse actions.  This includes:
123
    * "mouseup", "mousedown", "mousemove", "mouseover", "mouseout".
124
    * This function executes in the context of the MapContainer node, 
125
    * ie. this = MapContainer node
126
    * It will set some properties on this node, which is passed on for further 
127
    * use by any regsitered listeners:
128
    * * evpl      pixel/line of the event relative to the upper left corner of the DIV.
129
    * * evxy      projection x and y of the event calculated via the context.extent.
130
    * * evTarget  projection x and y of the event calculated via the context.extent.
131
    * * evType    projection x and y of the event calculated via the context.extent.
132
    * * keypress state for ALT CTL and SHIFT keys.
133
    *
134
    * @param ev the mouse event oject passed in from the browser (will be null for IE)
135
    */
136
    this.eventHandler=function(ev) {
137
      if (window.event) {
138
        //IE events
139
        var p = window.event.clientX - this.offsetLeft + document.documentElement.scrollLeft + document.body.scrollLeft;
140
        var l = window.event.clientY - this.offsetTop + document.documentElement.scrollTop + document.body.scrollTop;
141
        this.evpl = new Array(p,l);
142
        this.eventTarget = window.event.srcElement;
143
        this.eventType = window.event.type;
144
        this.altKey = window.event.altKey;
145
        this.ctrlKey = window.event.ctrlKey;
146
        this.shiftKey = window.event.shiftKey;
147
        window.event.returnValue = false;
148
        window.event.cancelBubble = true;
149
      } else {
150
        //mozilla browsers
151
        var p = ev.clientX + window.scrollX - this.offsetLeft;
152
        var l = ev.clientY + window.scrollY - this.offsetTop;
153
        this.evpl = new Array(p,l);
154
        this.eventTarget = ev.target;
155
        this.eventType = ev.type;
156
        this.altKey = ev.altKey;
157
        this.ctrlKey = ev.ctrlKey;
158
        this.shiftKey = ev.shiftKey;
159
        ev.stopPropagation();
160
      }
161

    
162
      this.containerModel.setParam(this.eventType,this);
163
      return false;
164
    }
165
    this.eventHandler = this.eventHandler;
166

    
167
    containerNode.onmousemove = this.eventHandler;
168
    containerNode.onmouseout = this.eventHandler;
169
    containerNode.onmouseover = this.eventHandler;
170
    containerNode.onmousedown = this.eventHandler;
171
    containerNode.onmouseup = this.eventHandler;
172
    this.node.appendChild(containerNode);
173
  }
174
  this.node = document.getElementById(this.containerNodeId);
175

    
176
  this.setContainerWidth = this.setContainerWidth;
177
  this.containerModel.addFirstListener( "loadModel", this.setContainerWidth, this );
178
  this.containerModel.addListener( "bbox", this.paint, this );
179
}
(73-73/145)