summaryrefslogtreecommitdiff
path: root/itjs/it.js
blob: ce154dac0de686f912c6ffacbe10bc063b2cd83d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// $Id$

/**
 * Clear contents of element 'jsdebug'
 */
function CED(txt)
{
	var element = document.getElementById('jsdebug');
	if (element)
		element.innerHTML = txt ? txt : "";
}

/**
 * Return string with dump of all arguments
 */
function D()
{
	var text = "";
	for (var i = 0; i < arguments.length; i++)
	{
		var variable = arguments[i];

		if (typeof variable == "string")
			variable = variable.replace(/&/g, '&amp;').replace(new RegExp("<", "g"), '&lt;');

		text += (typeof variable) + " " + variable;

		if (typeof variable == "object")
		{
			text += ":";

			for (field in variable)
			{
				text += field + "=";

				try { text += typeof variable[field] == 'function' ? 'function' : variable[field]; }
				catch (e) { text += "*" + e + "*"; }

				text += "\n";
			}
			text += "\n";
		}

		text += "\n";
	}
	return text;
}

/**
 * Add debugging output to element 'jsdebug'
 */
function ED()
{
	var element;

	if (!(element = document.getElementById('jsdebug')))
		return; // var element = it_create_element(document.body, 'div', {id:'jsdebug', style:{position:'absolute', left:'100px', top:'10px', opacity:'0.8'}})

	element.innerHTML += '<pre style="background-color:#FEE; margin:0">'  + D.apply(this, arguments) + '<' + '/pre>';
}

/**
 * Quote HTML special chars
 * @return Text string with & " < > htmlentities-encoded
 */
function Q(value)
{
	return typeof value == "undefined" ? "" : value.toString().replace(/&/g, '&amp;').replace(/\"/g, '&quot;').replace(new RegExp("<", "g"), '&lt;').replace(/>/g, '&gt;');
}

/**
 * String class: Replaces variables of the form {var} with values from given array
 * @param values Associative array containing values to fill in (optional)
 * @return Text string with variables replaced by their values
 */
String.prototype.T = function(values)
{
	var result = this;

	for (key in values)
		result = result.replace(new RegExp("{" + key + "}", "g"), values[key]);

	return result;
}

/**
 * Insert an event handler on top of chain
 * @param p.element Element to handle event for
 * @param p.event Name of event:'focus', 'click', ... (without 'on')
 * @param p.object Object that contains handler method
 * @param p.method Method of p.object to call on p.event
 */
function it_event(p)
{
	var oldhandler = p.element["on" + p.event];

	p.element["on" + p.event] = function(ev)
	{
		var pp = arguments.callee.p ? arguments.callee.p : p;
		var oo = arguments.callee.oldhandler ? arguments.callee.oldhandler : oldhandler;

		var result = pp.object[pp.method](ev ? ev : window.event, pp);

		if (result && oo)
			result = oo(ev);

		return result;
	}
	p.element["on" + p.event].p = p;
	p.element["on" + p.event].oldhandler = oldhandler;
}

function it_add_event(p)
{
	if (!p.object || !p.method)  // not enough arguments
		return;
	if (!p.element)
		p.element = document;

	if (!p.object._it_events)
		p.object._it_events = [];
	
	var evt = p.event;
	var key = p.event + '*' + p.method;
	var p_closure = p;	// Needed for Konqueror 3.4.2 as p is (wrongly) shadowed by global input element named p
	if (!p.object._it_events[key])
		p.object._it_events[key] = function(e){ return p_closure.object[p_closure.method](e, p_closure); };

	if (p.element.addEventListener)
		p.element.addEventListener(evt, p.object._it_events[key], false);
	else if (p.element.attachEvent)
		p.element.attachEvent('on'+evt, p.object._it_events[key]);
	else
	{
		p.element['on'+evt] = function(e)
		{
			var ret = true;
			for (var k in p_closure.object._it_events)
				if (p_closure.object._it_events[k] && k.indexOf(evt) == 0)
					ret = p_closure.object._it_events[k](e);
			return ret;
		};
	}
}

function it_remove_event(p)
{
	if (!p.element)
		p.element = document;

	var key = p.event + '*' + p.method;
	if (p.object && p.object._it_events && p.object._it_events[key]) {
		if (p.element.removeEventListener)
			p.element.removeEventListener(p.event, p.object._it_events[key], false);
		else if (p.element.detachEvent)
			p.element.detachEvent('on'+p.event, p.object._it_events[key]);

		p.object._it_events[key] = null;
	}
}

/**
 * Prevent event propagation and bubbeling
 */
function it_event_void(evt)
{
	var e = evt ? evt : window.event;
	if (e.preventDefault)
		e.preventDefault();
	if (e.stopPropagation)
		e.stopPropagation();

	e.cancelBubble = true;
	e.returnValue = false;
	return false;
}

/* Get object pixel position. Based on quirksmode.org's code */
function it_get_obj_x(obj)
{       
	if("getBoundingClientRect" in obj)
		return obj.getBoundingClientRect().left + ((window.pageXOffset !== undefined) ? window.pageXOffset : (document.documentElement || document.body.parentNode || document.body).scrollLeft);
	else
	{
		// legacy for Android 1.6
		var curleft = 0;
		if (obj.offsetParent)
			while (obj)
			{
				curleft += obj.offsetLeft;
				obj = obj.offsetParent;
			}
		else if (obj.x)
			curleft += obj.x;
		return curleft;
	}
}       

function it_get_obj_y(obj)
{	       
	if("getBoundingClientRect" in obj)
		return obj.getBoundingClientRect().top + ((window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop);
	else
	{
		// legacy for Android 1.6
		var curtop = 0;
		if (obj.offsetParent)
			while (obj)
			{
				curtop += obj.offsetTop;
				obj = obj.offsetParent;
			}
		else if (obj.y)
			curtop += obj.y;
		return curtop;
	}
}

/* Common accessor for dom elements */
function it_find_obj(obj)
{
	if (document.getElementById)
		return document.getElementById(obj);
	else if (document.all)
		return document.all[obj];
	else if (document.layers)
   		return document.layers[obj];
	return null;
}

/* Get dom element by ID but always return a valid object */
function it_element(label)
{
	var tmp = it_find_obj(label);
	return tmp ? tmp : { style:{}, src:"", value:"", isundefined:true };
}

/* Get an iframe's content document in a compatible way */
function it_get_iframe_document(iframe)
{
	return iframe.contentWindow ? iframe.contentWindow.document : iframe.contentDocument;
}

/* Create a new dom element and append to doc */
function it_create_element(doc, type, init)
{
	var e = document.createElement(type);
	it_set(e, init);
	doc.appendChild(e);
	return e;
}

/**
 * Copy attributes from src to dst in a recursive manner.
 * @param dst Destination object which gets attributes
 * @param src Source object containing attributes
 */
function it_set(dst, src)
{
	if (dst)
	{
		for (var i in src)
		{
			if (typeof src[i] == 'object')
			{
				if (dst[i])
					it_set(dst[i], src[i]);
			}
			else
				dst[i] = src[i];
		}
	}
}

/**
 * Return the current timestamp
 */
function it_now()
{
	return new Date().getTime();
}

/**
 * Encodes arbitrary string for use in an url
 * @param str string to be encoded
 */
function it_url_encode(str)
{
	var result = window.encodeURIComponent ? encodeURIComponent(str) : escape(str).replace(/\+/g, "%2B");

	return result.replace(/%20/gi, "+").replace(/%2C/gi, ",").replace(/%3B/gi, ";").replace(/%28/gi, "(").replace(/%29/gi, ")");
}

/**
 * Unicode-safe equivalent of unescape()
 * @param str string URL encoded string to be decoded
 */
function it_url_decode(str)
{
	str = str.replace(/\+/g, '%20');

	// catch URI malformed errors
	try {
		if (window.decodeURIComponent)
			return decodeURIComponent(str);
	}
	catch(e) {}

	return unescape(str);
}

/**
 * Patch PNG transparency for IE 5.5-6 on the given image
 */
function it_pngfix(img, w, h, mode)
{
	var old_IE = navigator.platform == "Win32" && String(navigator.userAgent).match(/MSIE ((5\.5)|6)/);
	if (img.src && img.src.match(/\.png($|\?)/) && old_IE) {
		img.style.width = (w || img.width) + 'px';
		img.style.height = (h || img.height) + 'px';
		img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img.src+"',sizingMethod='"+(mode ? mode : 'crop')+"')";
		img.src = '/images/0.gif';
	}
	else if (img && old_IE)
		img.style.filter = 'none';
}