Project

General

Profile

1
/*
2
Author:       Steven Ottens AT geodan.nl
3
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
4
$Id: History.js 2956 2007-07-09 12:17:52Z steven $
5
*/
6

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

    
10

    
11
/**
12
 * A tool designed to store the history of the extent during a session
13
 *
14
 * @constructor
15
 * @base ToolBase
16
 * @param toolNode  The node for this tool from the configuration document.
17
 * @param model     The model object that contains this tool
18
 */
19
function History(toolNode, model) {
20
  ToolBase.apply(this, new Array(toolNode, model));
21

    
22
  /**
23
   * Inititialising the history and setting start parameters
24
   * @param objRef  pointer to this object.
25
   */
26

    
27
	this.init = function(objRef) {
28
		objRef.model.active = -1;
29
    objRef.model.historyList = new Array();
30
    objRef.add(objRef);
31
  }
32

    
33
  /**
34
   * This adds the current extent to the historyList
35
   * @param objRef  pointer to this object.
36
   */
37
  this.add = function(objRef) {
38
    if (objRef.model.active!=null) {
39
      var place = objRef.model.active;
40
      var list = objRef.model.historyList;
41
      var center = objRef.targetModel.map.getExtent().getCenterLonLat();
42
      // take the current scale -1, otherwise we get troubles when
43
      // fixed scales are defined
44
      var scale = objRef.targetModel.map.getScale()-1;
45
      if (place > -1) {
46
        // check if current and previous history entry would result
47
        // in same center point and zoom level. If this is the case,
48
        // we do not want a new entry in the list
49
        if (center.toString() == list[place].center.toString() &&
50
            scale == list[place].scale) {
51
          return;
52
        }
53
      }
54
      var newExtent = new Object({center:center, scale:scale});
55

    
56
      if( place==(list.length-1)) { //If we are already at the end of the list add a new item
57
        list.push(newExtent); 
58
        place = place+1; 
59
      }
60
      else { //If we are somewhere in the middle of the list clear the rest of the list and add a new item
61
        place = place+1;
62
        list = list.slice(0,place);
63
        list.push(newExtent);
64
      }
65
      objRef.model.active = place;
66
      objRef.model.historyList = list;
67
    }
68
  }
69

    
70
  /**
71
   * This returns the previous extent in the list
72
   * @param objRef  pointer to this object.
73
   */
74

    
75
  this.back = function(objRef){
76
    var place = objRef.model.active;
77
    if(place<1) {
78
      objRef.model.previousExtent = null;
79
      alert(mbGetMessage("cantGoBack"));
80
    }
81
    else {
82
      place = place -1;
83
      objRef.model.active = place;
84
      objRef.model.previousExtent = objRef.model.historyList[place];
85
    }
86

    
87
  }
88
  /**
89
   * This returns the next extent in the list
90
   * @param objRef  pointer to this object.
91
   */
92
  this.forward = function(objRef) {
93
    var place = objRef.model.active;
94
    if(place<(objRef.model.historyList.length-1)) {
95
      place = place +1;
96
      objRef.model.active = place;
97
      objRef.model.nextExtent = objRef.model.historyList[place];
98
    }
99
    else {
100
      objRef.model.nextExtent = null;
101
      alert(mbGetMessage("cantGoForward"));
102
    }
103
  }
104

    
105
  /**
106
   * This stops the listener, to prevent the undo/redo steps to appear in the list
107
   * @param objRef  pointer to this object.
108
   */
109
  this.stop = function(objRef) {
110
    objRef.model.removeListener("bbox",objRef.add, objRef);
111
  }
112
  
113
  /**
114
   * This restarts the listener after undo/redo is done.
115
   * @param objRef  pointer to this object.
116
   */
117
  this.start = function(objRef) {
118
    objRef.model.addListener("bbox",objRef.add, objRef);
119
  }
120
  /**
121
    * Set the loadModel listener in response to the init event
122
    * @param objRef pointer to this object.
123
    */
124
  this.initReset = function(objRef) {
125
    objRef.targetModel.addListener("bbox", objRef.add, objRef);
126
    objRef.targetModel.addListener("loadModel", objRef.init, objRef);
127
	}
128

    
129
	this.model.addListener("historyBack", this.back, this);
130
	this.model.addListener("historyForward", this.forward, this);
131
	this.model.addListener("historyStart", this.start, this);
132
	this.model.addListener("historyStop", this.stop, this);
133
	this.model.addListener("init", this.initReset, this);
134
}
(8-8/15)