-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcustom.js
1197 lines (1030 loc) · 41.7 KB
/
custom.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
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
// ==================== Sourcebook instances are top-level objects that hold the custom data ====================
var Sourcebook = Class.extend({
all: new Hash(),
init: function init(name) {
this.name = name;
this.data = new Hash();
this.all.set(name, this);
},
getData: function getData(key) {
if (!this.data.contains(key)) {
this.data.set(key, new Hash());
}
return this.data.get(key);
},
addData: function addData(key, dataName, data) {
this.getData(key).set(dataName, data);
},
removeData: function removeData(key, dataName) {
var deleted = false;
var data = this.getData(key);
if (data.contains(dataName)) {
data.remove(dataName);
deleted = true;
}
if (data.length == 0) {
this.data.remove(key);
}
if (this.data.length == 0) {
Sourcebook.removeBook(this.name);
}
return deleted;
}
});
Sourcebook.hasBook = function hasBook(name) {
return Sourcebook.prototype.all.contains(name);
}
Sourcebook.getBook = function getBook(name) {
if (!Sourcebook.hasBook(name)) {
new Sourcebook(name);
}
return Sourcebook.prototype.all.get(name);
}
Sourcebook.allBooks = function allBooks() {
return Sourcebook.prototype.all.keys().sort();
}
Sourcebook.removeBook = function removeBook(name) {
return Sourcebook.prototype.all.remove(name);
}
// ==================== ExpandingTree is a hierarchical tree of data or controls ====================
var ExpandingTree = Class.extend({
init: function init(parentNode, text, controlFn) {
this.parentNode = parentNode;
this.text = text;
this.children = new Hash();
this.expanded = false;
this.div = $("<div/>").addClass('expandingTree');
if (!parentNode) {
this.level = 0;
this.controlFn = controlFn;
} else {
this.level = parentNode.level + 1;
this.controlFn = parentNode.controlFn;
parentNode.children.set(this.text, this);
}
if (this.text) {
this.control = $('<span/>').addClass('treeControl').html('⊞').appendTo(this.div);
this.control.click($.proxy(this.clickControl, this));
$('<span/>').text(' ' + this.text).appendTo(this.div);
}
this.childDiv = $('<div/>').addClass('treeContent').appendTo(this.div);
},
expand: function expand() {
if (!this.expanded) {
this.control.html('⊟');
this.expanded = true;
this.childDiv.show();
this.refresh();
}
},
collapse: function collapse() {
if (this.expanded) {
this.control.html('⊞');
this.expanded = false;
this.childDiv.hide();
}
},
clickControl: function clickControl(evt) {
if (!this.expanded) {
this.expand();
} else {
this.collapse();
}
},
refresh: function refresh() {
if (this.expanded) {
if (this.controlFn) {
var names = this.controlFn(this, true);
$.each(names, $.proxy(function (index, name) {
if (!this.children.contains(name)) {
new ExpandingTree(this, name);
}
this.childDiv.append(this.children.get(name).div);
}, this));
if (names.length < this.children.length) {
// remove any children no longer in names
this.children.each($.proxy(function (childName, child) {
if (names.indexOf(childName) < 0) {
child.div.remove();
this.children.remove(childName);
}
}, this));
}
}
this.children.each($.proxy(function (key, child) {
child.refresh();
}, this));
}
}
});
// ==================== CustomPanel is the base class for all custom panels ====================
var CustomPanel = Class.extend({
nonCustomPanels: new Hash(),
all: new Hash(),
typeMap: new Hash(),
init: function init(data, dontEval, custom) {
this.custom = !!custom;
this.subPanels = new Hash();
this.setData(data, custom);
this.compiled = [];
if (!dontEval)
{
var problem = this.execCode();
if (problem)
alert(problem);
}
},
makeCustom: function makeCustom() {
this.custom = true;
this.subPanels.each(function (key, value) {
value.makeCustom();
});
},
setData: function setData(data, custom) {
this.data = new Hash(data);
if (this.data.contains('subPanels')) {
var subPanelJSON = this.data.remove('subPanels');
return this.buildSubPanelsFromJSON(subPanelJSON, custom);
}
},
getSaveData: function getSaveData(includeNonCustom) {
var result = { };
this.data.each(function (key, value) {
result[key] = value;
});
var subPanels = [];
if (this.subPanels && this.subPanels.length > 0) {
this.subPanels.each(function (id, panel) {
if (includeNonCustom || panel.custom) {
subPanels.push(panel.getSaveData(includeNonCustom));
}
});
if (subPanels.length > 0) {
result['subPanels'] = subPanels;
}
}
return [ this.className, result ];
},
editSubPanel: function editSubPanel(evt, panel) {
evt.stopPropagation();
var panel = evt.data;
panel.showPanel();
},
deleteSubPanel: function deleteSubPanel(evt) {
evt.stopPropagation();
var panel = evt.data;
if (confirm('Are you sure you want to delete ' + panel.getId() + '?')) {
panel.remove();
this.refreshSubPanelsTable();
return true;
}
return false;
},
remove: function remove() {
this.parentPanel.subPanels.remove(this.getId());
this.removeFromSource(this.getSource(), this.panelTitle, this.getShortName());
this.all.get(this.panelTitle).remove(this.getShortName());
if (this.custom) {
var previous = this.nonCustomPanels.remove(this.getId());
if (previous) {
this.addSubPanel(previous);
}
}
},
debugPanelCode: function debugPanelCode(evt) {
evt.stopPropagation();
var panel = evt.data;
debugger;
panel.compile(true);
},
showJSON: function showJSON(evt) {
evt.stopPropagation();
var panel = evt.data;
console.info(JSON.stringify(panel.getSaveData(true)));
},
refreshSubPanelsTable: function refreshSubPanelsTable(panelDiv, subPanels) {
panelDiv = panelDiv || this.panelDiv;
if (!panelDiv) {
return;
}
if (!panelDiv.data('panelTable')) {
panelDiv.data('panelTable', $('<tbody/>').appendTo($('<table/>').addClass('customTable').appendTo(panelDiv)));
}
var table = panelDiv.data('panelTable');
table.empty();
subPanels = subPanels || this.subPanels;
var keys = subPanels.keys().sort();
$.each(keys, $.proxy(function (index, key) {
var panel = subPanels.get(key);
var row = $('<tr/>').appendTo(table);
var cell = $('<td/>').css('width', '1%').html(panel.getShortName()).appendTo(row);
if (!panel.custom) {
cell.addClass('hardcodedFeature');
}
$('<button/>').text('Edit').click(panel, $.proxy(this.editSubPanel, this)).appendTo($('<td/>').css('width', '1%').appendTo(row));
if (panel.custom) {
$('<button/>').text('Delete').click(panel, $.proxy(this.deleteSubPanel, this)).appendTo($('<td/>').css('width', '1%').appendTo(row));
} else {
$('<td/>').css('width', '1%').appendTo(row);
}
if (window.location.protocol == "file:") {
$('<button/>').text('Debug Code').click(panel, $.proxy(this.debugPanelCode, this)).appendTo($('<td/>').css('width', '1%').appendTo(row));
$('<button/>').text('Show JSON in console').click(panel, $.proxy(this.showJSON, this)).appendTo($('<td/>').css('width', '1%').appendTo(row));
} else {
$('<td/>').css('width', '1%').appendTo(row);
$('<td/>').css('width', '1%').appendTo(row);
}
$('<td/>').css('width', '94%').appendTo(row);
}, this));
},
newSubPanel: function newSubPanel(panelType, data, custom) {
var panel = new panelType(data, true, custom);
if (custom) {
panel.makeCustom();
}
this.addSubPanel(panel);
return panel;
},
addSubPanel: function addSubPanel(panel) {
var previous = this.subPanels.set(panel.getId(), panel);
if (panel.custom && previous && !previous.custom) {
this.nonCustomPanels.set(previous.getId(), previous);
}
if (previous) {
previous.removeCompiled();
}
panel.parentPanel = this;
panel.addToSource();
if (!this.all.contains(panel.panelTitle)) {
this.all.set(panel.panelTitle, new Hash());
}
this.all.get(panel.panelTitle).set(panel.getShortName(), panel);
},
openSubPanel: function openSubPanel(evt) {
evt.stopPropagation();
var panelType = evt.data;
this.newSubPanel(panelType).showPanel();
},
buildSubPanelsFromJSON: function buildSubPanelsFromJSON(data, custom) {
var result = [];
$.each(data, $.proxy(function (index, panelData) {
var panelType = CustomPanel.prototype.typeMap.get(panelData[0]);
result.push(this.newSubPanel(panelType, panelData[1], custom));
}, this));
return result;
},
addSubPanelButtons: function addSubPanelButtons(panelTypes) {
var sorted = panelTypes.sort(function (o1, o2) {
var name1 = o1.prototype.className;
var name2 = o2.prototype.className;
if (name1 < name2) {
return -1;
} else if (name1 > name2) {
return 1;
} else {
return 0;
}
});
$.each(panelTypes.sort(), $.proxy(function (index, panelType) {
$('<button/>').html('Add ' + panelType.prototype.panelTitle).click(panelType, $.proxy(this.openSubPanel, this)).appendTo(this.panelDiv);
}, this));
},
renderPanel: function renderPanel() {
var parentDiv;
if (this.parentPanel)
{
if (!this.parentPanel.panelDiv)
this.parentPanel.renderPanel();
parentDiv = this.parentPanel.panelDiv;
}
else
parentDiv = $(document.body);
if (this.panelDiv) {
this.panelDiv.empty();
} else {
this.panelDiv = $('<div/>').addClass('customPanel').appendTo(parentDiv);
}
$('<h4/>').html(this.getPanelTitle()).appendTo(this.panelDiv);
this.form = $('<form/>').appendTo(this.panelDiv);
},
getPanelTitle: function getPanelTitle() {
return this.panelTitle;
},
appendFormTableRow: function appendFormTableRow(title, dataId, type, options) {
var focus = false;
if (!this.formTable) {
this.formTable = $('<tbody/>').appendTo($('<table/>').addClass('customTable').appendTo(this.form));
focus = true;
}
var row = $('<tr/>').appendTo(this.formTable);
var header = $('<th/>').html(title).appendTo(row);
if (!type) {
type = 'text';
}
var input;
if (!dataId) {
header.attr('colspan', 2);
} else if (type == 'select') {
input = $('<select/>').appendTo($('<td/>').appendTo(row)).attr('name', dataId);
$.each(options, function (index, value) {
var option = $("<option/>").appendTo(input);
option.attr('value', value);
option.text(value);
});
} else if (type == 'textarea') {
input = $('<textarea/>').appendTo($('<td/>').appendTo(row)).attr('name', dataId);
} else {
var cell = $('<td/>').appendTo(row);
input = $('<input/>').appendTo(cell).attr('name', dataId);
input.attr('type', type);
if (type == 'text' && options) {
input.autocomplete({ source: options, appendTo: cell });
}
}
if (input && this.data.contains(dataId)) {
input.val(this.data.get(dataId));
}
if (input && focus) {
input.focus();
}
return input;
},
appendSourceRow: function appendSourceRow() {
var input = this.appendFormTableRow('Source book', 'source', 'text', Sourcebook.allBooks());
},
appendFooter: function appendFooter(subpanels) {
this.refreshSubPanelsTable();
$('<hr/>').appendTo(this.panelDiv);
if (subpanels) {
this.addSubPanelButtons(subpanels);
$('<hr/>').appendTo(this.panelDiv);
}
$('<button/>').text('Done').click($.proxy(this.commitData, this)).appendTo(this.panelDiv);
$('<input/>').text('Reset').attr('type', 'reset').appendTo(this.panelDiv);
$('<button/>').text('Cancel').click($.proxy(this.hidePanel, this)).appendTo(this.panelDiv);
},
showPanel: function showPanel() {
if (!this.panelDiv) {
this.renderPanel();
}
this.panelDiv.show();
},
hidePanel: function hidePanel(evt) {
if (evt) {
evt.stopPropagation();
}
if (this.panelDiv) {
this.panelDiv.hide();
}
},
getSource: function getSource() {
if (this.data.contains('source')) {
return this.data.get('source');
} else {
return 'Custom';
}
},
getShortName: function getShortName() {
throw 'Error - a subclass didn\'t override getShortName';
},
getId: function getId() {
return this.getSource() + ' ' + this.panelTitle + ' ' + this.getShortName();
},
execCode: function execCode() {
var result = this.compile(true);
if (typeof(result) == 'string')
return result;
else
return null;
},
removeCompiled: function removeCompiled() {
if (this.compiled) {
$.each(this.compiled, function (index, modifier) {
modifier.remove();
});
}
this.compiled = [];
this.subPanels.each(function (key, subPanel) {
subPanel.removeCompiled();
});
},
compile: function compile(execute) {
return 'Error - Custom panel ' + this.getId() + ' failed to override compile.';
},
collectData: function collectData() {
var result = new Hash();
$.each(this.form.serializeArray(), function (index, pair) {
result.set(pair.name, pair.value);
});
if (this.subPanels) {
result.set('subPanels', this.subPanels);
}
this.data = result;
return result;
},
commitData: function commitData(evt) {
evt.stopPropagation();
if (this.parentPanel && this.parentPanel.commitPanel(this)) {
this.hidePanel();
}
},
inspect: function inspect() {
var result = [];
result.push(this.className);
this.data.each(function (key, value) {
if (value) {
result.push(key);
if (value instanceof Hash) {
result.push(value.values());
} else {
result.push(value);
}
}
});
return result.inspect();
},
addToSource: function addToSource() {
if (this.data.contains('source')) {
var book = Sourcebook.getBook(this.getSource());
book.addData(this.panelTitle, this.getShortName(), this);
}
},
removeFromSource: function removeFromSource(source, panelTitle, shortName) {
var deleted = false;
if (Sourcebook.hasBook(source)) {
var book = Sourcebook.getBook(source);
deleted = book.removeData(panelTitle, shortName);
}
return deleted;
},
// A sub-panel has been committed for us
commitPanel: function commitPanel(panel) {
var oldData = panel.data;
var oldId = panel.getId();
var oldSource = panel.getSource();
var oldTitle = panel.panelTitle;
var oldShortName = panel.getShortName();
panel.collectData();
var problem = panel.compile(true);
if (typeof(problem) == 'string') {
// There's a problem - abort.
alert(problem);
panel.data = oldData;
return false;
}
this.subPanels.remove(oldId);
this.removeFromSource(oldSource, oldTitle, oldShortName);
this.all.get(oldTitle).remove(oldShortName);
panel.makeCustom();
this.addSubPanel(panel);
this.refreshSubPanelsTable();
return true;
}
});
(function augmentExtend() {
var prevExtend = CustomPanel.extend;
var newExtend = function extend() {
var newClass = prevExtend.apply(this, arguments);
CustomPanel.prototype.typeMap.set(newClass.prototype.className, newClass);
newClass.extend = newExtend;
return newClass;
};
CustomPanel.extend = newExtend;
})();
// -------------------- TopPanel is the top-level source data panel --------------------
var TopPanel = CustomPanel.extend({
panelTitle: 'Source Data',
className: 'TopPanel',
init: function init() {
this._super(null, true);
},
getShortName: function getShortName() {
return 'Custom Panel';
},
indexOfLastCommonSpace: function indexOfLastCommonSpace(s1, s2) {
var lastSpace = -1;
for (var index = 0; index < s1.length && index < s2.length; ++index) {
if (s1[index] != s2[index]) {
return lastSpace;
} else if (s1[index] == ' ') {
lastSpace = index;
}
}
return lastSpace;
},
getCommonPrefixWords: function getCommonPrefixWords(list) {
var result = [ ];
for (var index = 0; index < list.length; ++index) {
var entry = list[index];
if (index == 0) {
result.push(entry);
} else if (entry.indexOf(result[result.length - 1]) != 0) {
var lastCommonSpace = this.indexOfLastCommonSpace(result[result.length - 1], entry);
// ignore "The " as a common prefix
if (lastCommonSpace == -1 || (lastCommonSpace == 3 && entry.indexOf('The ') == 0)) {
result.push(entry);
} else {
result[result.length - 1] = result[result.length - 1].substring(0, lastCommonSpace + 1);
}
}
}
return result;
},
treeControlFn: function treeControlFn (parentNode, refreshOnly) {
var names = [];
if (parentNode.level == 0) {
Sourcebook.prototype.all.each(function (key, value) {
if (value.data && value.data.length > 0)
names.push(key);
});
names = names.sort();
} else if (parentNode.level == 1) {
var sourceName = parentNode.text;
var book = Sourcebook.prototype.all.get(sourceName);
if (book) {
names = book.data.keys().sort(function (a, b) {
if (a == "Custom" || a < b) {
return -1;
} else if (b == "Custom" || a > b) {
return 1;
} else {
return 0;
}
});
}
} else {
var levelTwo = (parentNode.level == 3) ? parentNode.parentNode : parentNode;
var sourceName = levelTwo.parentNode.text;
var type = levelTwo.text;
var book = Sourcebook.prototype.all.get(sourceName);
var entries = book.data.get(type);
if (parentNode.level == 2 && entries.length > 20) {
names = this.getCommonPrefixWords(entries.keys().sort());
// Don't bother doing the extra level for things that don't break down nicely.
if (names.length > entries.length/2 || names.length < 3) {
names = [];
}
} else if (parentNode.level == 3) {
// Filter entries by parent's prefix
var prefix = parentNode.text;
var newEntries = new Hash();
entries.each(function (name, entry) {
if (name.indexOf(prefix) == 0) {
newEntries.set(name, entry);
}
});
entries = newEntries;
}
if (names.length == 0) {
this.refreshSubPanelsTable(parentNode.childDiv, entries);
}
}
return names;
},
appendTree: function appendTree() {
this.tree = new ExpandingTree(null, 'Sourcebooks', $.proxy(this.treeControlFn, this));
this.tree.div.appendTo(this.panelDiv);
this.tree.expand(true);
},
refreshSubPanelsTable: function refreshSubPanelsTable(panelDiv, subPanels) {
if (!panelDiv && !subPanels) {
this.tree.refresh();
} else {
this._super(panelDiv, subPanels);
}
},
renderPanel: function renderPanel() {
if (this.tree && this.tree.div.parentNode == this.panelDiv)
this.panelDiv.removeChild(this.tree.div);
this._super();
$("<p/>").text('The classes, moves and other features of characters are editable from this page. Grey entries are pre-defined, while black are custom. Custom features are saved in your character\'s save file, and will execute when that character is loaded. Editing a pre-defined feature will override it with a custom version.').appendTo(this.panelDiv);
$("<p/>").text('You can also create new custom features by clicking the buttons at the bottom of this page.').appendTo(this.panelDiv);
this.appendTree();
$('<hr/>').appendTo(this.panelDiv);
this.addSubPanelButtons([ CharacterClassPanel, RacePanel, ClassMovePanel, AlignmentPanel, GearPanel ]);
$('<hr/>').appendTo(this.panelDiv);
$('<button/>').text('Done').click($.proxy(this.hidePanel, this)).appendTo(this.panelDiv);
},
setData: function setData(data, custom) {
var subPanels = this._super(data, custom);
if (this.tree) {
this.tree.refresh();
}
if (subPanels) {
$.each(subPanels, function (index, subPanel) {
subPanel.compile(true);
});
}
}
});
// -------------------- CharacterClassPanel defines class data --------------------
var CharacterClassPanel = CustomPanel.extend({
panelTitle: 'Class',
className: 'CharacterClassPanel',
getShortName: function getShortName() {
return this.data.get('name');
},
gearWithTags: function (request, response, keepCost) {
var hash = CustomPanel.prototype.all.get('Gear');
var searchTerm = request.term.toLowerCase();
var multiples = searchTerm.match(/([0-9]+\s*x\s*)(.*)/);
var prefix = '';
if (multiples) {
prefix = multiples[1];
searchTerm = multiples[2];
}
if (searchTerm.length >= 2) {
var result = $.map(hash.keys().sort(), function (entryKey) {
var entry = hash.get(entryKey);
var result = entry.data.get('name');
if (entry.data.get('tags')) {
var tags = entry.data.get('tags');
if (!keepCost) {
tags = tags.replace(/(^|,\s*)[0-9]+ coin(s?)(\s*,\s*)?/, '$1');
}
result += ' (' + tags + ')';;
}
if (result.toLowerCase().indexOf(searchTerm) >= 0) {
return prefix + result;
} else {
return undefined;
}
});
response(result);
}
},
gearListToEnglish: function gearListToEnglish(gearList) {
var items = gearList.toLowerCase().split(/\s*;\s*/);
if (items.length > 1) {
var last = items.pop();
items[items.length - 1] += ' and ' + last;
}
return items.join(', ');
},
renderPanel: function renderPanel() {
this._super();
this.appendFormTableRow('Class Name', 'name');
this.appendSourceRow();
var iconInput = this.appendFormTableRow('Class Icon', 'classIcon', 'textarea');
var iconDisplay = $('<div/>').addClass('iconDisplay').html(this.data.get('classIcon'));
iconInput.attr('rows', 10).attr('cols', 80).after(iconDisplay);
iconInput.change(function (evt) {
iconDisplay.html(iconInput.val());
});
this.appendFormTableRow('Look');
this.appendFormTableRow('Look suggestions 1', 'look1').attr('size', 50);
this.appendFormTableRow('Look suggestions 2', 'look2').attr('size', 50);
this.appendFormTableRow('Look suggestions 3', 'look3').attr('size', 50);
this.appendFormTableRow('Look suggestions 4', 'look4').attr('size', 50);
this.appendFormTableRow('Combat');
this.appendFormTableRow('Damage Die', 'damage', 'select', [ 'd4', 'd6', 'd8', 'd10' ] );
this.appendFormTableRow('Base HP', 'baseHp');
this.appendFormTableRow('Gear');
this.appendFormTableRow('Load capacity', 'load').after($('<span/>').text(' + Str').addClass('roll'));
var initialGear = this.appendFormTableRow('Initial gear - separate multiple items with ;', 'gear', 'text', $.proxy(this.gearWithTags, this));
initialGear.css('width', '60em');
multiAutocomplete(initialGear, ';');
this.appendFooter([ GearChoicePanel, ClassAlignmentPanel, ClassBondPanel ]);
},
compile: function compile(execute) {
if (!this.data.get("name"))
return "Class must have a name!";
if (execute) {
this.removeCompiled();
var name = this.data.get("name");
this.compiled.push(new ModifierClass('lookSuggestions1', name, this.data.get('look1')));
this.compiled.push(new ModifierClass('lookSuggestions2', name, this.data.get('look2')));
this.compiled.push(new ModifierClass('lookSuggestions3', name, this.data.get('look3')));
this.compiled.push(new ModifierClass('lookSuggestions4', name, this.data.get('look4')));
var diceIcon = "<svg><use xlink:href='#" + this.data.get('damage') + "SVG' /></svg>";
this.compiled.push(new ModifierClass('diceIcon', name, diceIcon));
this.compiled.push(new ModifierClass('baseHp', name, this.data.get('baseHp')));
this.compiled.push(new ModifierClass('classIcon', name, this.data.get('classIcon')));
this.compiled.push(new ModifierClass('baseLoad', name, this.data.get('load')));
}
this.subPanels.each(function (key, subPanel) {
subPanel.compile(execute);
});
}
});
// -------------------- GearChoicePanel defines a choice of gear for a character --------------------
var GearChoicePanel = CustomPanel.extend({
panelTitle: 'Choice of Gear',
className: 'GearChoicePanel',
orderSpaces: function orderSpaces() {
var order = parseInt(this.data.get('order')) || 0;
return Array(10 - order).join(' ');
},
getShortName: function getShortName() {
return this.orderSpaces() + this.panelTitle + ' "' + this.data.get('instructions') + '"';
},
renderPanel: function renderPanel() {
this._super();
this.appendFormTableRow('Instructions (e.g. "Choose your defenses")', 'instructions');
this.appendFormTableRow('Order on sheet', 'order', 'select', [1, 2, 3, 4, 5, 6, 7, 8, 9]);
this.appendFormTableRow('Number of selections', 'selections');
this.appendFooter([ GearOptionPanel ]);
},
compile: function compile(execute) {
if (!this.data.get("instructions"))
return "Gear choice must have instructions!";
if (!this.data.get("selections"))
return "Gear choice must nominate the number of selections!";
if (!this.subPanels || this.subPanels.size == 0)
return "Gear choice must have some options!";
this.subPanels.each(function (key, subPanel) {
subPanel.compile(execute);
});
}
});
// -------------------- GearOptionPanel defines a single gear option that can be selected --------------------
var GearOptionPanel = CustomPanel.extend({
panelTitle: 'Gear Option',
className: 'GearOptionPanel',
getShortName: function getShortName() {
return this.panelTitle + ' "' + this.data.get('gear') + '"';
},
renderPanel: function renderPanel() {
this._super();
var gear = this.appendFormTableRow('Gear - separate multiple items with ;', 'gear', 'text', $.proxy(this.parentPanel.parentPanel.gearWithTags, this));
gear.css('width', '60em');
multiAutocomplete(gear, ';');
this.appendFooter();
},
compile: function compile(execute) {
if (!this.data.get("gear"))
return "Gear option must specify the gear!";
}
});
// -------------------- AlignmentPanel defines a single alignment move --------------------
var AlignmentPanel = CustomPanel.extend({
panelTitle: 'Alignment Move',
className: 'AlignmentPanel',
getShortName: function getShortName() {
return this.data.get('alignment') + ' Alignment Move "' + this.data.get('move') + '"';
},
renderPanel: function renderPanel(hideSource) {
this._super();
if (!hideSource) {
this.appendSourceRow();
}
this.appendFormTableRow('Alignment', 'alignment');
this.appendFormTableRow('Move', 'move', 'textarea').attr('rows', 4).attr('cols', 120);
this.appendFooter();
},
compile: function compile(execute) {
if (!this.data.get("alignment"))
return "Alignment must have a name!";
if (!this.data.get("move"))
return "Alignment Move for Class must have a move!";
if (execute) {
this.removeCompiled();
}
}
});
// -------------------- ClassAlignmentPanel defines a single class-specific alignment move --------------------
var ClassAlignmentPanel = AlignmentPanel.extend({
panelTitle: 'Alignment Move for Class',
className: 'ClassAlignmentPanel',
getShortName: function getShortName() {
return 'Alignment Move "' + this.data.get('alignment') + '"';
},
getPanelTitle: function getPanelTitle() {
return this.panelTitle + ' ' + this.parentPanel.form.find('input[name=name]').val();
},
renderPanel: function renderPanel() {
this._super(true);
},
compile: function compile(execute) {
this._super(execute);
if (execute) {
var className = this.parentPanel.data.get('name');
this.compiled.push(new ModifierClassHashSet('alignment', className, this.data.get('alignment'), this.data.get('move')));
}
}
});
// -------------------- ClassBondPanel defines a single class-specific bond --------------------
var ClassBondPanel = CustomPanel.extend({
panelTitle: 'Initial Bond for Class',
className: 'ClassBondPanel',
getShortName: function getShortName() {
return 'Initial Bond "' + this.data.get('bond') + '"';
},
renderPanel: function renderPanel() {
this._super();
this.appendFormTableRow('Bond', 'bond').css('width', '60em');
this.appendFooter();
},
getPanelTitle: function getPanelTitle() {
return this.panelTitle + ' ' + this.parentPanel.form.find('input[name=name]').val();
},
compile: function compile(execute) {
if (!this.data.get("bond"))
return "Bond may not be empty!";
if (execute) {
this.removeCompiled();
var className = this.parentPanel.data.get('name');
var bond = this.data.get('bond').replace(/_/, '________');
this.compiled.push(new ModifierClassAppend('bonds', className, bond));
}
}
});
// -------------------- RacePanel defines race data --------------------
var RacePanel = CustomPanel.extend({
panelTitle: 'Race',
className: 'RacePanel',
getShortName: function getShortName() {
return this.data.get('name');
},
renderPanel: function renderPanel() {
this._super();
this.appendFormTableRow('Race Name', 'name');
this.appendSourceRow();
this.appendFormTableRow('<br/>Race moves and suggested names for classes');
this.appendFooter([ RaceClassPanel ]);
},
compile: function compile(execute) {
if (!this.data.get("name"))
return "Race must have a name!";
if (execute) {
this.removeCompiled();
var name = this.data.get("name");
this.subPanels.each(function (key, subPanel) {
subPanel.compile(execute);
});
}
}
});
// -------------------- RaceClassPanel defines a single race+class move --------------------
var RaceClassPanel = CustomPanel.extend({