source: SMC/trunk/SMC/src/web/scripts/js/mdservice/mdservice_helpers.js @ 2438

Last change on this file since 2438 was 2438, checked in by vronk, 11 years ago

reorganized web-resources for SMC browser (starting: src/web/index.html)
partly reused web-resources from mdservice

File size: 4.0 KB
Line 
1/**
2 * @fileOverview The file mainly should contain javascript assistance functions, which are not related directly to UI.
3 * Actualy includes string additional functions,  Url decoding , datetime conversions
4 * @author
5 * @version
6 */
7
8// url params reading
9var params;
10function getUrlVars(url)
11{
12        if (url == undefined){
13                url = window.location.href;
14        }
15    var vars = [], hash;
16    var hashes = url.slice(url.indexOf('?') + 1).split('&');
17    for(var i = 0; i < hashes.length; i++)
18    {
19        hash = hashes[i].split('=');
20        vars.push(hash[0]);
21        vars[hash[0]] = hash[1];
22    }
23    return vars;
24    //return getUrlVars(window.location.href);
25};
26
27
28
29if(typeof String.prototype.trim !== 'function') {   String.prototype.trim = function() {     return this.replace(/^\s+|\s+$/g, '');    } }
30
31
32String.prototype.beginsWith = function(t, i) { if (i==false) { return 
33         (t == this.substring(0, t.length)); } else { return (t.toLowerCase() 
34         == this.substring(0, t.length).toLowerCase()); } } ;
35
36
37String.prototype.endsWith = function(t, i) { if (i==false) { return (t 
38         == this.substring(this.length - t.length)); } else { return 
39         (t.toLowerCase() == this.substring(this.length - 
40         t.length).toLowerCase()); } } ;
41
42
43var Url = {
44                 
45                        // public method for url encoding
46                encode : function (string) {
47                        return escape(this._utf8_encode(string));
48                },
49         
50                // public method for url decoding
51                decode : function (string) {
52                        return this._utf8_decode(unescape(string));
53                },
54         
55                // private method for UTF-8 encoding
56                _utf8_encode : function (string) {
57                        string = string.replace(/\r\n/g,"\n");
58                        var utftext = "";
59         
60                        for (var n = 0; n < string.length; n++) {
61         
62                                var c = string.charCodeAt(n);
63         
64                                if (c < 128) {
65                                        utftext += String.fromCharCode(c);
66                                }
67                                else if((c > 127) && (c < 2048)) {
68                                        utftext += String.fromCharCode((c >> 6) | 192);
69                                        utftext += String.fromCharCode((c & 63) | 128);
70                                }
71                                else {
72                                        utftext += String.fromCharCode((c >> 12) | 224);
73                                        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
74                                        utftext += String.fromCharCode((c & 63) | 128);
75                                }
76         
77                        }
78         
79                        return utftext;
80                },
81         
82                // private method for UTF-8 decoding
83                _utf8_decode : function (utftext) {
84                        var string = "";
85                        var i = 0;
86                        var c = c1 = c2 = 0;
87         
88                        while ( i < utftext.length ) {
89         
90                                c = utftext.charCodeAt(i);
91         
92                                if (c < 128) {
93                                        string += String.fromCharCode(c);
94                                        i++;
95                                }
96                                else if((c > 191) && (c < 224)) {
97                                        c2 = utftext.charCodeAt(i+1);
98                                        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
99                                        i += 2;
100                                }
101                                else {
102                                        c2 = utftext.charCodeAt(i+1);
103                                        c3 = utftext.charCodeAt(i+2);
104                                        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
105                                        i += 3;
106                                }
107         
108                        }
109         
110                        return string;
111                }
112         
113};
114
115function dateFormat(dt){
116       
117        var str = "";
118        var month = dt.getMonth() + 1;
119        if (month < 10){
120                month = "0" + month                     
121        }
122        var day = dt.getDay();
123        if (day < 10){
124                day = "0" + day;
125        }
126        var hours = dt.getHours();
127        if (hours < 10) {
128                hours = "0" + hours;
129        }
130        var minute = dt.getMinutes();
131        if (minute < 10) {
132                minute = "0" + hours;
133        }
134        var second = dt.getSeconds();
135        if (second < 10) {
136                second = "0" + second;
137        }
138        str = dt.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minute + ":" + second;
139       
140        return str;
141}
142
143function link(action,format,params) {
144        var l = actions[action].base_uri + format;
145
146        // default param is q
147        if (params){
148                if ( ! $.isArray(params)) {
149                        //l += '/' + params;
150                        l += params;
151                } else {
152                        l += '?' + $.param(params);
153                }
154        }
155        notifyUser("l:"+ l,'debug');
156        return l;
157}
158
159function CloneObject(inObj) {
160        for (i in inObj) {
161                this[i] = inObj[i];
162        }
163}// Usage:x = new CloneObject(obj);
164
165function findPos(obj) {
166        var curleft = obj.offsetLeft || 0;
167        var curtop = obj.offsetTop || 0;
168        while (obj = obj.offsetParent) {
169                curleft += obj.offsetLeft;
170                curtop += obj.offsetTop;
171        }
172        return {x:curleft,y:curtop};
173}
Note: See TracBrowser for help on using the repository browser.