scinote-web/app/assets/javascripts/sidebar.js

103 lines
3 KiB
JavaScript
Raw Normal View History

2018-07-19 23:56:42 +08:00
/**
* The functions here are global because they need to be
* accesed from outside (in reports view).
*/
2020-02-28 21:03:08 +08:00
/* global PerfectSb */
2020-02-27 23:04:44 +08:00
var Sidebar = (function() {
const SIDEBAR_ID = '#slide-panel';
const STORAGE_TREE_KEY = 'scinote-sidebar-tree-state';
const STORAGE_SCROLL_TREE_KEY = 'scinote-sidebar-tree-scroll-state';
function reloadSidebar() {
let url = $('#wrapper').find('.sidebar-root').data('sidebar-url');
$.get(url, function(result) {
$('#sidebar-wrapper').html(result.html);
});
}
2020-02-28 21:03:08 +08:00
function toggleTree($treeChildren) {
$treeChildren.toggleClass('hidden');
$.each($treeChildren, (i, treeChild) => {
$(treeChild).closest('.branch').find('.tree-toggle').first()
.toggleClass('fa-caret-down')
.toggleClass('fa-caret-right');
});
2020-02-27 23:04:44 +08:00
}
function saveCurrentState() {
2020-02-28 21:03:08 +08:00
var config = [];
2020-02-27 23:04:44 +08:00
$.each($(SIDEBAR_ID).find('.tree-child:not(:hidden)'), (i, branch) => {
2020-02-28 21:03:08 +08:00
config.push(`[data-branch-id=${branch.dataset.branchId}]`);
});
2020-02-27 23:04:44 +08:00
sessionStorage.setItem(STORAGE_TREE_KEY, config.join(','));
}
2020-02-28 21:03:08 +08:00
function loadLastState() {
2020-04-01 04:12:38 +08:00
var currentProject = $(SIDEBAR_ID).attr('data-current-project');
var currentExperiment = $(SIDEBAR_ID).attr('data-current-experiment');
var currentTask = $(SIDEBAR_ID).attr('data-current-task');
if (currentProject) {
let projectBranch = $(`.tree-child[data-branch-id="pro${currentProject}"]`).attr('data-active', 'true');
projectBranch.prev().addClass('active');
if (!currentExperiment) projectBranch.closest('.branch').addClass('active');
}
if (currentExperiment) {
let experimentBranch = $(`.tree-child[data-branch-id="exp${currentExperiment}"]`).attr('data-active', 'true');
experimentBranch.prev().addClass('active');
if (!currentTask) {
experimentBranch.closest('.branch')
.addClass('show-canvas-handler')
.addClass('active');
}
}
if (currentTask) {
$(`.leaf[data-module-id="${currentTask}"]`).addClass('active');
}
2020-02-28 21:03:08 +08:00
toggleTree($(SIDEBAR_ID).find('.tree-child[data-active="true"]'));
toggleTree($(SIDEBAR_ID).find('.tree-child.hidden').filter(sessionStorage.getItem(STORAGE_TREE_KEY)));
PerfectSb().update_all();
$(SIDEBAR_ID).find('.tree').scrollTo(sessionStorage.getItem(STORAGE_SCROLL_TREE_KEY));
}
2020-02-27 23:04:44 +08:00
function initSideBar() {
$(SIDEBAR_ID).on('click', '.tree-toggle', function() {
2020-02-28 21:03:08 +08:00
var $treeChild = $(this).closest('.branch').find('.tree-child').first();
toggleTree($treeChild);
2020-02-27 23:04:44 +08:00
saveCurrentState();
PerfectSb().update_all();
2020-02-28 21:03:08 +08:00
});
2020-02-27 23:04:44 +08:00
$(SIDEBAR_ID).find('.tree').scroll(function() {
sessionStorage.setItem(STORAGE_SCROLL_TREE_KEY, $(this).scrollTop());
2020-02-28 21:03:08 +08:00
});
2020-02-27 23:04:44 +08:00
$('#wrapper').show();
2020-02-28 21:03:08 +08:00
loadLastState();
2020-02-27 23:04:44 +08:00
}
return {
2020-02-28 21:03:08 +08:00
init: () => {
2020-02-27 23:04:44 +08:00
if ($(SIDEBAR_ID).length) {
2020-02-28 21:03:08 +08:00
initSideBar();
2020-02-27 23:04:44 +08:00
}
2020-02-28 21:03:08 +08:00
},
loadLastState: () => {
loadLastState();
},
reload: () => {
reloadSidebar();
2020-02-27 23:04:44 +08:00
}
};
}());
$(document).on('turbolinks:load', function() {
Sidebar.init();
});