summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/hosting/server/htdocs/index.html
blob: b46141919d50ae7d5eec26f1cc7e44d81d1b255c (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
<!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, minimum-scale=1.0, maximum-scale=1.0"/> 
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black"/>
<link rel="apple-touch-icon" href="/public/touchicon.png"/>
<base href="/"/>
<script type="text/javascript">

window.appcache = {};

/**
 * Get and cache a resource.
 */
appcache.get = function(uri) {
    var h = uri.indexOf('#');
    var u = h == -1? uri : uri.substring(0, h);

    // Get resource from local storage first
    var ls = window.lstorage || localStorage;
    var item = null;
    try { item = ls.getItem(u); } catch(e) {}
    if (item != null && item != '')
        return item;

    // Get resource from network
    var http = new XMLHttpRequest();
    http.open("GET", u, false);
    http.setRequestHeader("Accept", "*/*");
    http.send(null);
    if (http.status == 200) {
        if (http.getResponseHeader("X-Login") != null) {
            if (debug) debug('http error', u, 'X-Login');
            // Redirect to login page if not signed in
            document.location = '/login/';
            return null;
        } else if (http.responseText == '' || http.getResponseHeader("Content-Type") == null) {
            if (debug) debug('http error', u, 'No-Content');
            return null;
        }
        try { ls.setItem(u, http.responseText); } catch(e) {}
        return http.responseText;
    }
    if (debug) debug('http error', u, http.status, http.statusText);
    // Redirect to login page if not signed in
    if (http.status == 403)
        document.location = '/login/';
    return null;
};

// Load Javascript and CSS
(function() {
    var bootjs = document.createElement('script');
    bootjs.type = 'text/javascript';
    bootjs.text = appcache.get('/all-min.js');
    document.head.appendChild(bootjs);
    document.head.appendChild(ui.declareCSS(appcache.get('/ui-min.css')));

    // Disable cache for testing
    lstorage.enabled = false;

})();

</script>
<script type="text/javascript">

// Redirect to login page if not signed in
if (document.location.protocol == 'https:' && !hasauthcookie())
    document.location = '/login/';

</script>
</head>
<body class="delayed" onload="onload();">
<div id="mainbodydiv" class="mainbody">

<div id="headdiv" class="hsection">
<script type="text/javascript">
(function() {
$('headdiv').appendChild(ui.declareScript(appcache.get('/config-min.js')));
})();
</script>
</div>

<div id="menubackground" class="tbarbackground fixed"></div>
<div id="menu" class="tbarmenu fixed"></div>

<div id="viewheadbackground" class="viewheadbackground fixed"></div>
<div id="viewhead" class="viewhead fixed"></div>

<div id="viewcontainer"></div>

<script type="text/javascript">

// Init service references
var editWidget = sca.component("EditWidget");
var user= sca.defun(sca.reference(editWidget, "user"));
var accounts = sca.reference(editWidget, "accounts");

// Set page title
document.title = ui.windowtitle(location.hostname);

// Init div variables
var bdiv = $('mainbodydiv');
var mdiv = $('menu'); 
var hdiv = $('viewhead'); 
var vcontainer = $('viewcontainer');
vcontainer.className = ui.isMobile()? 'viewcontainer3dm' : 'viewcontainer3d';

/**
 * The current user name and account entry.
 */
var username;
var accountentry;

/**
 * Pre-fetch app resources.
 */
var appresources = [
    ['/all-min.js'],
    ['/ui-min.css'],
    ['/account/', '9'],
    ['/clone/', '3'],
    ['/create/', '2'],
    ['/delete/', '3'],
    ['/graph/', '5'],
    ['/config-min.js'],
    ['/home/', '0'],
    ['/home/home.b64'],
    ['/page/', '4'],
    ['/public/app.b64'],
    ['/public/config-min.js'],
    ['/public/grid72.b64'],
    ['/public/iframe-min.html'],
    ['/public/img.b64'],
    ['/public/user.b64'],
    ['/stats/', '2'],
    ['/store/', '1']
];

/**
 * Handle application cache events.
 */
applicationCache.addEventListener('checking', function(e) {
    //debug('appcache checking', e);
    showStatus('Checking');
}, false);
applicationCache.addEventListener('error', function(e) {
    //debug('appcache error', e);
    showStatus(defaultStatus());
}, false);
applicationCache.addEventListener('noupdate', function(e) {
    //debug('appcache noupdate', e);
    showStatus(defaultStatus());
}, false);
applicationCache.addEventListener('downloading', function(e) {
    //debug('appcache downloading', e);
    showStatus('Updating');
}, false);
applicationCache.addEventListener('progress', function(e) {
    //debug('appcache progress', e);
    showStatus('Updating');
}, false);
applicationCache.addEventListener('updateready', function(e) {
    //debug('appcache updateready', e);
    try {
        applicationCache.swapCache();
    } catch(e) {}
    showStatus(defaultStatus());
    //debug('appcache swapped', e);
}, false);
applicationCache.addEventListener('cached', function(e) {
    //debug('appcache cached', e);
    map(function(res) {
        showStatus('Updating');
        appcache.get(res[0]);
    }, appresources);
    showStatus(defaultStatus());
}, false);

/**
 * Handle network offline/online events.
 */
window.addEventListener('offline', function(e) {
    //debug('going offline');
    showStatus('Offline');
}, false);
window.addEventListener('online', function(e) {
    //debug('going online');
    showStatus('Online');
}, false);

//debug(navigator.onLine? 'online' : 'offline');

/**
 * Handle view transitions.
 */

// Keep track of the current view url and uri
var viewurl = '';
var viewuri = '';
var viewidx = '';
var viewdiv;

/**
 * Record which transitions should be applied to each resource.
 */
var apptransitions = {};
map(function(res) {
    if (res.length > 1)
        apptransitions[res[0]] = res[1];
}, appresources);

/**
 * Return the transition that should be applied to a resource.
 */
function viewtransition(ouri, uri) {
    var ot = apptransitions[ouri];
    if (isNil(ot))
        return 'left';
    var t = apptransitions[uri];
    if (isNil(t))
        return 'left';
    return t < ot? 'right' : 'left';
}

/**
 * Create a new view div.
 */
function mkviewdiv(cname) {
    var vdiv = document.createElement('div');
    vdiv.className = cname;
    if (!ui.isMobile())
        return vdiv;

    // Handle view transition end
    function viewdivtransitionend(e) {
        if (e.target.className == 'leftviewunloaded3dm' || e.target.className == 'rightviewunloaded3dm')
            e.target.parentNode.removeChild(e.target);
    }
    vdiv.addEventListener('webkitTransitionEnd', viewdivtransitionend, false);
    vdiv.addEventListener('transitionend', viewdivtransitionend, false);
    return vdiv;
}

/**
 * Return the last visited location.
 */
function lastvisited() {
    return lstorage.getItem('ui.lastvisited');
}

/**
 * Build and show the menu bar.
 */
function showmenu(mdiv, view, appname) {
    mdiv.innerHTML = ui.menubar(
        append(mklist(ui.menu('Home', '/', '_view', view == 'home'), ui.menu('Store', '/#view=store', '_view', view === 'store')),
                (isNil(appname) || appname == 'undefined')?
                    mklist() :
                    mklist(
                        ui.menu('Stats', '/#view=stats&app=' + appname, '_view', view == 'stats'),
                        ui.menu('Page', '/#view=page&app=' + appname, '_view', view == 'page'),
                        ui.menu(config.logic, '/#view=graph&app=' + appname, '_view', view == 'graph'),
                        ui.menu('<span class="greentext" style="font-weight: bold">Run!</span>', '/' + appname + '/', '_blank', false))),
        (isNil(appname) || appname == 'undefined')? mklist(
            hasauthcookie()? ui.menufunc('Sign out', 'logout();', false) : ui.menu('Sign in', '/login/', '_self', false),
            ui.menu('Account', '/#view=account', '_view', view == 'account')) :
            mklist());
}

/**
 * Show a status message.
 */
function showStatus(s) {
    var sdiv = $('status');
    if (isNil(sdiv))
        return s;
    sdiv.style.color = '#808080'
    sdiv.innerHTML = s;
    return s;
}

/**
 * Show an error message.
 */
function showError(s) {
    var sdiv = $('status');
    if (isNil(sdiv))
        return s;
    sdiv.style.color = '#dd4b39'
    sdiv.innerHTML = s;
    return s;
}

/**
 * Return the default status message.
 */
function defaultStatus() {
    return navigator.onLine? 'Online' : 'Offline';
}

/**
 * Get the current user's account.
 */
function getaccount() {
    var doc = accounts.get();

    // Stop now if we didn't get an account
    if (doc == null) {
        username = 'anonymous';
        return false;
    }

    accountentry = car(elementsToValues(atom.readATOMEntry(mklist(doc))));
    username = cadr(assoc("'id", cdr(accountentry)));
    return true;
}

/**
 * Show a view.
 */
function showview(url) {
    //debug('showview', url);

    // Save last visited location
    lstorage.setItem('ui.lastvisited', url);

    // Determine the view to show
    var params = ui.fragmentParams(url);
    var view = isNil(params['view'])? 'home' : params['view'];;
    var uri = '/' + view + '/';
    var idx = isNil(params['idx'])? '1' : params['idx'];

    // Determine the transition to use
    var vtransition = uri == viewuri? (idx >= viewidx? 'left' : 'right') : viewtransition(viewuri, uri);

    // Track current view url and uri
    viewurl = url;
    viewuri = uri;
    viewidx = idx;

    // Show the menu bar
    var appname = params['app'];
    showmenu(mdiv, view, appname);

    // Scroll to the top and hide the address bar
    window.scrollTo(0, 0);

    // Start to unload the front view and create a new view
    if (ui.isMobile()) {
        // Prepare current view for transition out
        var ovdiv = viewdiv;
        if (!isNil(ovdiv)) {
            ovdiv.skipNode = true;
            ovdiv.className = 'viewunloading3dm';
        }

        // Load the requested doc into a new view
        var vdiv = mkviewdiv(vtransition + 'viewloading3dm');
        var vdoc = appcache.get(uri);
        vdiv.innerHTML = vdoc;
        vcontainer.appendChild(vdiv);
        map(ui.evalScript, ui.innerScripts(vdiv));

        // Make sure the top document is visible
        if (document.body.style.visibility != 'visible')
            document.body.style.visibility = 'visible';

        setTimeout(function() {
            // Transition the old view out
            if (!isNil(ovdiv))
                ovdiv.className = vtransition + 'viewunloaded3dm';

            // Transition the new view in
            vdiv.className = 'viewloaded3dm';
        }, 100);

    } else {
        // Prepare current view for transition out
        var ovdiv = viewdiv;
        if (!isNil(ovdiv))
            ovdiv.skipNode = true;

        // Load the requested doc into the view
        var vdiv = mkviewdiv('viewloading3d');
        var vdoc = appcache.get(uri);
        vdiv.innerHTML = vdoc;
        vcontainer.appendChild(vdiv);
        map(ui.evalScript, ui.innerScripts(vdiv));

        // Make sure the top document is visible
        if (document.body.style.visibility != 'visible')
            document.body.style.visibility = 'visible';

        setTimeout(function() {
            // Transition the new view in
            vdiv.className = 'viewloaded3d';

            // Transition the old view out
            if (!isNil(ovdiv))
                ovdiv.parentNode.removeChild(ovdiv);
        }, 100);
    }

    // Track the current visible view
    viewdiv = vdiv;

    return true;
}

/**
 * Update the browser window location.
 */
function updatelocation(url) {
    //debug('updatelocation', url);

    // Add url to the history if necessary
    if (url != ui.pathandparams(location)) {
        history.pushState(null, null, url);
        //debug('pushstate', history.length);

        // Update the location hash if necessary
        var f = ui.fragment(url);
        if (f != '' && f != location.hash) {
            location.hash = f;
            //debug('hash', f);
        }
    }
    return url;
}

/**
 * Handle navigations.
 */
window.onnavigate = function(url) {
    //debug('onnavigate', url);

    updatelocation(url);

    // Show the specified view
    if (url == viewurl)
        return true;
    return showview(url);
};

/**
 * Handle login redirect.
 */
window.onloginredirect = function(e) {
    document.location = '/login/';
};

/**
 * Log the current user out.
 */
function logout() {
    // Clear session cookie and user-specific local storage entries
    clearauthcookie();
    lstorage.removeItem('/r/EditWidget/accounts');
    lstorage.removeItem('/r/EditWidget/dashboards');
    document.location = '/login/';
    return true;
}

/**
 * Handle history.
 */
window.addEventListener('popstate', function(e) {
    //debug('onpopstate', history.length);
    var furl = ui.fragment(location);
    var url = location.pathname + (furl == ''? '' : '#' + furl);

    // Show the current view
    if (url == viewurl)
        return true;
    return showview(url);

}, false);

window.addEventListener('hashchange', function(e) {
    //debug('onhashchange');
    var furl = ui.fragment(location);
    var url = location.pathname + (furl == ''? '' : '#' + furl);

    // Show the current view
    if (url == viewurl)
        return true;
    return showview(url);

}, false);

/**
 * Handle orientation change.
 */
document.body.onorientationchange = function(e) {
    //debug('onorientationchange');
    ui.onorientationchange(e);

    // Resize menu and view header
    mdiv.style.width = ui.pixpos(document.documentElement.clientWidth);
    hdiv.style.width = ui.pixpos(document.documentElement.clientWidth);
    return true;
};

/**
 * Document load post processing.
 */
function onload() {
    //debug('onload', history.length);
    ui.onload();

    // Get the current user account
    getaccount();

    // Show the view specified in the given url fragment
    var furl = ui.fragment(location);
    if (furl != '') {
        var url = location.pathname + '#' + furl;
        if (url == viewurl)
            return true;
        return showview(url);
    }

    // Show the last visited view
    if (ui.isMobile() && (document.referrer == null || document.referrer == '')) {
        //debug('show lastvisited');
        var lv = lastvisited();
        var url = isNil(lv)? location.pathname : lv;
        updatelocation(url);
        if (url == viewurl)
            return true;
        return showview(url);
    }

    // Show the main home view
    var url = location.pathname;
    if (url == viewurl)
        return true;
    return showview(url);
}

</script>

<div id="footdiv" class="fsection">
</div>

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