forked from viresh-ratnakar/exolve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exolve.html
5637 lines (5381 loc) · 175 KB
/
exolve.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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<!--
MIT License
Copyright (c) 2019 Viresh Ratnakar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
The latest code and documentation for exolve can be found at:
https://github.com/viresh-ratnakar/exolve
-->
<title>Exolve: An Easily Configurable Interactive Crossword Solver</title>
<script>
const puzzleText = `
======REPLACE WITH YOUR PUZZLE BELOW======
exolve-begin
exolve-id: some-unique-identifier
exolve-title: Quick 3x3 (replace with puzzle title)
exolve-setter: Gussalufz (replace with setter's pseudonym)
exolve-copyright: 2020 Copyright Holder(s) (delete or replace)
exolve-width: 3
exolve-height: 3
exolve-grid:
000
0.0
000
exolve-across:
1 Running with placement, essentially, for single (3)
3 Oddly fluent and entertaining (3)
exolve-down:
1 Retreating thief forgot to hide bananas (3)
2 One suffering for a long time (3)
exolve-end
======REPLACE WITH YOUR PUZZLE ABOVE======
`;
/**
* The Exolve code creates only the following names at global scope:
*
* - Exolve
* - createExolve
* - createPuzzle (deprecated).
* - exolvePuzzles
*
* - The most generic way to create a puzzle is with "new Exolve(...)".
* - The createExolve() function is a covenient wrapper.
* - The createExolve() function looks for and calls the function
* customizeExolve() if it exists (passing it the created puzzle).
* - The createPuzzle() function is similar to createExolve(), and
* is deprecated (as the name may conflict with some other code).
* - All HTML ids/class names begin with "xlv"
*/
/**
* This is the global object in which *all* Exolve puzzles rendered on a single
* web page are stored as properties, with the puzzle IDs being the keys.
*/
var exolvePuzzles;
/**
* Constructor to create an Exolve puzzle.
*
* puzzleText contains the puzzle specs.
* containerId is the optional HTML id of the container element in which you
* want to create this puzzle. If empty, the puzzle is created inside
* the element with id "exolve" if it exists (and its id is changed to
* exolve<N> in that case, where <N> is the index of this puzzle among
* all the pages on the page). If containerId is empty and there is no
* element with id "exolve", the the puzzle is created at the end of the
* web page.
* customized is an optional function that will get called after the puzzle
* is set up. The Exolve object will be passed to the function.
* addStateToUrl should be set to false only if you do *not* want to save
* the puzzle state in the URL (the puzzle state is also saved in a
* cookie, but that does not work for local files). Unless you are
* embedding the puzzle in an iframe for some reason, set this to true.
* visTop should be set to the height of any sticky/fixed position elements
* at the top of the page (normally just 0).
* maxDim If non-zero, use this as the suggested max size of the container
* in px.
*/
function Exolve(puzzleText,
containerId="",
customizer=null,
addStateToUrl=true,
visTop=0,
maxDim=0) {
this.VERSION = 'Exolve v0.94 October 4 2020'
this.puzzleText = puzzleText
this.containerId = containerId
this.customizer = customizer
this.addStateToUrl = addStateToUrl
this.gridWidth = 0
this.gridHeight = 0
this.credits = []
this.questionTexts = []
// Each nina will be an array containing location [i,j] pairs and/or span
// class names.
this.ninas = []
// For span-class-specified ninas, ninaClassElements[] stores the elements
// along with the colours to apply to them when showing the ninas.
this.ninaClassElements = []
this.grid = []
this.clues = {}
this.cellColours = []
this.submitURL = null
this.submitKeys = []
this.hasDgmlessCells = false
this.hasUnsolvedCells = false
this.hasReveals = false
this.hasAcrossClues = false
this.hasDownClues = false
this.hasNodirClues = false
// Clues labeled non-numerically (like [A] a clue...) use this to create a
// unique clueIndex.
this.nextNonNumId = 1
this.offNumClueIndices = {}
// Objects with keys being JSON arrays of cells, mapping to orphan clues
// they belong to, for revealing.
this.cellsToOrphan = {}
this.szCellsToOrphan = 0
this.visTop = visTop
this.maxDim = maxDim
this.MAX_GRID_SIZE = 100
this.GRIDLINE = 1
this.BAR_WIDTH = 3
this.BAR_WIDTH_BY2 = 2
this.SEP_WIDTH = 2
this.SEP_WIDTH_BY2 = 1.5
this.NUMBER_START_X = 2
this.answersList = []
this.revelationList = []
this.inClueAnnoReveals = {}
// State of navigation
this.currRow = -1
this.currCol = -1
this.currDir = 'A'
this.currClueIndex = null
this.usingGnav = false
this.lastOrphan = null
this.activeCells = [];
this.activeClues = [];
this.showingNinas = false
// Couple of vars to get long click on check/reveal to use just a cell.
this.cellLightToggleTimer = null;
this.cellNotLight = false;
this.numCellsToFill = 0
this.numCellsFilled = 0
this.numCellsPrefilled = 0
this.allClueIndices = []
this.DEFAULT_ORPHAN_LEN = 15
this.BLOCK_CHAR = '⬛';
// We have special meanings for 0 (unfilled) and 1 (block in diagramless cell)
// in solution states. For crosswords with digits, we use these to stand for
// 0 and 1 respectively, in solution states.
this.DIGIT0 = '-'
this.DIGIT1 = '~'
this.scriptRE = null
this.scriptLowerCaseRE = null
this.colorScheme = {
'background': 'black',
'cell': 'white',
'active': 'mistyrose',
'currclue': 'white',
'orphan': 'linen',
'input': '#ffb6b4',
'light-label': 'black',
'light-text': 'black',
'light-label-input': 'black',
'light-text-input': 'black',
'circle': 'gray',
'circle-input': 'gray',
'caret': 'gray',
'arrow': 'mistyrose',
'prefill': 'blue',
'anno': 'darkgreen',
'solved': 'dodgerblue',
'separator': 'blue',
'imp-text': 'darkgreen',
'button': '#4caf50',
'button-text': 'white',
'button-hover': 'darkgreen',
'small-button': 'inherit',
'small-button-text': 'darkgreen',
'small-button-hover': 'lightpink',
}
this.nextLine = 0
this.STATE_SEP = 'xlv'
this.STATES_SEP = 'xxllvv' // xxllvv<id1>......xxllvv<id2>.....
this.textLabels = {
'clear': 'Clear this',
'clear.hover': 'Clear highlighted clues and squares. Clear crossers from full clues with a second click',
'clear-all': 'Clear all!',
'clear-all.hover': 'Clear everything! A second click clears all placeholder entries in clues without known squares',
'check': 'Check this',
'checkcell': 'Check cell',
'check.hover': 'Erase mistakes in highlighted squares. Long-click to check just the current cell',
'check-all': 'Check all',
'check-all.hover': 'Erase all mistakes. Reveal any available annos if no mistakes',
'reveal': 'Reveal this',
'revealcell': 'Reveal cell',
'reveal.hover': 'Reveal highlighted clue/squares. Long-click to reveal just the current cell',
'show-ninas': 'Show ninas',
'show-ninas.hover': 'Show ninas hidden in the grid/clues',
'hide-ninas': 'Hide ninas',
'hide-ninas.hover': 'Hide ninas shown in the grid/clues',
'reveal-all': 'Reveal all!',
'reveal-all.hover': 'Reveal all solutions, available annos, answers, notes!',
'submit': 'Submit',
'submit.hover': 'Submit the solution!',
'setter-by': 'By',
'curr-clue-prev': '‹',
'curr-clue-prev.hover': 'Previous clue',
'curr-clue-next': '›',
'curr-clue-next.hover': 'Next clue',
'squares-filled': 'Squares filled',
'across-label': 'Across',
'down-label': 'Down',
'tools-link': 'Tools',
'tools-link.hover': 'Show/hide tools: list of control keys and scratch pad',
'tools-msg': `
<ul>
<li><b>Tab/Shift-Tab:</b> Jump to the next/previous clue in the current direction</li>
<li><b>Enter, Click/Tap:</b> Toggle current direction</li>
<li><b>Arrow keys:</b> Move to the nearest light square in that direction</li>
<li><b>Spacebar:</b> Place/clear block in the current square if it's diagramless</li>
</ul>`,
'exolve-link': 'Exolve on GitHub',
'report-bug': 'Report bug',
'saving-msg': 'Your entries are auto-saved in cookies, for puzzles accessed over HTTPS and not from local files.',
'saving-bookmark': 'You can bookmark/save this link as additional back-up:',
'saving-url': 'URL',
'shuffle': 'Scratch pad: (click here to shuffle)',
'shuffle.hover': 'Shuffle selected text (or all text, if none selected)',
'across-letter': 'a',
'down-letter': 'd',
'mark-clue.hover': 'Click to forcibly mark/unmark as solved',
'curr-clue-prev': '‹',
'curr-clue-prev.hover': 'Previous clue',
'curr-clue-next': '›',
'curr-clue-next.hover': 'Next clue',
'placeholder.hover': 'You can record your solution here before copying to squares',
'placeholder-copy': '⇲',
'placeholder-copy.hover': 'Copy into currently highlighted squares',
'confirm-clear-all': 'Are you sure you want to clear every entry!?',
'confirm-clear-all-orphans1': 'Are you sure you want to clear every entry!? (The placeholder entries will not be cleared. To clear the placeholders, click on clear-all again after clearing the grid.)',
'confirm-clear-all-orphans2': 'Are you sure you want to clear every entry including all the placeholder entries!?',
'confirm-check-all': 'Are you sure you want to clear mistakes everywhere!?',
'confirm-mismatched-copy': 'Are you sure you want to do this mismatched copy (#letters-from : #squares-to)? ',
'confirm-show-ninas': 'Are you sure you want to reveal the nina(s)!?',
'confirm-reveal-all': 'Are you sure you want to reveal the whole solution!?',
'confirm-submit': 'Are you sure you are ready to submit!?',
'confirm-incomplete-submit': 'Are you sure you want to submit an INCOMPLETE solution!?',
}
// Variables set by exolve-option
this.hideInferredNumbers = false
this.cluesPanelLines = -1
this.allowDigits = false
this.hideCopyPlaceholders = false
this.addSolutionToAnno = true
this.offsetLeft = 0
this.offsetTop = 0
this.language = ''
this.languageScript = ''
this.langMaxCharCodes = 1
this.createPuzzle()
}
// Create the basic HTML structure.
// Set up globals, version number and user agent in bug link.
Exolve.prototype.init = function() {
this.parseOverall()
this.parseRelabel()
this.computeGridSize(this.maxDim)
if (!this.id || !this.id.match(/^[a-zA-Z][a-zA-Z\d-]*$/)) {
this.throwErr(
'Puzzle id should be non-empty, should start with a letter, ' +
'and should only have alphanumeric characters or -: ' + this.id)
}
if (!exolvePuzzles) {
exolvePuzzles = {'42xlvIndex42': 1}
}
if (exolvePuzzles[this.id]) {
this.throwErr('Puzzle id ' + this.id + ' is already in use')
}
exolvePuzzles[this.id] = this
this.index = exolvePuzzles['42xlvIndex42']++
this.prefix = 'xlv' + this.index
const basicHTML = `
<div class="xlv-frame xlv-flex-col" id="${this.prefix}-frame">
<h2 id="${this.prefix}-title" class="xlv-title"></h2>
<div id="${this.prefix}-setter" class="xlv-setter"></div>
<div id="${this.prefix}-preamble" class="xlv-preamble"></div>
<div id="${this.prefix}-curr-clue-parent" class="xlv-curr-clue-parent">
<div id="${this.prefix}-curr-clue" class="xlv-curr-clue"></div>
</div>
<div class="xlv-flex-row">
<div id="${this.prefix}-grid-panel" class="xlv-grid-panel">
<div id="${this.prefix}-grid-parent-centerer"
class="xlv-grid-parent-centerer">
<div id="${this.prefix}-grid-parent" class="xlv-grid-parent">
<svg id="${this.prefix}-grid" class="xlv-grid"
zoomAndPan="disable"></svg>
<div id="${this.prefix}-grid-input-wrapper"
class="xlv-grid-input-wrapper"
style="display:none;left:0;top:0"><input
id="${this.prefix}-grid-input" type="text" maxlength="2"
autocomplete="off" spellcheck="false"
class="xlv-grid-input xlv-cell-text"/></div>
<div id="${this.prefix}-nina-group"
style="display:none;left:0;top:0"></div>
</div> <!-- xlv-grid-parent -->
</div> <!-- xlv-grid-parent-centerer -->
<div id="${this.prefix}-controls-etc" class="xlv-controls-etc">
<div id="${this.prefix}-controls" class="xlv-controls xlv-wide-box">
<div id="${this.prefix}-button-row-1" class="xlv-controls-row">
<button id="${this.prefix}-clear"
class="xlv-button">${this.textLabels['clear']}</button>
<button id="${this.prefix}-clear-all"
class="xlv-button">${this.textLabels['clear-all']}</button>
<button id="${this.prefix}-check" class="xlv-button"
style="display:none">${this.textLabels['check']}</button>
<button id="${this.prefix}-check-all" class="xlv-button"
style="display:none">${this.textLabels['check-all']}</button>
</div> <!-- xlv-button-row-1 -->
<div id="${this.prefix}-button-row-2" class="xlv-controls-row">
<button id="${this.prefix}-reveal" class="xlv-button"
style="display:none">${this.textLabels['reveal']}</button>
<button id="${this.prefix}-ninas" class="xlv-button"
style="display:none">${this.textLabels['show-ninas']}</button>
<button id="${this.prefix}-reveal-all" class="xlv-button"
style="display:none">${this.textLabels['reveal-all']}</button>
</div> <!-- xlv-button-row-2 -->
</div> <!-- xlv-controls -->
<div id="${this.prefix}-errors" class="xlv-errors"></div>
<div id="${this.prefix}-status" class="xlv-status">
<span id="${this.prefix}-squares-filled">${this.textLabels['squares-filled']}</span>:
<span id="${this.prefix}-status-num-filled">0</span>/<span
id="${this.prefix}-status-num-total"></span>
</div> <!-- xlv-status -->
<div id="${this.prefix}-saving" class="xlv-wide-box xlv-saving">
<span id="${this.prefix}-saving-msg">
${this.textLabels['saving-msg']}
</span>
</div> <!-- xlv-saving -->
<div id="${this.prefix}-small-print" class="xlv-wide-box xlv-small-print">
<div id="${this.prefix}-tools" style="display:none">
<div id="${this.prefix}-tools-msg">
${this.textLabels['tools-msg']}
</div>
<div>
<span id="${this.prefix}-shuffle" class="xlv-shuffle"
title="${this.textLabels['shuffle.hover']}"
>${this.textLabels['shuffle']}</span>
<textarea
id="${this.prefix}-scratchpad" class="xlv-scratchpad"
spellcheck="false" rows="2"></textarea>
</div>
</div>
<a id="${this.prefix}-tools-link" href=""
title="${this.textLabels['tools-link.hover']}"
>${this.textLabels['tools-link']}</a>
<a id="${this.prefix}-report-bug"
href="https://github.com/viresh-ratnakar/exolve/issues/new">${this.textLabels['report-bug']}</a>
<a id="${this.prefix}-exolve-link"
href="https://github.com/viresh-ratnakar/exolve">${this.textLabels['exolve-link']}</a>
<span id="${this.prefix}-copyright"></span>
</div> <!-- xlv-small-print -->
<div id="${this.prefix}-questions" class="xlv-wide-box"></div>
<div id="${this.prefix}-submit-parent">
<button id="${this.prefix}-submit"
class="xlv-button" style="display:none">${this.textLabels['submit']}</button>
</div> <!-- submit-parent -->
<div id="${this.prefix}-explanations" class="xlv-wide-box
xlv-explanations" style="display:none"></div>
</div> <!-- xlv-controls-etc -->
<br/>
</div> <!-- xlv-grid-panel -->
<div id="${this.prefix}-clues" class="xlv-flex-row xlv-clues">
<div id="${this.prefix}-across-clues-panel" class="xlv-clues-box"
style="display:none">
<hr/>
<span id="${this.prefix}-across-label"
style="font-weight:bold">${this.textLabels['across-label']}</span>
<table id="${this.prefix}-across"></table>
<br/>
</div>
<div id="${this.prefix}-down-clues-panel" class="xlv-clues-box"
style="display:none">
<hr/>
<span id="${this.prefix}-down-label"
style="font-weight:bold">${this.textLabels['down-label']}</span>
<table id="${this.prefix}-down"></table>
<br/>
</div>
<div id="${this.prefix}-nodir-clues-panel"
class="xlv-clues-box" style="display:none">
<hr/>
<table id="${this.prefix}-nodir"></table>
<br/>
</div>
</div> <!-- xlv-clues -->
</div>
</div> <!-- xlv-frame -->
`
if (document.getElementById(this.prefix + '-frame')) {
this.throwErr('Element with id ' + this.prefix + 'frame already exists')
}
if (!this.containerId) {
this.containerId = 'exolve'
}
const exolveHolder = document.getElementById(this.containerId)
if (exolveHolder) {
if (this.containerId == 'exolve') {
exolveHolder.id = 'exolve' + this.index
}
exolveHolder.insertAdjacentHTML('beforeend', basicHTML)
} else {
document.body.insertAdjacentHTML('beforeend', basicHTML)
}
this.frame = document.getElementById(this.prefix + '-frame')
let title = document.getElementById(this.prefix + '-title')
if (this.title) {
title.innerHTML = this.title
} else {
title.style.display = 'none'
this.title = ''
}
let setter = document.getElementById(this.prefix + '-setter')
if (this.setter) {
setter.innerHTML = `<span id="${this.prefix}-setter-by">${this.textLabels['setter-by']}</span> ${this.setter}`
setter.style.color = this.colorScheme['imp-text']
} else {
setter.style.display = 'none'
this.setter = ''
}
if (this.copyright) {
document.getElementById(this.prefix + '-copyright').innerHTML =
'Ⓒ ' + this.copyright
} else {
this.copyright = ''
}
let smallPrintBox = document.getElementById(this.prefix + '-small-print')
for (credit of this.credits) {
smallPrintBox.insertAdjacentHTML('beforeend', '<div>' + credit + '</div>')
}
this.gridPanel = document.getElementById(this.prefix + '-grid-panel');
this.svg = document.getElementById(this.prefix + '-grid');
this.gridInputWrapper = document.getElementById(this.prefix + '-grid-input-wrapper');
this.gridInputWrapper.style.width = '' + this.squareDim + 'px'
this.gridInputWrapper.style.height = '' + (this.squareDim - 2) + 'px'
this.gridInputWrapper.insertAdjacentHTML('beforeend',
`<div id="${this.prefix}-grid-input-rarr" class="xlv-grid-input-rarr">▸</div>`)
this.gridInputWrapper.insertAdjacentHTML('beforeend',
`<div id="${this.prefix}-grid-input-darr" class="xlv-grid-input-darr">▾</div>`)
this.gridInputRarr = document.getElementById(this.prefix + '-grid-input-rarr');
this.gridInputDarr = document.getElementById(this.prefix + '-grid-input-darr');
this.gridInputRarr.style.color = this.colorScheme['arrow']
this.gridInputDarr.style.color = this.colorScheme['arrow']
this.gridInputRarr.style.fontSize = this.arrowSize + 'px'
this.gridInputDarr.style.fontSize = this.arrowSize + 'px'
this.gridInput = document.getElementById(this.prefix + '-grid-input');
this.gridInput.style.caretColor = this.colorScheme['caret']
this.questions = document.getElementById(this.prefix + '-questions');
this.acrossPanel = document.getElementById(this.prefix + '-across-clues-panel')
this.downPanel = document.getElementById(this.prefix + '-down-clues-panel')
this.nodirPanel = document.getElementById(this.prefix + '-nodir-clues-panel')
this.acrossClues = document.getElementById(this.prefix + '-across')
this.downClues = document.getElementById(this.prefix + '-down')
this.nodirClues = document.getElementById(this.prefix + '-nodir')
this.currClue = document.getElementById(this.prefix + '-curr-clue')
this.currClueParent = document.getElementById(this.prefix + '-curr-clue-parent')
this.ninaGroup = document.getElementById(this.prefix + '-nina-group')
this.statusNumFilled = document.getElementById(this.prefix + '-status-num-filled')
this.statusNumTotal = document.getElementById(this.prefix + '-status-num-total')
if (this.addStateToUrl) {
document.getElementById(this.prefix + '-saving').insertAdjacentHTML('beforeend',
`<span id="${this.prefix}-saving-bookmark">
${this.textLabels['saving-bookmark']} </span>
<a id="${this.prefix}-saving-url" href="">URL</a>`);
this.savingURL = document.getElementById(this.prefix + '-saving-url')
}
this.clearButton = document.getElementById(this.prefix + '-clear')
this.clearButton.addEventListener('click', this.clearCurr.bind(this));
this.clearAllButton = document.getElementById(this.prefix + '-clear-all')
this.clearAllButton.addEventListener('click', this.clearAll.bind(this));
this.checkButton = document.getElementById(this.prefix + '-check')
this.checkButton.addEventListener('mousedown', this.cellLightToggler.bind(
this, this.checkButton, this.textLabels['checkcell']));
this.checkButton.addEventListener('mouseup', this.checkCurr.bind(this));
this.checkAllButton = document.getElementById(this.prefix + '-check-all')
this.checkAllButton.addEventListener('click', this.checkAll.bind(this));
this.ninasButton = document.getElementById(this.prefix + '-ninas')
this.ninasButton.addEventListener('click', this.toggleNinas.bind(this));
this.revealButton = document.getElementById(this.prefix + '-reveal')
this.revealButton.addEventListener('mousedown', this.cellLightToggler.bind(
this, this.revealButton, this.textLabels['revealcell']));
this.revealButton.addEventListener('mouseup', this.revealCurr.bind(this));
this.revealAllButton = document.getElementById(this.prefix + '-reveal-all')
this.revealAllButton.addEventListener('click', this.revealAll.bind(this));
this.submitButton = document.getElementById(this.prefix + '-submit')
this.submitButton.addEventListener('click', this.submitSolution.bind(this));
document.getElementById(this.prefix + '-tools-link').addEventListener(
'click', this.toggleTools.bind(this));
this.scratchPad = document.getElementById(this.prefix + '-scratchpad')
this.scratchPad.style.color = this.colorScheme['imp-text']
document.getElementById(this.prefix + '-shuffle').addEventListener(
'click', this.scratchPadShuffle.bind(this));
let info = 'Version: ' + this.VERSION + ', User Agent: ' + navigator.userAgent
document.getElementById(this.prefix + '-report-bug').href =
'https://github.com/viresh-ratnakar/exolve/issues/new?body=' +
encodeURIComponent(info);
this.CURR_ORPHAN_ID = this.prefix + '-curr-orphan'
this.followDirOrder()
// Sets language of puzzle elements if exolve-language was specified.
if (this.language) {
this.frame.lang = this.language
this.gridInput.lang = this.language
this.questions.lang = this.language
this.gridInput.maxLength = '' + (2 * this.langMaxCharCodes)
}
}
Exolve.prototype.log = function(msg) {
console.log('Exolve puzzle #' + this.index + ' [' + this.id + ']: ' + msg)
}
// The overall parser for the puzzle text.
Exolve.prototype.parseOverall = function() {
this.specLines = []
let rawLines = this.puzzleText.trim().split('\n');
for (let rawLine of rawLines) {
let cIndex = rawLine.indexOf('#');
// A # followed by a non-space/non-eol character is not a comment marker.
while (cIndex >= 0 && cIndex + 1 < rawLine.length &&
rawLine.charAt(cIndex + 1) != ' ') {
cIndex = rawLine.indexOf('#', cIndex + 1);
}
if (cIndex >= 0) {
rawLine = rawLine.substr(0, cIndex).trim()
}
if (!rawLine) {
continue;
}
this.specLines.push(rawLine)
}
this.numLines = this.specLines.length
let parsedSec = this.parseSection()
while (parsedSec && parsedSec.section != 'end') {
let firstLine = this.nextLine
let nextParsedSec = this.parseSection()
let lastLine = this.nextLine - 2
if (parsedSec.section == 'begin') {
} else if (parsedSec.section == 'id') {
this.id = parsedSec.value.trim()
} else if (parsedSec.section == 'title') {
this.title = parsedSec.value.trim()
} else if (parsedSec.section == 'setter') {
this.setter = parsedSec.value.trim()
} else if (parsedSec.section == 'copyright') {
this.copyright = parsedSec.value.trim()
} else if (parsedSec.section == 'credits') {
this.credits.push(parsedSec.value.trim())
} else if (parsedSec.section == 'width') {
this.gridWidth = parseInt(parsedSec.value)
} else if (parsedSec.section == 'height') {
this.gridHeight = parseInt(parsedSec.value)
} else if (parsedSec.section == 'preamble' ||
parsedSec.section == 'prelude') {
this.preludeFirstLine = firstLine
this.preludeLastLine = lastLine
} else if (parsedSec.section == 'postscript') {
this.psFirstLine = firstLine
this.psLastLine = lastLine
} else if (parsedSec.section == 'grid') {
this.gridFirstLine = firstLine
this.gridLastLine = lastLine
} else if (parsedSec.section == 'nina') {
this.parseNina(parsedSec.value)
} else if (parsedSec.section == 'colour' ||
parsedSec.section == 'color') {
this.parseColour(parsedSec.value)
} else if (parsedSec.section == 'question') {
this.questionTexts.push(parsedSec.value)
} else if (parsedSec.section == 'submit') {
this.parseSubmit(parsedSec.value)
} else if (parsedSec.section == 'across') {
this.acrossFirstLine = firstLine
this.acrossLastLine = lastLine
} else if (parsedSec.section == 'down') {
this.downFirstLine = firstLine
this.downLastLine = lastLine
} else if (parsedSec.section == 'nodir') {
this.nodirFirstLine = firstLine
this.nodirLastLine = lastLine
} else if (parsedSec.section == 'option') {
this.parseOption(parsedSec.value)
} else if (parsedSec.section == 'language') {
this.parseLanguage(parsedSec.value)
} else if (parsedSec.section == 'explanations') {
this.explnFirstLine = firstLine
this.explnLastLine = lastLine
} else if (parsedSec.section == 'relabel') {
this.relabelFirstLine = firstLine
this.relabelLastLine = lastLine
}
parsedSec = nextParsedSec
}
this.dirOrder = {}
this.dirOrder['A'] = this.acrossFirstLine || 0
this.dirOrder['D'] = this.downFirstLine || 0
this.dirOrder['X'] = this.nodirFirstLine || 0
}
// specLines[] has been parsed till line # nextLine. Find the
// next line beginning with 'exolve-<section>' and return <section> as well
// as the 'value' of the section (the part after ':').
Exolve.prototype.parseSection = function() {
const MARKER = 'exolve-'
while (this.nextLine < this.numLines &&
this.specLines[this.nextLine].trim().indexOf(MARKER) != 0) {
this.nextLine++;
}
if (this.nextLine >= this.numLines) {
return null
}
// Skip past MARKER
let line = this.specLines[this.nextLine].trim().substr(MARKER.length)
let index = line.indexOf(':')
if (index < 0) {
index = line.length
}
this.nextLine++
return {section: line.substr(0, index).trim().toLowerCase(),
value: line.substr(index + 1).trim()}
}
// If s is like 15a or 16D, return A15 or D16. Otherwise just return s.
Exolve.prototype.maybeClueIndex = function(s) {
if (s.trim().match(/^\d+[aAdD]$/)) {
s = s.trim()
s = s.substr(s.length - 1).toUpperCase() + s.substr(0, s.length - 1)
}
return s
}
// Parse a nina line, which consists of cell locations or clue indices.
// Convert the cell locations to [row col] and push an array of these to the
// global ninas array.
Exolve.prototype.parseNina = function(s) {
let nina = []
let cellsOrOthers = s.split(' ')
for (let cellOrOther of cellsOrOthers) {
if (!cellOrOther) {
continue
}
let cellLocation = this.parseCellLocation(cellOrOther)
if (!cellLocation) {
// Must be a class name, for a span-class-specified nina OR a clue index
nina.push(this.maybeClueIndex(cellOrOther))
} else {
nina.push(cellLocation)
}
}
if (nina.length > 0) {
this.ninas.push(nina)
}
}
Exolve.prototype.parseColour = function(s) {
let colourAndCells = s.split(' ')
let colour = ''
for (let c of colourAndCells) {
if (!c) {
continue
}
if (!colour) {
colour = c
continue;
}
let cellLocation = this.parseCellLocation(c)
if (!cellLocation) {
this.cellColours.push([this.maybeClueIndex(c), colour]) // clue index
} else {
this.cellColours.push(cellLocation.concat(colour))
}
}
}
Exolve.prototype.answerListener = function(answer, forceUpper) {
this.deactivateCurrCell()
this.deactivateCurrClue()
this.usingGnav = false
let cursor = answer.selectionStart
if (forceUpper) {
answer.value = answer.value.toUpperCase().trimLeft()
} else {
answer.value = answer.value.trimLeft()
}
answer.selectionEnd = cursor
this.updateAndSaveState()
}
// Parse a questionTexts and create the question elements for (which include
// an input box for the answer). The solution answer may be provided after the
// last ')'.
Exolve.prototype.displayQuestions = function() {
for (let s of this.questionTexts) {
let enumParse = this.parseEnum(s)
let inputLen = enumParse.placeholder.length
let afterEnum = enumParse.afterEnum
let rawQ = s.substr(0, afterEnum)
let hideEnum = false
if (inputLen > 0) {
if (s.substr(afterEnum, 1) == '*') {
let beforeEnum = s.lastIndexOf('(', afterEnum - 1)
if (beforeEnum < 0) {
this.throwErr('Could not find open-paren in question')
}
rawQ = s.substr(0, beforeEnum)
afterEnum++
hideEnum = true
}
}
s = s.substr(afterEnum).trim();
let forceUpper = true;
if (s.substr(0,14) == "[lowercase-ok]") {
forceUpper = false;
s = s.substr(14).trim();
}
let correctAnswer = s;
const question = document.createElement('div')
question.setAttributeNS(null, 'class', 'xlv-question');
const questionText = document.createElement('span')
questionText.innerHTML = rawQ
question.appendChild(questionText)
question.appendChild(document.createElement('br'))
if (inputLen == 0) {
hideEnum = true
inputLen = '30'
}
let rows = Math.floor(inputLen / this.textAreaCols)
if (rows * this.textAreaCols < inputLen) {
rows++
}
let cols = (rows > 1) ? this.textAreaCols : inputLen
let aType = 'input'
if (rows > 1) {
aType = 'textarea'
}
const answer = document.createElement(aType)
if (rows > 1) {
answer.setAttributeNS(null, 'rows', '' + rows);
answer.setAttributeNS(null, 'cols', '' + cols);
} else {
answer.setAttributeNS(null, 'size', '' + cols);
}
answer.setAttributeNS(null, 'class', 'xlv-answer');
this.answersList.push({
ans: correctAnswer,
input: answer,
isq: true,
});
if (!hideEnum) {
answer.setAttributeNS(null, 'placeholder', enumParse.placeholder);
}
answer.setAttributeNS(null, 'class', 'xlv-answer');
answer.style.color = this.colorScheme['imp-text']
if (rows == 1) {
answer.setAttributeNS(null, 'type', 'text');
}
answer.setAttributeNS(null, 'maxlength', '' + inputLen * this.langMaxCharCodes);
answer.setAttributeNS(null, 'autocomplete', 'off');
answer.setAttributeNS(null, 'spellcheck', 'false');
question.appendChild(answer)
this.questions.appendChild(question)
answer.addEventListener('input', this.answerListener.bind(this, answer, forceUpper));
}
}
Exolve.prototype.parseSubmit = function(s) {
let parts = s.split(' ')
if (s.length < 2) {
this.throwErr('Submit must have a URL and a param name for the solution')
}
this.submitURL = parts[0]
this.submitKeys = []
for (let i = 1; i < parts.length; i++) {
this.submitKeys.push(parts[i])
}
}
Exolve.prototype.parseOption = function(s) {
let sparts = s.split(' ')
for (let spart of sparts) {
spart = spart.trim().toLowerCase()
if (spart == "hide-inferred-numbers") {
this.hideInferredNumbers = true
continue
}
if (spart == "allow-digits") {
this.allowDigits = true
continue
}
if (spart == "hide-copy-placeholder-buttons") {
this.hideCopyPlaceholders = true
continue
}
if (spart == "no-auto-solution-in-anno") {
this.addSolutionToAnno = false
continue
}
let kv = spart.split(':')
if (kv.length != 2) {
this.throwErr('Expected exolve-option: key:value, got: ' + spart)
}
if (kv[0] == 'clues-panel-lines') {
this.cluesPanelLines = parseInt(kv[1])
if (isNaN(this.cluesPanelLines)) {
this.throwErr('Unexpected val in exolve-option: clue-panel-lines: ' + kv[1])
}
continue
}
if (kv[0] == 'offset-left') {
this.offsetLeft = parseInt(kv[1])
if (isNaN(this.offsetLeft)) {
this.throwErr('Unexpected val in exolve-option: offset-left: ' + kv[1])
}
continue
}
if (kv[0] == 'offset-top') {
this.offsetTop = parseInt(kv[1])
if (isNaN(this.offsetTop)) {
this.throwErr('Unexpected val in exolve-option: offset-top: ' + kv[1])
}
continue
}
if (kv[0] == 'grid-background') {
this.colorScheme['background'] = kv[1]
continue
}
if (kv[0].substr(0, 6) == 'color-' || kv[0].substr(0, 7) == 'colour-') {
let key = kv[0].substr(kv[0].indexOf('-') + 1);
if (!this.colorScheme[key]) {
this.throwErr('Unsupported coloring option: ' + kv[0])
}
this.colorScheme[key] = kv[1]
continue
}
this.throwErr('Unexpected exolve-option: ' + spart)
}
}
Exolve.prototype.parseLanguage = function(s) {
const parts = s.trim().split(' ')
if (parts.length < 2) {
this.throwErr('Usage: exolve-language: ' + s + 'cannot be parsed ' +
'as "language-code Script [max-char-codes]"')
}
this.language = parts[0]
this.languageScript = parts[1]
try {
this.scriptRE = new RegExp('\\p{Script=' + this.languageScript + '}', 'u')
this.scriptLowerCaseRE = new RegExp('\\p{Lowercase}', 'u')
} catch (err) {
this.throwErr(
'Your browser ' +
'<a href="https://caniuse.com/#search=Unicode%20property%20escapes"' +
'>does not support Unicode property escapes</a> OR you\'ve provided ' +
'an invalid Script name: ' + this.languageScript)
}
// Hard-code some known scripts requiring langMaxCharCodes
if (this.languageScript.toLowerCase() == 'devanagari') {
this.langMaxCharCodes = 4
}
if (parts.length > 2) {
this.langMaxCharCodes = parseInt(parts[2])
if (isNaN(this.langMaxCharCodes) || this.langMaxCharCodes < 1) {
this.throwErr('invalid max-char-codes in exolve-language: ' + parts[2])
}
}
}
// Extracts the prelude from its previously identified lines and sets up
// its display.
Exolve.prototype.parseAndDisplayPrelude = function() {
if (this.preludeFirstLine >= 0 && this.preludeFirstLine <= this.preludeLastLine) {
let preludeText = this.specLines[this.preludeFirstLine]
let l = this.preludeFirstLine + 1
while (l <= this.preludeLastLine) {
preludeText = preludeText + '\n' + this.specLines[l]
l++;
}
document.getElementById(this.prefix + '-preamble').innerHTML = preludeText
}
}
Exolve.prototype.parseAndDisplayPS = function() {
if (this.psFirstLine >= 0 && this.psFirstLine <= this.psLastLine) {
let psText = this.specLines[this.psFirstLine]
let l = this.psFirstLine + 1
while (l <= this.psLastLine) {
psText = psText + '\n' + this.specLines[l]
l++;
}
psHTML = `<div id='${this.prefix}-postscript' class='xlv-postscript'><hr> ${psText} </div>`;
this.frame.insertAdjacentHTML('beforeend', psHTML);
}
}
// Extracts the explanations section from its previously identified lines,
// populates its element, and adds it to revelationList.
Exolve.prototype.parseAndDisplayExplanations = function() {
if (this.explnFirstLine >= 0 &&
this.explnFirstLine <= this.explnLastLine) {
let explnText = this.specLines[this.explnFirstLine]
let l = this.explnFirstLine + 1
while (l <= this.explnLastLine) {
explnText = explnText + '\n' + this.specLines[l]
l++;
}
const expln = document.getElementById(this.prefix + '-explanations')
expln.innerHTML = explnText
this.revelationList.push(expln)