~ubuntu-branches/ubuntu/lucid/libebox/lucid

« back to all changes in this revision

Viewing changes to www/js/table-helper.js

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2008-02-27 13:19:28 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080227131928-8mefc35x4h4njvr0
Tags: 0.11.99-0ubuntu1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
function addNewRow(url, table, fields, directory)
12
12
{
13
13
        var pars = 'action=add&tablename=' + table + '&directory=' + directory + '&';
 
14
 
 
15
    pars += '&page=0';
 
16
        pars += '&filter=' + inputValue(table + '_filter');
 
17
        pars += '&pageSize=' + inputValue(table + '_pageSize');
14
18
        
15
19
        cleanError(table);
16
20
 
17
21
        for (var i = 0; i < fields.length; i++) {
18
22
                var field = fields[i];
19
 
                var value = $F(table + '_' + field);
 
23
                //var value = $F(table + '_' + field);
 
24
                var value = inputValue(table + '_' + field);
20
25
                if (value) {
21
26
                  if (pars.length != 0) {
22
27
                                pars += '&';
40
45
                          stripe('dataTable', '#ecf5da', '#ffffff'); 
41
46
                        },
42
47
                        onFailure: function(t) {
43
 
                          restoreHidden('buttons');
 
48
                          restoreHidden('buttons_' + table, table);
44
49
                        }
45
50
                });
46
51
 
47
 
        setLoading('buttons', true);
 
52
        setLoading('buttons_' + table, table, true);
48
53
 
49
54
}
50
55
 
51
 
function changeRow(url, table, fields, directory, id)
 
56
function changeRow(url, table, fields, directory, id, page, force)
52
57
{
53
 
        var pars = 'action=edit&tablename=' + table + '&directory=' + directory + '&id=' + id + '&';
54
 
        
 
58
        var pars = 'action=edit&tablename=' + table + '&directory='
 
59
                   + directory + '&id=' + id + '&';
 
60
        if ( page != undefined ) {
 
61
           pars += '&page=' + page;
 
62
         }
 
63
        if ( page != undefined ) {
 
64
          pars += '&page=' + page;
 
65
        }
 
66
        pars += '&filter=' + inputValue(table + '_filter');
 
67
        pars += '&pageSize=' + inputValue(table + '_pageSize');
 
68
 
 
69
        // If force parameter is ready, show it
 
70
        if ( force ) {
 
71
          pars += '&force=1';
 
72
        }
55
73
 
56
74
        cleanError(table);
57
75
        
58
76
        for (var i = 0; i < fields.length; i++) {
59
77
                var field = fields[i];
60
 
                var value = $F(table + '_' + field);
 
78
                var value = inputValue(table + '_' + field);
61
79
                if (value) {
62
80
                        if (pars.length != 0) {
63
81
                                pars += '&';
82
100
                          stripe('dataTable', '#ecf5da', '#ffffff');
83
101
                        },
84
102
                        onFailure: function(t) {
85
 
                          restoreHidden('buttons');
 
103
                          restoreHidden('buttons_' + table, table );
86
104
                        }
87
105
                });
88
106
 
89
 
         setLoading('buttons', true);
 
107
         setLoading('buttons_' + table, table, true);
90
108
 
91
109
}
92
110
 
108
126
 
109
127
*/
110
128
 
111
 
function actionClicked(url, table, action, rowId, paramsAction, directory) {
 
129
function actionClicked(url, table, action, rowId, paramsAction, directory, page) {
112
130
 
113
131
  var pars = '&action=' + action + '&id=' + rowId;
114
132
 
115
133
  if ( paramsAction != '' ) {
116
134
    pars += '&' + paramsAction;
117
135
  }
 
136
  if ( page != undefined ) {
 
137
    pars += '&page=' + page;
 
138
  }
118
139
 
 
140
  pars += '&filter=' + inputValue(table + '_filter');
 
141
  pars += '&pageSize=' + inputValue(table + '_pageSize');
119
142
  pars += '&directory=' + directory + '&tablename=' + table;
120
143
 
121
144
  cleanError(table);
135
158
                  stripe('dataTable', '#ecf5da', '#ffffff');
136
159
                },
137
160
                onFailure: function(t) {
138
 
                  restoreHidden('actionsCell_' + rowId);
 
161
                  restoreHidden('actionsCell_' + rowId, table);
139
162
                }
140
163
            });
141
164
 
142
165
  if ( action == 'del' ) {
143
 
    setLoading('actionsCell_' + rowId);
 
166
    setLoading('actionsCell_' + rowId, table);
144
167
  }
145
168
  else if ( action == 'move' ) {
146
 
    setLoading('actionsCell_' + rowId);
 
169
    setLoading('actionsCell_' + rowId, table);
147
170
  }
148
171
 
149
172
}
150
173
 
151
 
function changeView(url, table, directory, action, id)
 
174
function changeView(url, table, directory, action, id, page, isFilter)
152
175
{
153
176
        var pars = 'action=' + action + '&tablename=' + table + '&directory=' + directory + '&editid=' + id;
154
177
        
 
178
        pars += '&filter=' + inputValue(table + '_filter');
 
179
        pars += '&pageSize=' + inputValue(table + '_pageSize');
 
180
        pars += '&page=' + page;
 
181
 
155
182
        cleanError(table);
156
183
        
157
184
        var MyAjax = new Ajax.Updater(
167
194
                        evalScripts: true,
168
195
                        onComplete: function(t) { 
169
196
                          // Highlight the element
170
 
                          highlightRow(id, true);
 
197
                          if (id != undefined) {
 
198
                            highlightRow(id, true);
 
199
                          }
171
200
                          // Stripe again the table
172
201
                          stripe('dataTable', '#ecf5da', '#ffffff');
173
202
                          if ( action == 'changeEdit' ) {
174
 
                            restoreHidden('actionsCell_' + id);
 
203
                            restoreHidden('actionsCell_' + id, table);
175
204
                          }
176
205
                        },
177
206
                        onFailure: function(t) {
178
207
                          if ( action == 'changeAdd' ) {
179
 
                            restoreHidden('creatingForm');
 
208
                            restoreHidden('creatingForm_' + table, table);
180
209
                          }
181
210
                          else if ( action == 'changeList' ) {
182
 
                            restoreHidden('buttons');
 
211
                            if (! isFilter ) {
 
212
                              restoreHidden('buttons_' + table, table);
 
213
                            }
183
214
                          }
184
215
                          else if ( action == 'changeEdit' ) {
185
 
                            restoreHidden('actionsCell_' + id);
 
216
                            restoreHidden('actionsCell_' + id, table);
186
217
                          }
187
218
                        }
188
219
                        
189
220
                });
190
221
 
191
222
        if ( action == 'changeAdd' ) {
192
 
          setLoading('creatingForm', true);
 
223
          setLoading('creatingForm_' + table, table, true);
193
224
        }
194
225
        else if ( action == 'changeList' ) {
195
 
          setLoading('buttons', true);
 
226
          if ( ! isFilter ) {
 
227
            setLoading('buttons_' + table, table, true);
 
228
          }
196
229
        }
197
230
        else if ( action == 'changeEdit' ) {
198
 
          setLoading('actionsCell_' + id, true);
 
231
          setLoading('actionsCell_' + id, table, true);
199
232
        }
200
233
 
201
234
}
213
246
        errorId - div identifier
214
247
        url - the URL where the CGI which generates the HTML is placed
215
248
        formId - form identifier which has the parameters to pass to the CGI
 
249
        loadingId - String element identifier that it will substitute by the loading image
 
250
        *(Optional)* Default: 'loadingTable'
216
251
 
217
252
*/
218
 
function hangTable(successId, errorId, url, formId)
 
253
function hangTable(successId, errorId, url, formId, loadingId)
219
254
{
220
255
 
221
256
  // Cleaning manually
222
257
  $(errorId).innerHTML = "";
223
258
 
 
259
  if ( ! loadingId ) {
 
260
    loadingId = 'loadingTable';
 
261
  }
 
262
 
224
263
  var ajaxUpdate = new Ajax.Updater( 
225
264
  {
226
265
  success: successId,
231
270
        method: 'post',
232
271
        parameters: Form.serialize(formId, true), // The parameters are taken from the form
233
272
        asynchronous: true,
234
 
        onComplete: function(t) {
235
 
          restoreHidden('loadingTable');
236
 
        }
237
 
      }
238
 
  );
239
 
 
240
 
  setLoading('loadingTable');
241
 
 
242
 
}
 
273
        evalScripts: true,
 
274
        onFailure: function(t) {
 
275
          restoreHidden(loadingId);
 
276
        }
 
277
      }
 
278
  );
 
279
 
 
280
  //setLoading(loadingId);
 
281
 
 
282
}
 
283
 
 
284
/*
 
285
Function: selectComponentToHang
 
286
 
 
287
        Call to a component to be hang in a select entry
 
288
 
 
289
Parameters:
 
290
 
 
291
        successId - div identifier where the new table will be on on success
 
292
        errorId - div identifier
 
293
        formId - form identifier which has the parameters to pass to the CGI
 
294
        urls - associative array which contains tthe URL where the CGI which generates the HTML is placed
 
295
        loadingId - String element identifier that it will substitute by the loading image
 
296
        *(Optional)* Default: 'loadingTable'
 
297
 
 
298
*/
 
299
function selectComponentToHang(successId, errorId, formId, urls, loadingId)
 
300
{
 
301
 
 
302
  // Cleaning manually
 
303
  $(errorId).innerHTML = "";
 
304
 
 
305
  if ( ! loadingId ) {
 
306
    loadingId = 'loadingTable';
 
307
  }
 
308
 
 
309
  // Currently buggy, since select elements are not inputs
 
310
  // var selects = $(formId).getInputs('select');
 
311
  var children = $(formId).immediateDescendants();
 
312
  var select;
 
313
  for ( var i = 0; i < children.length; i++) {
 
314
    if ( children[i].tagName == 'SELECT' ) {
 
315
      select = children[i];
 
316
    }
 
317
  }
 
318
  var url = urls[ $F(select.id) ]
 
319
 
 
320
  var pars = "action=view"; // FIXME: maybe the directory could be sent
 
321
 
 
322
  var ajaxUpdate = new Ajax.Updater( 
 
323
  {
 
324
  success: successId,
 
325
  failure: errorId 
 
326
  },
 
327
  url,
 
328
      {
 
329
        method: 'post',
 
330
        parameters: pars,
 
331
        asynchronous: true,
 
332
        evalScripts: true,
 
333
        onSuccess: function(t) {
 
334
          restoreHidden(loadingId);
 
335
        },
 
336
        onFailure: function(t) {
 
337
          restoreHidden(loadingId);
 
338
        }
 
339
      }
 
340
  );
 
341
 
 
342
  setLoading(loadingId);
 
343
 
 
344
}
 
345
 
243
346
 
244
347
/*
245
348
Function: showSelected
269
372
      var node = $(nodeId).childNodes[idx];
270
373
      // I'd like to use constant but in IE 6 simply they don't exist
271
374
      if ( node.nodeType == 1 /* Node.ELEMENT_NODE */ ) {
 
375
        var descendants = $(node).descendants();
272
376
        if ( node.id == selectedValue ) {
273
377
          show( node.id );
 
378
          // Show not only the selected by its children
 
379
          descendants.each(function(desc) {
 
380
                  show(desc.id);
 
381
              });
274
382
        } 
275
383
        else {
 
384
          // Hide not only the selected by its children
276
385
          hide( node.id );
 
386
          descendants.each(function(desc) {
 
387
                  hide(desc.id);
 
388
              });
277
389
        }
278
390
      }
279
391
    }
314
426
 
315
427
}
316
428
 
 
429
/* TODO: showPortRange and showPort do things in common
 
430
         like showing/hiding elments depending on which value
 
431
         is selected elsewhere. We should refactor this
 
432
         and provide a generic function to do that. Logic should
 
433
         come from model and translated in javascript.
 
434
/*
 
435
Function: showPortRange
 
436
 
 
437
    Show/Hide elements in PortRange view
 
438
 
 
439
Parameters:
 
440
 
 
441
    id - the select identifier which the protocol is chosen
 
442
 
 
443
*/
 
444
function showPortRange(id)
 
445
{
 
446
 
 
447
  var selectId = id + "_range_type";
 
448
  var selectedIdx = $(selectId).selectedIndex;
 
449
  var selectedValue = $(selectId).options[selectedIdx].value;
 
450
 
 
451
  if ( selectedValue == "range") {
 
452
    show(id + "_range");
 
453
    hide(id + "_single");
 
454
    $(id + "_single_port").value = "";
 
455
  } else if (selectedValue == "single") {
 
456
    hide(id + "_range");
 
457
    show(id + "_single");
 
458
    $(id + "_to_port").value = "";
 
459
    $(id + "_from_port").value = "";
 
460
  } else {
 
461
    hide(id + "_range");
 
462
    hide(id + "_single");
 
463
    $(id + "_to_port").value = "";
 
464
    $(id + "_from_port").value = "";
 
465
    $(id + "_single_port").value = "";
 
466
  }
 
467
}
 
468
 
317
469
/*
318
470
Function: setLoading
319
471
 
320
472
        Set the loading icon on the given HTML element erasing
321
 
        everything which were there
 
473
        everything which were there. If modelName is set, isSaved parameter can be used
322
474
 
323
475
Parameters:
324
476
 
325
477
        elementId - the element identifier
 
478
        modelName - the model name to distinguish among hiddenDiv tags *(Optional)*
326
479
        isSaved   - boolean to indicate if the inner HTML should be saved
327
 
        at *hiddenDiv* in order to be rescued afterwards *(Optional)*
 
480
        at *hiddenDiv_<modelName>* in order to be rescued afterwards *(Optional)*
 
481
        
328
482
 
329
483
*/
330
 
function setLoading (elementId, isSaved)
 
484
function setLoading (elementId, modelName, isSaved)
331
485
{
332
486
 
333
 
  if ( isSaved ) {
334
 
    $('hiddenDiv').innerHTML = $(elementId).innerHTML;
 
487
  var hiddenDivId = 'hiddenDiv';
 
488
  if ( modelName ) {
 
489
    hiddenDivId = hiddenDivId + '_' + modelName;
 
490
    if ( isSaved ) {
 
491
      $(hiddenDivId).innerHTML = $(elementId).innerHTML;
 
492
    }
335
493
  }
336
494
 
337
495
  $(elementId).innerHTML = "<img src='/data/images/ajax-loader.gif' " +
340
498
}
341
499
 
342
500
/*
 
501
Function: setDone
 
502
 
 
503
        Set the done icon (a tick) on the given HTML element erasing
 
504
        everything which were there.
 
505
 
 
506
Parameters:
 
507
 
 
508
        elementId - String the element identifier
 
509
        
 
510
 
 
511
*/
 
512
function setDone (elementId)
 
513
{
 
514
 
 
515
  $(elementId).innerHTML = "<img src='/data/images/apply.gif' " +
 
516
                           "alt='done' class='tcenter'/>";
 
517
 
 
518
}
 
519
 
 
520
 
 
521
/*
343
522
Function: restoreHidden
344
523
 
345
524
        Restore HTML stored in *hiddenDiv*
347
526
Parameters:
348
527
 
349
528
        elementId - the element identifier where to restore the HTML hidden
 
529
        modelName - the model name to distinguish among hiddenDiv tags
350
530
 
351
531
*/
352
 
function restoreHidden (elementId)
 
532
function restoreHidden (elementId, modelName)
353
533
{
354
534
 
355
 
  $(elementId).innerHTML = $('hiddenDiv').innerHTML;
356
 
  $('hiddenDiv').innerHTML = '';
 
535
  if ( modelName ) {
 
536
    var hiddenDivId = 'hiddenDiv' + '_' + modelName;
 
537
    if ( $(hiddenDivId).innerHTML != '' ) {
 
538
      $(elementId).innerHTML = $(hiddenDivId).innerHTML;
 
539
    }
 
540
    $(hiddenDivId).innerHTML = '';
 
541
  }
 
542
 
 
543
  // Remove the loading image if any
 
544
  if ( $(elementId).firstChild ) {
 
545
    if ( $(elementId).firstChild.alt == 'loading...' ) {
 
546
      $(elementId).innerHTML = '';
 
547
    }
 
548
  }
 
549
 
357
550
 
358
551
}
359
552
 
413
606
  }
414
607
 
415
608
}
 
609
 
 
610
/*
 
611
Function: inputValue
 
612
 
 
613
        Use $F() to return an input value. It firstly checks
 
614
        using $() if the id exits
 
615
 
 
616
Parameters:
 
617
 
 
618
        elementId - the input element to fetch the value from
 
619
 
 
620
Returns:
 
621
 
 
622
        input value if it exits, otherwise empty string
 
623
*/
 
624
function inputValue(elementId) {
 
625
 
 
626
        if ($(elementId)) {
 
627
                return $F(elementId);
 
628
        } else {
 
629
                return '';
 
630
        }
 
631
}
 
632