summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/modules/edit/htdocs/app/index.html
blob: f62cc5d0eba02becb04361fd34ecde8aa8387b51 (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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
<!DOCTYPE html>
<!--
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
-->
<html manifest="/cache-manifest.cmf">
<head>
<title></title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0"/> 
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black"/>
<script type="text/javascript">
document.title = window.location.hostname.split('.')[0];
</script>
<link rel="apple-touch-icon" href="/public/touchicon.png"/>
<link rel="stylesheet" type="text/css" href="/ui-min.css"/>
<script type="text/javascript" src="/all-min.js"></script>
</head>
<body class="delayed" onload="ui.onload();" onbeforeunload="ui.onbeforeunload();">
<div id="bodydiv" class="bodydiv">

<div id="headdiv" class="hsection">
<script type="text/javascript" src="/headconfig.js"></script>
</div>

<div id="app">
<iframe id="appframe" style="position: relative; height: 5000px; width: 100%;" scrolling="no" frameborder="0" src="/frame.html"></iframe>
</div>

<div id="appbuffer" style="visibility: hidden">
</div>

<script type="text/javascript">
ui.initbody();

/**
 * The main app div.
 */
var appdiv = $('app');
var appframe = $('appframe');
var appbody;

/**
 * Start, stop, timer, animation and location components.
 */
var startcomp = sca.httpclient('start', '/start');
var stopcomp = sca.httpclient('stop', '/stop');
var timercomp = sca.httpclient('timer', '/timer');
var animationcomp = sca.httpclient('animation', '/animation');
var locationcomp = sca.httpclient('location', '/location');

/**
 * Find a named value in a tree of elements. The value name is given
 * as a list of ids.
 */
function namedvalue(l, id) {
    if (isNil(l))
        return null;
    var e = car(l);

    // Element matches id segment
    if (car(id) == elementName(e)) {

        // Found element matching the whole id path
        if (isNil(cdr(id)))
            return e;

        // Search for next id segments in child elements
        if (!elementHasValue(e)) {
            var v = namedvalue(elementChildren(e), cdr(id));
            if (v != null)
                return v;
        }
    }

    // Search for id through the whole element tree
    if (!elementHasValue(e)) {
        var v = namedvalue(elementChildren(e), id);
        if (v != null)
            return v;
    }
    return namedvalue(cdr(l), id);
}

/**
 * Return the value of an input element.
 */
function inputvalue(e) {
    if (e.className == 'entry' || e.className == 'password') {
        return car(childElements(e)).value;
    }
    if (e.className == 'button') {
        return car(childElements(e)).value;
    }
    if (e.className == 'checkbox') {
        if (!car(childElements(e)).checked)
            return null;
        return car(childElements(e)).value;
    }
    if (e.className == 'select') {
        return car(childElements(car(childElements(e)))).value;
    }
    return null;
};

/**
 * Set a value into a widget.
 */
function setwidgetvalue(e, dv) {
    var htattrs = namedElementChild("'htattrs", dv);

    function attr(ce) {
        return mklist(elementName(ce) == "'htstyle"? 'style' : elementName(ce).substring(1), elementHasValue(ce)? elementValue(ce) : elementChildren(ce));
    }

    function vattr(dv) {
        return (elementHasValue(dv) && !isNil(elementValue(dv)))? mklist(mklist('value', isNil(elementValue(dv))? '' : elementValue(dv))) : mklist();
    }

    function sattr(dv) {
        var s = namedElementChild("'htstyle", dv);
        return isNil(s)? mklist() : mklist(mklist('style', elementHasValue(s)? elementValue(s) : elementChildren(s)))
    }

    var attrs = append(append(isNil(htattrs)?  mklist() : map(attr, elementChildren(htattrs)), vattr(dv)), sattr(dv));

    // Set the attributes of the widget
    function setattrs(vsetter, attrs, ce) {
        return map(function(a) {
            if (car(a) == 'value')
                return vsetter(a, ce);

            if (car(a) == 'style') {
                // Split a style property between a style attribute
                // and a stylesheet definition in the document's head

                function prop(s) {
                    if (s == ';')
                        return '';
                    var i = s.indexOf('<style>');
                    if (i == -1)
                        return s;
                    var j = s.indexOf('</style>');
                    return s.substring(0, i) + prop(s.substring(j + 8));
                }

                function sheet(s) {
                    var i = s.indexOf('<style>');
                    if (i == -1)
                        return '';
                    var j = s.indexOf('</style>');
                    return s.substring(i + 7, j) + sheet(s.substring(j + 8));
                }

                var st = cadr(a).replace(new RegExp('{id}', 'g'), e.id);
                var p = prop(st);
                var s = sheet(st);

                // Define the stylesheet
                if (s != '') {
                    var esheet = appframe.contentDocument.getElementById('style_' + e.id);
                    if (isNil(esheet)) {
                        var nesheet = document.createElement('style');
                        nesheet.id = 'style_' + e.id;
                        nesheet.type = 'text/css';
                        appframe.contentDocument.getElementsByTagName('head')[0].appendChild(nesheet);
                        nesheet.innerHTML = s;
                    } else {
                        esheet.innerHTML = s;
                    }
                }

                var aname = ce.style.webkitAnimationName;

                // Set the style attribute
                ce.setAttribute('style', p);

                // Restart current animation if necessary
                if (!isNil(aname) && ce.style.webkitAnimationName == aname) {
                    ce.style.webkitAnimationName = '';
                    setTimeout(function() {
                        ce.style.webkitAnimationName = aname;
                    }, 0);
                }
                return a;
            }

            ce.setAttribute(car(a), cadr(a));
            return a;
        }, attrs);
    }

    if (e.className == 'h1' || e.className == 'h2' || e.className == 'text' || e.className == 'section') {
        var ce = car(childElements(e));
        return setattrs(function(a, ce) { ce.innerHTML = cadr(a); }, attrs, ce);
    }
    if (e.className == 'entry' || e.className == 'password') {
        var ce = car(childElements(e));
        return setattrs(function(a, ce) { ce.defaultValue = cadr(a); }, attrs, ce);
    }
    if (e.className == 'button') {
        var ce = car(childElements(e));
        return setattrs(function(a, ce) { ce.value = cadr(a); }, attrs, ce);
    }
    if (e.className == 'checkbox') {
        var ce = car(childElements(e));

        function setcheckvalue(a, ce) {
            var v = cadr(a);
            ce.value = v;
            map(function(n) { if (n.nodeName == "SPAN") n.innerHTML = v; return n; }, nodeList(e.childNodes));
            return true;
        }

        return setattrs(setcheckvalue, attrs, ce);
    }
    if (e.className == 'select') {
        var ce = car(childElements(car(childElements(e))));

        function setselectvalue(a, ce) {
            var v = cadr(a);
            ce.value = v;
            ce.innerHTML = v;
            return true;
        }

        return setattrs(setselectvalue, attrs, ce);
    }
    if (e.className == 'list') {
        var dl = ui.datalist(isNil(dv)? mklist() : mklist(dv));
        e.innerHTML = dl;
        return dl;
    }
    if (e.className == 'table') {
        var dl = ui.datatable(isNil(dv)? mklist() : mklist(dv));
        e.innerHTML = dl;
        return dl;
    }
    if (e.className == 'link') {
        var ce = car(childElements(e));

        function setlinkvalue(a, ce) {
            var v = cadr(a);
            if (isList(v)) {
                ce.href = car(v);
                ce.innerHTML = cadr(v);
                return true;
            }
            ce.href = v;
            ce.innerHTML = v;
            return true;
        }

        return setattrs(setlinkvalue, attrs, ce);
    }
    if (e.className == 'img') {
        var ce = car(childElements(e));
        return setattrs(function(a, ce) { ce.setAttribute('src', cadr(a)); }, attrs, ce);
    }
    if (e.className == 'iframe') {
        var ce = car(childElements(e));
        return setattrs(function(a, ce) { ce.setAttribute('src', cadr(a)); }, attrs, ce);
    }
    return '';
};

/**
 * Update the app page with the given app data.
 */
function updatepage(l) {
    if (isNil(l))
        return true;

    // Update the widgets values
    function updatewidget(e) {
        var dv = namedvalue(l, map(function(t) { return "'" + t; }, e.id.split('.')));
        if (dv == null || isNil(dv))
            return e;
        setwidgetvalue(e, dv);
        return e;
    }

    map(updatewidget, filter(function(e) { return !isNil(e.id) && e.id.substring(0, 5) != 'page:'; }, nodeList(ui.elementByID(appbody, 'page').childNodes)));
    return true;
}

/**
 * Convert a document to application data.
 */
function docdata(doc) {
    if (isNil(doc))
        return null;

    if (json.isJSON(mklist(doc)))
        return json.readJSON(mklist(doc));

    if (atom.isATOMEntry(mklist(doc)))
        return atom.readATOMEntry(mklist(doc));

    if (atom.isATOMFeed(mklist(doc)))
        return atom.readATOMFeed(mklist(doc));

    return doc;
}

/**
 * Bind a handler to a widget.
 */
function bindwidgethandler(e) {
    if (e.className == 'button') {
        var b = car(childElements(e));
        b.name = e.id;
        b.onclick = function() { return buttonClickHandler(b.value); };
        return e;
    }
    if (e.className == 'link') {
        var l = car(childElements(e));
        var hr = l.href;
        if (hr.substring(0, 5) == 'link:' && hr.indexOf('://') == -1) {
            var f = function(e) {
                e.preventDefault();
                return buttonClickHandler(hr.substring(5));
            };
            l.ontouchstart = l.onclick = f;
            l.href = 'javascript:void()';
        }
        return e;
    }
    if (e.className == 'entry' || e.className == 'password' || e.className == 'checkbox') {
        car(childElements(e)).name = e.id;
        return e;
    }
    if (e.className == 'select') {
        var ce = car(childElements(car(childElements(e))));
        ce.name = e.id;
        return e;
    }
    return e;
}

/**
 * Initial fixup of a widget.
 */
function fixupwidget(e) {
    if (e.className == 'h1' || e.className == 'h2' || e.className == 'text' || e.className == 'section') {
        if (e.className == 'section')
            e.style.width = '100%';
        var ce = car(childElements(e));
        if (ce.innerHTML == '=' + e.id)
            ce.innerHTML = '';
        return e;
    }
    if (e.className == 'entry' || e.className == 'password') {
        var ce = car(childElements(e));
        if (ce.defaultValue == '=' + e.id)
            ce.defaultValue = '';
        return e;
    }
    if (e.className == 'button') {
        var ce = car(childElements(e));
        if (ce.value == '=' + e.id)
            ce.value = '';
        return e;
    }
    if (e.className == 'checkbox') {
        var ce = car(childElements(e));
        if (ce.value == '=' + e.id) {
            ce.value = '';
            map(function(n) { if (n.nodeName == "SPAN") n.innerHTML = ''; return n; }, nodeList(e.childNodes));
        }
        return e;
    }
    if (e.className == 'select') {
        var ce = car(childElements(car(childElements(e))));
        if (ce.value == '=' + e.id) {
            ce.value = '';
            ce.innerHTML = '';
        }
        return e;
    }
    if (e.className == 'list') {
        car(childElements(e)).innerHTML = '';
        e.style.width = '100%';
        car(childElements(e)).style.width = '100%';
        return e;
    }
    if (e.className == 'table') {
        car(childElements(e)).innerHTML = '';
        e.style.width = '100%';
        car(childElements(e)).style.width = '100%';
        return e;
    }
    if (e.className == 'link') {
        var ce = car(childElements(e));
        if (ce.innerHTML == '=' + e.id)
            ce.innerHTML = '';
        return e;
    }
    if (e.className == 'img') {
        var ce = car(childElements(e));
        return e;
    }
    if (e.className == 'iframe') {
        var ce = car(childElements(e));
        e.innerHTML = '<iframe src="' + ce.href + '" frameborder="no" scrolling="no"></iframe>';
        return e;
    }
    return e;
}

/**
 * Set initial value of a widget.
 */
function initwidget(e) {
    if (!isNil(e.id) && e.id.substring(0, 5) != 'page:')
        setwidgetvalue(e, mklist());
    return e;
}

/**
 * Get document from a component.
 */
function getdoc(comp, name, uri) {
    try {
        return comp.getnocache(uri);
    } catch(e) {
        log('exception on get(' + name + ', ' + uri + ')', e);
        return null;
    }
}

/**
 * Get app data from the main app page component.
 */
function getpagedata() {
    try {

        // Display component data on the page
        function displaypage(doc) {
            updatepage(docdata(doc));
            return true;
        }

        // Eval a component init script
        function evalcompinit(doc) {
            if (isNil(doc))
                return true;
            var js = car(json.readJSON(mklist(doc)));
            if (!elementHasValue(js))
                return true;
            eval(elementValue(js));
            return true;
        }

        // Initial setup of a widget
        function setupwidget(e) {
            initwidget(e);
            fixupwidget(e);
            bindwidgethandler(e);
        }

        // Get the component app data
        var doc = getdoc(startcomp, 'start', window.location.search);

        // Prepare app HTML page
        appbody = appframe.contentDocument.body;
        var appbufferbody = $('appbufferframe').contentDocument.body;
        appbody.innerHTML = appbufferbody.innerHTML;
        appbufferbody.innerHTML = '';

        // Setup the widgets
        map(setupwidget, filter(function(e) { return !isNil(e.id); }, nodeList(ui.elementByID(appbody, 'page').childNodes)));

        // Display data on the page
        displaypage(doc);

        // Get and eval the optional timer, animation and location watch setup scripts
        evalcompinit(getdoc(timercomp, 'timer', 'setup'));
        evalcompinit(getdoc(animationcomp, 'animation', 'setup'));
        evalcompinit(getdoc(locationcomp, 'location', 'setup'));

        return true;

    } catch(e) {
        log('exception in getpagedata()', e);
        return true;
    }
}

/**
 * Build a query string from the values of the page's input fields.
 */
function compquery() {
    function queryarg(e) {
        return e.id + '=' + inputvalue(e);
    }

    function childrenList(n) {
        return append(nodeList(n.childNodes), reduce(append, mklist(), map(childrenList, nodeList(n.childNodes))));
    }

    var args = map(queryarg, filter(function(e) { return !isNil(e.id) && !isNil(inputvalue(e)); }, childrenList(ui.elementByID(appbody, 'page'))));

    // Append current location properties if known
    if (!isNil(geoposition)) {
        var g = geoposition;
        args = append(args, mklist('latitude=' + g.coords.latitude, 'longitude=' + g.coords.longitude, 'altitude=' + g.coords.altitude,
                        'accuracy=' + g.coords.accuracy, 'altitudeAccuracy=' + g.coords.altitudeAccuracy, 'heading=' + g.coords.heading,
                        'speed=' + g.coords.speed));
    }

    return '?' + args.join('&');
}

/**
 * Handle a button click event.
 */
function buttonClickHandler(id) {
    try {
        return updatepage(docdata(getdoc(sca.component(id), 'button', compquery())));
    } catch(e) {
        log('exception in buttonClickHandler()', e);
        return true;
    }
}

/**
 * Handle a timer interval event.
 */
function intervalHandler() {
    try {
        return updatepage(docdata(getdoc(timercomp, 'timer', compquery())));
    } catch(e) {
        log('exception in intervalHandler()', e);
        return true;
    }
}

/**
 * Setup an interval timer.
 */
function setupIntervalHandler(msec) {
    intervalHandler();
    try {
        return setInterval(intervalHandler, msec);
    } catch(e) {
        log('exception in setupIntervalHandler()', e);
        return true;
    }
}

/**
 * Handle an animation event.
 */
var animationData = null;
var currentAnimationData = null;
var animationLoop = 0;
var currentAnimationLoop = 0;

function animationHandler() {
    try {
        // Get animation data if necessary
        if (isNil(animationData)) {
            animationData = docdata(getdoc(animationcomp, 'animation', compquery()));
            if (isNil(animationData)) {
                // Retry later
                return true;
            }
            currentAnimationData = animationData;
            currentAnimationLoop = animationLoop;
        }

        // Update page with animation data
        updatepage(car(currentAnimationData));

        // End of animation?
        if (isNil(cdr(currentAnimationData))) {
            if (currentAnimationLoop == -1) {
                // Repeat current animation forever
                currentAnimationData = animationData;
                return true;
            }

            currentAnimationLoop = currentAnimationLoop - 1;
            if (currentAnimationLoop <= 0) {
                // Get next animation data
                currentAnimationData = null;
                animationData = null;
                return true;
            }

            // Repeat animation
            currentAnimationData = animationData;
            return true;
        }

        // Move to the next animation frame
        currentAnimationData = cdr(currentAnimationData);
        return true;

    } catch(e) {
        log('exception in animationHandler()', e);
        return true;
    }
}

/**
 * Setup an animation.
 */
function setupAnimationHandler(msec, loop) {
    animationLoop = loop;
    animationHandler();
    try {
        return setInterval(animationHandler, msec);
    } catch(e) {
        log('exception in setupAnimationHandler()', e);
        return true;
    }
}

/**
 * Handle a location watch event.
 */
var locationWatch = null;
var geoposition = null;

function locationHandler(pos) {
    try {
        geoposition = pos;
        return updatepage(docdata(getdoc(locationcomp, 'location', compquery())));
    } catch(e) {
        locationErrorHandler(e);
    }
}

function locationErrorHandler(e) {
    log('location error', e);
    if (!isNil(locationWatch)) {
        try {
            navigator.geolocation.clearWatch(locationWatch);
        } catch(e) {}
        locationWatch = null;
    }
    return true;
}

/**
 * Setup a location watch handler.
 */
function setupLocationHandler() {
    function installLocationHandler() {
        if (!isNil(locationWatch))
            return true;
        try {
            locationWatch = navigator.geolocation.watchPosition(locationHandler, locationErrorHandler);
        } catch(e) {
            log('exception in installLocationHandler()', e);
        }
    }

    installLocationHandler();
    setInterval(installLocationHandler, 10000);
    return true;
}

// Load the app frame
$('appbuffer').innerHTML = '<iframe id="appbufferframe" style="position: relative; height: 5000px; width: 100%;" scrolling="no" frameborder="0" src="app.html" onload="getpagedata()"></iframe>';
</script>

<div id="footdiv" class="fsection">
<script type="text/javascript" src="/footconfig.js"></script>
</div>

</div>
</body>
</html>