~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/util/commons.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/**
 
3
 * Returns the value of the selected option in the list with the given identifier.
 
4
 * 
 
5
 * @param listId the list identifier.
 
6
 */
 
7
function getListValue( listId )
 
8
{
 
9
    var list = document.getElementById( listId );
 
10
    var value = list.options[ list.selectedIndex ].value;
 
11
    
 
12
    return value;
 
13
}
 
14
 
 
15
/**
 
16
 * Hides the document element with the given identifier.
 
17
 * 
 
18
 * @param elementId the element identifier.
 
19
 */
 
20
function hideById( elementId )
 
21
{
 
22
  document.getElementById( elementId ).style.display = "none";
 
23
}
 
24
 
 
25
/**
 
26
 * Shows the document element with the given identifier.
 
27
 * 
 
28
 * @param elementId the element identifier.
 
29
 */
 
30
function showById( elementId )
 
31
{
 
32
  document.getElementById( elementId ).style.display = "block";
 
33
}
 
34
 
 
35
/**
 
36
 * Returns true if the element with the given identifier has text, false if not.
 
37
 * 
 
38
 * @param inputId the identifier of the input element.
 
39
 */
 
40
function hasText( inputId )
 
41
{
 
42
    return document.getElementById( inputId ).value != "";
 
43
}
 
44
 
 
45
/**
 
46
 * Returns true if the element with the given identifier is checked, false if not
 
47
 * or if it does not exist.
 
48
 * 
 
49
 * @param checkboxId the identifier of the checkbox element.
 
50
 */
 
51
function isChecked( checkboxId )
 
52
{
 
53
    var checkBox = document.getElementById( checkboxId );
 
54
    
 
55
    if ( checkBox )
 
56
    {
 
57
        return checkBox.checked;
 
58
    }
 
59
    
 
60
    return false;
 
61
}
 
62
 
 
63
/**
 
64
 * Checks the checkbox with the given identifier if the checkbox exists.
 
65
 */
 
66
function check( checkBoxId )
 
67
{
 
68
    var checkBox = document.getElementById( checkBoxId );
 
69
    
 
70
    if ( checkBox )
 
71
    {
 
72
        checkBox.checked = true;
 
73
    }
 
74
}
 
75
 
 
76
/**
 
77
 * Unchecks the checkbox with the given identifier if the checkbox exists.
 
78
 */
 
79
function uncheck( checkBoxId )
 
80
{
 
81
    var checkBox = document.getElementById( checkBoxId );
 
82
    
 
83
    if ( checkBox )
 
84
    {
 
85
        checkBox.checked = false;
 
86
    }
 
87
}
 
88
 
 
89
/**
 
90
 * Enables the element with the given identifier if the element exists.
 
91
 */
 
92
function enable( elementId )
 
93
{
 
94
    var element = document.getElementById( elementId );
 
95
    
 
96
    if ( element )
 
97
    {
 
98
        element.disabled = false;
 
99
    }
 
100
}
 
101
 
 
102
/**
 
103
 * Disables the element with the given identifier if the element exists.
 
104
 */
 
105
function disable( elementId )
 
106
{
 
107
    var element = document.getElementById( elementId );
 
108
    
 
109
    if ( element )
 
110
    {
 
111
        element.disabled = true;
 
112
    }
 
113
}
 
114
 
 
115
/**
 
116
 * Returns true if the element with the given identifier has selected elements
 
117
 * associated with it, false if not.
 
118
 * 
 
119
 * @param listId the identifier of the list element.
 
120
 */
 
121
function hasElements( listId )
 
122
{
 
123
    return document.getElementById( listId ).options.length > 0;
 
124
}
 
125
 
 
126
/**
 
127
 * Returns true if the element with the given identifier exists, false if not.
 
128
 * 
 
129
 * @param elementId the identifier of the element.
 
130
 */
 
131
function isNotNull( elementId )
 
132
{
 
133
    return document.getElementById( elementId ) != null ? true : false;
 
134
}
 
135
 
 
136
/**
 
137
 * HTML encodes the given string.
 
138
 * 
 
139
 * @param str the input string.
 
140
 * @return the HTML encoded string.
 
141
 */
 
142
function htmlEncode( str )
 
143
{
 
144
    str = str.replace( /\ /g, "%20" );
 
145
    str = str.replace( /\!/g, "%21" );
 
146
    str = str.replace( /\"/g, "%22" );
 
147
    str = str.replace( /\#/g, "%23" );
 
148
    str = str.replace( /\$/g, "%24" );
 
149
    str = str.replace( /\&/g, "%26" );
 
150
    str = str.replace( /\'/g, "%27" );
 
151
    str = str.replace( /\(/g, "%28" );
 
152
    str = str.replace( /\)/g, "%29" );
 
153
    str = str.replace( /\*/g, "%2a" );
 
154
    str = str.replace( /\+/g, "%2b" );
 
155
    str = str.replace( /\,/g, "%2c" );
 
156
    str = str.replace( /\-/g, "%2d" );
 
157
    str = str.replace( /\./g, "%2e" );
 
158
    str = str.replace( /\//g, "%2f" );
 
159
    str = str.replace( /\:/g, "%3a" );
 
160
    str = str.replace( /\;/g, "%3b" );
 
161
    str = str.replace( /\</g, "%3c" );
 
162
    str = str.replace( /\=/g, "%3d" );
 
163
    str = str.replace( /\>/g, "%3e" );
 
164
    str = str.replace( /\?/g, "%3f" );
 
165
    str = str.replace( /\@/g, "%40" );
 
166
    
 
167
    return str;
 
168
}
 
169
 
 
170
/**
 
171
 * Gets the value for the element with the given name from the DOM object.
 
172
 * 
 
173
 * @param parentElement the DOM object.
 
174
 * @param childElementName the name of the element.
 
175
 */
 
176
function getElementValue( parentElement, childElementName )
 
177
{
 
178
    var textNode = parentElement.getElementsByTagName( childElementName )[0].firstChild;
 
179
    
 
180
    return textNode ? textNode.nodeValue : null;
 
181
}
 
182
 
 
183
/**
 
184
 * Gets the value from the given DOM element.
 
185
 * 
 
186
 * @param rootElement the DOM object.
 
187
 */
 
188
function getRootElementValue( rootElement )
 
189
{
 
190
    var textNode = rootElement.firstChild;
 
191
    
 
192
    return textNode ? textNode.nodeValue : null;
 
193
}
 
194
 
 
195
/**
 
196
 * Gets the value of the attribute with the given name from the given DOM element.
 
197
 * 
 
198
 * @param rootElement the DOM object.
 
199
 * @param attributeName the name of the attribute.
 
200
 */
 
201
function getRootElementAttribute( rootElement, attributeName )
 
202
{
 
203
   return rootElement.getAttribute( attributeName );
 
204
}
 
205
 
 
206
/**
 
207
 * Sets a value on the given element.
 
208
 * 
 
209
 * @param fieldId the identifier of the element.
 
210
 * @param value the value to set.
 
211
 */
 
212
function setFieldValue( fieldId, value )
 
213
{
 
214
    document.getElementById( fieldId ).innerHTML = value;
 
215
}
 
216
 
 
217
/**
 
218
 * Gets a value from the given element.
 
219
 * 
 
220
 * @param fieldId the identifier of the element.
 
221
 * @return a value corresponding to the given identifier.
 
222
 */
 
223
function getFieldValue( fieldId )
 
224
{
 
225
    return document.getElementById( fieldId ).value;
 
226
}
 
227
 
 
228
/**
 
229
 * Sets a message in the "message" span and makes the span visible.
 
230
 * 
 
231
 * @param message the message. 
 
232
 */
 
233
function setMessage( message )
 
234
{
 
235
    document.getElementById( 'message' ).innerHTML = message;   
 
236
    document.getElementById( 'message' ).style.display = 'block';
 
237
}
 
238
 
 
239
/**
 
240
 * Makes the "message" span invisible.
 
241
 */
 
242
function hideMessage()
 
243
{
 
244
    document.getElementById( 'message' ).style.display = 'none';
 
245
}
 
246
 
 
247
function setInfo( message )
 
248
{
 
249
    document.getElementById( 'info' ).innerHTML = message;
 
250
    document.getElementById( 'info' ).style.display = 'block';
 
251
}
 
252
 
 
253
function hideInfo()
 
254
{
 
255
    document.getElementById( 'info' ).style.display = 'block';
 
256
}
 
257
 
 
258
/**
 
259
 * Makes the "detailsArea" span visible.
 
260
 */
 
261
function showDetails()
 
262
{
 
263
    $( '#detailsArea' ).show( "fast" ); //jquery specific
 
264
    
 
265
    //document.getElementById( 'detailsArea' ).style.display = 'block';
 
266
}
 
267
 
 
268
/**
 
269
 * Makes the "detailsArea" span invisible.
 
270
 */
 
271
function hideDetails()
 
272
{
 
273
    $( '#detailsArea' ).hide( "fast" ); //jquery specific
 
274
    
 
275
    //document.getElementById( 'detailsArea' ).style.display = 'none';
 
276
}
 
277
 
 
278
/**
 
279
 * Makes the "warningArea" span visible.
 
280
 */
 
281
function showWarning()
 
282
{
 
283
    $( '#warningArea' ).show( "fast" ); //jquery specific
 
284
    
 
285
    //document.getElementById( 'warningArea' ).style.display = 'block';
 
286
}
 
287
 
 
288
/**
 
289
 * Makes the "warningArea" span invisible.
 
290
 */
 
291
function hideWarning()
 
292
{
 
293
    $( '#warningArea' ).hide( "fast" ); //jquery specific
 
294
    
 
295
    //document.getElementById( 'warningArea' ).style.display = 'none';
 
296
}
 
297
 
 
298
/**
 
299
 * Convenience method for getting a document element.
 
300
 * 
 
301
 * @param id id of the element to get.
 
302
 */
 
303
function byId( elementId )
 
304
{
 
305
  return document.getElementById( elementId );
 
306
}
 
307
 
 
308
/**
 
309
 * Toggles visibility for an element.
 
310
 * 
 
311
 * @param id the identifier of the element.
 
312
 * @param display boolean indicator.
 
313
 */
 
314
function toggleByIdAndFlag( id, display )
 
315
{
 
316
    var node = byId( id );
 
317
    
 
318
    if ( ! node )
 
319
    {
 
320
        return;
 
321
    }
 
322
    
 
323
    node.style.display = ( display ? 'block' : 'none' );
 
324
}
 
325
 
 
326
/**
 
327
 * Toggles visibility for an element.
 
328
 * 
 
329
 * @param id the identifier of the element.
 
330
 */
 
331
function toggleById( id )
 
332
{
 
333
    var node = byId( id );
 
334
 
 
335
    if ( ! node )
 
336
    {
 
337
        return;
 
338
    }
 
339
    
 
340
    var display = node.style.display;
 
341
    
 
342
    node.style.display = ( display == 'none' || display == '' ? 'block' : 'none' );
 
343
}
 
344
 
 
345
/**
 
346
 * Show div at center of screen.
 
347
 */
 
348
function showDivCenter( id )
 
349
{
 
350
        var div = document.getElementById(id);
 
351
        
 
352
        var width = div.style.width;
 
353
        var height = div.style.height;
 
354
        
 
355
        var x = (document.documentElement.clientHeight / 2) - new Number(height.replace('px',''))/2;
 
356
        var y = (document.documentElement.clientWidth / 2) - new Number(width.replace('px',''))/2;      
 
357
        
 
358
        div.style.display = 'block';
 
359
        div.style.top = x +"px";
 
360
        div.style.left  = y +"px";              
 
361
}
 
362
 
 
363
/**
 
364
 * Toggles visibility of document element.
 
365
 */
 
366
function showHideDiv( elementId )
 
367
{       
 
368
        if ( document.getElementById( elementId ).style.display == "none" )
 
369
        {
 
370
                document.getElementById( elementId ).style.display = "block";
 
371
        }
 
372
        else
 
373
        {
 
374
                document.getElementById( elementId ).style.display = "none";
 
375
        }       
 
376
}
 
377
 
 
378
/**
 
379
 * Adds a div with 50 % opacity on the document.
 
380
 */
 
381
function showDivEffect()
 
382
{
 
383
        var width = document.documentElement.clientWidth;
 
384
        var height = document.documentElement.clientHeight;     
 
385
        var divEffect = document.createElement( 'div' );
 
386
        
 
387
        divEffect.id = "divEffect";
 
388
        divEffect.style.position = "fixed";
 
389
        divEffect.style.top = 0;
 
390
        divEffect.style.width = width + "px";
 
391
        divEffect.style.height = height + "px";
 
392
        divEffect.style.background = "#000000";
 
393
        divEffect.style.opacity = 0.5;
 
394
        
 
395
        document.body.appendChild( divEffect ); 
 
396
}
 
397
 
 
398
/**
 
399
 * Removes the opacity div from the document.
 
400
 */
 
401
function deleteDivEffect()
 
402
{
 
403
        var divEffect = document.getElementById( 'divEffect' );
 
404
        
 
405
        document.body.removeChild(divEffect);
 
406
}
 
407
 
 
408
/**
 
409
 * Returns a query string with all selected element values in the select with
 
410
 * the specified identifier.
 
411
 */
 
412
function getParamString( elementId )
 
413
{
 
414
    var list = document.getElementById( elementId );
 
415
    
 
416
    var params = "";
 
417
    
 
418
    for ( var i = 0; i < list.options.length; i++ )
 
419
    {
 
420
        params += elementId + "=" + list.options[i].value + "&";
 
421
    }
 
422
    
 
423
    return params;
 
424
}
 
425
 
 
426
/**
 
427
 * Creates an option and adds it to the list.
 
428
 * 
 
429
 * @param list the list.
 
430
 * @param optionValue the option value.
 
431
 * @param optionText the option text.
 
432
 */
 
433
function addOptionToList( list, optionValue, optionText )
 
434
{
 
435
    var option = document.createElement( "option" );
 
436
    option.value = optionValue;
 
437
    option.text = optionText;
 
438
    list.add( option, null );
 
439
}