summaryrefslogtreecommitdiff
path: root/webcards/scripts/gui/input.js
blob: b0bbec05889a148a8c8e0e31e92c1ea702e0dba4 (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
function customSelectValue (el) {
    var sel = el.getAttribute("selected");
        
    if(typeof sel != "undefined") {
        return el.children[parseInt(sel)].getAttribute("value");
    }

    return "";
}

function customSelectOption (el) {
    var sn = Array.prototype.indexOf.call(el.parentElement.children, el);
    var psn = el.parentElement.getAttribute("selected");

    if(typeof psn == "string")
        el.parentElement.children[parseInt(psn)].setAttribute("selected", false);

    if(typeof sn == "string")
        el.parentElement.setAttribute("selected", parseInt(sn));

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

var InputFuncs = {
    createInput: function(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;
    },

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

    colorInput: function(value, id) {
        var el = this.createInput("color", id);
        el.value = value;
        return el;
    },

    textInput: function(value, placeholder, id) {
        var el = this.createInput("text", id);
        el.setAttribute("placeholder", placeholder);
        el.value = value;
        return el;
    },

    numberInput: function(value, id) {
        var el = this.createInput("number", id);
        el.value = value;
        return el;
    },

    //To fix
    fileInput: function(value, id) {
        var el = this.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;
    },

    checkboxInput: function(checked = false, id) {
        var el = this.createInput("checkbox", false, id);
        if(checked)
            el.setAttribute("checked");
        return el;
    },

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

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

        let toWrap = [];

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

        var wrapper = this.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;
    },

    selectOption: function(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
    },

    selectInput: function(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(this.selectOption(names[i], values[i], i == select));
        }

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

        return wrapper;
    },

    wrapInputs: function(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;
    }
};

function Settings (settings = {}) {
    this.settings = settings;

    this.genSettings();
}

Settings.prototype = {
    getSettings: function() {
        var out = {};
        for(let key in this.settings) {

        }
    },

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

    genSettings: function() {
        for(let key in this.settings) {
            switch(this.settings[key].type) {
                case "radio":
                    this.settings[key] = inputFuncs.radioInputs(...this.settings[key].args);
                default:
                    if(typeof inputFuncs[this.settings[key].type+"Input"] != null)
                        this.settings[key] = inputFuncs[this.settings[key].type+"Input"](...this.settings[key].args);
            }
        }
    }
};