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) {
sessionStorage.setItem('inventree-sidenav-state', 'open');
$(navId).animate({ var resize = true;
width: '250px',
'min-width': '200px',
display: 'block'
}, 50);
if ('resize' in options) {
} resize = options.resize;
function closeSideNav(navId) {
if (!navId) {
navId = '#sidenav-left';
} }
sessionStorage.setItem('inventree-sidenav-state', 'closed'); var label = options.label || 'nav';
$(navId).animate({ var stateLabel = `${label}-tree-state`;
var widthLabel = `${label}-tree-width`;
var treeId = options.treeId || '#sidenav-left';
var toggleId = options.toggleId;
// Initially hide the tree
$(treeId).animate({
width: '0px', width: '0px',
'min-width': '0px', }, 0, function() {
display: 'none',
}, 50);
//document.getElementById("sidenav").style.display = "none"; if (resize) {
//document.getElementById("sidenav").style.width = "0"; $(treeId).resizable({
//document.getElementById("inventree-content").style.marginLeft = "0px"; 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);
function toggleSideNav(nav) { sessionStorage.setItem(stateLabel, 'closed');
if ($(nav).width() <= 0) { } else {
openSideNav(nav); sessionStorage.setItem(stateLabel, 'open');
} sessionStorage.setItem(widthLabel, `${width}px`);
else { }
closeSideNav(nav); }
} });
} }
function initSideNav(navId) { var state = sessionStorage.getItem(stateLabel);
var width = sessionStorage.getItem(widthLabel) || '300px';
// Make it resizable if (state && state == 'open') {
if (!navId) { $(treeId).animate({
navId = '#sidenav-left'; width: width,
} }, 50);
$(navId).resizable({
minWidth: '100px',
maxWidth: '500px',
stop: function(event, ui) {
console.log(ui.element.width());
//console.log(ui.size.width);
} }
}); });
if (sessionStorage.getItem("inventree-sidenav-state") && sessionStorage.getItem('inventree-sidenav-state') == 'open') { // Register callback for 'toggle' button
openSideNav(navId); if (toggleId) {
}
else { $(toggleId).click(function() {
closeSideNav(navId);
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');
}
});
} }
} }
/** /**
* 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 %}