-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleform.js
More file actions
152 lines (138 loc) · 5.76 KB
/
Copy pathsimpleform.js
File metadata and controls
152 lines (138 loc) · 5.76 KB
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
const ScriptGenerator = (function ($) {
if (!!!$) throw Error('jQuery is required. Try to add the following code to you page:<script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"><\/script>');
const formFieldTemplate = `
<div class="inputForm">
<label for="{{fieldName}}">
<span class="form-label">{{fieldName}}:</span>
{{#if values}}
<input name="{{fieldName}}_text" class="{{fieldName}}_text" placeholder="{{placeholder}}" />
<input type="hidden" name="{{fieldName}}" class="{{fieldName}}" />
{{else}}
<input name="{{fieldName}}" class="{{fieldName}}" placeholder="{{placeholder}}" />
{{/if}}
</label>
</div>
`;
const displayFieldTemplate = `
{{#each .}}
<h2>{{name}}</h2>
<div id="{{id}}" class="code"></div>
{{/each}}
`;
const bodyContentTemplate = `
<!-- BODY CONTENT -->
<div class="container">
<div class="formContainer" style="display: none;">
<div class="formInputContainer">
</div>
<div class="inputForm"><button type="button">Execute</button></div>
</div>
<div class="scriptsContainer" style="display: none;">
<button type="button">Reset</button>
<pre class="scriptsDisplay"></pre>
</div>
</div>
`;
class ScriptGenerator {
constructor(formSchema, generatorId) {
this.componentSelector = generatorId ? $(`#${generatorId}`) : $;
this.formSchema = formSchema;
this.loadTemplates();
this.createForm();
}
get(selector) {
return this.componentSelector.find(selector);
}
getTemplate(templateName) {
return $(`script[type="script/templates"][name="${templateName}"]`);
}
loadTemplates() {
this.TEMPLATES = {
resultTemplates: $.makeArray(this.get('script[type="text/templates"]')).map(this.toTemplateItem),
formFieldTemplate: Handlebars.compile(formFieldTemplate),
displayFieldTemplate: Handlebars.compile(displayFieldTemplate)
};
}
loadAndCompileTemplate(name) {
return Handlebars.compile(this.getTemplate(name).html());
}
toTemplateItem(element) {
element = $(element);
return {
name: element.attr("name"),
id: element.attr("name").replace(/\W/g,'_'),
template: Handlebars.compile(element.html())
};
}
createForm() {
this.componentSelector.append(bodyContentTemplate);
this.get('.formInputContainer').html(this.formSchema.map(this.TEMPLATES.formFieldTemplate).join(''));
this.formSchema.forEach(this.setupField.bind(this));
this.get('.formContainer button').click(this.execute.bind(this));
this.get('.scriptsContainer button').click(this.resetForm.bind(this));
this.resetForm();
}
setupField(schema) {
schema.field = schema.values
? new AutoSuggest(this.get(`.${schema.fieldName}`), this.get(`.${schema.fieldName}_text`), schema.values)
: this.get(`.${schema.fieldName}`);
}
resetForm() {
this.get('.formContainer').show();
this.get('.scriptsContainer').hide();
}
execute() {
let params = this.formSchema.reduce((target, item) => (target[item.fieldName]=item.field.val(), target), {});
this.get('.formContainer').hide();
this.createScriptsDisplay(this.TEMPLATES.resultTemplates, params);
this.get('.scriptsContainer').show();
}
createScriptsDisplay(templates, params) {
this.get('.scriptsDisplay').html(this.TEMPLATES.displayFieldTemplate(this.TEMPLATES.resultTemplates));
this.TEMPLATES.resultTemplates.forEach(template => {
this.get(`#${template.id}`).html(template.template(params));
});
}
}
class AutoSuggest {
constructor(field, fieldText, values) {
this.field = field;
this.fieldText = fieldText;
this.values = values;
this.setupEvent();
}
setupEvent() {
this.fieldText.keyup(this.handleSuggestions.bind(this));
}
handleSuggestions(event) {
if(event.originalEvent.keyCode < 65 || event.originalEvent.keyCode > 90) return;
let filter = this.text();
let filterMatcher = this.getFilterMatcher(filter);
let suggestion = this.values.find(filterMatcher);
if (suggestion) {
this.updateFields(suggestion, filter);
} else {
this.val('');
}
}
getFilterMatcher(filter) {
filter = filter.toLowerCase();
return (evaluable) => (evaluable.text || evaluable).toLowerCase().indexOf(filter)===0;
}
updateFields(suggestion, filter) {
let text = suggestion.text || suggestion;
let val = suggestion.value || suggestion;
this.text(text);
this.fieldText.focus();
this.fieldText.get(0).setSelectionRange(filter.length, text.length);
this.val(this.values.indexOf(suggestion) >= 0 ? val : '');
}
val(text) {
return typeof text === 'undefined' ? this.field.val() : this.field.val(text);
}
text(text) {
return typeof text === 'undefined' ? this.fieldText.val() : this.fieldText.val(text);
}
}
return ScriptGenerator;
})(jQuery)