-
Notifications
You must be signed in to change notification settings - Fork 23
/
demo.html
733 lines (638 loc) · 19.4 KB
/
demo.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
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
<!DOCTYPE html>
<html>
<head>
<title>Demo for jinplace</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<!-- The following groupd of js and css files are just for the demo -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/2.2.1/knockout-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/json2/20121008/json2.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shCore.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shBrushXml.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shBrushJScript.min.js"></script>
<link rel=stylesheet href="http://jinplace.org/css/shCore.css">
<link rel=stylesheet href="http://jinplace.org/css/shCoreDefault.css">
<!-- These are the required files. Need nQuery 1.7+ -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="js/jinplace.js"></script>
<style>
html {
font-size: 100%;
overflow-y: scroll;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
body, form, textarea, button, input, select {
margin: 0 2px;
font-size: 16px;
font-family: arial, helvetica, sans-serif;
}
h1 {
font-size: 1.4em;
color: darkred;
}
h2 {
font-size: 1.2em;
color: #eb754a;
}
/* Make lists consistent */
ul, ol { margin-left: 0; }
ol { padding-left: 2em; }
ul {
list-style: square;
padding-left: 1em;
}
li {
padding: 3px 0;
}
li p {
padding:0;
margin:8px 0 0 0;
}
table {max-width: 900px;}
th {
background-color: #eee;;
}
td {
background-color: #f5f5f5;
}
.editable-display-box {
background: white;
padding: 10px;
margin: 10px;
}
.editable {
border-bottom: 1px dashed #84ae84;
}
#page {
max-width: 900px;
margin: 0 auto 0 auto;
}
.intro {
width: 700px;
}
#result-table {
float:left;
background: #eee;
padding: 10px;
margin-bottom: 12px;
border: #888 solid 1px;
z-index: 2;
}
#result-table-placeholder { display: none; height: 104px;}
.demo {
overflow: hidden;
border-top: 1px solid #ccc;
border-bottom: 1px solid #eee;
padding-bottom: 20px;;
margin-bottom: 10px;
clear: both;
}
.description {
overflow: hidden;
}
.example {
padding: 1px;
background-color: #dce6ea;
white-space: pre;
}
.syntaxhighlighter {
position: static !important;
margin: 2px 0;
}
.syntaxhighlighter table td.code .container { position: static !important;}
.try-it {
background-color: #dce6ea;
padding-bottom: 20px;
}
.try-it > p, .try-it > h2 {
margin-left: 10px;;
}
.description > p { margin-right: 10px;}
.warn {
color: #ac0000;
}
input.short {
width: 8em;
}
.button {
display: inline;
font-size: 13px;
padding: 2px 10px;
margin: 0 0 0 3px;
border: solid 1px #eee;
color: #000000;
font-weight: bold;
background: #98b095;
text-decoration: none;
cursor: pointer;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
background: -webkit-linear-gradient(top, #bbdcb6, #a0b0a0);
background: -moz-linear-gradient(top, #bbdcb6, #a0b0a0);
background: -ms-linear-gradient(top, #bbdcb6, #a0b0a0);
background: -o-linear-gradient(top, #bbdcb6, #a0b0a0);
}
@media all and (min-width: 1270px) {
#page {
max-width: 1250px;
width: 1250px;
}
.description {
float: left;
width: 900px;
}
.example {
margin-right: 20px;
}
.try-it {
float: left;
width: 350px;
padding-bottom: 1000px;
margin-bottom: -1000px;
}
}
</style>
</head>
<body>
<div id="page">
<h1>Demo of the jinplace jquery plugin</h1>
<div id="invalid" class="warn">
<p>ERROR: Something has gone wrong and at least one of the required javascript libraries has not loaded.
Please check this before going further.
</div>
<!--[if lte IE 8]>
<div>
<p>Jinplace and this demo are fully functional on IE6-8.
However the demo does not look quite as good on the earlier browsers.
</div>
<![endif]-->
<div class="intro">
<p>In this demo there is no actual communication with a remote server.
Instead all load and save operations take place on a mini database within the
page itself.
As you work through the examples and make changes they will appear in
the table below.
</p>
</div>
<div style="clear: both;">
<p>See also <a class="button" href="https://bitbucket.org/itinken/jinplace/wiki/Documentation">Documentation</a> and <a class="button" href="https://bitbucket.org/itinken/jinplace/downloads">Downloads</a></p>
</div>
<div id="result-table">
<table>
<caption>The database. See your changes here.</caption>
<tr>
<th>Object</th>
<th>First name</th>
<th>Last name</th>
<th>Favourites</th>
<th>Direction</th>
</tr>
<tr>
<td>person</td>
<td><span data-bind="text: person.firstname"></span>
<td><span data-bind="text: person.lastname"></span>
<td> <div style="width: 200px" data-bind="text: person.favourites"></div>
<td><span data-bind="text: person.direction"></span>
</tr>
<tr>
<td>person2</td>
<td><span data-bind="text: person2.firstname"></span>
<td><span data-bind="text: person2.lastname"></span>
<td> <div style="width: 200px" data-bind="text: person2.favourites"></div>
<td><span data-bind="text: person2.direction"></span>
</tr>
</table>
</div>
<div id="result-table-placeholder"> </div>
<p>
<div id="example-input" class="demo">
<div class="description">
<h2>Simple text input field</h2>
First example is a straightforward text input. Click on the underlined
text to modify it.
<ul>
<li>The url to post to is supplied.</li>
<li>Clicking outside the edit field submits the data</li>
<li>Hiting return normally submits the data too</li>
</ul>
<pre class="example">
Firstname <span
class="editable"
data-url="/internal/person/firstname">John</span>
<!-- This will set up all edit fields that have class 'editable' on the page -->
<!-- So it will only be shown in this example -->
<script>
$('.editable').jinplace();
</script>
</pre>
</div>
<div class="try-it">
<p>Click on the name to edit
</div>
</div>
<div id="example-textarea" class="demo">
<div class="description">
<h2>Using a textarea</h2>
By default you get a normal text input. To get a textarea, then you need to set the
type to <em>textarea</em>.
<ul>
<li>Set <em>data-type</em> to <kbd>textarea</kbd></li>
<li>Everything else is the same as for regular inputs</li>
</ul>
<pre class="example">
<div class="editable"
data-type="textarea" data-url="/internal/person/favourites">
Chocolate & Chips
</div>
</pre>
</div>
<div class="try-it">
<p>Click textarea, modify and click outside it.</p>
</div>
</div>
<div id="example-select" class="demo">
<div class="description">
<h2>Using a select dropdown</h2>
You can also have a list of choices with a select list.
In this case you must supply the list of possible choices in the <b>data-data</b>
attribute.
<ul>
<li>Set type to 'select'</li>
<li>The data <b>must</b> be given.</li>
<li>The data is a JSON string.</li>
</ul>
<p>The JSON would normally be generated when the page is built.
<pre class="example">
Direction:
<span class="editable" data-type="select" data-url="/internal/person/direction"
data-data='[["E", "East"], ["W", "West"]]'>East</span>
</pre>
</div>
<div class="try-it">
<p>Click to enable the select box.</p>
</div>
</div>
<div id="example-ok" class="demo">
<div class="description">
<h2>With ok button</h2>
You can add an OK button which must be clicked to accept the change.
<ul>
<li>Adds an OK button that is clicked to submit the data.
<li>The text on the button is set to your value.
<li>Clicking outside the field, cancels
</ul>
<pre class="example">
Last name <span class="editable"
data-url="/internal/person/lastname"
data-input-class="short"
data-ok-button="Go">Smith</span>
</pre>
</div>
<div class="try-it">
<p>Editing field and click the "Go" button.</p>
</div>
</div>
<div id="example-ok-cancel" class="demo">
<div class="description">
<h2>With OK and cancel button</h2>
You can also add a cancel button, again with the text that you specify.
<ul>
<li>Adds an OK button that is clicked to submit the data.
<li>Adds a cancel button that is clicked to abandon the edit.
<li>The text on the buttons are set to your values.
<li>Clicking outside the field does nothing.
</ul>
<p>This is particularly useful for large text areas where it is easy
to lose a lot of work it you accidentally lose focus.</p>
<pre class="example">
Some of their favourite things
<div class="editable"
data-url="/internal/person/favourites"
data-type="textarea"
data-ok-button="OK"
data-cancel-button="Cancel">
Chocolate and Chips
</div>
</pre>
</div>
<div class="try-it">
<p>Click OK to accept changes, Cancel to throw them away.
</div>
</div>
<div id="example-activator" class="demo">
<div class="description">
<h2>Separate activation button</h2>
You can have a separate button to click to start editing,
rather that just clicking on the text.
Any element can be used.
<ul>
<li>Pass the css selector of the edit element in activator.
<li>Clicking the text no longer starts editing (unless
that element is included in the selector!)
<li>The data-input-class sets the class that will be applied to the input field</li>
</ul>
<pre class="example">
First name:
<span class="editable"
data-url="/internal/person/firstname"
data-activator="#edit-activator"
data-input-class="short"
>John</span>
<span id="edit-activator" class="button">Edit</span>
</pre>
</div>
<div class="try-it">
<p>Click on the "edit" button to start. Clicking on the field does
not activate the edit controls.</p>
</div>
</div>
<div id="example-form" class="demo">
<div class="description">
<h2>Full form with settings.</h2>
<p>As well as configuring by using data attributes on the html elements, you can also
configure during the .jinplace() call in the usual jQuery manner.
You can of course use a mixture of both as you see fit.
<ul>
<li>The url is set, so it is the same for all fields</li>
<li>The field to set is determined by the data-attribute</li>
<li>The 'placeholder' setting is what is shown when the field is blank</li>
<li>The data-placeholder setting can be given on the html element to override the
general one.</li>
</ul>
<p>There are several more settings and most of them can be used on either the html element
or in the call to .jinplace().
<pre class="example">
<p>Firstname: <span class="editable2" data-attribute="firstname">Mary</span>
<p>Lastname: <span class="editable2" data-attribute="lastname">Jones</span>
<p>Direction: <span class="editable2" data-type="select"
data-data='[["N","North"],["S","South"],["E","East"],["W","West"]]'
data-attribute="direction">West</span>
<p>Favourites:
<div class="editable2" data-type="textarea" data-attribute="favourites"
data-placeholder="[Click to add]">
</div>
<script>
$('.editable2').jinplace({
url: '/internal/person2',
placeholder: '[Edit]'
});
</script>
</pre>
</div>
<div class="try-it">
<p>All the fields can be edited and viewed in the person2 row at the top of
this page.</p>
</div>
</div>
<div id="example-load" class="demo">
<div class="description">
<h2>Load data from server.</h2>
<p>Sometimes you want to edit a different version of the text than the one that is
displayed.
The canonical example of this is in wiki text where you need to edit the plain
text version, but display the html version of the page.
<p>There are two ways of doing this</p>
<ul>
<li>By using the data-data attribute</li>
<li>By using the data-loadurl attribute</li>
<li>Using the loadurl config setting</li>
</ul>
<pre class="example">
<p>Firstname: <span class="editable3" data-attribute="firstname">Mary</span>
<p>Lastname: <span class="editable3" data-attribute="lastname">Jones</span>
<p>Direction: <span class="editable3" data-type="select"
data-attribute="direction">West</span>
<p>Favourites:
<div class="editable3" data-type="textarea" data-attribute="favourites"
data-placeholder="[Click to add]">
</div>
<script>
$('.editable3').jinplace({
url: '/internal/person2',
loadurl: '/internal/person2'
});
</script>
</pre>
</div>
<div class="try-it">
<p>When you click to edit, the edit text will be fetched from the server.
So the current (probably stale in this demo) text is disregarded.
You can view the results of your edits in the person2 row at the top
of this page.</p>
</div>
</div>
<div id="example-text-only-true" class="demo">
<div class="description">
<h2>Text only true</h2>
<p>By default the input field is treated as text. Any html like tags are shown literally and are not
treated as markup.</p>
<pre class="example">
<div id="text-only-true" class="editable"
data-type="textarea"
data-url="/internal/person/favourites"
data-text-only="true">
Chocolate & Chips
&lt;hr>
More text
</div>
</pre>
</div>
<div class="try-it">
<p>Any html you enter here will appear literally when the edit is finished.</p>
</div>
</div>
<div id="example-text-only-false" class="demo">
<div class="description">
<h2>Text only false</h2>
<p>If the text-only attribute is set to false, then html markup in the value is treated as markup.</p>
<p>You are responisable for ensuring that there are no security implications. A typical use is to use the
<i>loadurl</i> attribute to load a markdown version of the text to edit and the resulting html is returned
to display.</p>
<pre class="example">
<div class="editable"
data-type="textarea" data-url="/internal/person/favourites"
data-text-only="false">
Chocolate & Chips
<hr>
More text
</div>
</pre>
</div>
<div class="try-it">
<p>Any html you enter here will be used to format the text when the edit is finished.</p>
</div>
</div>
<div style="float:left; margin-right: 20px;">
<p>See also <a class="button" href="https://bitbucket.org/itinken/jinplace/wiki/Documentation">Documentation</a> and
<a class="button" href="https://bitbucket.org/itinken/jinplace/downloads">Downloads</a></p>
</div>
<!-- Start of scripts -->
<script>
// Just in case there is no console...
if (!window.console) {
var console = {
log: function () {
var a = arguments;
}
}
}
$(function() {
/**
* @name ko
* @class
* @property observable
* @property applyBindings
*/
// Internal 'server' to keep track of the data that is submitted.
// Our database...
var db = {
person: {
firstname: ko.observable('John'),
lastname: ko.observable('Smith'),
favourites: ko.observable('Chocolate and Chips'),
direction: ko.observable('E')
},
person2: {
firstname: ko.observable('Mary'),
lastname: ko.observable('Jones'),
favourites: ko.observable(''),
direction: ko.observable('W')
},
allowedDirections: [
['N', 'North'], ['NW', 'North West'], ['NE', 'North East'],
['E', 'East'], ['W', 'West'],
['S', 'South'], ['SW', 'South West'], ['SE', 'South East']
]
};
ko.applyBindings(db);
// Redefine the $.ajax call to keep the data local to the page.
var dataFunc = function (opts, value) {
var params = {
id: opts.elementId,
object: opts.object,
attribute: opts.attribute,
value: value
};
var url = opts.url;
console.log("The URL", url);
console.log("The data", params);
var m = url.match(new RegExp('/internal(?:/(\\w*)(?:/(\\w*))?)?'));
var object = m[1] || params.object;
var field = m[2] || params.attribute;
if (value !== undefined) {
// set the database value
db[object][field](value);
if (field == 'direction') {
$.each(db.allowedDirections, function(i, opt) {
if (opt[0] == value)
value = opt[1];
});
}
} else {
value = db[object][field]();
if (field == 'direction') {
var dir = $.extend(true, [], db.allowedDirections);
$.each(dir, function(i, opt) {
if (opt[0] == value) {
opt[2] = 1;
}
});
value = JSON.stringify(dir);
}
}
return value;
};
$.fn.jinplace.defaults.submitFunction = dataFunc;
$.fn.jinplace.defaults.loadFunction = dataFunc;
$(function(){
var rt = $('#result-table');
var rtp = $('#result-table-placeholder');
var tableTop = rt.offset().top;
// Keep the current state, to avoid pointless changes on every scroll or resize. This was a
// bit of a problem on mobile devices.
var inline;
var floating;
function recalc() {
var ninline = ($(window).width() < 1270); // Table is below the introduction text, rather than over on the right.
var nfloating = ($(window).scrollTop() > tableTop); // Table is inline and floating at the top of the page.
if (ninline !== inline || nfloating !== floating) {
inline = ninline;
floating = nfloating;
if (inline) {
if (floating) {
rt.css({'position': 'fixed', 'top': '0', 'right': 'inherit'});
rtp.css('display', 'block');
} else {
rt.css({'position': 'static'});
rtp.css('display', 'none');
}
} else {
rt.css({'position': 'fixed', 'top': '0', right: '0'});
rtp.css('display', 'none');
}
}
}
recalc();
// Bind to resize and scroll events.
$(window).resize(recalc);
$(window).scroll(recalc);
});
});
</script>
<script>
// Arrange to syntax highlight everything and make real working code from the text of the
// example.
$(function() {
$('.example').each(function () {
var $this = $(this);
var exampleText = $this.text();
var syntax = $('<pre class="brush: js; html-script:true"></pre>');
// We need the actual text inside the example box so that it can be syntax highlighted,
// so we convert all '<' to entities.
syntax.text(exampleText);
$this.html(syntax);
// Parse the example into html and place it in the try it out column. Voila guaranteed to
// be the same as the visible example with no repetition.
// Note: parseHTML is only available in jQ 1.8+. Use it if available
// otherwise fall back to using jQuery(). This demo currently runs
// with jQ 1.7
var html;
if ($.parseHTML)
html = $.parseHTML(exampleText, null, true);
else
html = $(exampleText);
var try_it = $this.parent().parent().find('.try-it');
var heading = $('<h2>Try it out</h2>');
try_it.prepend(heading);
var dbox = $('<div class="editable-display-box"/>');
dbox.html(html);
try_it.append(dbox);
});
$('.editable').jinplace();
$('.editable2').jinplace({
url: '/internal/person2',
placeholder: '[Edit]'
});
$('.editable3').jinplace({
url: '/internal/person2',
loadurl: '/internal/person2'
});
});
</script>
<script>
$(function() {
SyntaxHighlighter.defaults.toolbar = false;
SyntaxHighlighter.defaults['tab-size'] = 2;
SyntaxHighlighter.all();
});
</script>
<script>
// If we got here without errors then hide the warning box.
if (jQuery && ko && jQuery.fn.jinplace) {
$('#invalid').hide();
}
</script>
</div>
</body>