-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcomic.js
557 lines (550 loc) · 18.3 KB
/
comic.js
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
// Used to store global state for building the query string.
var QS = {};
// Given a context will set the various elements in the comic strip to reflect
// the state described therein.
function update_scene(context) {
// Set the title and author.
if(context.title) {
$('#title').text(context.title);
} else {
$('#title').text('Un-named Comic (Click to edit)');
}
if(context.author) {
$('#author').text(context.author);
} else {
$('#author').text('Anonymous (Click to edit)');
}
// Set the backgrounds.
if(context.bg1) {
$('#bg1').show();
$('#code1').hide();
$('#if1').hide();
if(context.bg1 === 'random') {
$('#bg1').attr('src', 'http://lorempixel.com/240/240/?v=1');
} else {
$('#bg1').attr('src', context.bg1);
}
} else {
$('#bg1').hide();
}
if(context.bg2) {
$('#bg2').show();
$('#code2').hide();
$('#if2').hide();
if(context.bg2 === 'random') {
$('#bg2').attr('src', 'http://lorempixel.com/240/240/?v=2');
} else {
$('#bg2').attr('src', context.bg2);
}
} else {
$('#bg2').hide();
}
if(context.bg3) {
$('#bg3').show();
$('#code3').hide();
$('#if3').hide();
if(context.bg3 === 'random') {
$('#bg3').attr('src', 'http://lorempixel.com/240/240/?v=3');
} else {
$('#bg3').attr('src', context.bg3);
}
} else {
$('#bg3').hide();
}
// Set the code examples.
if(context.code1) {
$('#bg1').hide();
$('#if1').hide();
$('#code1').show();
$('#code1 code').text(context.code1);
} else {
$('#code1').hide();
}
if(context.code2) {
$('#bg2').hide();
$('#if2').hide();
$('#code2').show();
$('#code2 code').text(context.code2);
} else {
$('#code2').hide();
}
if(context.code3) {
$('#bg3').hide();
$('#if3').hide();
$('#code3').show();
$('#code3 code').text(context.code3);
} else {
$('#code3').hide();
}
// Set the micro:bit sims
if(context.mb1) {
$('#bg1').hide();
$('#code1').hide();
$('#if1').show();
$('#if1').attr('src', context.mb1);
} else {
$('#if1').hide();
}
if(context.mb2) {
$('#bg2').hide();
$('#code2').hide();
$('#if2').show();
$('#if2').attr('src', context.mb2);
} else {
$('#if2').hide();
}
if(context.mb3) {
$('#bg3').hide();
$('#code3').hide();
$('#if3').show();
$('#if3').attr('src', context.mb3);
} else {
$('#if3').hide();
}
// Set the snakes.
$('#pane1').removeClass();
$('#pane2').removeClass();
$('#pane3').removeClass();
if(context.s1) {
if(context.s1==='both') {
$('#pane1').addClass('left-python-yellow right-python-blue');
} else if(context.s1==='yellow') {
$('#pane1').addClass('left-python-yellow');
} else if(context.s1==='blue') {
$('#pane1').addClass('right-python-blue');
}
}
if(context.s2) {
if(context.s2==='both') {
$('#pane2').addClass('left-python-yellow right-python-blue');
} else if(context.s2==='yellow') {
$('#pane2').addClass('left-python-yellow');
} else if(context.s2==='blue') {
$('#pane2').addClass('right-python-blue');
}
}
if(context.s3) {
if(context.s3==='both') {
$('#pane3').addClass('left-python-yellow right-python-blue');
} else if(context.s3==='yellow') {
$('#pane3').addClass('left-python-yellow');
} else if(context.s3==='blue') {
$('#pane3').addClass('right-python-blue');
}
}
// Headers
if(context.h1) {
$('#header1').show();
$('#header1').text(context.h1);
} else {
$('#header1').hide();
}
if(context.h2) {
$('#header2').show();
$('#header2').text(context.h2);
} else {
$('#header2').hide();
}
if(context.h3) {
$('#header3').show();
$('#header3').text(context.h3);
} else {
$('#header3').hide();
}
// Speech bubbles
if(context.lt1) {
$('#pane1 .bubble-top.left').show();
$('#pane1 .bubble-top.left p').text(context.lt1);
} else {
$('#pane1 .bubble-top.left').hide();
}
if(context.lb1) {
$('#pane1 .bubble-bottom.left').show();
$('#pane1 .bubble-bottom.left p').text(context.lb1);
} else {
$('#pane1 .bubble-bottom.left').hide();
}
if(context.rt1) {
$('#pane1 .bubble-top.right').show();
$('#pane1 .bubble-top.right p').text(context.rt1);
} else {
$('#pane1 .bubble-top.right').hide();
}
if(context.rb1) {
$('#pane1 .bubble-bottom.right').show();
$('#pane1 .bubble-bottom.right p').text(context.rb1);
} else {
$('#pane1 .bubble-bottom.right').hide();
}
if(context.lt2) {
$('#pane2 .bubble-top.left').show();
$('#pane2 .bubble-top.left p').text(context.lt2);
} else {
$('#pane2 .bubble-top.left').hide();
}
if(context.lb2) {
$('#pane2 .bubble-bottom.left').show();
$('#pane2 .bubble-bottom.left p').text(context.lb2);
} else {
$('#pane2 .bubble-bottom.left').hide();
}
if(context.rt2) {
$('#pane2 .bubble-top.right').show();
$('#pane2 .bubble-top.right p').text(context.rt2);
} else {
$('#pane2 .bubble-top.right').hide();
}
if(context.rb2) {
$('#pane2 .bubble-bottom.right').show();
$('#pane2 .bubble-bottom.right p').text(context.rb2);
} else {
$('#pane2 .bubble-bottom.right').hide();
}
if(context.lt3) {
$('#pane3 .bubble-top.left').show();
$('#pane3 .bubble-top.left p').text(context.lt3);
} else {
$('#pane3 .bubble-top.left').hide();
}
if(context.lb3) {
$('#pane3 .bubble-bottom.left').show();
$('#pane3 .bubble-bottom.left p').text(context.lb3);
} else {
$('#pane3 .bubble-bottom.left').hide();
}
if(context.rt3) {
$('#pane3 .bubble-top.right').show();
$('#pane3 .bubble-top.right p').text(context.rt3);
} else {
$('#pane3 .bubble-top.right').hide();
}
if(context.rb3) {
$('#pane3 .bubble-bottom.right').show();
$('#pane3 .bubble-bottom.right p').text(context.rb3);
} else {
$('#pane3 .bubble-bottom.right').hide();
}
}
// Given a potential text item, will check and set it in the context for the
// named field, or else remove it from the context.
function check_item(item, context, field_name) {
if(item.trim()) {
context[field_name] = item.trim();
} else {
delete context[field_name];
}
}
// If the state changes then call this function to remove the tweet button and
// direct link that will need re-building.
function clear_on_change() {
$('#direct-link').attr('href', '#');
$('#direct-link').text('');
$('#twitter-button').html('');
}
// Add various UI controls for updating the output of the device.
function setup_editor() {
$('#editor-help').show();
$('#home-link').hide();
$('.frame form').show();
// Set text fields contenteditable.
$('#title').attr('contenteditable', 'true');
$('#author').attr('contenteditable', 'true');
$('#title').focus(function() {
clear_on_change();
});
$('#author').focus(function() {
clear_on_change()
});
$('.textual').attr('contenteditable', 'true');
// Connect the configuration forms to the panels.
var form1 = $('#form1');
var form2 = $('#form2');
var form3 = $('#form3');
var pane1 = $('#pane1');
var pane2 = $('#pane2');
var pane3 = $('#pane3');
form1.change(function() {
clear_on_change();
});
form2.change(function() {
clear_on_change();
});
form3.change(function() {
clear_on_change();
});
// A function to connect a form to a panel.
function connect(form, pane) {
var form_header = form.find('#form-heading');
var pane_header = pane.find('.header');
var form_background = form.find('#form-background');
var pane_background = pane.find('.bg_image');
var pane_code = pane.find('.pre_code');
var pane_iframe = pane.find('iframe');
var form_image_url = form.find('#form-image-url');
var mb_omatic = form.find('.mb-link');
pane_code.hide();
pane_background.hide();
pane_iframe.hide();
mb_omatic.hide();
form_header.change(function(e) {
if(form_header[0].checked) {
pane_header.show();
if(!pane_header.text()) {
pane_header.text('Header text...');
}
} else {
pane_header.hide();
}
});
form.find('#form-background').change(function(e) {
var bg = $(this).find("option:selected").attr('value');
form.find('#form-image-url').prop('disabled', true);
mb_omatic.hide();
if(bg==='custom') {
pane_background.show();
pane_code.hide();
pane_iframe.hide();
var custom_url = form.find('#form-image-url');
custom_url.prop('disabled', false);
if(custom_url.val()) {
var url = custom_url.val();
pane_background.attr('src', url);
}
custom_url.focus();
} else if(bg==='microbit') {
pane_background.hide();
pane_code.hide();
pane_iframe.show();
mb_omatic.show();
var custom_url = form.find('#form-image-url');
custom_url.prop('disabled', false);
if(custom_url.val()) {
var url = custom_url.val();
pane_iframe.attr('src', url);
}
custom_url.focus();
} else if(bg==='none') {
pane_background.hide();
pane_code.hide();
pane_iframe.hide();
} else if(bg==='code') {
pane_background.hide();
pane_iframe.hide();
pane_code.show();
var code_holder = pane.find('.source_code');
if(!code_holder.text()) {
code_holder.text('from microbit import *\n\n# Edit your code here!\n\ndisplay.scroll("Hello, World!")');
}
} else {
pane_background.show();
pane_code.hide();
if(bg === 'random') {
var seed = Math.floor((Math.random() * 1000) + 1);
pane_background.attr('src', 'http://lorempixel.com/320/320/?v='+seed);
} else {
pane_background.attr('src', 'bg/'+bg);
}
}
});
form.find('.custom-image').on('input', function(e) {
var url = $(this).val();
var bg = form.find('#form-background').find("option:selected").attr('value');
if(bg==='custom') {
pane_background.attr('src', url);
pane_iframe.attr('src', '');
} else {
pane_iframe.attr('src', url);
pane_background.attr('src', '');
}
});
form.find('#form-snakes').change(function(e) {
var snakes = $(this).find("option:selected").attr('value');
pane.removeClass();
if(snakes==='both') {
pane.addClass('left-python-yellow right-python-blue');
} else if(snakes==='yellow') {
pane.addClass('left-python-yellow');
} else if(snakes==='blue') {
pane.addClass('right-python-blue');
}
});
form.find('#form-lt').change(function(e) {
var holder = pane.find('.bubble-top.left');
var text_p = pane.find('.bubble-top.left p');
if(this.checked) {
holder.show();
if(!text_p.text()) {
text_p.text('Edit this..!');
}
} else {
holder.hide();
}
});
form.find('#form-lb').change(function(e) {
var holder = pane.find('.bubble-bottom.left');
var text_p = pane.find('.bubble-bottom.left p');
if(this.checked) {
holder.show();
if(!text_p.text()) {
text_p.text('Edit this..!');
}
} else {
holder.hide();
}
});
form.find('#form-rt').change(function(e) {
var holder = pane.find('.bubble-top.right');
var text_p = pane.find('.bubble-top.right p');
if(this.checked) {
holder.show();
if(!text_p.text()) {
text_p.text('Edit this..!');
}
} else {
holder.hide();
}
});
form.find('#form-rb').change(function(e) {
var holder = pane.find('.bubble-bottom.right');
var text_p = pane.find('.bubble-bottom.right p');
if(this.checked) {
holder.show();
if(!text_p.text()) {
text_p.text('Edit this..!');
}
} else {
holder.hide();
}
});
form.find('#reset-button').click(function(e) {
pane_header.hide();
pane_background.hide();
pane.removeClass();
pane.find('.bubble-top.left').hide();
pane.find('.bubble-bottom.left').hide();
pane.find('.bubble-top.right').hide();
pane.find('.bubble-bottom.right').hide();
pane.find('.textual').text('');
});
}
connect(form1, pane1);
connect(form2, pane2);
connect(form3, pane3);
$('#direct-button').click(function(e) {
var state = get_state_from_dom();
set_qs(state);
});
}
// Use arguments from the query string to setup the device to behave in certain
// ways.
function setup_from_url() {
QS = get_qs_context();
update_scene(QS);
$('.frame form').hide();
$('#editor-help').hide();
$('#home-link').show();
$('.edit-me').hide();
}
function get_state_from_dom() {
var result = {};
var form1 = $('#form1');
var form2 = $('#form2');
var form3 = $('#form3');
var pane1 = $('#pane1');
var pane2 = $('#pane2');
var pane3 = $('#pane3');
result.title = $('#title').text();
result.author = $('#author').text();
if(pane1.find('.header').is(':visible')) {
result.h1 = pane1.find('.header').text();
}
if(pane2.find('.header').is(':visible')) {
result.h2 = pane2.find('.header').text();
}
if(pane3.find('.header').is(':visible')) {
result.h3 = pane3.find('.header').text();
}
if($('#bg1').is(":visible")) {
result.bg1 = $('#bg1').attr('src');
} else if ($('#code1').is(':visible')) {
result.code1 = $('#code1 code')[0].innerHTML.replace(new RegExp('<br>', 'g'), '\n');
} else if ($('#if1').is(':visible')) {
result.mb1 = $('#if1').attr('src');
}
if($('#bg2').is(":visible")) {
result.bg2 = $('#bg2').attr('src');
} else if ($('#code2').is(':visible')) {
result.code2 = $('#code2 code')[0].innerHTML.replace(new RegExp('<br>', 'g'), '\n');
} else if ($('#if2').is(':visible')) {
result.mb2 = $('#if2').attr('src');
}
if($('#bg3').is(":visible")) {
result.bg3 = $('#bg3').attr('src');
} else if ($('#code3').is(':visible')) {
result.code3 = $('#code3 code')[0].innerHTML.replace(new RegExp('<br>', 'g'), '\n');
} else if ($('#if3').is(':visible')) {
result.mb3 = $('#if3').attr('src');
}
var s1 = form1.find('#form-snakes').find("option:selected").attr('value');
if(s1!=='none') {
result.s1 = s1;
}
var s2 = form2.find('#form-snakes').find("option:selected").attr('value');
if(s2!=='none') {
result.s2 = s2;
}
var s3 = form3.find('#form-snakes').find("option:selected").attr('value');
if(s3!=='none') {
result.s3 = s3;
}
if(pane1.find('.bubble-top.left').is(':visible')) {
result.lt1 = pane1.find('.bubble-top.left p').text();
}
if(pane1.find('.bubble-top.right').is(':visible')) {
result.rt1 = pane1.find('.bubble-top.right p').text();
}
if(pane1.find('.bubble-bottom.left').is(':visible')) {
result.lb1 = pane1.find('.bubble-bottom.left p').text();
}
if(pane1.find('.bubble-bottom.right').is(':visible')) {
result.rb1 = pane1.find('.bubble-bottom.right p').text();
}
if(pane2.find('.bubble-top.left').is(':visible')) {
result.lt2 = pane2.find('.bubble-top.left p').text();
}
if(pane2.find('.bubble-top.right').is(':visible')) {
result.rt2 = pane2.find('.bubble-top.right p').text();
}
if(pane2.find('.bubble-bottom.left').is(':visible')) {
result.lb2 = pane2.find('.bubble-bottom.left p').text();
}
if(pane2.find('.bubble-bottom.right').is(':visible')) {
result.rb2 = pane2.find('.bubble-bottom.right p').text();
}
if(pane3.find('.bubble-top.left').is(':visible')) {
result.lt3 = pane3.find('.bubble-top.left p').text();
}
if(pane3.find('.bubble-top.right').is(':visible')) {
result.rt3 = pane3.find('.bubble-top.right p').text();
}
if(pane3.find('.bubble-bottom.left').is(':visible')) {
result.lb3 = pane3.find('.bubble-bottom.left p').text();
}
if(pane3.find('.bubble-bottom.right').is(':visible')) {
result.rb3 = pane3.find('.bubble-bottom.right p').text();
}
return result;
}
// Run on start-up
$(document).ready(function() {
var querystring = window.location.search.substring(1);
// If there's something in the URL's querystring, use it to display the
// device as specified therein.
if(querystring) {
setup_from_url();
set_qs(QS);
} else {
// Otherwise, show some UI to allow people to edit their own settings.
setup_editor();
};
});