Refactor side navigation tree

This commit is contained in:
Oliver Walters 2021-02-28 11:48:20 +11:00
parent 5cdae04c62
commit 6f2ba71339
4 changed files with 81 additions and 69 deletions

View File

@ -725,6 +725,7 @@ input[type="submit"] {
top: 70px; top: 70px;
position: sticky; position: sticky;
font-size: 115%; font-size: 115%;
margin-left: 5px;
} }
.sidenav-right svg { .sidenav-right svg {

View File

@ -67,79 +67,91 @@ function loadTree(url, tree, options={}) {
}); });
} }
function openSideNav(navId) {
// document.getElementById("sidenav").style.display = "block";
// document.getElementById("sidenav").style.width = "250px";
if (!navId) { /**
navId = '#sidenav-left'; * Initialize navigation tree display
*/
function initNavTree(options) {
var resize = true;
if ('resize' in options) {
resize = options.resize;
} }
sessionStorage.setItem('inventree-sidenav-state', 'open'); var label = options.label || 'nav';
$(navId).animate({ var stateLabel = `${label}-tree-state`;
width: '250px', var widthLabel = `${label}-tree-width`;
'min-width': '200px',
display: 'block'
}, 50);
var treeId = options.treeId || '#sidenav-left';
var toggleId = options.toggleId;
} // Initially hide the tree
$(treeId).animate({
function closeSideNav(navId) {
if (!navId) {
navId = '#sidenav-left';
}
sessionStorage.setItem('inventree-sidenav-state', 'closed');
$(navId).animate({
width: '0px', width: '0px',
'min-width': '0px', }, 0, function() {
display: 'none',
if (resize) {
$(treeId).resizable({
minWidth: '0px',
maxWidth: '500px',
handles: 'e, se',
grid: [5, 5],
stop: function(event, ui) {
var width = Math.round(ui.element.width());
if (width < 75) {
$(treeId).animate({
width: '0px'
}, 50); }, 50);
//document.getElementById("sidenav").style.display = "none"; sessionStorage.setItem(stateLabel, 'closed');
//document.getElementById("sidenav").style.width = "0"; } else {
//document.getElementById("inventree-content").style.marginLeft = "0px"; sessionStorage.setItem(stateLabel, 'open');
sessionStorage.setItem(widthLabel, `${width}px`);
}
function toggleSideNav(nav) {
if ($(nav).width() <= 0) {
openSideNav(nav);
}
else {
closeSideNav(nav);
} }
} }
});
function initSideNav(navId) {
// Make it resizable
if (!navId) {
navId = '#sidenav-left';
} }
$(navId).resizable({ var state = sessionStorage.getItem(stateLabel);
minWidth: '100px', var width = sessionStorage.getItem(widthLabel) || '300px';
maxWidth: '500px',
stop: function(event, ui) { if (state && state == 'open') {
console.log(ui.element.width());
//console.log(ui.size.width); $(treeId).animate({
width: width,
}, 50);
} }
}); });
if (sessionStorage.getItem("inventree-sidenav-state") && sessionStorage.getItem('inventree-sidenav-state') == 'open') { // Register callback for 'toggle' button
openSideNav(navId); if (toggleId) {
$(toggleId).click(function() {
var state = sessionStorage.getItem(stateLabel) || 'closed';
var width = sessionStorage.getItem(widthLabel) || '300px';
if (state == 'open') {
$(treeId).animate({
width: '0px'
}, 50);
sessionStorage.setItem(stateLabel, 'closed');
} else {
$(treeId).animate({
width: width,
}, 50);
sessionStorage.setItem(stateLabel, 'open');
} }
else { });
closeSideNav(navId);
} }
} }
/** /**
* Handle left-hand icon menubar display * Handle left-hand icon menubar display
*/ */

View File

@ -29,8 +29,6 @@ InvenTree | {% trans "Part List" %}
{% block js_ready %} {% block js_ready %}
{{ block.super }} {{ block.super }}
closeSideNav();
loadTree("{% url 'api-part-tree' %}", loadTree("{% url 'api-part-tree' %}",
"#part-tree", "#part-tree",
{ {
@ -38,11 +36,9 @@ InvenTree | {% trans "Part List" %}
} }
); );
$("#toggle-part-tree").click(function() { initNavTree({
toggleSideNav("#sidenav-left"); label: 'part',
return false; treeId: '#sidenav-left',
toggleId: '#toggle-part-tree',
}); });
initSideNav();
{% endblock %} {% endblock %}

View File

@ -11,7 +11,9 @@ InvenTree | Stock
{% endblock %} {% endblock %}
{% block sidenav %} {% block sidenav %}
<div id='stock-tree'></div> <div id='stock-tree'>
{% trans "Loading..." %}
</div>
{% endblock %} {% endblock %}
{% block pre_content %} {% block pre_content %}
@ -24,6 +26,7 @@ InvenTree | Stock
{% block js_ready %} {% block js_ready %}
{{ block.super }} {{ block.super }}
loadTree("{% url 'api-stock-tree' %}", loadTree("{% url 'api-stock-tree' %}",
"#stock-tree", "#stock-tree",
{ {
@ -31,10 +34,10 @@ InvenTree | Stock
} }
); );
$("#toggle-stock-tree").click(function() { initNavTree({
toggleSideNav("#sidenav-left"); label: 'stock',
return false; treeId: '#sidenav-left',
}) toggleId: '#toggle-stock-tree',
});
initSideNav();
{% endblock %} {% endblock %}