/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function HISTORY(callback) {
    window.dhtmlHistory.create({
        toJSON: function(o) {
                return Object.toJSON(o);
        },
        fromJSON: function(s) {
                return s.evalJSON();
        }
    });
    
    dhtmlHistory.initialize();
//    dhtmlHistory.addListener(callback);

    var curtime = new Date();
    this.busy = curtime.getTime();

    this.endpos = false;
    this.automode = false;
    this.started = false;
    this.callback = callback;
    dhtmlHistory.addListener(this.onHistoryEvent(this));
}

HISTORY.prototype.onHistoryEvent = function(self) {
    return function(newLocation, historyData) {
	if (newLocation == "__startup__") {
	    history.forward();
	    if (!self.automode) {
		adeiReportError(translate("History is exhausted"));
	    }

	    return;
	}
	
	var props = self.GetProps(newLocation);
	var hid = self.GetID(newLocation);
	
	var res = self.callback(props, historyData, hid);

	self.automode = false;

	var curtime = new Date();
	self.busy = curtime.getTime();
	
	return res;
    }
}

HISTORY.prototype.GetProps = function(res) {
    if (typeof res == "undefined")
	var res = dhtmlHistory.getCurrentLocation();

    return res.replace(/\&history_id=\d+/,"");
}

HISTORY.prototype.GetID = function(res) {
    if (typeof res == "undefined")
	var res = dhtmlHistory.getCurrentLocation();

    var m = /\&history_id=(\d+)/.exec(res);
    if (m) return m[1];
    return 0;
}

HISTORY.prototype.CreateLocation = function(props, hid) {
/*    if (props.indexOf("#")>=0) {*/
	var res = props + "&history_id=" + hid;
/*    } else {
	var res = props + "#history_id=" + hid;
    }*/
    return res;
}

HISTORY.prototype.Add = function(page, cfg) {
    if (!this.started) {
	dhtmlHistory.add("__startup__", cfg);
	this.started = true;
    }

    var curtime = new Date();
    this.endpos = curtime.getTime();

    var loc = this.CreateLocation(page, this.endpos)
    dhtmlHistory.add(loc, cfg);
}

HISTORY.prototype.ReplaceLocation = function(new_props, props, hid) {
    if (typeof current == "undefined") {
        var current = dhtmlHistory.getCurrentLocation();

	if (typeof hid == "undefined")
	    var hid = this.GetID(current);
    }
	
    var loc = this.CreateLocation(props, hid)
    var new_loc = this.CreateLocation(new_props, hid)
    
    var orig = window.location.toString();
    var replacement;
    if (orig.match('#')) {
	replacement = orig.replace(/#.*$/, '#' + new_loc);
	this.AddSynonim(loc, new_loc);
    } else replacement = '#' + new_loc;

    window.location.replace(replacement);
}

HISTORY.prototype.AddSynonim = function(old_page, new_page) {
    var cfg = historyStorage.get(old_page);
    
    if (cfg) {    
	if (historyStorage.hasKey(new_page))
    	    historyStorage.remove(new_page);

	historyStorage.put(new_page, cfg);
    }
}

HISTORY.prototype.Back = function() {
    if (!this.busy) return;

    var curtime = new Date();
    if ((curtime.getTime() - this.busy) < 50) return;

    this.busy = false;
    this.automode = true;
    history.back();
}

HISTORY.prototype.Forward = function() {
    var pos = this.GetID();
    if (pos == this.endpos) return;
    
    if (!this.busy) return;
    
    var curtime = new Date();
    if ((curtime.getTime() - this.busy) < 50) return;

	// if no room to go forward, the busy flag will remain
    this.busy = false;
    history.forward();
}