/adei/ui

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/ui

« back to all changes in this revision

Viewing changes to js/updater.js

  • Committer: Suren A. Chilingaryan
  • Date: 2008-04-02 10:23:22 UTC
  • Revision ID: csa@dside.dyndns.org-20080402102322-okib92sicg2dx3o3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
9
 
 
10
    this.config = config;
 
11
    this.module = module;
 
12
    this.popup = popup;
 
13
    this.control = new Array();
 
14
 
 
15
    config.RegisterUpdater(this);
 
16
}
 
17
 
 
18
UPDATER.prototype.RegisterControl = function (control) {
 
19
    this.control.push(control);
 
20
}
 
21
 
 
22
UPDATER.prototype.Start = function(rate) {
 
23
    this.popup.RegisterReWidthCallback(updaterUpdate, this);
 
24
 
 
25
    this.rate = rate * 1000;
 
26
    this.run_flag = 1;
 
27
    this.Run();
 
28
}
 
29
 
 
30
 
 
31
UPDATER.prototype.Stop = function() {
 
32
    this.run_flag = 0;
 
33
}
 
34
 
 
35
UPDATER.prototype.Run = function () {
 
36
    if (this.run_flag) {
 
37
        this.tm = setTimeout(function(param) { return function() { param.Run(); } } (this), this.rate);
 
38
        if (this.idle) {
 
39
            this.idle = 0;
 
40
            
 
41
            this.Iteration();
 
42
            
 
43
            this.idle = 1;
 
44
        }
 
45
    }
 
46
}
 
47
 
 
48
UPDATER.prototype.Update = function () {
 
49
    if (this.tm) {
 
50
        clearTimeout(this.tm);
 
51
        this.tm = null;
 
52
    }
 
53
    this.forced = true;
 
54
    this.Run();
 
55
}
 
56
 
 
57
UPDATER.prototype.Notify = function(notifier, mod) {
 
58
    if (typeof mod != "undefined") {
 
59
        if (mod) {
 
60
            var m = this.module.GetModule(mod);
 
61
        } else {
 
62
            var m = this.module.GetOpenModule(mod);
 
63
        }
 
64
        if (m) {    
 
65
            eval("if (m.Notify" + notifier + ") m.Notify" + notifier + "();");
 
66
        }
 
67
    } else {
 
68
        for (var i = 0; i < this.module.names.length; i++) {
 
69
            this.Notify(notifier, this.module.names[i]);
 
70
        }
 
71
    }
 
72
}
 
73
 
 
74
function updaterRequest(updater) {
 
75
 return function(transport) {
 
76
    if ((!transport)||(!transport.responseText)) {
 
77
        adeiReportError("Strange internal error. No data returned by ADEI service");
 
78
    } else if (transport.responseText.charAt(0) == '{') {
 
79
        var json = transport.responseText.evalJSON(false);
 
80
        if (json.error) {
 
81
            alert(json.error);
 
82
        } else {
 
83
            if (typeof json.module  == "undefined") {
 
84
                // Everything Ok, just nothing to update
 
85
//              alert('strange: ' + transport.responseText);
 
86
            } else {
 
87
                var module = updater.module.GetModule(json.module);
 
88
                if ((module)&&(typeof module.Update != "undefined")) {
 
89
                    module.Update(json, updater.forced);
 
90
                    updater.forced = false;
 
91
                }
 
92
            }
 
93
        }       
 
94
    } else {
 
95
        alert("Unexpected content: '" + transport.responseText + "'");
 
96
    }
 
97
    
 
98
    updater.updating = false;
 
99
 }
 
100
}
 
101
 
 
102
UPDATER.prototype.Request = function() {
 
103
    
 
104
    if (this.updating) return;  /* DS: Schedule, if forced? */
 
105
    else this.updating = true;
 
106
    
 
107
    new Ajax.Request(adei.GetServiceURL("update"), 
 
108
        {
 
109
            method: 'post',
 
110
            requestHeaders: {Accept: 'application/json'},
 
111
            parameters: { props: this.config.GetJSON() },
 
112
            onSuccess: updaterRequest(this),
 
113
            onFailure: function() {
 
114
                this.updating = false;
 
115
                alert('Update is failed');
 
116
            }
 
117
        });
 
118
}
 
119
 
 
120
UPDATER.prototype.Iteration = function() {
 
121
    var current_module = this.module.GetOpenModule();
 
122
 
 
123
 
 
124
//    alert(current_module);
 
125
    if (current_module) {
 
126
        if (typeof current_module.Prepare != "undefined")
 
127
            current_module.Prepare();
 
128
 
 
129
        if (typeof current_module.Request == "undefined")
 
130
            return this.Request();
 
131
        else
 
132
            return current_module.Request();
 
133
    }
 
134
    
 
135
    return false;
 
136
}
 
137
 
 
138
 
 
139
function updaterUpdate(updater) {
 
140
    return updater.Update();
 
141
}