|
1 | 1 | <script> |
2 | 2 | {% for f, url in form.selectize_fields.items %} |
3 | | - $('#id_{{f}}').selectize({ |
4 | | - plugins: ['remove_button'], |
5 | | - valueField: 'id', |
6 | | - labelField: 'value', |
7 | | - searchField: 'value', |
8 | | - {%if url%} |
9 | | - load: function(query, callback) { |
10 | | - if (!query.length) return callback(); |
11 | | - $.ajax({ |
12 | | - 'url': '{{url}}', |
13 | | - 'type': 'GET', |
14 | | - 'dataType': 'json', |
15 | | - 'data': { |
16 | | - 'query': query, |
17 | | - }, |
18 | | - 'error': function() { callback();}, |
19 | | - 'success': function(res) { callback(res.values);}, |
20 | | - }); |
| 3 | + {% if f == 'tags' or f == 'tag' %} |
| 4 | + $('#id_{{f}}').selectize({ |
| 5 | + plugins: ['remove_button'], |
| 6 | + valueField: 'id', |
| 7 | + labelField: 'name', |
| 8 | + searchField: ['name', 'description'], |
| 9 | + placeholder: 'Select tags (type to search by name or description)...', |
| 10 | + {% if tags_data %} |
| 11 | + options: {{ tags_data|safe }}, |
| 12 | + {% endif %} |
| 13 | + render: { |
| 14 | + option: function(item, escape) { |
| 15 | + return '<div class="option">' + |
| 16 | + '<span class="tag-color" style="background-color: ' + escape(item.color) + '; width: 12px; height: 12px; display: inline-block; border-radius: 2px; margin-right: 6px;"></span>' + |
| 17 | + '<strong>' + escape(item.name) + '</strong>' + |
| 18 | + (item.description ? '<br><small class="text-muted">' + escape(item.description) + '</small>' : '') + |
| 19 | + '</div>'; |
| 20 | + }, |
| 21 | + item: function(item, escape) { |
| 22 | + return '<div class="item">' + |
| 23 | + '<span class="tag-color" style="background-color: ' + escape(item.color) + '; width: 10px; height: 10px; display: inline-block; border-radius: 2px; margin-right: 4px; vertical-align: middle;"></span>' + |
| 24 | + escape(item.name) + |
| 25 | + '</div>'; |
| 26 | + } |
21 | 27 | }, |
22 | | - {%endif%} |
23 | | - onFocus: function() { |
24 | | - if (this.$input.is('[multiple]')) { |
25 | | - return; |
26 | | - } |
27 | | - this.lastValue = this.getValue(); |
28 | | - this.clear(false); |
29 | | - }, |
30 | | - onBlur: function() { |
31 | | - if (this.$input.is('[multiple]')) { |
32 | | - return; |
33 | | - } |
34 | | - if(this.getValue() == '') { |
35 | | - this.setValue(this.lastValue); |
| 28 | + onDropdownOpen: function() { |
| 29 | + var self = this; |
| 30 | + var $dropdown = self.$dropdown; |
| 31 | + var $options = $dropdown.find('[data-selectable]'); |
| 32 | + var totalOptions = self.options ? Object.keys(self.options).length : 0; |
| 33 | + var visibleOptions = $options.length; |
| 34 | + |
| 35 | + // Add footer hint if there are more options than visible (due to search filtering or scrolling) |
| 36 | + if (totalOptions > 6 && !$dropdown.find('.selectize-scroll-hint').length) { |
| 37 | + $dropdown.append('<div class="selectize-scroll-hint text-center text-muted small py-2 px-3" style="border-top: 1px solid #dee2e6; background-color: #f8f9fa;">' + |
| 38 | + '⬇ Scroll to see all ' + totalOptions + ' tags, or type to search' + |
| 39 | + '</div>'); |
| 40 | + } |
36 | 41 | } |
37 | | - }, |
38 | | - }); |
| 42 | + }); |
| 43 | + {% else %} |
| 44 | + $('#id_{{f}}').selectize({ |
| 45 | + plugins: ['remove_button'], |
| 46 | + valueField: 'id', |
| 47 | + labelField: 'value', |
| 48 | + searchField: 'value', |
| 49 | + {%if url%} |
| 50 | + load: function(query, callback) { |
| 51 | + if (!query.length) return callback(); |
| 52 | + $.ajax({ |
| 53 | + 'url': '{{url}}', |
| 54 | + 'type': 'GET', |
| 55 | + 'dataType': 'json', |
| 56 | + 'data': { |
| 57 | + 'query': query, |
| 58 | + }, |
| 59 | + 'error': function() { callback();}, |
| 60 | + 'success': function(res) { callback(res.values);}, |
| 61 | + }); |
| 62 | + }, |
| 63 | + {%endif%} |
| 64 | + onFocus: function() { |
| 65 | + if (this.$input.is('[multiple]')) { |
| 66 | + return; |
| 67 | + } |
| 68 | + this.lastValue = this.getValue(); |
| 69 | + this.clear(false); |
| 70 | + }, |
| 71 | + onBlur: function() { |
| 72 | + if (this.$input.is('[multiple]')) { |
| 73 | + return; |
| 74 | + } |
| 75 | + if(this.getValue() == '') { |
| 76 | + this.setValue(this.lastValue); |
| 77 | + } |
| 78 | + }, |
| 79 | + }); |
| 80 | + {% endif %} |
39 | 81 | {%endfor%} |
40 | 82 | </script> |
41 | 83 |
|
0 commit comments