| 1 |
var Admin = window.Admin || {}; |
| 2 |
|
| 3 |
(function(A) { |
| 4 |
|
| 5 |
function getJSON(url, cb) { |
| 6 |
fetch(url, {credentials: 'same-origin'}) |
| 7 |
.then(function(resp) { return resp.json(); }) |
| 8 |
.then(cb); |
| 9 |
} |
| 10 |
|
| 11 |
function debounce(fn, wait) { |
| 12 |
var timer = null; |
| 13 |
return function() { |
| 14 |
var args = arguments, self = this; |
| 15 |
clearTimeout(timer); |
| 16 |
timer = setTimeout(function() { fn.apply(self, args); }, wait); |
| 17 |
}; |
| 18 |
} |
| 19 |
|
| 20 |
/* paginated list of models displayed in a modal window */ |
| 21 |
function AjaxModalList(modal_elem, on_select) { |
| 22 |
var self = this; |
| 23 |
this.modal_elem = modal_elem; |
| 24 |
this.modal = new bootstrap.Modal(modal_elem); |
| 25 |
this.input = modal_elem.querySelector('.fk-lookup-input'); |
| 26 |
this.list = modal_elem.querySelector('ul.result-list'); |
| 27 |
this.next_btn = modal_elem.querySelector('a.next'); |
| 28 |
this.prev_btn = modal_elem.querySelector('a.previous'); |
| 29 |
this.on_select = on_select; |
| 30 |
|
| 31 |
this.input.addEventListener('keyup', debounce(function() { |
| 32 |
self.load(self.input.dataset.ajaxUrl); |
| 33 |
}, 200)); |
| 34 |
|
| 35 |
[this.prev_btn, this.next_btn].forEach(function(btn) { |
| 36 |
btn.addEventListener('click', function(e) { |
| 37 |
e.preventDefault(); |
| 38 |
if (!btn.classList.contains('disabled') && btn.dataset.page) { |
| 39 |
self.load(self.input.dataset.ajaxUrl + '&page=' + btn.dataset.page); |
| 40 |
} |
| 41 |
}); |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
AjaxModalList.prototype.load = function(url) { |
| 46 |
var self = this; |
| 47 |
getJSON(url + '&query=' + encodeURIComponent(this.input.value), function(data) { |
| 48 |
self.list.innerHTML = ''; |
| 49 |
data.object_list.forEach(function(o) { |
| 50 |
var link = document.createElement('a'); |
| 51 |
link.href = '#'; |
| 52 |
link.textContent = o.repr; |
| 53 |
link.addEventListener('click', function(e) { |
| 54 |
e.preventDefault(); |
| 55 |
self.on_select(o); |
| 56 |
self.modal.hide(); |
| 57 |
}); |
| 58 |
var li = document.createElement('li'); |
| 59 |
li.appendChild(link); |
| 60 |
self.list.appendChild(li); |
| 61 |
}); |
| 62 |
|
| 63 |
[[self.prev_btn, data.prev_page], [self.next_btn, data.next_page]].forEach(function(pair) { |
| 64 |
pair[0].classList.toggle('disabled', !pair[1]); |
| 65 |
pair[0].dataset.page = pair[1] || ''; |
| 66 |
}); |
| 67 |
}); |
| 68 |
}; |
| 69 |
|
| 70 |
AjaxModalList.prototype.open = function() { |
| 71 |
this.load(this.input.dataset.ajaxUrl); |
| 72 |
this.modal.show(); |
| 73 |
}; |
| 74 |
|
| 75 |
/* raw-id foreign key: hidden input + button that opens a lookup modal */ |
| 76 |
A.ModelAdminRawIDField = function(field_name) { |
| 77 |
this.field_name = field_name; |
| 78 |
}; |
| 79 |
|
| 80 |
A.ModelAdminRawIDField.prototype.init = function(repr) { |
| 81 |
var hidden_elem = document.getElementById(this.field_name), |
| 82 |
modal_elem = document.getElementById('modal-' + this.field_name), |
| 83 |
btn = document.createElement('a'); |
| 84 |
|
| 85 |
btn.className = 'btn btn-outline-primary'; |
| 86 |
btn.href = '#'; |
| 87 |
btn.textContent = repr || 'Select...'; |
| 88 |
|
| 89 |
var list = new AjaxModalList(modal_elem, function(o) { |
| 90 |
btn.textContent = o.repr; |
| 91 |
hidden_elem.value = o.id; |
| 92 |
}); |
| 93 |
|
| 94 |
btn.addEventListener('click', function(e) { |
| 95 |
e.preventDefault(); |
| 96 |
list.open(); |
| 97 |
}); |
| 98 |
hidden_elem.insertAdjacentElement('afterend', btn); |
| 99 |
}; |
| 100 |
|
| 101 |
/* upgrade an ajax-select: a search input that repopulates the select's |
| 102 |
options from the model admin's ajax_list endpoint */ |
| 103 |
A.ajaxSelect = function(select) { |
| 104 |
var search = document.createElement('input'); |
| 105 |
search.type = 'text'; |
| 106 |
search.placeholder = 'Type to search...'; |
| 107 |
search.className = 'form-control form-control-sm w-auto'; |
| 108 |
|
| 109 |
search.addEventListener('input', debounce(function() { |
| 110 |
var url = select.dataset.source + |
| 111 |
'?field=' + encodeURIComponent(select.dataset.param) + |
| 112 |
'&query=' + encodeURIComponent(search.value); |
| 113 |
getJSON(url, function(data) { |
| 114 |
select.innerHTML = ''; |
| 115 |
data.object_list.forEach(function(o) { |
| 116 |
var opt = document.createElement('option'); |
| 117 |
opt.value = o.id; |
| 118 |
opt.textContent = o.repr; |
| 119 |
select.appendChild(opt); |
| 120 |
}); |
| 121 |
}); |
| 122 |
}, 200)); |
| 123 |
|
| 124 |
select.insertAdjacentElement('beforebegin', search); |
| 125 |
}; |
| 126 |
|
| 127 |
/* filter class */ |
| 128 |
A.ModelAdminFilter = function() { |
| 129 |
this.wrapper = document.getElementById('filter-wrapper'); |
| 130 |
this.lookups_elem = document.getElementById('filter-fields'); |
| 131 |
this.filter_list = this.wrapper && this.wrapper.querySelector('.filter-list'); |
| 132 |
}; |
| 133 |
|
| 134 |
A.ModelAdminFilter.prototype.init = function() { |
| 135 |
var self = this; |
| 136 |
document.querySelectorAll('a.field-filter').forEach(function(link) { |
| 137 |
link.addEventListener('click', function(e) { |
| 138 |
e.preventDefault(); |
| 139 |
self.add_row(link.dataset.field, link.dataset.select); |
| 140 |
}); |
| 141 |
}); |
| 142 |
|
| 143 |
if (!this.filter_list) return; |
| 144 |
|
| 145 |
this.filter_list.addEventListener('click', function(e) { |
| 146 |
var btn = e.target.closest('a.remove-filter'); |
| 147 |
if (btn) { |
| 148 |
e.preventDefault(); |
| 149 |
btn.closest('.filter-row').remove(); |
| 150 |
} |
| 151 |
}); |
| 152 |
|
| 153 |
/* rows for active filters are rendered server-side */ |
| 154 |
this.filter_list.querySelectorAll('.filter-row').forEach(function(row) { |
| 155 |
self.bind_row(row); |
| 156 |
}); |
| 157 |
}; |
| 158 |
|
| 159 |
A.ModelAdminFilter.prototype.bind_row = function(row) { |
| 160 |
var select_elem = row.children[1], |
| 161 |
input_elem = row.children[2]; |
| 162 |
|
| 163 |
/* certain operations want a different input type than the field's |
| 164 |
default, e.g. "within X days ago" takes a number, not a date */ |
| 165 |
if (input_elem.tagName === 'INPUT') { |
| 166 |
var lookup = this.lookups_elem.querySelector('[name="' + input_elem.name + '"]'), |
| 167 |
default_type = lookup ? lookup.type : input_elem.type; |
| 168 |
var sync_input_type = function() { |
| 169 |
var opt = select_elem.selectedOptions[0]; |
| 170 |
input_elem.type = (opt && opt.dataset.inputType) || default_type; |
| 171 |
}; |
| 172 |
select_elem.addEventListener('change', sync_input_type); |
| 173 |
sync_input_type(); |
| 174 |
} |
| 175 |
|
| 176 |
if (input_elem.dataset.role === 'ajax-select') { |
| 177 |
A.ajaxSelect(input_elem); |
| 178 |
} |
| 179 |
}; |
| 180 |
|
| 181 |
A.ModelAdminFilter.prototype.add_row = function(qf_v, qf_s) { |
| 182 |
var select_clone = this.lookups_elem.querySelector('#' + CSS.escape(qf_s)).cloneNode(true), |
| 183 |
input_clone = this.lookups_elem.querySelector('#' + CSS.escape(qf_v)).cloneNode(true), |
| 184 |
field_label = document.getElementById('filter-' + qf_s).textContent.trim(); |
| 185 |
|
| 186 |
select_clone.removeAttribute('id'); |
| 187 |
input_clone.removeAttribute('id'); |
| 188 |
input_clone.classList.add( |
| 189 |
input_clone.tagName === 'SELECT' ? 'form-select' : 'form-control', |
| 190 |
input_clone.tagName === 'SELECT' ? 'form-select-sm' : 'form-control-sm', |
| 191 |
'w-auto'); |
| 192 |
|
| 193 |
var row = document.createElement('div'); |
| 194 |
row.className = 'filter-row d-flex align-items-center gap-2 mb-2'; |
| 195 |
|
| 196 |
var remove_btn = document.createElement('a'); |
| 197 |
remove_btn.className = 'btn btn-danger btn-sm remove-filter'; |
| 198 |
remove_btn.href = '#'; |
| 199 |
remove_btn.title = 'click to remove'; |
| 200 |
remove_btn.textContent = field_label; |
| 201 |
|
| 202 |
row.appendChild(remove_btn); |
| 203 |
row.appendChild(select_clone); |
| 204 |
row.appendChild(input_clone); |
| 205 |
|
| 206 |
this.bind_row(row); |
| 207 |
this.filter_list.prepend(row); |
| 208 |
this.wrapper.classList.remove('d-none'); |
| 209 |
|
| 210 |
return row; |
| 211 |
}; |
| 212 |
|
| 213 |
/* add a filter of a given type */ |
| 214 |
A.ModelAdminFilter.prototype.add_filter = function(elem) { |
| 215 |
return this.add_row(elem.dataset.field, elem.dataset.select); |
| 216 |
}; |
| 217 |
|
| 218 |
/* bulk action helper for the model list */ |
| 219 |
A.index_submit = function(action) { |
| 220 |
var form = document.getElementById('model-list'); |
| 221 |
form.querySelector('input[name=action]').value = action; |
| 222 |
form.submit(); |
| 223 |
}; |
| 224 |
|
| 225 |
document.addEventListener('DOMContentLoaded', function() { |
| 226 |
var clock = document.getElementById('taskbar-clock'); |
| 227 |
if (!clock) return; |
| 228 |
var tick = function() { |
| 229 |
var d = new Date(); |
| 230 |
clock.textContent = ('0' + d.getHours()).slice(-2) + ':' + ('0' + d.getMinutes()).slice(-2); |
| 231 |
}; |
| 232 |
tick(); |
| 233 |
setInterval(tick, 15000); |
| 234 |
}); |
| 235 |
|
| 236 |
})(Admin); |