summaryrefslogtreecommitdiff
path: root/webcards/scripts/gui/input.js
blob: 6ed3d39349e6e269cb3137c90c633ca0930b7b95 (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
'use strict';

//This whole clusterfuq of functions needs fixing.
class MakeInput {
    static createInput(type = "text", id)
    {
        var el = document.createElement("input");
        el.setAttribute("type", type);
        
        if(typeof id == "string")
            el.setAttribute("id", id);

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

        return el;
    }

    static inputLabel(text, id)
    {
        var el = document.createElement("label");
        el.innerText = text;
        if(typeof id == "string")
            el.setAttribute("for", id);
        return el;
    }

    static colorInput (value, id) {
        var el = MakeInput.createInput("color", id);
        el.value = value;
        return el;
    }

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

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

    //To fix
    static fileInput (value, id) {
        var el = MakeInput.createInput("file", id);
        
        el.value = value;
        
        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 (checked = false, id) {
        var el = MakeInput.createInput("checkbox", false, id);
        if(checked)
            el.setAttribute("checked");
        return el;
    }

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

    static radioInputs (group, names, values, checked = 0, id) {

        let toWrap = [];

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

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

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

        if(typeof id == "string")
            wrapper.setAttribute("id", id);

        return wrapper;
    }

    static selectOption (text, value, selected) {
        var so = document.createElement("div");
        so.innerText = text;
        so.setAttribute("value", value);
        so.addEventListener("mousedown", customSelectOption.bind(null, so));

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

        return so
    }

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

        for(let i in names)
        {
            se.appendChild(MakeInput.selectOption(names[i], values[i], i == select));
        }

        if(typeof id == "string")
            se.setAttribute("id", id);
        
        var wrapper = MakeInput.wrapInputs("select", se);
        wrapper.getValue = MakeInput.selValue.bind(null, se);
        wrapper.setAttribute("tabindex", 0);

        return wrapper;
    }

    static wrapInputs (type, ...el) {

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

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

        return wrapper;
    }

    static selValue (el) {
        let sel = parseInt(el.getAttribute("selected"));
            
        if(typeof sel != "undefined") {
            return el.children[sel].getAttribute("value");
        }
    
        return "";
    }
    
    static selOption (el) {
        let sn = Array.prototype.indexOf.call(el.parentElement.children, el);
        let psn = parseInt(el.parentElement.getAttribute("selected"));
    
        if(Number.isInteger(psn))
            el.parentElement.children[psn].setAttribute("selected", false);
    
        el.parentElement.setAttribute("selected", sn);
        el.setAttribute("selected", true);
    }
}

class Settings {
    constructor (template = {})
    {
        this.settings = Settings.genSettings(template);
    }

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

        for(let key in template)
        {
            switch(template[key].type)
            {
                case "radio":
                    out[key] = MakeInput.radioInputs(...template[key].args);
                    break;
                default:
                    if(typeof MakeInput[template[key].type+"Input"] != null)
                    out[key] = MakeInput[template[key].type+"Input"](...template[key].args);
            }
        }
        
        return out;
    }

    getSettings ()
    {
        var out = {};

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

    putSettings (el)
    {
        for(let key in this.settings)
            el.appendChild(this.settings[key]);
    }
}