forked from samohtred/X-Tree-M
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglobal_functions.js
More file actions
384 lines (328 loc) · 10.9 KB
/
Copy pathglobal_functions.js
File metadata and controls
384 lines (328 loc) · 10.9 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// #############################################################################
// ### compatibility functions
// #############################################################################
// grundlegendes Escape, um Text in HTML-Elementen nicht uminterpretiert zu bekommen
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
// find out current width of window object "my_window"
function get_total_win_width(my_window)
{
var ret_val = 0;
// IE
if(!my_window.innerWidth)
{
if(!(my_window.documentElement.clientWidth == 0))
{
//strict mode
ret_val = my_window.documentElement.clientWidth;
}
else
{
//quirks mode
ret_val = my_window.body.clientWidth;
}
}
else
{
//w3c
ret_val = my_window.innerWidth;
}
return ret_val;
}
// find out current height of window object "my_window"
function get_total_win_height(my_window)
{
var ret_val = 0;
// IE
if(!window.innerHeight)
{
if(!(document.documentElement.clientHeight == 0))
{
//strict mode
ret_val = document.documentElement.clientHeight;
}
else
{
//quirks mode
ret_val = document.body.clientHeight;
}
}
else
{
//w3c
ret_val = window.innerHeight;
}
return ret_val;
}
// code to make sure that the method of a class can see its own instance "this"
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
// contains function
function arrayContains(arr, findValue) {
var i = arr.length;
while (i--) {
if (arr[i] === findValue) return true;
}
return false;
}// See more at: http://www.codemiles.com/javascript-examples/check-array-contains-a-value-using-javascript-t7796.html#sthash.urq6GPZj.dpuf
function strStartsWith(myString, mySubstring)
{
return myString.indexOf(mySubstring) == 0;
}
function strEndsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
// change GUI item ID (parentID#itemID) to item ID
function get_id(gui_id)
{
var sep_idx = gui_id.indexOf("#");
return gui_id.substring(sep_idx + 1, gui_id.length);
}
// extract parent ID from GUI item ID
function get_parent_id(gui_id)
{
var sep_idx = gui_id.indexOf("#");
return gui_id.substring(0, sep_idx);
}
function getParentIdx(parentId, parentItems)
{
var parentIdx = -1;
for (var i=0; i<parentItems.length; i++)
if (parentItems[i] == parentId)
return i;
return null;
}
function getChildren(myObj)
{
if (myObj.children != null)
return myObj.children;
else
return myObj.childNodes;
}
function getDBElementById(domObj, id)
{
var myChildNodes = getChildren(getChildren(domObj)[0]);
for (var i = 0; i < myChildNodes.length; i++)
{
if (myChildNodes[i].attributes != null)
{
if (myChildNodes[i].attributes.length != 0)
{
if (myChildNodes[i].attributes["id"] != undefined)
{
if (myChildNodes[i].attributes["id"].value == id)
return myChildNodes[i];
}
}
}
}
return undefined;
}
function getXMLElementById(domObj, id)
{
var myChildNodes = domObj.children;
for (var i = 0; i < myChildNodes.length; i++)
{
if (myChildNodes[i].attributes.length != 0)
{
if (myChildNodes[i].attributes["id"] != undefined)
{
if (myChildNodes[i].attributes["id"].value == id)
return myChildNodes[i];
}
}
return getXMLElementById(myChildNodes[i], id);
}
return undefined;
}
function setInnerHTML(myObj, content)
{
var is_value = false;
if (myObj.value != undefined)
{
if (isNaN(myObj.value))
{
is_value = true;
}
}
if (is_value == true)
myObj.value = content;
else
if (myObj.innerHTML != undefined)
myObj.innerHTML = content;
else
myObj.textContent = content;
/*
if (myObj.innerHTML != undefined)
myObj.innerHTML = content;
else
if (myObj.textContent != undefined)
myObj.textContent = content;
else
myObj.value = content;
*/
}
function getInnerHTML(myObj)
{
if (myObj.id != "")
return $('#'+myObj.id).html();
else
{
if (myObj.value != undefined)
return myObj.value;
else
if (myObj.innerHTML != undefined)
return myObj.innerHTML;
else
return myObj.textContent;
}
/*
if (myObj.innerHTML != undefined)
return myObj.innerHTML;
else
if (myObj.textContent != undefined)
return myObj.textContent;
else
return myObj.value;
*/
}
//replace multiple URLs inside a string in html links
function URLlinks(text) {
// ### Case 1 : URL-Text -> Link
// find http:// or https:// or ftp:// or file:// or www. but not directly after an HTML tag, after
// a quote mark or in the middle of words to avoid wrong replacements; here is a sample text to prove
// the correctness :
// var mytext = 'www.saturn.de Link:www.ntv.de sdoinsdnf <br>http://www.wetter.net osdinosdf<br>sodinfodisf ftp://www.tagesschau.de/imag003.png<br>sdoifnsdwww.kika.deoninoiibiuno<br><span contenteditable=\"false\"><a href=\"www.uno.org\" target=\"_blank\">www.uno.org</a></span>';
var regExUrl = /[^>\"\w]((https?|ftp|file):\/\/|www.)\S+(\s+|\<|\n)/gi;
// place spaces at beginning and end to avoid omitting URLs at beginning or ending of whole text
var mytext = ' ' + text + ' ';
// change <br> to \n to make them string delimiters instead of normal HTML tags
mytext = mytext.replace(/<br>/gi,'\n');
// replace URLs by correct HTML code; unfortunately the delimiters are the 1st and last char (must be treated specially)
mytext = mytext.replace(regExUrl, function f(x){ return x.substr(0,1) + '<span contenteditable=\"false\"><a href=\"' + x.substr(1, x.length-2) + '\" target=\"_blank\">' + x.substr(1, x.length-2) + '</a></span>' + x.substr(x.length-1,1)});
// change back \n to <br>
mytext = mytext.replace(/\n/gi,'<br>');
// remove spaces before and after again
mytext = mytext.substr(1,mytext.length-2);
// ### Case 2 : Link-Konsistenz (href mit innerem Text synchronisieren)
return mytext;
}
//replace line breaks with <br> html tags
function nl2br (str, is_xhtml) {
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
// alert('#'+str+'#');
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
/////////////////////////////////////////////////////////////////////////////////////////
/// GUI FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////
// displays bar showing the approval rate of an item
//////////////////////////////////////////////////////
// my_eval_area = div container where the eval bar should be placed
// my_eval_value = -100 .. +100 (absolute value is displayed; negative
// values in red; positive values in green)
/////////////////////////////////////////////////////
function f_eval_bar(my_eval_area, my_eval_value)
{
// setups
var my_eval_val_abs = Math.abs(my_eval_value);
var c_NUM_GUI_ELEMS = 100;
var c_SCALE_BAR_DIST = c_NUM_GUI_ELEMS / 5;
var c_SCALE_BAR_BOT_FORMAT = "2px solid #000000";
var c_SCALE_BAR_TOP_FORMAT = "1px solid #000000";
var c_EVAL_GREEN_BOT_FORMAT = "1px solid #00B000";
var c_EVAL_GREEN_TOP_FORMAT = "1px solid #00B000";
var c_EVAL_GREEN_COLOR = "rgb(0, 245, 0)";
var c_EVAL_RED_BOT_FORMAT = "1px solid #FF3030";
var c_EVAL_RED_TOP_FORMAT = "1px solid #FF3030";
var c_EVAL_RED_COLOR = "rgb(255, 140, 140)";
var c_EVAL_GREY_BOT_FORMAT = "1px solid #B0B0B0";
var c_EVAL_GREY_TOP_FORMAT = "1px solid #B0B0B0";
var c_EVAL_GREY_COLOR = "rgb(245, 245, 245)";
var c_EVAL_FIELD_WIDTH = 4;
var c_EVAL_FIELD_HEIGHT = 1;
if (my_eval_value<0)
{
var c_EVAL_BOT_FORMAT = c_EVAL_RED_BOT_FORMAT;
var c_EVAL_TOP_FORMAT = c_EVAL_RED_TOP_FORMAT;
var c_EVAL_COLOR = c_EVAL_RED_COLOR;
}
else
{
var c_EVAL_BOT_FORMAT = c_EVAL_GREEN_BOT_FORMAT;
var c_EVAL_TOP_FORMAT = c_EVAL_GREEN_TOP_FORMAT;
var c_EVAL_COLOR = c_EVAL_GREEN_COLOR;
}
// create formatted container for eval bar
var my_curr_eval_disp = document.createElement("div");
my_curr_eval_disp.style.width = ((c_NUM_GUI_ELEMS+2) * c_EVAL_FIELD_WIDTH).toString()+"px";
my_curr_eval_disp.style.cssFloat = "none";
// generate eval bar itself
for (var j=0; j<=c_NUM_GUI_ELEMS; j++)
{
var my_curr_eval_field = document.createElement("div");
if (j != c_NUM_GUI_ELEMS)
{
my_curr_eval_field.style.cssFloat = "left";
}
if (j<my_eval_val_abs )
{
if (j%c_SCALE_BAR_DIST == 0)
{
my_curr_eval_field.style.borderBottom = c_SCALE_BAR_BOT_FORMAT;
my_curr_eval_field.style.borderTop = c_SCALE_BAR_TOP_FORMAT;
}
else
{
my_curr_eval_field.style.borderBottom = c_EVAL_BOT_FORMAT;
my_curr_eval_field.style.borderTop = c_EVAL_TOP_FORMAT;
}
my_curr_eval_field.style.backgroundColor = c_EVAL_COLOR;
}
else
{
if (j%c_SCALE_BAR_DIST == 0)
{
my_curr_eval_field.style.borderBottom = c_SCALE_BAR_BOT_FORMAT;
my_curr_eval_field.style.borderTop = c_SCALE_BAR_TOP_FORMAT;
}
else
{
my_curr_eval_field.style.borderBottom = c_EVAL_GREY_BOT_FORMAT;
my_curr_eval_field.style.borderTop = c_EVAL_GREY_TOP_FORMAT;
}
my_curr_eval_field.style.backgroundColor = c_EVAL_GREY_COLOR;
}
my_curr_eval_field.style.width = c_EVAL_FIELD_WIDTH.toString()+"px";
my_curr_eval_field.style.height = c_EVAL_FIELD_HEIGHT.toString()+"px";
my_curr_eval_disp.appendChild(my_curr_eval_field);
}
my_eval_area.appendChild(my_curr_eval_disp);
return my_curr_eval_disp;
}
function f_append_to_pad(pad, msg)
{
pad_obj = document.getElementById(pad);
curr_pad_content = getInnerHTML(pad_obj);
setInnerHTML(pad_obj, curr_pad_content+'<BR>'+msg);
}