/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/tools.js

  • Committer: Suren A. Chilingaryan
  • Date: 2008-10-30 01:27:40 UTC
  • mto: This revision was merged to the branch mainline in revision 116.
  • Revision ID: csa@dside.dyndns.org-20081030012740-svt0yigxj5uzwxyj
Few steps on source tree integration in javascript frontend

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
function adeiBusy(msg, module_name, module) {
2
 
    
3
 
}
4
 
 
5
 
function adeiGetErrorMessage(msg, module_name, module) {
6
 
    if (typeof module_name == "undefined")
7
 
        return msg;
8
 
    else
9
 
        return "Error in '" + module_name + "': " + msg;
10
 
}
11
 
 
12
 
function adeiReportError(msg, module_name, module) {
13
 
    alert(adeiGetErrorMessage(msg, module_name, module));
14
 
}
15
 
 
16
 
function adeiGetExceptionMessage(e, msg) {
17
 
    var emsg;
18
 
    if (msg) emsg = msg + ", " + translate("error: ");
19
 
    else emsg = translate("Exception caught, error: ");
20
 
 
21
 
    if (e.description) emsg += e.description;
22
 
    else if (e.message) emsg += e.message;
23
 
    else emsg += translate("Unknown error");
24
 
    
25
 
    if (e.fileName) {
26
 
        emsg += "(" + e.fileName;
27
 
        if (e.lineNumber) emsg += ":" + e.lineNumber;
28
 
        emsg += ")";
29
 
    }
30
 
    
31
 
    if (e.stack) {
32
 
        emsg += "\n\n" + e.stack;
33
 
    }
34
 
    
35
 
    return emsg;
36
 
}
37
 
 
38
 
function adeiReportException(e, msg, module_name, module) {
39
 
    var emsg = adeiGetExceptionMessage(e, msg);
40
 
    adeiReportError(emsg, module_name, module);
41
 
}
42
 
 
43
 
 
44
 
function adeiReplaceProp(props, prop, value) {
45
 
    /* To be implemented */
46
 
}
 
1
function nope() {
 
2
}
 
3
 
 
4
 
 
5
 
47
6
 
48
7
function objectClone(obj, skip) {
49
8
    if (typeof skip == "undefined") skip = new Array();
58
17
    return res;
59
18
}
60
19
 
61
 
function urlConcatenate(base, extra) {
62
 
    if (extra) {
63
 
        if (/\?/.exec(base)) return base + "&" + extra;
64
 
        else return base + "?" + extra;
65
 
    }
66
 
    return base;
67
 
}
68
 
 
69
 
function urlAddProperty(base, name, value) {
70
 
    if (/\?/.exec(base)) return base + "&" + name + "=" + value;
71
 
    else return base + "?" + name + "=" + value;
72
 
}
73
 
 
74
 
function adeiDateFormat(d, range) {
75
 
    var mydate = new Date(Math.floor(d)*1000);
76
 
    if ((range)&&(range <= adei.cfg.subsecond_threshold)) {
77
 
        var ds = d.toString();
78
 
        var subsecs = ds.indexOf('.');
79
 
        if (subsecs >= 0) return dateFormat(mydate) + '.' + ds.substr(subsecs + 1);
80
 
        else return dateFormat(mydate);
81
 
    } else return dateFormat(mydate);
82
 
}
83
 
 
84
 
function adeiDateReadableFormat(d, range) {
85
 
    if ((range)&&(range <= adei.cfg.subsecond_threshold)) {
86
 
        var mydate = new Date(Math.floor(d*1000));
87
 
    } else {
88
 
        var mydate = new Date(Math.floor(d)*1000);
89
 
    }
90
 
 
91
 
//    return mydate.toUTCString();
92
 
    return mydate.format("isoFullDateTime", true);
93
 
}
94
 
 
95
 
function adeiDateParse(d) {
96
 
    var subsecs = d.indexOf('.');
97
 
    if (subsecs < 0) {
98
 
        var res = Date.parse(d + " UTC");
99
 
        if (isNaN(res)) {
100
 
            res = serverGetResult(adei.GetToolService("parse_date", "timezone=UTC&date=" + d));
101
 
            if (isNaN(res)) return "";
102
 
            return res;
103
 
        } 
104
 
        return res / 1000;
105
 
    }
106
 
    
107
 
    var dd = d.substr(0,subsecs);
108
 
    if (dd.indexOf('.') < 0)  {
109
 
        var res = Date.parse(dd + " UTC");
110
 
        if (isNaN(res)) {
111
 
            res = serverGetResult(adei.GetToolService("parse_date", "timezone=UTC&date=" + d));
112
 
            if (isNaN(res)) return "";
113
 
            return res;
114
 
        }
115
 
    } else {
116
 
        res = serverGetResult(adei.GetToolService("parse_date", "timezone=UTC&date=" + d));
117
 
        if (isNaN(res)) return "";
118
 
        return res;
119
 
    }
120
 
    
121
 
    return Math.floor(res / 1000) + '.' + d.substr(subsecs + 1);
122
 
}
123
 
 
124
 
function adeiMathPreciseSubstract(a, b) {
125
 
    var ra, rb;
126
 
    var astr = a.toString();
127
 
    var bstr = b.toString();
128
 
 
129
 
    var pos = astr.indexOf(".");
130
 
    if (pos < 0) ra = 0;
131
 
    else ra = parseFloat("0." + astr.substr(pos + 1));
132
 
 
133
 
    pos = bstr.indexOf(".");
134
 
    if (pos < 0) rb = 0;
135
 
    else rb = parseFloat("0." + bstr.substr(pos + 1));
136
 
 
137
 
    if ((ra)||(rb)) {
138
 
        var ia = Math.floor(a);
139
 
        var ib = Math.floor(b);
140
 
 
141
 
        var r = ra - rb;
142
 
        if (r < 0) {
143
 
            var rstr = (r+1).toString();
144
 
            pos = rstr.indexOf('.');
145
 
            return (ia - ib - 1).toString() + rstr.substr(pos);
146
 
        } else if (r > 0) {
147
 
            var rstr = r.toString();
148
 
            pos = rstr.indexOf('.');
149
 
            return (ia - ib).toString() + rstr.substr(pos);
150
 
        } else return ia - ib;
151
 
    } 
152
 
    return a - b;       
153
 
}
154
 
 
155
 
function adeiMathPreciseAdd(a, b) {
156
 
    var ra, rb;
157
 
 
158
 
    var astr = a.toString();
159
 
    var bstr = b.toString();
160
 
    
161
 
    var pos = astr.indexOf(".");
162
 
    if (pos < 0) ra = 0;
163
 
    else ra = parseFloat("0." + astr.substr(pos + 1));
164
 
 
165
 
    pos = bstr.indexOf(".");
166
 
    if (pos < 0) rb = 0;
167
 
    else rb = parseFloat("0." + bstr.substr(pos + 1));
168
 
 
169
 
    if ((ra)||(rb)) {
170
 
        var ia = Math.floor(a);
171
 
        var ib = Math.floor(b);
172
 
 
173
 
        var r = ra + rb;
174
 
        if (r > 1) {
175
 
            var rstr = r.toString();
176
 
            pos = rstr.indexOf('.');
177
 
            return (ia + ib + 1).toString() + rstr.substr(pos);
178
 
        } else if (r < 1) {
179
 
            var rstr = r.toString();
180
 
            pos = rstr.indexOf('.');
181
 
            return (ia + ib).toString() + rstr.substr(pos);
182
 
        } else return ia + ib + 1;
183
 
    } 
184
 
    return a + b;       
185
 
}
186
 
 
187
 
function translate(msg) {
188
 
    var str = msg;
189
 
 
190
 
    if (!arguments || arguments.length < 2 || !RegExp) {
191
 
        return str;
192
 
    }
193
 
    
194
 
    var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
195
 
    var a = b = [], numSubstitutions = 0, numMatches = 0;
196
 
    while (a = re.exec(str))
197
 
    {
198
 
        var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
199
 
        var pPrecision = a[5], pType = a[6], rightPart = a[7];
200
 
 
201
 
        numMatches++;
202
 
        if (pType == '%')
203
 
        {
204
 
            subst = '%';
205
 
        }
206
 
        else
207
 
        {
208
 
            numSubstitutions++;
209
 
            if (numSubstitutions >= arguments.length)
210
 
            {
211
 
                alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
212
 
            }
213
 
            var param = arguments[numSubstitutions];
214
 
            var pad = '';
215
 
            if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
216
 
            else if (pPad) pad = pPad;
217
 
            var justifyRight = true;
218
 
            if (pJustify && pJustify === "-") justifyRight = false;
219
 
            var minLength = -1;
220
 
            if (pMinLength) minLength = parseInt(pMinLength);
221
 
            var precision = -1;
222
 
            if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
223
 
            var subst = param;
224
 
            if (pType == 'b') subst = parseInt(param).toString(2);
225
 
            else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
226
 
            else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
227
 
            else if (pType == 'u') subst = Math.abs(param);
228
 
            else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
229
 
            else if (pType == 'o') subst = parseInt(param).toString(8);
230
 
            else if (pType == 's') subst = param;
231
 
            else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
232
 
            else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
233
 
        }
234
 
        str = leftpart + subst + rightPart;
235
 
    }
236
 
    return str;
237
 
}
238
 
 
239
 
// This code is in the public domain. Feel free to link back to http://jan.moesen.nu/
240
 
function sprintf()
241
 
{
242
 
    if (!arguments || arguments.length < 1 || !RegExp)
243
 
    {
244
 
        return;
245
 
    }
246
 
    var str = arguments[0];
247
 
    var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
248
 
    var a = b = [], numSubstitutions = 0, numMatches = 0;
249
 
    while (a = re.exec(str))
250
 
    {
251
 
        var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
252
 
        var pPrecision = a[5], pType = a[6], rightPart = a[7];
253
 
 
254
 
        //alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);
255
 
 
256
 
        numMatches++;
257
 
        if (pType == '%')
258
 
        {
259
 
            subst = '%';
260
 
        }
261
 
        else
262
 
        {
263
 
            numSubstitutions++;
264
 
            if (numSubstitutions >= arguments.length)
265
 
            {
266
 
                alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
267
 
            }
268
 
            var param = arguments[numSubstitutions];
269
 
            var pad = '';
270
 
            if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
271
 
            else if (pPad) pad = pPad;
272
 
            var justifyRight = true;
273
 
            if (pJustify && pJustify === "-") justifyRight = false;
274
 
            var minLength = -1;
275
 
            if (pMinLength) minLength = parseInt(pMinLength);
276
 
            var precision = -1;
277
 
            if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
278
 
            var subst = param;
279
 
            if (pType == 'b') subst = parseInt(param).toString(2);
280
 
            else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
281
 
            else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
282
 
            else if (pType == 'u') subst = Math.abs(param);
283
 
            else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
284
 
            else if (pType == 'o') subst = parseInt(param).toString(8);
285
 
            else if (pType == 's') subst = param;
286
 
            else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
287
 
            else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
288
 
        }
289
 
        str = leftpart + subst + rightPart;
290
 
    }
291
 
    return str;
292
 
}
 
20
 
 
21