summaryrefslogtreecommitdiff
path: root/scripts/gui/input.js
blob: 24450f09cab985b234e6f614e27d447ae1f1e4ef (plain)
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
'use strict';

//Mostly fixed now
class MakeInput {
	static createInput(type = "text", wrap = false, getset = true)
	{
		let el = document.createElement("input");
		el.setAttribute("type", type);

		if(wrap) {
			return this.wrapInputs(type, el);
		}

		if(getset) {
			el.getValue = function () {
				return this.value;
			}

			el.setValue = function(value) {
				this.value = value
			}
		}

		return el;
	}

	// Function deprecated, finding another way to do this
	static inputLabel(text, id)
	{
		let el = document.createElement("label");
		el.innerText = text;
		if(typeof id == "string")
			el.setAttribute("for", id);
		return el;
	}

	static colorInput (value) {
		let el = MakeInput.createInput("color", true);
		el.setValue(value);
		return el;
	}

	static textInput (value, placeholder)
	{
		let el = MakeInput.createInput("text");
		el.setAttribute("placeholder", placeholder);
		el.value = value;
		return el;
	}

	static buttonInput (text)
	{
		let el = MakeInput.createInput("button", false, false);
		el.textContent = text;
		return el;
	}

	static passwordInput (value, placeholder)
	{
		let el = MakeInput.createInput("password");
		el.setAttribute("placeholder", placeholder);
		el.value = value;
		return el;
	}

	static numberInput (value)
	{
		let el = MakeInput.createInput("number");
		el.value = value;
		return el;
	}

	static fileInput (accepts = "", multiple = false) {
		let el = MakeInput.createInput("file", true, false);
		
		let e = el.getElement();
		e.setAttribute("accepts", accepts);
		e.multiple = multiple;
		el.getValue = function() {
			return e.files;
		}
		
		el.setAttribute("data-files", "Choose a file");

		el.firstElementChild.onchange = function () {
			let text = "";
			switch (this.files.length) {
				case 0:
					text = "Choose a file";
					break;
				case 1:
					text = "File: " + this.files[0].name;
					break;
				default:
					text = "Files: " + this.files[0].name + "...";
					break;
			}
			el.setAttribute("data-files", text);
		}

		return el;
	}

	static checkboxInput (value = false) {
		let el = MakeInput.createInput("checkbox", false, false);

		el.getValue = function() {
			return el.checked;
		}

		el.setValue = function(check) {
			el.checked = check;
		}

		el.setValue(value);
		
		return el;
	}

	static radio (value, group, checked = false) {
		let el = MakeInput.createInput("radio", false, false);
		el.setAttribute("name", group);
		el.setAttribute("value", value);
		if(checked)
			el.checked = true;
		return el;
	}

	static radioInput (values, names, group, prompt = "Select One", select = 0) {

		let toWrap = [];

		for(let i = 0; i < values.length; i++) {
			toWrap.push(MakeInput.inputLabel(names[i]));
			if(i == select)
				toWrap.push(MakeInput.radio(values[i], group, true));
			else
				toWrap.push(MakeInput.radio(values[i], group, false));
			toWrap.push(document.createElement("br"));
		}

		let wrapper = MakeInput.wrapInputs("radio", ...toWrap);

		wrapper.setAttribute("data-prompt", prompt);

		wrapper.getValue = function() {
			for(let i = 0; i < this.children.length; i++){
				if(this.children[i].checked)
					return this.children[i].value;
			}
		};

		wrapper.setValue = function(value) {
			for(let i = 0; i < this.children.length; i++){
				if(this.children[i].value == value){
					this.children[i].checked = true;
					return;
				}
			}
		};

		return wrapper;
	}

	static wrapInputs (type, ...el) {

		let wrapper = document.createElement("div");
		wrapper.className = "input-container";
		wrapper.setAttribute("type", type);

		for(let i = 0; i < el.length; i++)
		{
			wrapper.appendChild(el[i]);
		}

		if(el.length == 1)
		{
			wrapper.getValue = function () {return el[0].value;}

			wrapper.setValue = function(value) {el[0].value = value;}

			wrapper.onclick = el[0].click.bind(el[0]);

			wrapper.getElement = function(){return el[0];}
		}

		return wrapper;
	}

	static selectOption (value, text, index, selected) {
		let so = document.createElement("div");
		so.innerText = text;
		so.selectValue = value;
		so.selectIndex = index;
		so.setAttribute("tabindex", 0);
		so.addEventListener("mousedown", MakeInput.selOption.bind(null, so));

		so.addEventListener("keydown", (e) => {
			if (e.key === "Enter") {
				MakeInput.selOption(so);
			}
		});

		if(selected === true)
			so.setAttribute("selected", true);

		return so
	}

	static selectInput (values, names, select = 0) {
		let se = document.createElement("div");
		se.className = "input-select";
		se.setAttribute("selected", select);

		for(let i in names)
		{
			se.appendChild(MakeInput.selectOption(values[i], names[i], i, i == select));
		}
		
		let wrapper = MakeInput.wrapInputs("select", se);
		wrapper.getValue = MakeInput.selValue.bind(null, se);
		wrapper.getIndex = MakeInput.selIndex.bind(null, se);
		wrapper.addOption = MakeInput.selAdd.bind(se);
		wrapper.removeOption = MakeInput.selRem.bind(se);
		wrapper.setIndex = MakeInput.selSet.bind(se);
		wrapper.setAttribute("tabindex", 0);

		return wrapper;
	}

	static selValue (el) {
		let sel = parseInt(el.getAttribute("selected"));
			
		if(typeof sel != "undefined") {
			return el.children[sel].selectValue;
		}

		return "";
	}

	static selIndex (el) {
		return parseInt(el.getAttribute("selected"));
	}

	static selOption (el, dispatch = true) {
		let sn = el.selectIndex;
		let psn = parseInt(el.parentElement.getAttribute("selected"));

		if(Number.isInteger(psn) && psn < el.parentElement.childElementCount)
			el.parentElement.children[psn].setAttribute("selected", false);

		el.parentElement.setAttribute("selected", sn);
		el.setAttribute("selected", true);

		if(dispatch)
			el.parentElement.parentElement.dispatchEvent(new InputEvent("change"));
	}

	static selSet (index) {
		MakeInput.selOption(this.children[index], false);
	}

	static selAdd (name, value) {
		this.appendChild(MakeInput.selectOption(value, name, this.childElementCount, false));
		return this.childElementCount - 1;
	}

	static selRem (index) {
		if(index >= this.childElementCount)
			return false;

		this.children[index].remove();

		for(let i = index; i < this.childElementCount; i++) {
			this.children[index].selectIndex = "" + i;
		}

		return true;
	}

	static titleWrap(el, title) {
		let wrapper = document.createElement("div");
		wrapper.className = "input-title-wrapper";
		wrapper.setAttribute("type", el.getAttribute("type"));
		wrapper.setAttribute("data-title", title);
		
		wrapper.appendChild(el);

		if(el.getAttribute("type") == "checkbox")
		{
			wrapper.onclick = el.click.bind(el);
		}

		return wrapper;
	}
}

// Mostly fixed now
class Settings extends EventTarget {
	constructor (template = {})
	{
		super();
		this.settings = Settings.genSettings(template);
		this.applyEvents(template);

		this.wrappers = {};
	}

	applyEvents (template) {
		for(let key in template)
		{
			if (template[key].type == "button")
				this.settings[key].el.onclick = (function() {this.dispatchEvent(new CustomEvent("click", {detail: key}))}).bind(this);
			else if(typeof MakeInput[template[key].type+"Input"] != null)
				this.settings[key].el.onchange = (function () {this.dispatchEvent(new CustomEvent("change", {detail: key}));}).bind(this);
		}
	}

	static genSettings (template)
	{
		let out = {};

		for(let key in template)
		{
			if(typeof MakeInput[template[key].type+"Input"] != null)
				out[key] = {el: MakeInput[template[key].type+"Input"](...template[key].args), title: template[key].title};
		}
		
		return out;
	}

	getSettings ()
	{
		let out = {};

		for(let key in this.settings)
			out[key] = this.settings[key].el.getValue();
		
		return out;
	}

	putSettings (el)
	{
		this.cleanup();

		this.wrappers = {};

		for(let key in this.settings) {
			this.wrappers[key] = MakeInput.titleWrap(this.settings[key].el, this.settings[key].title)
			el.appendChild(this.wrappers[key]);
		}
			
	}

	cleanup ()
	{
		for(let key in this.wrappers)
			this.wrappers[key].remove();
	}
}