File: autocomplete.js

package info (click to toggle)
zoph 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,632 kB
  • sloc: php: 28,044; javascript: 10,435; sql: 527; sh: 153; makefile: 4
file content (402 lines) | stat: -rw-r--r-- 13,955 bytes parent folder | download | duplicates (2)
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// This file is part of Zoph.
//
// Zoph is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Zoph is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Zoph; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


var autocomplete=function() {
    var selectedvalue=[];
    var dataarray=[];
    var keyarray=[];
    var oldvalue=[];
    var oldtext;

    var open;

    function init() {
        var ac=getElementsByClass('autocomplete');

        for (var el of ac) {
            inputToAutocomplete(el);
        }
        setTimeout(autocomplete.setpos,10);

    }

    function inputToAutocomplete(el) {
        // This function converts an input field to an autocomplete field

        // Take _id from the id.
        var id = el.id;
        var underscore=id.indexOf("_");
        if(underscore>0) {
            id = id.substring(0,underscore);
        } else {
            // prevent duplicate id
            el.id=id + "_id";
        }

        var text=el;
        text.id=id;
        text.addEventListener("mousedown", show);
        text.addEventListener("keyup", change);
        text.addEventListener("focus", focus);
        text.addEventListener("blur", unfocus);
        text.addEventListener("mouseup", change);
        text.addEventListener("keydown", handleKeys);

        text.setAttribute("autocomplete", "off");

        text.className=text.className.replace("autocomplete", "autocompinput");
        text.style.width="200px";

        var dropdown=document.createElement("ul");

        dropdown.className="autocompdropdown";
        dropdown.id=id + "dropdown";

        dropdown.style.position="absolute";
        dropdown.style.display="none";

        el.parentNode.insertBefore(dropdown,el.nextSibling);

        if(el.parentNode.className.indexOf("multiple")>=0 && el.id.indexOf("[")) {
            // This is a field that can appear multiple times, so we add a
            // 'remove' link
            var imgRemove=document.createElement("img");
            imgRemove.setAttribute("onClick", "autocomplete.remove(this); return false");
            imgRemove.setAttribute("src", icons["remove"]);
            imgRemove.className="actionlink";

            el.parentNode.insertBefore(imgRemove,el.nextSibling.nextSibling);

        }
    }

    function remove(obj) {
        obj.parentNode.removeChild(obj.previousSibling); // remove dropdown
        obj.parentNode.removeChild(obj.previousSibling); // remove input
        obj.parentNode.removeChild(obj.previousSibling); // remove hidden input
        obj.parentNode.removeChild(obj);             // remove icon
    }

    function httpResponse(object, xml) {
        var dropdown=document.getElementById(object + "dropdown");
        var text=document.getElementById(object);
        var root=[];
        removeChildren(dropdown);
        text.style.backgroundImage=icons["down2"];
        var xmlobj=object.split("_");
        if(xmlobj[1]=="parent") {
            xmlobj.shift();
        }
        var node=xmlobj[1];
        root=xml.getElementsByTagName(XML.rootnode[node]);

        // These will be rebuilt during the XML processing
        //
        dataarray=[];
        keyarray=[];
        selectedvalue[dropdown.id]=0;
        build_tree(root[0], dropdown, XML.rootnode[node], XML.node[node]);
    }


    function setpos() {
        var input=getElementsByClass("autocompinput");
        var dropdown=getElementsByClass("autocompdropdown");
        for (var i=0; i<input.length; i++) {
            dropdown[i].style.left=input[i].offsetLeft + "px";
            dropdown[i].style.top=input[i].offsetTop + input[i].offsetHeight + "px";
        }
    }
    function show() {
        showdropdown(this);
    }

    function showdropdown(obj) {
        var dropdown=document.getElementById(obj.id + "dropdown");
        open=dropdown;
        var autocompdropdown=[];
        oldvalue[obj.id]=obj.value;
        oldtext=-1;
        autocompdropdown=getElementsByClass("autocompdropdown");

        for (var ac of autocompdropdown) {
            removeChildren(ac);
            hidedropdown(ac.previousSibling);
        }

        obj.value="";
        dropdown.style.display="block";
        dropdown.style.width=obj.offsetWidth + "px";
        setpos();
        obj.focus();
        obj.removeEventListener("mousedown", show);
        obj.addEventListener("mousedown", hide);
    }

    function hide() {
        hidedropdown(this);
    }

    function hidedropdown(obj) {
        var dropdown=document.getElementById(obj.id + "dropdown");
        dropdown.style.display="none";
        if(oldvalue[obj.id]) {
            obj.value=oldvalue[obj.id];
        }
        obj.removeEventListener("mousedown", hide);
        obj.addEventListener("mousedown", show);
        window.focus();
    }

    function build_tree(xmltree, parent, branchname, nodename) {
        var dropdown=open;
        var children=xmltree.childNodes;
        var li, span, key, name;
        for (var i=0; i<children.length; i++) {
            if(children[i].nodeName==branchname) {
               var ul=document.createElement("ul");
               ul=build_tree(children[i], ul, branchname, nodename);
               parent.appendChild(ul);
            } else if (children[i].nodeName==nodename) {
                if(children[i].childNodes.length> 0 && children[i].childNodes[0].nodeName!=branchname) {
                    li=document.createElement("li");
                    span=document.createElement("span");
                    key=children[i].childNodes[0].firstChild.nodeValue;
                    keyarray.push(parseInt(key,10));
                    name=children[i].childNodes[1].firstChild.nodeValue;
                    dataarray.push(name);
                    li.appendChild(span);
                    li.onclick=clickli;

                    if (nodename=="place") {
                        li.className="location";
                    } else {
                        li.className=nodename;
                    }
                    span.appendChild(document.createTextNode(key));
                    span.style.display="none";
                    if (name=="&nbsp;") {
                        // You cannot use &nbsp; in a textnode
                        // However, to minimize cross site scripting attacks
                        // I don't want to use innerHTML for all elements
                        li.innerHTML=("&nbsp;");
                    } else {
                        li.appendChild(document.createTextNode(name));
                    }
                    parent.appendChild(li);
                }
                parent=build_tree(children[i], parent, branchname, nodename);
            }
            if (i==selectedvalue[dropdown.id] && i!==0) {
                li.id="selected";
            }
        }
        return parent;
    }

    function change() {
        update(this.id);
    }

    function update(objid) {
        var obj=document.getElementById(objid);

        var dropdown = document.getElementById(obj.id + "dropdown");

        if(dropdown.style.display!="none") {
            // if the dropdown is invisible, don't bother updating it.
            var value=obj.value;
            if(oldtext!=value) {
                XML.getData(obj.id, value);
                oldtext=value;
            }
            var selectedli=document.getElementById("selected");
            if (selectedli) {
                selectedli.scrollIntoView(true);
            }
        }
    }

    function focus() {
        var dropdown=document.getElementById(this.id + "dropdown");
        if (dropdown.style.display=="none") {
            showdropdown(this);
        }
    }

    function unfocus() {
        // Whenever a selection from a list is made, the textbox will
        // also lose focus, this delay is made to give the browser
        // time to process the click, before the dropdown is destroyed.
        var obj=this;
        setTimeout(function() { autocomplete.hidedropdown(obj); } , 200);
    }

    function clickli() {
        var li=this;
        var key=li.firstChild.innerHTML;
        var newvalue=li.lastChild.nodeValue;
        oldvalue[open.previousSibling.id]=null;
        selectli(open.id, key, newvalue);
        open=false;
    }

    function selectli(dropdownid, key, newvalue) {
        var dropdown = document.getElementById(dropdownid);
        var field = dropdown.previousSibling;
        var orig_field = field.previousSibling;

        field.value = newvalue;

        orig_field.value = key;

        hidedropdown(field);
        if(field.parentNode.className.indexOf("multiple")>=0 && field.id.indexOf("[")) {
            // if a dropdown field is inside a fieldset with class 'multiple'
            // we will automatically generate a new dropdown for this field.
            createNewInput(field);
        }
    }

    function createNewInput(after) {
        var input=after.cloneNode(true);
        var hidden=after.previousSibling.cloneNode(true);

        input.id=increaseValueInBrackets(after.id);
        if(!document.getElementById(input.id)) {
            input.name=increaseValueInBrackets(after.name);

            hidden.id=increaseValueInBrackets(after.previousSibling.id);
            hidden.name=increaseValueInBrackets(after.previousSibling.name);

            input.value="";
            hidden.value="";
            after.parentNode.insertBefore(input,after.nextSibling.nextSibling.nextSibling);
            after.parentNode.insertBefore(hidden,input);
            inputToAutocomplete(input);
        }
    }

    function handleKeys(event) {
        var obj=this;
        var dropdown = document.getElementById(obj.id + "dropdown");
        var keycode=event.code;

        var maxlength=0;
        var key;
        var match=[];
        var nowselected;

        if(keycode=="Tab") {
            event.preventDefault();
            var constraint=obj.value;
            match = dataarray.filter(data => data.substring(0,constraint.length).toUpperCase() == constraint.toUpperCase());
            maxlength = match.reduce((max, current) => Math.max(current.length, max), 0);
            if(match.length>1) {
                for (var m=0; m<=maxlength; m++) {
                    if (!match.every((matchval) => matchval.substring(0,m).toUpperCase() == match[0].substring(0,m).toUpperCase())) {
                        break;
                    }
                }
                obj.value=trim(match[0].substring(0,m - 1));

            } else if (match.length==1) {
                obj.value=trim(match[0]);
            }
        } else if (keycode=="ArrowUp") {
            event.preventDefault();
            selectedvalue[dropdown.id]--;

            // First deselect the currently selected
            nowselected=document.getElementById("selected");
            if(nowselected) {
                nowselected.id="";
            }
            var flattree=flattentree(dropdown, "LI");
            if (selectedvalue[dropdown.id] < 0) {
                selectedvalue[dropdown.id]=0;
            }
            flattree[selectedvalue[dropdown.id]].id="selected";
        } else if (keycode=="ArrowDown") {
            event.preventDefault();
            selectedvalue[dropdown.id]++;
            nowselected=document.getElementById("selected");
            if(nowselected) {
                nowselected.id="";
            }
            flattree=flattentree(dropdown, "LI");
            if (selectedvalue[dropdown.id] > (flattree.length - 1)) {
                selectedvalue[dropdown.id] =flattree.length - 1;
            }
            flattree[selectedvalue[dropdown.id]].id="selected";
        } else if (keycode=="Enter") {
            event.preventDefault();
            var nextTab;

            flattree=flattentree(dropdown, "LI");
            if(flattree.length==2) {
                // If there's only one element (+ empty entry) in the list
                // we suppose one will select that on pressing enter
                flattree[1].id="selected";
            }
            nowselected=document.getElementById("selected");

            if(nowselected) {
                oldvalue[open.previousSibling.id]=null;
                key=parseInt(nowselected.firstChild.innerHTML, 10);
                var newvalue=nowselected.lastChild.nodeValue;
                selectli(dropdown.id, key, newvalue);
            }
            var inputfields=document.getElementsByTagName("input");
            for (var f=0; f<inputfields.length; f++) {
                if(inputfields[f]==open.previousSibling) {
                    nextTab=inputfields[f+1];
                    break;
                }
            }
            nextTab.focus();
        }
    }

    function flattentree(root, element, flattree) {
        if (!flattree) {
            flattree=[];
        }

        for (var node of root.childNodes) {
            if (node.tagName==element) {
                flattree.push(node);
            }
            if(node.childNodes.length>0) {
                flattree=flattentree(node, element, flattree);
            }
        }
        return flattree;
    }

    return {
        setpos:setpos,
        init:init,
        hidedropdown:hidedropdown,
        httpResponse:httpResponse,
        remove:remove
    };
}();

if(window.addEventListener) {
    window.addEventListener("load",autocomplete.init,false);
    window.addEventListener("resize",autocomplete.setpos, false);
}