File: aws_kernel.tjs

package info (click to toggle)
libaws 2.5~124785-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,488 kB
  • ctags: 419
  • sloc: ada: 61,158; makefile: 1,262; ansic: 219; xml: 196; java: 112; python: 66; sed: 43; sh: 34
file content (656 lines) | stat: -rw-r--r-- 18,381 bytes parent folder | download
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
@@----------------------------------------------------------------------------
@@--                              Ada Web Server
@@--
@@--                         Copyright (C) 2007-2008
@@--                                 AdaCore
@@--
@@--  This library is free software; you can redistribute it and/or modify
@@--  it under the terms of the GNU General Public License as published by
@@--  the Free Software Foundation; either version 2 of the License, or (at
@@--  your option) any later version.
@@--
@@--  This library is distributed in the hope that it will be useful, but
@@--  WITHOUT ANY WARRANTY; without even the implied warranty of
@@--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
@@--  General Public License for more details.
@@--
@@--  You should have received a copy of the GNU General Public License
@@--  along with this library; if not, write to the Free Software Foundation,
@@--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
@@--
@@----------------------------------------------------------------------------

@@SET@@ AWS_CONTEXT_PER_WINDOW = true
@@--  If the context should be renew on a new window (or tab) creation

@@SET@@ CTX_WB = "CTX_WB"
@@--  The context parameter name

@@---------------------------------------------------------------- AWS

var AWS = {
   Context : null,
   Ajax    : null,
   Tools   : null
}

@@---------------------------------------------------------------- TOOLS

AWS.Tools = new function() {

   @@-----------
   @@-- PUBLIC
   @@-----------

   @@-- --------------------------------
   @@-- onload event to update CTX_WB

   this.addLoadEvent = function (func)
   {
      var oldonload = window.onload;

      if (typeof window.onload != 'function') {
         window.onload = func;
      } else {
         window.onload = function() { oldonload(); func(); }
      }
   }

   @@-- --------------------------------
   @@-- Used to report javascript errors

   this.report_error = function (request)
   {
      alert ('There was! an error on this Ajax request.');
   }

   @@-- Clear select content

   this.clearSelect = function (select_id)
   {
      while ($(select_id).length > 0) {
         $(select_id).remove(0);
      }
   }

   @@-- Delete a given option from the select

   this.delSelectOption = function (select_id, option)
   {
      var idx = 0;
      while (idx < $(select_id).length) {
         if ($(select_id).options[idx].value == option) {
            $(select_id).remove(idx);
            return;
         }
         idx = idx + 1;
      }
   }

   @@-- Add option_value/option_content into the given select

   this.addToSelect =
      function (select_id, option_value, option_content, option_class)
   {
      // Create a new option
      var new_option = document.createElement("option");
      new_option.value = option_value;
      new_option.className = option_class;
      var content = document.createTextNode(option_content);
      new_option.appendChild(content);

      // And add it to select
      $(select_id).appendChild(new_option);
      return new_option;
   }

   @@-- Select the given option from the select

   this.selectChangeSelected =
      function (select_id, option, hook_func, hook_param)
   {
      var idx = 0;
      while (idx < $(select_id).length) {
         if ($(select_id).options[idx].value == option) {
            $(select_id).selectedIndex = idx;
            if (hook_func != null && hook_func != "") {
               hook_func($(select_id).options[idx], hook_param);
            }
            return;
         }
         idx = idx + 1;
      }
   }
}

@@---------------------------------------------------------------- CONTEXT

AWS.Context = new function() {

   @@-----------
   @@-- PUBLIC
   @@-----------

   @@-- --------------------------------
   @@-- Returns the context id

   this.get = function ()
   {
      var context = document.getElementById('@_CTX_WB_@');

      if (context == null)
          return "";
      else
          return $(context).innerHTML;
   }

   @@-- --------------------------------
   @@-- Update the context tag in anchors and forms

   this.update = function ()
   {

      @@IF@@ @_AWS_CONTEXT_PER_WINDOW_@ = true
      @@--  Check if context has to be renewed

      if (!is_new_context && history.length == 1) {
	 @@--  This is a new page in the browser
	 @@--  Renew the context

	 is_new_context = true;
	 AWS.Ajax.XML.request
           ("/xml_reset_context.xml?CTX_WB_COPY=true", "", AWS.Context.update);
      }
      @@END_IF@@

      var k = 0;

      @@-- anchors

      var anchors = document.getElementsBySelector('a');

      for (k=0; k<anchors.length; k++) {
         var href = anchors[k].getAttribute('href');
         if (href != null) {
            @@--  IE insists on returning the derived absolute URL,
            @@--  make sure that the ref is on the current page if the
            @@--  prefix is HTTP.
            if ((href.indexOf("http:",0) != 0
                || href.indexOf(window.location.hostname) == 7)
                && href.indexOf("mailto:",0) != 0
                && href.indexOf("#",0) != 0) {

                anchors[k].setAttribute('href',
                                        AWS.Context.href_add_anchor(href));
            }
         }
      }

      @@-- forms

      var forms = document.getElementsBySelector('form');

      for (k=0; k<forms.length; k++) {
         var action = forms[k].getAttribute('action');
         if (action != null) {
            if (action != ""
                && action.indexOf("http:",0) != 0
                && action.indexOf("#", 0) != 0
                && forms[k].getAttribute('onsubmit') == null) {
                add_context_to_form (forms[k]);
            }
         }
      }
   }

   @@-- --------------------------------
   @@-- Add Context parameter CTX_WB for Ajax

   this.add_ajax = function (pars)
   {
      if (pars == "")
          pars = pars + "?";
      else
          pars = pars + "&";

      pars = pars + "@_CTX_WB_@=" + this.get();

      return pars;
   }

   @@-- --------------------------------
   @@-- Add context parameter CTX_WB for anchors

   this.add_anchor = function (a)
   {
      a.href = this.href_add_anchor(a.href);
   }

   this.href_add_anchor = function (href)
   {
      var href_last = "";       // Contains fragment

      if (href.indexOf('#', 0) != -1) {
         var splitted_href = href.split('#');
         href = splitted_href[0];
         href_last = splitted_href[1];
      }

      // Remove CTX_WB if exists

      if (href.indexOf("@_CTX_WB_@", 0) != -1) {
         href = href.split("@_CTX_WB_@")[0];
         href = href.substr(0, href.length - 1); // Remove & or ?
      }

      if (href.indexOf('?',0) == -1) {
          href = href + '?';
      } else {
          href = href + '&';
      }

      href = href + "@_CTX_WB_@=" + this.get();

      if (href_last != "") {
         href = href + "#" + href_last;
      }
      return (href);
   }

   @@------------
   @@-- PRIVATE
   @@------------

   @@-- --------------------------------
   @@-- Append or update context into form

   function add_context_to_form (form)
   {
      var k = 0;
      var items = form.elements;

      for (k=0; k<items.length; k++) {
         if (items[k].tagName == "INPUT"
             && items[k].getAttribute('NAME') == '@_CTX_WB_@') {

            // Update context if needed

            if (items[k].getAttribute('VALUE') != AWS.Context.get()) {
               items[k].setAttribute ('VALUE', AWS.Context.get());
            }
            return;
         }
      }

      //  Insert new context field

      hi = document.createElement('input');
      hi.setAttribute('type', 'hidden');
      hi.setAttribute('name', '@_CTX_WB_@');
      hi.setAttribute('value', AWS.Context.get());
      form.appendChild(hi);
   }

   @@--

   var is_new_context = false;
}

@@---------------------------------------------------------------- AJAX

AWS.Ajax = new function() {

   @@-----------
   @@-- PUBLIC
   @@-----------

   this.XML = null;

   @@-- --------------------------------
   @@-- Update the behavior rules, needed when loading new section
   @@-- containing JavaScript code. Also update the CTX_WB.

   this.update_framework = function()
   {
      Behaviour.apply ();
      AWS.Context.update();
   }

   @@-- --------------------------------
   @@-- Call url?pars and put result into placeholder

   this.replace = function (url, pars, placeholder, oncomplete)
   {
      var rplaceholder = $(placeholder);

      if (rplaceholder.tagName == "TEXTAREA" ||
          rplaceholder.tagName == "INPUT")
         var O_ajax = new Ajax.Request
             (url,
              {method:     'get',
               parameters: AWS.Context.add_ajax(pars),
               onFailure:  AWS.Tools.report_error,
               onComplete: function(req)
               {rplaceholder.value = req.responseText;
                if (oncomplete != "") oncomplete();}});
      else
         var O_ajax = new Ajax.Updater
             ({success: placeholder},
              url,
              {method:     'get',
              parameters: AWS.Context.add_ajax(pars),
              onFailure:  AWS.Tools.report_error,
              onComplete: function(req)
              {AWS.Ajax.update_framework();
               if (oncomplete != "") oncomplete();}});
   }

   @@-- --------------------------------
   @@-- Serialize a tag or all fields in a form

   this.serialize = function(elm, name) {
      var param = "";

      if ($(elm) == null) return;

      if ($(elm).tagName == "FORM") {
         param = Form.serialize (elm);
      } else if ($(elm).tagName == "UL") {
         param = Sortable.serialize(elm);
      } else if (name == "") {
         param = elm + "=" + $F(elm);
      } else {
         param = name + "=" + $F(elm);
      }
      return (param);
   }

}

AWS.Ajax.XML = new function() {

   @@-- --------------------------------
   @@-- Call url?pars and execute aws_xml_handler methods then the
   @@-- oncomplete method (to chain commands) if defined

   this.request = function (url, pars, oncomplete)
   {
      var myAjax = new Ajax.Request
         (url,
          {method: 'get',
           parameters: AWS.Context.add_ajax(pars),
           onFailure : AWS.Tools.report_error,
           onComplete : function(req)
           {AWS.Ajax.XML.handler(req);
            AWS.Ajax.update_framework();
            if (oncomplete != "") oncomplete();}});
   }

   @@-- Handle XML responses

   this.handler = function (result)
   {
      // get XML result
      var xml_result = result.responseXML;
      var response   = xml_result.getElementsByTagName('response');
      var i = 0;
      for (i = 0; i < response[0].childNodes.length; i++) {
          var node = response[0].childNodes[i];
          if (node.nodeName == 'replace') {
             handler_replace (node);
          } else if (node.nodeName == 'clear') {
             handler_clear (node);
          } else if (node.nodeName == 'select') {
             handler_select (node);
          } else if (node.nodeName == 'radio') {
             handler_radio (node);
          } else if (node.nodeName == 'check') {
             handler_check (node);
          } else if (node.nodeName == 'get') {
             handler_get (node);
          } else if (node.nodeName == 'location') {
             handler_location (node);
          } else if (node.nodeName == 'refresh') {
             window.location.reload (true);
          } else if (node.nodeName == 'create_sortable') {
             handler_create_sortable (node);
          } else if (node.nodeName == 'destroy_sortable') {
             handler_destroy_sortable (node);
          } else if (node.nodeName == 'apply_style') {
             handler_apply_style (node);
          } else if (node.nodeName == 'insert_after') {
             handler_insert_after (node);
          }
      }
   }

   @@-- Replace a node content by an HTML String

   function handler_replace (node)
   {
      var placeholder_id = node.getAttribute("id");
      var placeholder_content;

      if ($(placeholder_id) == null) return;

      if (node.childNodes.length > 1) {
         placeholder_content = node.childNodes[1].nodeValue;
      } else {
         placeholder_content = node.firstChild.nodeValue;
      }
      if ($(placeholder_id).tagName == "TEXTAREA" ||
          $(placeholder_id).tagName == "INPUT") {
         $(placeholder_id).value = placeholder_content;
      } else {
         $(placeholder_id).innerHTML = placeholder_content;
      }

      AWS.Ajax.update_framework();
   }

   @@-- Clear a node

   function handler_clear (node)
   {
      var placeholder_id = node.getAttribute("id");
      if ($(placeholder_id).tagName == "TEXTAREA" ||
         $(placeholder_id).tagName == "INPUT") {
         $(placeholder_id).value = "";
      } else if ( $(placeholder_id).tagName == "IFRAME") {
         window.frames[placeholder_id].document.body.innerHTML="";
      } else {
         $(placeholder_id).innerHTML = "";
      }
   }

   @@-- Action on select : add, delete or select an option, or clear a select

   function handler_select (node)
   {
      var select_action = node.getAttribute("action");
      var select_id     = node.getAttribute("id");
      if (select_action == "add") {
         var option_value = node.getAttribute("option_value");
         var option_content = node.getAttribute("option_content");
         AWS.Tools.addToSelect (select_id, option_value, option_content);
      } else if (select_action == "clear") {
         AWS.Tools.clearSelect (select_id);
      } else if (select_action == "delete") {
         var option_value = node.getAttribute("option_value");
         AWS.Tools.delSelectOption (select_id, option_value);
      } else if (select_action == "select") {
         var option_value = node.getAttribute("option_value");
         AWS.Tools.selectChangeSelected (select_id, option_value);
      }
   }

   @@-- Action on list : add an element

   function handler_insert_after (node)
   {
      var id = node.getAttribute('id');
      var placeholder_content;

      if (node.childNodes.length > 1) {
         placeholder_content = node.childNodes[1].nodeValue;
      } else {
         placeholder_content = node.firstChild.nodeValue;
      }

      new Insertion.After (id, placeholder_content);
      AWS.Ajax.update_framework();
   }

   @@-- Action on radio

   function handler_radio (node)
   {
      var action = node.getAttribute('action');
      var id     = node.getAttribute('id');
      if (action == 'select') {
         $(id).checked = 1;
      }
   }

   @@-- Action on check

   function handler_check (node)
   {
      var action = node.getAttribute('action');
      var id     = node.getAttribute('id');
      if (action == 'select') {
         $(id).checked = 1;
      } else if (action == 'clear') {
         $(id).checked = 0;
      }
   }

   @@-- Action on get

   function handler_get (node)
   {
      var url  = node.getAttribute('url');
      var pars = '';
      var i    = 0;

      for (i = 0; i < node.childNodes.length; i++) {
         var current_node = node.childNodes[i];
         if (current_node.nodeName == 'parameters') {
            if (pars != '') {
               pars = pars + '&';
            }
            pars = pars + current_node.getAttribute('value');
         } else if (current_node.nodeName == 'field') {
            if (pars != '') {
               pars = pars + '&';
            }
            var id = current_node.getAttribute('id');
            pars = pars + AWS.Ajax.serialize (id);
         }
      }
      AWS.Ajax.update_framework(); /* Update behaviour before request url */
      AWS.Ajax.XML.request (url, pars, '');
   }

   @@-- Action on location

   function handler_location (node)
   {
      var url = node.getAttribute('url');

      if (url.indexOf('http',0) == -1 && url.indexOf('@_CTX_WB_@') == -1) {
         @@--  pointing inside server and no context
         if (url.indexOf("?",0) == -1)
            url = url + '?';
         else
            url = url + '&';

         url = url + "@_CTX_WB_@=" + AWS.Context.get();
      }

      /* Redirect to url */
      document.location.href = url;
   }

   @@-- Action on create_sortable

   function handler_create_sortable (node)
   {
      var lists = []; /* Get all list for containment */

      var i = 0;

      for (i = 0; i < node.childNodes.length; i++) {
         var current_node = node.childNodes[i];
         if (current_node.nodeName == 'list') {
            lists.push (current_node.getAttribute('id'));
         }
      }

      /* Call Sortable.create for each element */

      for (i = 0; i < node.childNodes.length; i++) {
         var current_node = node.childNodes[i];
         if (current_node.nodeName == 'list') {
            var id = current_node.getAttribute('id');

            /* Add cursor style */

            var elm = document.getElementById (id);
            elm.style.cursor = 'move';

            /* Create sortable */

            Sortable.create
               (id, { containment:lists, constraint:false, dropOnEmpty:true });
         }
      }
   }

   @@-- Action on destroy_sortable

   function handler_destroy_sortable (node)
   {
      var i = 0;

      /* Call Sortable.destroy for each element */

      for (i = 0; i < node.childNodes.length; i++) {
         var current_node = node.childNodes[i];
         if (current_node.nodeName == 'list') {
            var list_id = current_node.getAttribute('id');

            /* Revert cursor style */

            var elm = document.getElementById (list_id);
            elm.style.cursor = 'default';

            /* Destroy sortable */
            Sortable.destroy (list_id);
         }
      }
   }

   @@-- Action on apply_style

   function handler_apply_style (node)
   {
      var elm_id = node.getAttribute('id');

      if ($(elm_id) == null) return;

      var i = 0;
      for (i = 0; i < node.childNodes.length; i++) {
         var current_node = node.childNodes[i];
         if (current_node.nodeName == 'attribute') {
            var id    = current_node.getAttribute('id');
            var value = current_node.getAttribute('value');
            $(elm_id).style[id] = value;
         }
      }
   }
}

@@-- INIT

AWS.Tools.addLoadEvent (AWS.Context.update);