mirror of
https://github.com/itflow-org/itflow
synced 2026-11-22 04:17:24 +00:00
Link opens the standard picker under the button instead of a window - linked records ticked, tick to link and untick to unlink, saved when the picker closes with one message - for every record link, contact and asset credentials, a contact's assets, project and service tickets, a ticket's services and related tickets, with a document's or file's Link grouped by kind and quick looks reloading in place; the old link windows and the handlers only they used are removed
This commit is contained in:
@@ -102,6 +102,8 @@ document.addEventListener('click', function (e) {
|
||||
wrapper.className = 'modal fade';
|
||||
wrapper.id = modalId;
|
||||
wrapper.tabIndex = -1;
|
||||
// What it was opened with, so it can load again in place (js/link_picker.js)
|
||||
wrapper.dataset.modalUrl = modalUrl;
|
||||
wrapper.innerHTML =
|
||||
'<div class="modal-dialog modal-' + modalSize + '">' +
|
||||
'<div class="modal-content border-dark">' +
|
||||
|
||||
@@ -72,6 +72,9 @@
|
||||
* .clear(), .setValue(v). itflowSelectAll(root) enhances anything not yet
|
||||
* done; it also happens by itself for selects added to the page later, and
|
||||
* again whenever includes/modal_footer.php re-runs this file for a modal.
|
||||
* Set .anchor to an element and the panel opens under that instead of the
|
||||
* field - a button that is the only way in to a hidden select (the Link
|
||||
* picker, js/link_picker.js). Closing fires itflow:close on the <select>.
|
||||
*
|
||||
* Loaded first in includes/footer.php, ahead of the heavy libraries, so the
|
||||
* swap happens before the page has finished drawing - see the note there.
|
||||
@@ -676,6 +679,7 @@
|
||||
this.dirty = false;
|
||||
this.emit();
|
||||
}
|
||||
this.select.dispatchEvent(new CustomEvent('itflow:close'));
|
||||
};
|
||||
|
||||
Picker.prototype.emit = function () {
|
||||
@@ -689,14 +693,15 @@
|
||||
if (!this.isOpen) {
|
||||
return;
|
||||
}
|
||||
if (!this.control.isConnected) {
|
||||
var at = this.anchor || this.control;
|
||||
if (!at.isConnected) {
|
||||
this.close(false, false);
|
||||
return;
|
||||
}
|
||||
if (this.sheet) {
|
||||
return;
|
||||
}
|
||||
var rect = this.control.getBoundingClientRect();
|
||||
var rect = at.getBoundingClientRect();
|
||||
var viewWidth = document.documentElement.clientWidth;
|
||||
var viewHeight = window.innerHeight;
|
||||
var edge = 8;
|
||||
@@ -705,9 +710,12 @@
|
||||
this.close(false, false);
|
||||
return;
|
||||
}
|
||||
var width = Math.min(Math.max(rect.width, 240), viewWidth - edge * 2);
|
||||
// Under a button it is as wide as a list of names needs, and lines up with
|
||||
// the button's right edge where its left would run off the page
|
||||
var width = Math.min(Math.max(rect.width, this.anchor ? 320 : 240), viewWidth - edge * 2);
|
||||
var left = this.anchor && rect.left + width > viewWidth - edge ? rect.right - width : rect.left;
|
||||
panel.style.width = width + 'px';
|
||||
panel.style.left = Math.min(Math.max(edge, rect.left), viewWidth - width - edge) + 'px';
|
||||
panel.style.left = Math.min(Math.max(edge, left), viewWidth - width - edge) + 'px';
|
||||
|
||||
this.list.style.maxHeight = '';
|
||||
var height = panel.offsetHeight;
|
||||
@@ -1241,13 +1249,17 @@
|
||||
watch(HTMLOptionElement.prototype, 'selected', selectOf);
|
||||
|
||||
// Outside click closes - and keeps what was typed into a data-tags field
|
||||
// (Its anchor is left to its own click - the Link button closes what it opened)
|
||||
function outside(target) {
|
||||
return current && !current.wrap.contains(target) && !current.panel.contains(target) && !(current.anchor && current.anchor.contains(target));
|
||||
}
|
||||
document.addEventListener('mousedown', function (e) {
|
||||
if (current && !current.wrap.contains(e.target) && !current.panel.contains(e.target)) {
|
||||
if (outside(e.target)) {
|
||||
current.close(true, false);
|
||||
}
|
||||
}, true);
|
||||
document.addEventListener('touchstart', function (e) {
|
||||
if (current && !current.wrap.contains(e.target) && !current.panel.contains(e.target)) {
|
||||
if (outside(e.target)) {
|
||||
current.close(true, false);
|
||||
}
|
||||
}, { capture: true, passive: true });
|
||||
|
||||
201
js/link_picker.js
Normal file
201
js/link_picker.js
Normal file
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
* ITFlow - Link picker: a Link button opens the picker every field has
|
||||
* (js/itflow_select.js) under it, rather than a window that held only that.
|
||||
*
|
||||
* <button data-link-picker data-link-from="contact" data-link-id="4"
|
||||
* data-link-to="asset" data-csrf="..."> - linkPickerButton()
|
||||
*
|
||||
* data-link-to empty is everything a document or file can be linked to, in
|
||||
* groups. The choices come from agent/ajax.php (link_picker), the ones linked
|
||||
* already ticked: tick to link, untick to unlink, and the changes are saved as
|
||||
* the picker closes (link_picker_save) - then the page loads again with the
|
||||
* message, so every list and count on it is right. In a window (a quick look)
|
||||
* the window loads again instead, and the message is a toast.
|
||||
*
|
||||
* In a .dropdown-menu the picker opens under the menu's button, as the menu
|
||||
* closes - and any menu opening closes the picker. functions/link_picker.php
|
||||
* has the server side.
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
if (window.itflowLinkPicker) {
|
||||
return; // re-run by an ajax modal - the listeners are already in place
|
||||
}
|
||||
window.itflowLinkPicker = true;
|
||||
|
||||
var open = null; // {button, select} while a picker is up
|
||||
|
||||
function toast(message, type) {
|
||||
if (typeof window.itflowToast === 'function') {
|
||||
window.itflowToast(message, type);
|
||||
}
|
||||
}
|
||||
|
||||
// A menu item's picker hangs off the menu's button
|
||||
function anchorFor(button) {
|
||||
var menu = button.closest('.dropdown-menu');
|
||||
var toggle = menu && menu.parentElement ? menu.parentElement.querySelector('[data-bs-toggle="dropdown"]') : null;
|
||||
return toggle || button;
|
||||
}
|
||||
|
||||
function chosen(select) {
|
||||
return Array.prototype.filter.call(select.options, function (option) {
|
||||
return option.selected;
|
||||
}).map(function (option) {
|
||||
return option.value;
|
||||
});
|
||||
}
|
||||
|
||||
function json(response) {
|
||||
return response.json().catch(function () {
|
||||
return { error: 'That didn\'t work - try again, or reload the page.' };
|
||||
});
|
||||
}
|
||||
|
||||
// The window it was in, loaded again - its lists show the change
|
||||
function reloadModal(modal) {
|
||||
fetch(modal.dataset.modalUrl, { headers: { 'Accept': 'application/json' }, credentials: 'same-origin' })
|
||||
.then(json)
|
||||
.then(function (response) {
|
||||
var content = modal.querySelector('.modal-content');
|
||||
if (!content || !response.content) {
|
||||
return;
|
||||
}
|
||||
content.innerHTML = response.content;
|
||||
if (typeof window.runScriptsInOrder === 'function') {
|
||||
window.runScriptsInOrder(Array.prototype.slice.call(content.querySelectorAll('script')));
|
||||
}
|
||||
if (typeof window.itflowTabFold === 'function') {
|
||||
window.itflowTabFold();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function save(button, add, remove) {
|
||||
var modal = button.closest('.modal');
|
||||
var inModal = !!(modal && modal.dataset.modalUrl);
|
||||
var body = new URLSearchParams();
|
||||
body.append('link_picker_save', '1');
|
||||
body.append('csrf_token', button.dataset.csrf || '');
|
||||
body.append('from', button.dataset.linkFrom);
|
||||
body.append('id', button.dataset.linkId);
|
||||
body.append('to', button.dataset.linkTo || '');
|
||||
add.forEach(function (value) {
|
||||
body.append('add[]', value);
|
||||
});
|
||||
remove.forEach(function (value) {
|
||||
body.append('remove[]', value);
|
||||
});
|
||||
if (!inModal) {
|
||||
body.append('flash', '1');
|
||||
}
|
||||
fetch('ajax.php', { method: 'POST', body: body, credentials: 'same-origin' })
|
||||
.then(json)
|
||||
.then(function (answer) {
|
||||
if (answer.error) {
|
||||
toast(answer.error, 'error');
|
||||
return;
|
||||
}
|
||||
if (inModal) {
|
||||
toast(answer.message.replace(/<[^>]*>/g, ''), answer.type);
|
||||
reloadModal(modal);
|
||||
} else {
|
||||
window.location.reload();
|
||||
}
|
||||
})
|
||||
.catch(function () {
|
||||
toast('That didn\'t save - try again.', 'error');
|
||||
});
|
||||
}
|
||||
|
||||
function show(button, data) {
|
||||
// A select no one sees, in the same window as the button, so the panel opens there
|
||||
var holder = document.createElement('div');
|
||||
holder.hidden = true;
|
||||
(button.closest('.modal-content') || document.body).appendChild(holder);
|
||||
var select = document.createElement('select');
|
||||
select.multiple = true;
|
||||
select.className = 'form-select';
|
||||
select.dataset.placeholder = data.title;
|
||||
select.dataset.search = 'true';
|
||||
select.dataset.recent = 'false';
|
||||
select.innerHTML = data.options;
|
||||
holder.appendChild(select);
|
||||
|
||||
var before = chosen(select);
|
||||
var picker = window.itflowSelect(select);
|
||||
if (!picker) {
|
||||
holder.remove();
|
||||
return;
|
||||
}
|
||||
picker.anchor = anchorFor(button);
|
||||
open = { button: button, select: select };
|
||||
button.setAttribute('aria-expanded', 'true');
|
||||
|
||||
select.addEventListener('itflow:close', function () {
|
||||
open = null;
|
||||
button.setAttribute('aria-expanded', 'false');
|
||||
var after = chosen(select);
|
||||
var add = after.filter(function (value) {
|
||||
return before.indexOf(value) < 0;
|
||||
});
|
||||
var remove = before.filter(function (value) {
|
||||
return after.indexOf(value) < 0;
|
||||
});
|
||||
setTimeout(function () {
|
||||
holder.remove();
|
||||
});
|
||||
if (add.length || remove.length) {
|
||||
save(button, add, remove);
|
||||
}
|
||||
}, { once: true });
|
||||
picker.open();
|
||||
}
|
||||
|
||||
document.addEventListener('click', function (e) {
|
||||
var button = e.target.closest('[data-link-picker]');
|
||||
if (!button) {
|
||||
return;
|
||||
}
|
||||
e.preventDefault();
|
||||
// Its own button again closes it
|
||||
if (open && open.button === button) {
|
||||
open.select.itflowSelect.close(true, false);
|
||||
return;
|
||||
}
|
||||
if (button.dataset.loading) {
|
||||
return;
|
||||
}
|
||||
button.dataset.loading = '1';
|
||||
var query = new URLSearchParams({
|
||||
link_picker: '1',
|
||||
from: button.dataset.linkFrom || '',
|
||||
id: button.dataset.linkId || '',
|
||||
to: button.dataset.linkTo || ''
|
||||
});
|
||||
fetch('ajax.php?' + query.toString(), { credentials: 'same-origin' })
|
||||
.then(json)
|
||||
.then(function (data) {
|
||||
delete button.dataset.loading;
|
||||
if (data.error) {
|
||||
toast(data.error, 'error');
|
||||
} else if (!data.count) {
|
||||
toast('There is nothing of this client\'s to link here yet.', 'info');
|
||||
} else {
|
||||
show(button, data);
|
||||
}
|
||||
})
|
||||
.catch(function () {
|
||||
delete button.dataset.loading;
|
||||
toast('That didn\'t load - try again.', 'error');
|
||||
});
|
||||
});
|
||||
|
||||
// Opening a menu closes the picker, as a click anywhere else would
|
||||
document.addEventListener('show.bs.dropdown', function () {
|
||||
if (open) {
|
||||
open.select.itflowSelect.close(true, false);
|
||||
}
|
||||
});
|
||||
})();
|
||||
@@ -29,7 +29,6 @@
|
||||
if (window.itflowTabFold) {
|
||||
return;
|
||||
}
|
||||
window.itflowTabFold = true;
|
||||
|
||||
var FOLDED = 'itflow-tab-folded';
|
||||
var BARS = '.nav.itflow-asset-tabs, .itflow-asset-tabs > .nav, .nav.itflow-asset-pills, .nav.itflow-asset-form-tabs, .nav.itflow-tabs-foldable';
|
||||
@@ -208,6 +207,9 @@
|
||||
});
|
||||
}
|
||||
|
||||
// Also for content drawn again in place (a window loaded again - js/link_picker.js)
|
||||
window.itflowTabFold = init;
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', init);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user