/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1 by Suren A. Chilingaryan
Initial import
1
function UPDATER(config, module, popup) {
2
    this.run_flag = 0;
3
    this.idle = 1;
4
    this.tm = null;
5
    
6
    this.forced = false;
7
    
8
    this.updating = false;
336 by Suren A. Chilingaryan
Reschedule forced update if currently busy...
9
    this.queued = false;
1 by Suren A. Chilingaryan
Initial import
10
11
    this.config = config;
12
    this.module = module;
13
    this.popup = popup;
14
    this.control = new Array();
252 by Suren A. Chilingaryan
Better notification mechanism in frontend, provide page geometry in props, allow to set custom properties without triggering update, support onload callback in UpdateDIV call
15
    this.listeners = new Array();
1 by Suren A. Chilingaryan
Initial import
16
17
    config.RegisterUpdater(this);
18
}
19
20
UPDATER.prototype.RegisterControl = function (control) {
21
    this.control.push(control);
22
}
23
252 by Suren A. Chilingaryan
Better notification mechanism in frontend, provide page geometry in props, allow to set custom properties without triggering update, support onload callback in UpdateDIV call
24
UPDATER.prototype.RegisterListener = function (obj) {
25
    this.listeners.push(obj);
26
}
27
28
1 by Suren A. Chilingaryan
Initial import
29
UPDATER.prototype.Start = function(rate) {
130 by Suren A. Chilingaryan
Minimized interface version
30
    if (this.popup) this.popup.RegisterReWidthCallback(updaterUpdate, this);
1 by Suren A. Chilingaryan
Initial import
31
32
    this.rate = rate * 1000;
33
    this.run_flag = 1;
34
    this.Run();
35
}
36
37
38
UPDATER.prototype.Stop = function() {
39
    this.run_flag = 0;
40
}
41
42
UPDATER.prototype.Run = function () {
43
    if (this.run_flag) {
44
	this.tm = setTimeout(function(param) { return function() { param.Run(); } } (this), this.rate);
45
	if (this.idle) {
46
	    this.idle = 0;
47
	    
48
	    this.Iteration();
49
	    
50
	    this.idle = 1;
51
	}
52
    }
53
}
54
55
UPDATER.prototype.Update = function () {
56
    if (this.tm) {
57
	clearTimeout(this.tm);
58
	this.tm = null;
59
    }
60
    this.forced = true;
61
    this.Run();
62
}
63
64
UPDATER.prototype.Notify = function(notifier, mod) {
65
    if (typeof mod != "undefined") {
66
	if (mod) {
67
	    var m = this.module.GetModule(mod);
68
	} else {
69
	    var m = this.module.GetOpenModule(mod);
70
	}
71
	if (m) {    
72
	    eval("if (m.Notify" + notifier + ") m.Notify" + notifier + "();");
73
	}
74
    } else {
75
	for (var i = 0; i < this.module.names.length; i++) {
76
	    this.Notify(notifier, this.module.names[i]);
77
	}
252 by Suren A. Chilingaryan
Better notification mechanism in frontend, provide page geometry in props, allow to set custom properties without triggering update, support onload callback in UpdateDIV call
78
	for (var i = 0; i < this.listeners.length; i++) {
79
	    eval("if (this.listeners[i].Notify" + notifier + ") this.listeners[i].Notify" + notifier + "();");
80
	}
1 by Suren A. Chilingaryan
Initial import
81
    }
82
}
83
84
function updaterRequest(updater) {
85
 return function(transport) {
86
    if ((!transport)||(!transport.responseText)) {
85 by Suren A. Chilingaryan
XML+XSLT modules support, Alarms support in frontend (mozilla only)
87
	adei.SetStatus(translate("Update failed: No data is returned by ADEI service"));
1 by Suren A. Chilingaryan
Initial import
88
    } else if (transport.responseText.charAt(0) == '{') {
89
	var json = transport.responseText.evalJSON(false);
11 by Suren A. Chilingaryan
Graph navigation: mouse wheel, modifier keys, etc.
90
	
91
        if (typeof json.module  == "undefined") {
92
	    if (json.error) {
85 by Suren A. Chilingaryan
XML+XSLT modules support, Alarms support in frontend (mozilla only)
93
//		adeiReportError(json.error);
94
		adei.SetStatus(translate("Update failed: %s", json.error), 0);
11 by Suren A. Chilingaryan
Graph navigation: mouse wheel, modifier keys, etc.
95
	    } else {
1 by Suren A. Chilingaryan
Initial import
96
		// Everything Ok, just nothing to update
97
//		alert('strange: ' + transport.responseText);
14 by Suren A. Chilingaryan
Some minimal information on status bar
98
		adei.SetStatus(translate("Done"));
11 by Suren A. Chilingaryan
Graph navigation: mouse wheel, modifier keys, etc.
99
	    }
100
	} else {
101
	    var module = updater.module.GetModule(json.module);
102
	    if ((module)&&(typeof module.Update != "undefined")) {
103
		var res = module.Update(json, updater.forced);
104
		if (res) {
85 by Suren A. Chilingaryan
XML+XSLT modules support, Alarms support in frontend (mozilla only)
105
		    if (typeof res == "string") {
106
//			adeiReportError(res);
107
			adei.SetStatus(translate("Update failed: %s", res), 0);
108
		    } else if (json.error) {
109
//			adeiReportError(json.error);
110
			adei.SetStatus(translate("Update failed: %s", json.error), 0);
111
		    }
112
		}  /* else {
14 by Suren A. Chilingaryan
Some minimal information on status bar
113
		    adei.SetStatus(translate("Done"));
15 by Suren A. Chilingaryan
Provide information in the statusbar, fix of global menu positioning
114
		} */
11 by Suren A. Chilingaryan
Graph navigation: mouse wheel, modifier keys, etc.
115
		
116
		updater.forced = false;
85 by Suren A. Chilingaryan
XML+XSLT modules support, Alarms support in frontend (mozilla only)
117
	    } else {
118
		adei.SetStatus(translate("Update failed: Invalid module is specified"), 0);
1 by Suren A. Chilingaryan
Initial import
119
	    }
85 by Suren A. Chilingaryan
XML+XSLT modules support, Alarms support in frontend (mozilla only)
120
	}
1 by Suren A. Chilingaryan
Initial import
121
    } else {
173 by Suren A. Chilingaryan
Support for jpgraph3
122
	adeiReportError(translate("Unexpected content: ") + transport.responseText);
1 by Suren A. Chilingaryan
Initial import
123
    }
124
    
125
    updater.updating = false;
126
 }
127
}
128
152 by Suren A. Chilingaryan
System hangup after update failure (POST request failed) is fixed
129
function updaterFailure(updater) {
154 by Suren A. Chilingaryan
Really fix the hangup after update failure
130
    return function() {
131
	adei.SetStatus(translate("Update is failed: POST request is failed"), 0);
132
	updater.updating = false;
133
    }
152 by Suren A. Chilingaryan
System hangup after update failure (POST request failed) is fixed
134
}
135
336 by Suren A. Chilingaryan
Reschedule forced update if currently busy...
136
UPDATER.prototype.Request = function(extra, postponned) {
169 by Suren A. Chilingaryan
Control subsystem
137
    var current_module = this.module.GetOpenModule();
138
    
139
    if (!extra) {
140
	if (!current_module) return false; // We have passive module without update capabilities
141
142
	if (this.updating) {
143
	    adei.SetExtraStatus("Busy, skipping update request");
336 by Suren A. Chilingaryan
Reschedule forced update if currently busy...
144
	    
145
		// Try to schedule if forced?
146
	    if ((this.forced)&&((postponned)||(!this.queued))) {
147
		this.queued = true;
148
		setTimeout(function(param) { return function() { param.Request(false, true); } } (this), 25);
149
	    }
150
	    return false;	
151
	} else {
152
	    if (postponned) {
153
		this.queued = false;
154
	    }
169 by Suren A. Chilingaryan
Control subsystem
155
	}
336 by Suren A. Chilingaryan
Reschedule forced update if currently busy...
156
    }
157
169 by Suren A. Chilingaryan
Control subsystem
158
    this.updating = true;
14 by Suren A. Chilingaryan
Some minimal information on status bar
159
160
    if (this.config.ready) {
86 by Suren A. Chilingaryan
Alarms support in all browsers
161
	if (this.forced) {
162
	    adei.SetStatus(translate("Updating..."), 0);
163
	} else {
164
	    adei.SetStatus(translate("Starting Pereodic Update..."), 0);
165
	}
14 by Suren A. Chilingaryan
Some minimal information on status bar
166
    }
169 by Suren A. Chilingaryan
Control subsystem
167
168
169
	// we can adjust configuration here
170
    if (typeof current_module.Prepare != "undefined") {
171
        current_module.Prepare();
172
    }
173
252 by Suren A. Chilingaryan
Better notification mechanism in frontend, provide page geometry in props, allow to set custom properties without triggering update, support onload callback in UpdateDIV call
174
169 by Suren A. Chilingaryan
Control subsystem
175
    var cfg = this.config.Get(extra);
1 by Suren A. Chilingaryan
Initial import
176
    
169 by Suren A. Chilingaryan
Control subsystem
177
    if (typeof current_module.Request == "undefined") {
178
	new Ajax.Request(adei.GetServiceURL("update"), {
1 by Suren A. Chilingaryan
Initial import
179
	    method: 'post',
180
	    requestHeaders: {Accept: 'application/json'},
169 by Suren A. Chilingaryan
Control subsystem
181
	    parameters: { props: Object.toJSON(cfg) },
1 by Suren A. Chilingaryan
Initial import
182
	    onSuccess: updaterRequest(this),
152 by Suren A. Chilingaryan
System hangup after update failure (POST request failed) is fixed
183
	    onFailure: updaterFailure(this)
1 by Suren A. Chilingaryan
Initial import
184
	});
169 by Suren A. Chilingaryan
Control subsystem
185
186
	return true;
187
    } else {
188
	this.updating = false;
189
        return current_module.Request(cfg);
190
    }
191
1 by Suren A. Chilingaryan
Initial import
192
}
193
194
UPDATER.prototype.Iteration = function() {
169 by Suren A. Chilingaryan
Control subsystem
195
    return this.Request();
1 by Suren A. Chilingaryan
Initial import
196
}
197
198
199
function updaterUpdate(updater) {
200
    return updater.Update();
201
}