-
Notifications
You must be signed in to change notification settings - Fork 15
/
tryit.3.7.html
584 lines (536 loc) · 17.6 KB
/
tryit.3.7.html
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
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<title>Tryit Editor v3.7 luce80</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/w3.css">
<link rel="stylesheet" href="css/codemirror.css">
<link rel="stylesheet" href="css/tit-fontello.css">
<script src="js/codemirror/codemirror.js"></script>
<script src="js/codemirror/addon/edit/closetag.js"></script>
<script src="js/codemirror/addon/edit/closebrackets.js"></script>
<script src="js/codemirror/addon/fold/xml-fold.js"></script>
<script src="js/codemirror/mode/xml/xml.js"></script>
<script src="js/codemirror/mode/javascript/javascript.js"></script>
<script src="js/codemirror/mode/css/css.js"></script>
<script src="js/codemirror/mode/htmlmixed/htmlmixed.js"></script>
<script>
function $Id(id) {return document.getElementById(id)};
function $Tag(tag) {return document.getElementsByTagName(tag)};
if (window.addEventListener) {
window.addEventListener("resize", browserResize);
} else if (window.attachEvent) {
window.attachEvent("onresize", browserResize);
}
//window.onbeforeunload = function(e) { if (e) {e.returnValue = "You have unsaved changes."}; return "Are you sure?"; }
function browserResize() {
if (window.screen.availWidth <= 768) {
restack(window.innerHeight > window.innerWidth);
}
showFrameSize();
}
</script>
<style>
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
background-color: #f1f1f1;
color: #000000;
margin: 0px;
}
ul {
list-style-type: none;
margin: 0;
padding: 0px 10px;
overflow: hidden;
}
li {
float: left;
margin: 2px;
}
li a {
display: inline-block;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
top: 44px;
min-width: 160px;
width: 40%;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.5);
background-color: hsl(0, 0%, 95%);
z-index: 10;
}
.dropdown-content p {
color: black;
padding: 2px 16px;
font-size: 12px;
}
.switch {
position: relative;
display: inline-block;
margin: 0 4px;
}
.switch input {
display: none;
}
.slider {
box-sizing: border-box;
position: static;
width: 50px;
height: 40px;
margin: 0px 4px;
background: white;
border: solid lightgrey;
border-radius: 20%;
border-width: 4px 20px 4px 4px;
display: inline-block;
vertical-align: -14px;
cursor: pointer;
transition: border 0.3s;
}
/*.slider:hover { } */
input:checked + .slider {
border-color: hsl(122, 39.4%, 49.2%);/*w3-green*/
border-width: 4px 4px 4px 20px;
}
.w3-bar .w3-bar-item:hover {
color: #757575 !important;
}
.w3-bar .w3-bar-item {
margin: 2px;
height: calc(100% - (2px + 2px));
padding: 2px 12px;
}
.dropdown {
display: inline;
z-index: 2;
}
.CodeMirror.cm-s-default {
line-height: normal;
padding: 4px;
height: 100%;
width: 100%;
}
#container {
margin: 0px 10px;
position: absolute;
height: calc(100% - 10px - 44px);
width: calc(100% - 20px);
top: 44px; bottom: 0px; left: 0; right: 0;
}
#textareacontainer, #dragbar, #iframecontainer {
float: left;
height: 100%;
width: calc(50% - 6px);
box-shadow: 0px 3px 5px -1px rgb(182, 181, 181);
}
#dragbar {
width: 12px;
box-shadow: none;
cursor: col-resize;
}
#filename {
border: 1px solid hsl(130, 100%, 30%);
background: white;
width: 300px;
padding: 7px 4px;
text-align: left;
}
#shield {
display: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 4;
}
#framesize span {
font-family: Consolas, monospace;
}
#iframeResult {
background-color: #ffffff;
border: none;
height: 100%;
width: 100%;
}
@media screen and (max-width: 1260px) {
#container {
top: 88px;
height: calc(100% - 10px - 88px);
}
}
@media screen and (max-width: 450px) {
#container {
top: 160px;
height: calc(100% - 10px - 160px);
}
}
@media only screen and (max-device-width: 768px) {
#container {min-width: 320px;}
}
[class*="tit-icon-"] {
/*font: normal normal normal 18px/1 tit-fontello;*/
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
padding: 8px 10px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* this MUST be the last rule*/
.show {display: block;}
</style>
<!--[if lt IE 8]>
<style>
#textareacontainer, #iframecontainer {width: 48%;}
#container {height: 500px;}
#textarea, #iframe {width: 90%;height: 450px;}
#textareaCode, #iframeResult {height: 450px;}
</style>
<![endif]-->
</head>
<body>
<ul class="w3-light-grey">
<li class="dropdown">
<button class="w3-button w3-bar-item tit-icon-help " title="Show info"
onclick='this.nextElementSibling.classList.toggle("show")' onblur='this.nextElementSibling.classList.remove("show")'
>
</button>
<div class="dropdown-content">
<p>Notes: </p>
<p>By default the preview frame's content is editable, but beware the input elements in the frame are NOT working.</p>
<p>If you do not see updates in the editor after making modifications inside the preview frame try reloading the page.</p>
<p>Because of Cross Origin Restrictions you will be able to edit only files saved in the same directory (or sub-directories) of this file, or
those saved in the local Storage.
</p>
<p>If, after restoring a file from local Storage, the page does not appear in the preview frame try reloading the page.</p>
<p>If OK button is disabled in downloading requester, try activating the request window by clicking inside it.</p>
</div>
</li>
<li><button class="w3-button w3-bar-item w3-hover-text-green tit-icon-arrows-cw" onclick="restack()" title="Change Orientation" ></button></li>
<li><button class="w3-button w3-bar-item w3-green w3-hover-white w3-hover-text-green tit-icon-upload-1" onclick="loadFile()" title="Load file"></button></li>
<li><input id="filename" class="w3-bar-item" value="./test/Test.html" onchange="loadFile()" title="File name" ></input></li>
<li><button class="w3-button w3-bar-item w3-green w3-hover-white w3-hover-text-green tit-icon-download-alt" onclick="downloadFile()" title="Download HTML"></button></li>
<li><button class="w3-button w3-bar-item w3-green w3-hover-white w3-hover-text-green tit-icon-code" onclick="viewSource();" title="View source"></button></li>
<li><button class="w3-button w3-green w3-hover-white w3-hover-text-green" onclick="loadFromLocalStorage()" title="Reload HTML from localStorage">Restore</button></li>
<li><button class="w3-button w3-bar-item w3-green w3-hover-white w3-hover-text-green" onclick="saveToLocalStorage()" title="Save HTML to localStorage">Store</button></li>
<li><button class="w3-button w3-bar-item w3-green w3-hover-white w3-hover-text-green" onclick="submitTryit(1)" title="Show HTML output">Run »</button></li>
<li><button class="w3-button w3-bar-item w3-green w3-hover-white w3-hover-text-green" onclick="reEdited()" title="Copy frame source to textarea">« Get</button></li>
<li><label class="switch">
<input id="checkedit" checked="checked" type="checkbox" onchange="frameEditable()">
Editable preview: <span class="slider"></span><span id="switchflag">ON</span>
</label></li>
<li style="float: right"><span class="w3-right w3-bar-item" style="padding: 9px 0;display: block;" id="framesize"></span></li>
</ul>
<div id="shield"></div>
<div id="container">
<div id="textareacontainer">
<textarea autocomplete="on" id="textareaCode" wrap="logical" spellcheck="false"><!DOCTYPE html>
<html>
<head>
<title>Sort a HTML List Alphabetically</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph. Click the button to sort the list alphabetically: </p>
<button onclick="sortList()">Sort</button>
<ul id="id01">
<li>Oslo</li>
<li>Stockholm</li>
<li>Helsinki</li>
<li>Berlin</li>
<li>Rome</li>
<li>Madrid</li>
</ul>
<script>
function sortList() {
var list, i, switching, b, shouldSwitch;
list = document.getElementById("id01");
switching = true;
/*Make a loop that will continue until
no switching has been done: */
while (switching) {
//start by saying: no switching is done:
switching = false;
b = list.getElementsByTagName("LI");
//Loop through all list-items:
for (i = 0; i < (b.length - 1); i++) {
//start by saying there should be no switching:
shouldSwitch = false;
/*check if the next item should
switch place with the current item: */
if (b[i].innerHTML.toLowerCase() > b[i + 1].innerHTML.toLowerCase()) {
/*if next item is alphabetically
lower than current item, mark as a switch
and break the loop: */
shouldSwitch= true;
break;
}
}
if (shouldSwitch) {
/*If a switch has been marked, make the switch
and mark the switch as done: */
b[i].parentNode.insertBefore(b[i + 1], b[i]);
switching = true;
}
}
}
</script>
</body>
</html>
</textarea>
</div>
<div id="dragbar"> </div>
<div id="iframecontainer">
<iframe id="iframeResult">
</iframe>
</div>
</div>
<script>
var framecontentedit = true;
submitTryit()
function submitTryit(n) {
if (window.editor) {
window.editor.save();
}
var text = $Id("textareaCode").value;
if (window.editor) {
var text = window.editor.getDoc().getValue("\n");
}
text = text.replace(/\n\n\n/g,"\n\n"); // normalize newlines (??!!)
var ifr = $Id("iframeResult");
var ifrw = (ifr.contentWindow) ? ifr.contentWindow : (ifr.contentDocument.document) ? ifr.contentDocument.document : ifr.contentDocument;
ifrw.document.open();
ifrw.document.write(text);
ifrw.document.close();
//23.02.2016: contentEditable is set to true, to fix text-selection (bug) in firefox.
//(and back to false to prevent the content from being editable)
//(To reproduce the error: Select text in the result window with, and without, the contentEditable statements below.)
if (ifrw.document.body && !ifrw.document.body.isContentEditable) {
ifrw.document.body.contentEditable = true;
ifrw.document.body.contentEditable = false;
}
ifrw.document.body.contentEditable = framecontentedit;
}
function reEdited() {
var text = frameHTML();
$Id("textareaCode").value = text;
window.editor.getDoc().setValue(text);
}
function showFrameSize() {
$Id("framesize").innerHTML = "Result Size: <span>" + $Id("iframecontainer")["clientWidth"] + " x " + $Id("iframecontainer")["clientHeight"] + "</span>";
}
var layout = "horizontal";
var leftwidthperc = 50 ; var leftheightperc = 50 ;
if ((window.screen.availWidth <= 768 && window.innerHeight > window.innerWidth) ) {restack();}
function restack() {
var l = $Id("textareacontainer");
var c = $Id("dragbar");
var r = $Id("iframecontainer");
if (layout == "vertical") {
l.style["height"] = c.style["height"] = r.style["height"] = "100%";
l.style["width"] = "calc(" + leftwidthperc + "% - 6px)";
c.style["width"] = "12px";
c.style["cursor"] = "col-resize";
r.style["width"] = "calc(" + (100 - leftwidthperc) + "% - 6px)";
layout = "horizontal"
} else {
l.style["width"] = c.style["width"] = r.style["width"] = "100%";
l.style["height"] = "calc(" + leftheightperc + "% - 6px)";
c.style["height"] = "12px";
c.style["cursor"] = "row-resize";
r.style["height"] = "calc(" + (100 - leftheightperc) + "% - 6px)";
layout = "vertical"
}
showFrameSize();
}
dragBalance($Id(("dragbar")));
function dragBalance(balancer) {
if (window.addEventListener) {
balancer.addEventListener("mousedown", function(e) {dragstart(e);});
balancer.addEventListener("touchstart", function(e) {dragstart(e);});
window.addEventListener("mousemove", function(e) {dragmove(e);});
window.addEventListener("touchmove", function(e) {dragmove(e);});
window.addEventListener("mouseup", dragend);
window.addEventListener("touchend", dragend);
}
var dragging = false;
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
function dragstart(e) {
e.preventDefault();
e = e || window.event;
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
dragging = true;
}
function dragmove(e) {
var perc;
if (dragging) {
// show overlay to avoid interfering of mouse moving with textarea
$Id("shield").style.display = "block";
e = e || window.event;
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new size:
if (layout == "vertical") {
var pos = pos2;
var axe1 = "clientHeight";
var axe2 = "height";
perc = (balancer.previousElementSibling[axe1] + (balancer[axe1] / 2) - pos) * 100 / balancer.parentElement[axe1];
leftheightperc = perc;
} else {
var pos = pos1;
var axe1 = "clientWidth";
var axe2 = "width";
perc = (balancer.previousElementSibling[axe1] + (balancer[axe1] / 2) - pos) * 100 / balancer.parentElement[axe1];
leftwidthperc = perc;
}
if (perc > 5 && perc < 95) {
balancer.previousElementSibling.style[axe2] = "calc(" + (perc) + "% - " + (balancer[axe1] / 2) + "px)";
balancer.nextElementSibling.style[axe2] = "calc(" + (100 - perc) + "% - " + (balancer[axe1] / 2) + "px)";
}
showFrameSize();
}
}
function dragend() {
$Id("shield").style.display = "none";
dragging = false;
if (window.editor) {
window.editor.refresh();
}
}
}
function keypressed(e) {
if (e.key != "ArrowLeft" && e.key != "ArrowRight" && e.key != "ArrowUp" && e.key != "ArrowDown") {submitTryit(1)};
}
function keypressedinframe(e) {
if (e.key != "ArrowLeft" && e.key != "ArrowRight" && e.key != "ArrowUp" && e.key != "ArrowDown") {reEdited()};
setTimeout(reEdited,100);
}
if (window.addEventListener) {
window.addEventListener("load", showFrameSize);
$Id("textareacontainer").addEventListener("keyup", function(e) {keypressed(e);});
}
frameWindow().addEventListener("keyup", keypressedinframe);
/*
function setFocusIframe() {frameWindow().focus();}
$Id("iframeResult").contentWindow.addEventListener("mousedown", function(e) {setTimeout(setFocusIframe, 100);return false});
*/
function colorcoding() {
window.editor = CodeMirror.fromTextArea($Id("textareaCode"), {
mode: "text/html",
htmlMode: true,
lineWrapping: false,
smartIndent: true,
indentUnit: 4,
tabSize: 4,
indentWithTabs: true,
addModeClass: true,
autoCloseTags: true,
autoCloseBrackets: true
});
//window.editor.on("change", function () {window.editor.save();});
//window.editor.on("change", function () {submitTryit(1)}); better avoid this due to "conflict" with contentEditable
}
colorcoding();
function frameWindow(){
var ifr = $Id("iframeResult");
var ifrw = (ifr.contentWindow) ? ifr.contentWindow : (ifr.contentDocument.document) ? ifr.contentDocument.document : ifr.contentDocument;
return ifrw;
}
function frameHTML() {
var ifrw = frameWindow();
ifrw.document.body.removeAttribute("contentEditable");// = false;
var text = "<!DOCTYPE html>\n<html>\n" + ifrw.document.documentElement.innerHTML.replace(/^\n+|\n+$/g,'').trim() + "\n</html>";
text = text.replace(/\n\n\n/g,"\n\n"); // normalize newlines (??!!)
ifrw.document.body.contentEditable = framecontentedit;
return text;
}
function loadFile() {
var dir = "";//location.href.slice(0,location.href.lastIndexOf("/") + 1);
var name = $Id("filename").value;
//console.log(dir + name);
frameWindow().location.href = dir + name;
setTimeout(reEdited,500);
setTimeout(submitTryit,1000);
}
function getName() {
var name = $Id("filename").value;
return name = name.slice(name.lastIndexOf("/") + 1);
}
function downloadFile() {
var text = frameHTML();
//text = window.editor.getDoc().getValue("\n");
var blob = new Blob([text], {type: "text/html;charset=utf-8"});
saveAs(blob, getName());
}
function loadFromLocalStorage() {
//Load saved Content
var text = localStorage.getItem(getName());
if (text != null) {
$Id("textareaCode").value = text;
window.editor.getDoc().setValue(text);
submitTryit();
}
}
function saveToLocalStorage() {
if (typeof(Storage) !== "undefined") {
var sHTML = frameHTML(); //Get content
localStorage.setItem(getName(), sHTML);
alert('Saved Successfully');
} else {
alert("No localStorage available")
}
}
function viewSource() {
var source = frameHTML();
//now we need to escape the html special chars, javascript has escape
//but this does not do what we want
source = source.replace(/</g, "<");
//now we add <pre> tags to preserve whitespace
source = "<pre>" + source + "</pre>";
var sourceWindow = window.open('Nice Title','Source of page','');
sourceWindow.document.write(source);
sourceWindow.document.close(); //close the document for writing, not the window
}
function frameEditable() {
$Id("checkedit").value = ~ $Id("checkedit").value;
if ($Id("checkedit").value == 0) {
framecontentedit = true;
$Id("switchflag").innerHTML = "ON";
} else {
framecontentedit = false;
$Id("switchflag").innerHTML = "OFF";
}
submitTryit();
reEdited();
}
</script>
<script src="js/FileSaver.js"></script>
<script>
/* alert before leaving page
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = 'It looks like you have been editing something. '
+ 'If you leave before saving, your changes will be lost.';
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
});
*/
</script>
</body>
</html>