Update to latest master (live mode, repeat-chars filter.)
Chris Pressey
9 years ago
54 | 54 | <script src="lexeduct-browser.js"></script> |
55 | 55 | <script> |
56 | 56 | (new LexeductUI()).init({ |
57 | 'container': document.getElementById('container'), | |
58 | 'initialText': document.getElementById('initial-text').innerHTML | |
57 | container: document.getElementById('container'), | |
58 | initialText: document.getElementById('initial-text').innerHTML, | |
59 | liveMode: true | |
59 | 60 | }); |
60 | 61 | </script> |
11 | 11 | |
12 | 12 | function LexeductUI() { |
13 | 13 | var container, input, output, processButton, tranformersPanel; |
14 | var liveMode; | |
15 | 14 | var MAX_TRANSFORMER_SLOTS = 8; // TODO dynamic |
16 | 15 | var transformerSlots = []; |
17 | 16 | var transformerNames; |
18 | 17 | |
19 | 18 | this.init = function(cfg) { |
19 | var $this = this; | |
20 | ||
20 | 21 | container = cfg.container; |
22 | cfg.liveMode = !!cfg.liveMode; | |
23 | ||
21 | 24 | input = yoob.makeTextArea(container, 40, 20, cfg.initialText); |
25 | input.onkeyup = function() { | |
26 | if ($this.liveMode) { | |
27 | $this.process(); | |
28 | } | |
29 | }; | |
22 | 30 | |
23 | 31 | var transformersPanel = yoob.makeDiv(container); |
24 | 32 | transformersPanel.style.border = "2px solid black"; |
33 | 41 | } |
34 | 42 | |
35 | 43 | processButton = yoob.makeButton(transformersPanel, "Process", this.process); |
36 | ||
37 | yoob.makeCheckbox(transformersPanel, false, "Live mode", function(b) { | |
38 | liveMode = b; | |
44 | yoob.makeCheckbox(transformersPanel, cfg.liveMode, "Live mode", function(b) { | |
45 | $this.setLiveMode(b); | |
39 | 46 | }); |
40 | 47 | yoob.makeLineBreak(transformersPanel); |
41 | 48 | |
45 | 52 | } |
46 | 53 | |
47 | 54 | output = yoob.makeTextArea(container, 40, 20); |
55 | ||
56 | this.setLiveMode(cfg.liveMode); | |
57 | }; | |
58 | ||
59 | this.setLiveMode = function(b) { | |
60 | this.liveMode = b; | |
61 | processButton.disabled = b; | |
48 | 62 | }; |
49 | 63 | |
50 | 64 | this.process = function() { |
66 | 80 | this.updateParametersPanel = function(slot, panel) { |
67 | 81 | var parameters = transformer[slot.name].parameters; |
68 | 82 | panel.innerHTML = ""; // delete any previous controls |
69 | for (var key in parameters) { | |
70 | if (parameters.hasOwnProperty(key)) { | |
71 | var desc = parameters[key][0]; | |
72 | var def = parameters[key][1]; | |
73 | var label = yoob.makeSpan(panel, key); | |
74 | var input = yoob.makeTextInput(panel, 24, def); | |
75 | slot.selectedParams[key] = def; | |
76 | input.onchange = function() { | |
77 | slot.selectedParams[key] = input.value; | |
78 | if (liveMode) { | |
79 | process(); | |
80 | } | |
81 | } | |
82 | yoob.makeLineBreak(panel); | |
83 | for (var paramName in parameters) { | |
84 | if (parameters.hasOwnProperty(paramName)) { | |
85 | var desc = parameters[paramName][0]; | |
86 | var def = parameters[paramName][1]; | |
87 | this.makeParameterEditor(slot, panel, paramName, desc, def); | |
83 | 88 | } |
84 | 89 | } |
90 | }; | |
91 | ||
92 | this.makeParameterEditor = function(slot, panel, paramName, desc, def) { | |
93 | var label = yoob.makeSpan(panel, paramName); | |
94 | var paramInput = yoob.makeTextInput(panel, 24, def); | |
95 | slot.selectedParams[paramName] = def; | |
96 | var $this = this; | |
97 | paramInput.onkeyup = function() { | |
98 | slot.selectedParams[paramName] = paramInput.value; | |
99 | if ($this.liveMode) { | |
100 | $this.process(); | |
101 | } | |
102 | }; | |
103 | yoob.makeLineBreak(panel); | |
85 | 104 | }; |
86 | 105 | |
87 | 106 | this.makeTransformerSlot = function(container, index) { |
98 | 117 | select.onchange = function(e) { |
99 | 118 | transformerSlots[index].name = select.options[select.selectedIndex].value; |
100 | 119 | $this.updateParametersPanel(transformerSlots[index], parametersPanel); |
101 | if (liveMode) { | |
102 | process(); | |
120 | if ($this.liveMode) { | |
121 | $this.process(); | |
103 | 122 | } |
104 | 123 | }; |
105 | 124 |
12 | 12 | transformer['identity'] = module.exports; |
13 | 13 | module.exports = { |
14 | 14 | makeTransformer: function(cfg) { |
15 | cfg.chance = parseInt(cfg.chance || "100", 10); | |
15 | 16 | return function(str, state) { |
16 | 17 | var s = ""; |
17 | 18 | for (var i = 0; i < str.length; i++) { |
18 | var c = cfg.chars.charAt(Math.floor(Math.random() * cfg.chars.length)); | |
19 | s += str.charAt(i) + c; | |
19 | s += str.charAt(i); | |
20 | if (Math.floor(Math.random() * 100) < cfg.chance) { | |
21 | s += cfg.chars.charAt( | |
22 | Math.floor(Math.random() * cfg.chars.length) | |
23 | ); | |
24 | } | |
20 | 25 | } |
21 | 26 | return s; |
22 | 27 | }; |
23 | 28 | }, |
24 | 29 | parameters: { |
25 | 'chars': ["The set of characters to select from", ""] | |
30 | 'chars': ["The set of characters to select from", ""], | |
31 | 'chance': ["Probability (0-100) of applying to any individual character", "100"] | |
26 | 32 | }, |
27 | 33 | description: "Insert a randomly-selected character after each character" |
28 | 34 | }; |
52 | 58 | transformer['lower'] = module.exports; |
53 | 59 | module.exports = { |
54 | 60 | makeTransformer: function(cfg) { |
61 | cfg.chance = parseInt(cfg.chance || "100", 10); | |
55 | 62 | return function(str, state) { |
56 | 63 | var s = ""; |
57 | 64 | for (var i = 0; i < str.length; i++) { |
58 | 65 | var c = str.charAt(i); |
59 | if (cfg.chars.indexOf(c) === -1) { | |
60 | s += c; | |
66 | if (cfg.chars.indexOf(c) > -1 && | |
67 | Math.floor(Math.random() * 100) < cfg.chance) { | |
68 | continue; | |
69 | } | |
70 | s += c; | |
71 | } | |
72 | return s; | |
73 | }; | |
74 | }, | |
75 | parameters: { | |
76 | 'chars': ["The set of characters to remove", ""], | |
77 | 'chance': ["Probability (0-100) of applying to any individual character", "100"] | |
78 | }, | |
79 | description: "Remove all occurrences of the specified characters" | |
80 | }; | |
81 | ||
82 | transformer['remove-chars'] = module.exports; | |
83 | module.exports = { | |
84 | makeTransformer: function(cfg) { | |
85 | cfg.count = parseInt(cfg.count || "1", 10); | |
86 | cfg.chance = parseInt(cfg.chance || "100", 10); | |
87 | return function(str, state) { | |
88 | var s = ""; | |
89 | for (var i = 0; i < str.length; i++) { | |
90 | var c = str.charAt(i); | |
91 | s += c; | |
92 | if (Math.floor(Math.random() * 100) < cfg.chance) { | |
93 | for (var j = 0; j < cfg.count; j++) { | |
94 | s += c; | |
95 | } | |
61 | 96 | } |
62 | 97 | } |
63 | 98 | return s; |
64 | 99 | }; |
65 | 100 | }, |
66 | 101 | parameters: { |
67 | 'chars': ["The set of characters to remove", ""] | |
102 | 'count': ["How many extra occurrences of the character to insert", "1"], | |
103 | 'chance': ["Probability (0-100) of applying to any individual character", "100"] | |
68 | 104 | }, |
69 | description: "Remove all occurrences of the specified characters" | |
105 | description: "Insert extra copies of the character after each character" | |
70 | 106 | }; |
71 | 107 | |
72 | transformer['remove-chars'] = module.exports; | |
108 | transformer['repeat-chars'] = module.exports; | |
73 | 109 | module.exports = { |
74 | 110 | makeTransformer: function(cfg) { |
75 | 111 | cfg.chance = parseInt(cfg.chance || "100", 10); |