summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/modules/edit/htdocs/page/index.html
blob: 0102f4edace5ad7a55cd9e624a238e68c49244f6 (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
<!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>
<head>
<title>Page</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"/>
<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>
<script type="text/javascript" src="/menu.js"></script>
<script type="text/javascript" src="page.js"></script>
</head>
<body class="delayed" onload="ui.onload();">
<div id="bodydiv" class="devicewidth">

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

<div id="menu"></div>

<table style="width: 100%;">
<tr>
<td><h2><span id="h1"></span><span id="appNameHeader"></span></h2></td>
<td style="vertical-align: middle; text-align: right; padding-right: 2px;"><span id="saveStatus" style="font-weight: bold; color: #808080;">Saved</span></td>
</tr>
</table>

<table style="width: 100%;">
<tr>
<th class="thr thl" style="padding-left: 2px; padding-right: 2px; width: 100%;">
<input id="widgetValue" type="text" value="" title="Widget value" autocapitalize="off" placeholder="Value" style="position: relative; width: 100%;"/>
</th>

<th class="thl thr" style="text-align: right; padding-right: 2px;">
<span id="deleteWidgetButton" title="Delete a Widget" class="redbutton" style="font-weight: bold; font-size: 16px; color: #808080; display: inline-block; width: 24px; height: 20px; padding-top: 0px; padding-bottom: 0px; vertical-align: middle; text-align: center; margin-left: 0px; margin-right: 0px;">-</span>

<span id="addWidgetButton" title="Add a Widget" class="greenbutton" style="font-weight: bold; font-size: 16px; display: inline-block; width: 24px; height: 20px; padding-top: 0px; padding-bottom: 0px; vertical-align: middle; text-align: center; margin-left: 0px; margin-right: 0px;">+</span>

<span id="playPageButton" title="View page" class="bluebutton" style="font-weight: bold; font-size: 16px; display: inline-block; width: 24px; height: 20px; padding-top: 0px; padding-bottom: 0px; vertical-align: middle; text-align: center; margin-left: 0px; margin-right: 0px;">&gt;</span>
</th>
</tr>
</table>

<div id="contentdiv" style="margin-top: 4px; width: 100%;">
<div id="editdiv" style="visibility: visible; position: relative; top: 0px; left: -2500px; right: 0px; height: 5000px;">

<div style="position: relative; left: 2500px; top: 0px; right: 0px; height: 5000px; border:1px; border-style: solid; border-color: #a2bae7; background: url(/public/grid72.png);"></div>
<div class="guide" style="position: absolute; left: 2500px; top: 0px; width: 320px; height: 460px;"></div>
<div class="guide" style="position: absolute; left: 2500px; top: 0px; width: 480px; height: 300px;"></div>
<div class="guide" style="position: absolute; left: 2500px; top: 0px; width: 768px; height: 911px;"></div>
<div class="guide" style="position: absolute; left: 2500px; top: 0px; width: 1024px; height: 655px;"></div>

<span class="h1" id="palette:h1" style="position: absolute; left: 0px; top: 0px;"><h1>Header1</h1></span>
<span class="h2" id="palette:h2" style="position: absolute; left: 0px; top: 30px;"><h2>Header2</h2></span>
<span class="section" id="palette:section" style="position: absolute; left: 0px; top: 60px; width: 200px;"><span class="section">section</span></span>
<span class="button" id="palette:button" style="position: absolute; left: 0px; top: 90px;"><input type="button" value="button" class="graybutton"/></span>
<span class="entry" id="palette:entry" style="position: absolute; left: 0px; top: 120px;"><input type="text" value="field" size="20" autocapitalize="off"/></span>
<span class="password" id="palette:password" style="position: absolute; left: 0px; top: 150px;"><input type="password" value="password" size="20"/></span>
<span class="checkbox" id="palette:checkbox" style="position: absolute; left: 0px; top: 180px;"><input type="checkbox" value="checkbox"/><span>checkbox</span></span>
<span class="select" id="palette:select" style="position: absolute; left: 0px; top: 210px;"><select><option value="select">select</option></select></span>
<span class="list" id="palette:list" style="position: absolute; left: 0px; top: 240px; width: 200px;">
<table class="datatable" style="width: 200px;"><tr><td class="datatd">list</td></tr><tr><td class="datatd">...</td></tr></table>
</span>
<span class="table" id="palette:table" style="position: absolute; left: 0px; top: 290px; width: 200px;">
<table class="datatable" style="width: 200px;"><tr><td class="datatdl">table</td><td class="datatdr">...</td></tr><tr><td class="datatdl">...</td><td class="datatdr">...</td></tr></table>
</span>
<span class="link" id="palette:link" style="position: absolute; left: 0px; top: 340px;"><a href="/"><span>link</span></a></span>
<span class="text" id="palette:text" style="position: absolute; left: 0px; top: 370px;"><span>text</span></span>
<span class="iframe fakeframe" id="palette:iframe" style="position: absolute; left: 0px; top: 400px; width: 200px;"><a href="/public/iframe.html"><span class="fakeframe"><span>frame ...</span></span></a></span>
<span class="img" id="palette:img" style="position: absolute; left: 0px; top: 430px;"><img src="/public/img.png"/></span>
</div>

<div id="playdiv" style="visibility: hidden; position: absolute; top: 0px; left: 0px; right: 0px; height: 5000px;">
</div>
</div>

<div id="buffer" style="visibility: hidden; width: 0px; height: 0px"></div>

<script type="text/javascript">
// Get the app name
var appname = ui.queryParams()['app'];
if (isNil(appname))
    window.open('/', '_self');

/**
 * Return the link to an app.
 */
function applink(appname) {
    var protocol = window.location.protocol;
    var host = window.location.hostname;
    var port = ':' + window.location.port;
    if (port == ':80' || port == ':443' || port == ':')
        port = '';
    var link = protocol + '//' + appname + '.' + host + port + '/';
    return link;
}

// Set page titles
document.title = windowtitle(window.location.hostname) + ' - Page - ' + appname;
//$('h1').innerHTML = hometitle(window.location.hostname);
$('appNameHeader').innerHTML = '<a href=\"' + applink(appname) + '\" target=\"' + '_blank' + '\">' + appname + '.' + window.location.hostname + '</a>';

// Load the menu bar
displaymenu();

/**
 * Page editor area, widget value field, add, delete and play page buttons.
 */
var cdiv = $('contentdiv');
var ediv = $('editdiv');
var evisible = true;
var pdiv = $('playdiv');
var wvalue = $('widgetValue');
var wadd = $('addWidgetButton');
var wdelete = $('deleteWidgetButton');
var pplay = $('playPageButton');

// Position edit and play divs inside the content div
ediv.style.position = 'absolute';
ediv.style.top = cdiv.offsetTop + 'px';
pdiv.style.position = 'absolute';
pdiv.style.top = cdiv.offsetTop + 'px';

/**
 * Adjust fields sizes.
 */
function resizeFields() {
    wvalue.style.width = '0px';
    wvalue.style.width = ui.pixpos(wvalue.parentNode.clientWidth - 18);
    return true;
}

resizeFields();
window.onresize = resizeFields;

// Init component references
var editWidget = sca.component("EditWidget");
var pages = sca.reference(editWidget, "pages");

/**
 * Return the page in an ATOM entry.
 */
function atompage(doc) {
    var entry = atom.readATOMEntry(mklist(doc));
    if (isNil(entry))
        return mklist();
    var content = namedElementChild("'content", car(entry));
    if (content == null)
        return mklist();
    return elementChildren(content);
}

/**
 * Track the current page saved XHTML content.
 */
var savedpagexhtml = '';

/**
 * Track the current widget.
 */
var widget = null;

/**
 * Get and display an app page.
 */
function getpage(name, ediv) {
    if (isNil(name))
        return false;
    return pages.get(name, function(doc) {

        // Convert the page to XHTML and place it in a hidden buffer
        var buffer = $('buffer');
        var el = atompage(doc);

        // Create a default empty page if necessary
        if (isNil(el))
            buffer.innerHTML = '<div id="page"></div>';
        else
            buffer.innerHTML = writeStrings(writeXML(atompage(doc), false));

        // Append page nodes to editor
        map(function(e) {
                ediv.appendChild(e);
                if (!isNil(e.style))
                    e.style.left = ui.pixpos(ui.numpos(e.style.left) + 2500);
                return page.cover(e);
            }, nodeList(buffer.childNodes[0].childNodes));

        savedpagexhtml = pagexhtml(ediv);
        return true;
    });
}

/**
 * Handle add widget button click event.
 */
wadd.onclick = function(e) {
    // Show the widget palette
    ediv.style.left = ui.pixpos(0);
};

/**
 * Return the current page XHTML content.
 */
function pagexhtml(ediv) {

    // Copy page DOM to hidden buffer
    var buffer = $('buffer');
    buffer.innerHTML = '<div id="page"></div>'
    var div = buffer.childNodes[0];

    // Capture the nodes inside the page div
    div.innerHTML = ediv.innerHTML;
    var nodes = nodeList(div.childNodes);
    map(function(e) {
        div.removeChild(e);
        return e;
    }, nodes);

    // Filter out palette and editor artifacts, which are not
    // part of the page, as well as nodes positioned out the
    // editing area
    var fnodes = filter(function(e) {
        if (isNil(e.id) || e.id == '' || e.id.substr(0, 8) == 'palette:')
            return false;
        var x = ui.numpos(e.style.left) - 2500;
        if (x < 0 || ui.numpos(e.style.top) < 0)
            return false;
        return true;
    }, nodes);

    // Reposition nodes
    map(function(e) {
        var x = ui.numpos(e.style.left) - 2500;
        e.style.left = ui.pixpos(x);
        return e;
    }, fnodes);

    // Sort them by position
    var snodes = fnodes.sort(function(a, b) {
        var ay = ui.numpos(a.style.top);
        var by = ui.numpos(b.style.top);
        if (ay < by) return -1;
        if (ay > by) return 1;
        var ax = ui.numpos(a.style.left);
        var bx = ui.numpos(b.style.left);
        if (ax < bx) return -1;
        if (ax > bx) return 1;
        return 0;
    });

    // Append the sorted nodes back to the div in order
    map(function(e) {
        div.appendChild(e);
        return e;
    }, snodes);

    // Convert the page to XHTML
    var lxhtml = readXHTMLElement(div);
    var xhtml = writeStrings(writeXML(lxhtml, false));
    return xhtml;
}

/**
 * Save the current page.
 */
function save(newxml) {
    $('saveStatus').innerHTML = 'Saving';

    // Get the current page XHTML content
    savedpagexhtml = newxml;

    // Update the page ATOM entry
    var entry = '<entry xmlns="http://www.w3.org/2005/Atom">' +
        '<title type="text">' + appname + '</title><id>' + appname + '</id><content type="application/xml">' +
        newxml + '</content></entry>';

    pages.put(appname, entry, function(r) {
        $('saveStatus').innerHTML = 'Saved';
    });
    return true;
};

/**
 * Handle a page change event
 */
function onpagechange(prop) {
    var newxml = pagexhtml(ediv);
    if (savedpagexhtml == newxml)
        return false;
    $('saveStatus').innerHTML = 'Modified';

    // Save property changes right away
    if (prop)
        return save(newxml);

    // Autosave other changes after 1 second
    setTimeout(function() {
        if (savedpagexhtml == newxml) {
            $('saveStatus').innerHTML = 'Saved';
            return false;
        }
        return save(newxml);
    }, 1000);
    return true;
}

/**
 * Return the link to a component value.
 */
function compvaluelink(appname, cname) {
    if (cname == '' || isNil(cname))
        return '';
    var protocol = window.location.protocol;
    var host = window.location.hostname;
    var port = ':' + window.location.port;
    if (port == ':80' || port == ':443' || port == ':')
        port = '';
    var link = protocol + '//' + appname + '.' + host + port + '/data/?component=' + cname;
    return link;
}

/**
 * Handle a widget select event.
 */
function onwidgetselect(w) {
    if (w == widget)
        return true;
    widget = w;
    var link = compvaluelink(appname, isNil(w)? '' : w.id);

    function updateButton(b, v) {
        b.style.color = v? '#000000' : '#808080';
    }

    updateButton(wdelete, link != '');
    return true;
}

/**
 * Play page in a frame.
 */
function playpage() {
    if (!evisible)
        return true;
    page.widgetselect(widget, false, wvalue, wdelete);
    page.selected = null;
    wvalue.value = applink(appname);
    pplay.innerHTML = '&lt;';
    ediv.style.visibility = 'hidden'
    evisible = false;
    pdiv.style.visibility = 'visible';
    pdiv.innerHTML = '<iframe id="playappframe" style="position: relative; height: 5000px; width: 100%; border: 0px;" scrolling="no" frameborder="0" src="' +
                        applink(appname) + '"></iframe>';
    return true;
}

/**
 * Show the page editor.
 */
function showedit() {
    if (evisible)
        return true;
    pplay.innerHTML = '&gt;';
    pdiv.style.visibility = 'hidden';
    pdiv.innerHTML = '';
    ediv.style.visibility = 'visible'
    evisible = true;
    page.widgetselect(widget, true, wvalue, wdelete);
    page.selected = widget;
    return true;
}

/**
 * Handle play page button event.
 */
pplay.onclick = function() {
    if (!evisible)
        return showedit();
    return playpage();
}

// Initialize the page editor
page.edit(ediv, wvalue, wadd, wdelete, onpagechange, onwidgetselect);

// Get and display the current app page
getpage(appname, ediv);
</script>

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

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