/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/select.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 selectUpdater(xmldoc, param) {
 
2
    // check errors
 
3
    var sel = param.select;
 
4
 
 
5
    var values = xmldoc.getElementsByTagName("Value");
 
6
    if (values.length > 0) {
 
7
        var value;
 
8
        var selnode = sel.node;
 
9
        
 
10
        while (selnode.hasChildNodes()) selnode.removeChild(selnode.firstChild);
 
11
                
 
12
        for (var i = 0; i < values.length; i++) {
 
13
            var value = values[i];
 
14
            var item_value = value.getAttribute("value");
 
15
            var item_text = value.getAttribute("name");
 
16
            if (!item_text) item_text = item_value;
 
17
            
 
18
            var opt = document.createElement('option');
 
19
            opt.setAttribute("value", item_value);
 
20
            if ((param.value)&&(item_value == param.value)) opt.setAttribute("selected", 1);
 
21
            opt.appendChild(document.createTextNode(item_text));
 
22
            selnode.appendChild(opt);
 
23
        }
 
24
/*
 
25
        innerHTML is not working due to the bug Q276228 in IE   
 
26
        sel.node.innerHTML = res;
 
27
*/
 
28
        if (sel.callback) {
 
29
            sel.callback(sel.cbattr, sel.GetValue()/*values[0].getAttribute("value")*/, param.opts);
 
30
        }
 
31
    } else {
 
32
        var error;
 
33
        var errors = xmldoc.getElementsByTagName("Error");
 
34
 
 
35
        if (errors.length > 0)
 
36
            error = errors[0].firstChild.data;
 
37
        else {
 
38
            var errors = xmldoc.getElementsByTagName("parsererror");
 
39
            if (errors.length > 0)
 
40
                error = errors[0].firstChild.data;
 
41
            else
 
42
                error = "Empty response";
 
43
        }
 
44
 
 
45
        adeiReportError(translate("SELECT \"%s\" update is failed. Error: %s", sel.id, error));
 
46
    }
 
47
    
 
48
}
 
49
 
 
50
 
 
51
function SELECT(id, cb, cbattr) {
 
52
    if (typeof cb == "undefined") this.callback = null;
 
53
    else this.callback = cb;
 
54
    if (typeof cbattr == "undefined") this.cbattr = null;
 
55
    else this.cbattr = cbattr;
 
56
 
 
57
    this.id = id;
 
58
    this.node = document.getElementById(this.id);
 
59
    if (!this.node) adeiReportError(translate('SELECT "%s" is not found', this.id));
 
60
}
 
61
 
 
62
/* DS: Should do something if request failed */
 
63
SELECT.prototype.Update = function(url, opts, value) {
 
64
    var param = new Object;
 
65
 
 
66
    param.select = this;
 
67
    if (typeof opts == "undefined") param.opts = null;
 
68
    else param.opts = opts;
 
69
    if (typeof value == "undefined") param.value = null;
 
70
    else param.value = value;
 
71
 
 
72
    queueXML(url, selectUpdater, param);
 
73
}
 
74
 
 
75
SELECT.prototype.GetLastIndex = function() {
 
76
    return this.node.length - 1;
 
77
}
 
78
 
 
79
SELECT.prototype.GetValue = function(idx) {
 
80
    if (typeof idx == "undefined")
 
81
        return this.node.value
 
82
    else if ((idx < this.node.length)&&(idx>=0))
 
83
        return this.node[idx].value;
 
84
 
 
85
    adeiReportError(translate('SELECT "%s". Index (%i) is out of range', this.id, idx));
 
86
}
 
87
 
 
88
SELECT.prototype.GetIndex = function(value) {
 
89
    if (typeof value == "undefined")
 
90
        return this.node.selectedIndex;
 
91
    else {
 
92
        for (var i = 0; i < this.node.length; i++) {
 
93
            if (this.node[i].value == value) {
 
94
                return i;
 
95
            }
 
96
        }
 
97
 
 
98
 
 
99
        adeiReportError(translate('SELECT "%s". Accessing non-exisiting value: %s', this.id, value));
 
100
    }
 
101
}
 
102
 
 
103
SELECT.prototype.SetIndex = function(idx) {
 
104
    if ((idx < this.node.length)&&(idx >= 0))
 
105
        this.node.selectedIndex = idx;
 
106
    else
 
107
        adeiReportError(translate('SELECT "%s". Index (%i) is out of range', this.id, idx));
 
108
}
 
109
 
 
110
SELECT.prototype.SetValue = function(value) {
 
111
    for (var i = 0; i < this.node.length; i++) {
 
112
        if (this.node[i].value == value) {
 
113
            this.node.selectedIndex = i;
 
114
            return;
 
115
        }
 
116
    }
 
117
    
 
118
    adeiReportError(translate('SELECT "%s". Setting non-exisiting value: %s', this.id, value));
 
119
}
 
120
 
 
121
 
 
122
SELECT.prototype.GetTitle = function(idx) {
 
123
    if (typeof idx == "undefined")
 
124
        idx = this.node.selectedIndex;
 
125
    else if ((idx<0)||(idx>=this.node.length))
 
126
        return adeiReportError(translate('SELECT "%s". Index (%i) is out of range', this.id, idx));
 
127
 
 
128
    var node = this.node.childNodes[idx];
 
129
    if (node) {
 
130
        var text = node.firstChild;
 
131
        if (text) return text.nodeValue;
 
132
    }
 
133
    return null;
 
134
}
 
135
 
 
136
 
 
137
SELECT.prototype.Show = function() {
 
138
    if (this.node) this.node.style.display = "inline";
 
139
}
 
140
 
 
141
SELECT.prototype.Hide = function() {
 
142
    if (this.node) this.node.style.display = "none";
 
143
}