1 <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
3 <div class="autocomplete" style="width:400px;">
4 <input id="user_field" name="ignore_this" class="form-control" autocomplete="off" type="text" placeholder="{{placeholder}}" value="{{initial.name}}" oninput="search(this.value)"
5 {% if disabled %} disabled {% endif %}
8 <input type="hidden" id="selector" name="{{ name }}" class="form-control" style="display: none;"
9 {% if disabled %} disabled {% endif %}
13 <ul id="drop_results"></ul>
16 <div id="default_entry_wrap" style="display: none;">
17 <div class="list_entry unremovable_list_entry">
18 <p id="default_text" class="full_name"></p>
19 <button class="btn-remove btn disabled">remove</button>
26 <div id="added_counter" style="text-align: center; margin: 10px;"><p id="added_number" style="display: inline;">0</p><p style="display: inline;">/
27 {% if selectable_limit > -1 %} {{ selectable_limit }} {% else %} ∞ {% endif %}added</p></div>
37 list-style-type: none;
44 border: solid 1px #ddd;
51 border: 1px solid #ddd;
52 background-color: #f6f6f6;
54 text-decoration: none;
67 border: 1px solid #ddd;
70 vertical-align: middle;
76 #drop_results li a:hover{
77 background-color: #ffffff;
81 display: inline-block;
85 display: inline-block;
91 <script type="text/javascript">
93 var show_from_noentry = {{show_from_noentry|default:"false"}};
94 var show_x_results = {{show_x_results|default:-1}};
95 var results_scrollable = {{results_scrollable|default:"false"}};
96 var selectable_limit = {{selectable_limit|default:-1}};
97 var field_name = "{{name|default:"users"}}";
98 var placeholder = "{{placeholder|default:"begin typing"}}";
99 var default_entry = "{{default_entry}}";
102 var items = {{items|safe}}
105 var expanded_name_trie = {}
106 expanded_name_trie.isComplete = false;
107 var small_name_trie = {}
108 small_name_trie.isComplete = false;
110 string_trie.isComplete = false;
112 var added_items = [];
114 var added_template = {{ added_list|default:"{}" }};
118 var default_entry_div = document.getElementById("default_entry_wrap");
119 default_entry_div.style.display = "inherit";
121 var entry_p = document.getElementById("default_text");
122 entry_p.innerText = default_entry;
127 if( show_from_noentry )
133 var textfield = document.getElementById("user_field");
134 var drop = document.getElementById("drop_results");
136 textfield.disabled = "True";
137 drop.style.display = "none";
139 var btns = document.getElementsByClassName("btn-remove");
140 for( var i = 0; i < btns.length; i++ )
142 btns[i].classList.add("disabled");
147 build_all_tries(items);
149 var initial = {{ initial|safe }};
151 for( var i = 0; i < initial.length; i++)
153 select_item(String(initial[i]));
155 if(initial.length == 1)
157 search(items[initial[0]]["small_name"]);
158 document.getElementById("user_field").value = items[initial[0]]["small_name"];
162 function build_all_tries(dict)
170 function add_item(item)
173 add_to_tree(item['expanded_name'], id, expanded_name_trie);
174 add_to_tree(item['small_name'], id, small_name_trie);
175 add_to_tree(item['string'], id, string_trie);
178 function add_to_tree(str, id, trie)
183 if( !inner_trie[str.charAt(0)] )
186 inner_trie[str.charAt(0)] = new_trie;
190 new_trie = inner_trie[str.charAt(0)];
193 if( str.length == 1 )
195 new_trie.isComplete = true;
196 new_trie.itemID = id;
198 inner_trie = new_trie;
199 str = str.substring(1);
203 function search(input)
205 if( input.length == 0 && !show_from_noentry){
209 else if( input.length == 0 && show_from_noentry)
211 dropdown(items); //show all items
216 var tr1 = getSubtree(input, expanded_name_trie);
218 var tr2 = getSubtree(input, small_name_trie);
220 var tr3 = getSubtree(input, string_trie);
222 var results = collate(trees);
227 function getSubtree(input, given_trie)
230 recursive function to return the trie accessed at input
233 if( input.length == 0 ){
238 var substr = input.substring(0, input.length - 1);
239 var last_char = input.charAt(input.length-1);
240 var subtrie = getSubtree(substr, given_trie);
241 if( !subtrie ) //substr not in the trie
245 var indexed_trie = subtrie[last_char];
250 function serialize(trie)
253 takes in a trie and returns a list of its item id's
258 return itemIDs; //empty, base case
260 for( var key in trie )
266 itemIDs = itemIDs.concat(serialize(trie[key]));
268 if ( trie.isComplete )
270 itemIDs.push( trie.itemID );
276 function collate(trees)
279 takes a list of tries
280 returns a list of ids of objects that are available
283 for( var i in trees )
285 var available_IDs = serialize(trees[i]);
286 for( var j=0; j<available_IDs.length; j++){
287 var itemID = available_IDs[j];
288 results[itemID] = items[itemID];
294 function dropdown(ids)
297 takes in a mapping of ids to objects in items
298 and displays them in the dropdown
300 var drop = document.getElementById("drop_results");
301 while(drop.firstChild)
303 drop.removeChild(drop.firstChild);
308 var result_entry = document.createElement("li");
309 var result_button = document.createElement("a");
311 var result_text = document.createTextNode(obj['small_name'] + " : " + obj['expanded_name']);
312 result_button.appendChild(result_text);
313 result_button.setAttribute('onclick', 'select_item("' + obj['id'] + '")');
314 result_entry.appendChild(result_button);
315 drop.appendChild(result_entry);
318 if( !drop.firstChild )
320 drop.style.display = 'none';
324 drop.style.display = 'inherit';
328 function select_item(item_id)
331 var item = items[item_id];
332 if( (selectable_limit > -1 && added_items.length < selectable_limit) || selectable_limit < 0 )
334 if( added_items.indexOf(item) == -1 )
336 added_items.push(item);
340 update_selected_list();
341 document.getElementById("user_field").focus();
344 function remove_item(item_ref)
347 item = Object.values(items)[item_ref];
348 var index = added_items.indexOf(item);
349 added_items.splice(index, 1);
351 update_selected_list()
352 document.getElementById("user_field").focus();
355 function edit_item(item_id){
356 var wf_type = "{{wf_type}}";
357 parent.add_edit_wf(wf_type, item_id);
360 function update_selected_list()
362 document.getElementById("added_number").innerText = added_items.length;
363 selector = document.getElementById('selector');
364 selector.value = JSON.stringify(added_items);
365 added_list = document.getElementById('added_list');
367 while(selector.firstChild)
369 selector.removeChild(selector.firstChild);
371 while(added_list.firstChild)
373 added_list.removeChild(added_list.firstChild);
378 for( var key in added_items )
380 item = added_items[key];
382 list_html += '<div class="list_entry"><p class="full_name">'
383 + item["expanded_name"]
384 + '</p><p class="small_name">, '
386 + '</p><button onclick="remove_item('
387 + Object.values(items).indexOf(item)
388 + ')" class="btn-remove btn">remove</button>';
390 list_html += '<button onclick="edit_item('
392 + ')" class="btn-remove btn">edit</button>';
394 list_html += '</div>';
397 added_list.innerHTML = list_html;
403 display: inline-block;
406 display: inline-block;