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

256 lines
6.7 KiB
JavaScript
Raw Normal View History

2016-02-12 23:52:43 +08:00
/**
* The functions here are global because they need to be
* accesed from outside (in reports view).
*/
var STORAGE_TREE_KEY = "scinote-sidebar-tree-collapsed-ids";
var STORAGE_TOGGLE_KEY = "scinote-sidebar-toggled";
var SCREEN_SIZE_LARGE = 928;
/**
* Get all collapsed sidebar elements.
* @return An array of sidebar element IDs.
*/
function sessionGetCollapsedSidebarElements() {
var val = sessionStorage.getItem(STORAGE_TREE_KEY);
console.log(val);
2016-02-12 23:52:43 +08:00
if (val === null) {
val = "[]";
sessionStorage.setItem(STORAGE_TREE_KEY, val);
}
2016-08-31 14:24:59 +08:00
val = JSON.parse(val);
2016-08-31 21:13:17 +08:00
// console.log(val);
// val = val.filter(Number);
2016-08-31 14:24:59 +08:00
return val;
2016-02-12 23:52:43 +08:00
}
/**
* Collapse a specified element in the sidebar.
* @param id - The collapsed element's ID.
*/
2016-08-31 21:13:17 +08:00
function sessionCollapseSidebarElement(project, id) {
2016-02-12 23:52:43 +08:00
var ids = sessionGetCollapsedSidebarElements();
2016-08-31 21:13:17 +08:00
if (_.indexOf(ids[project], id) === -1) {
var stored_projects = _.pluck(ids, 'name');
if( _.contains(stored_projects, project)){
_.findWhere(ids, {name: project}).ids.push(id);
} else {
var collapsed = { name: project, ids: [] };
collapsed.ids.push(id);
ids.push(collapsed);
}
2016-02-12 23:52:43 +08:00
sessionStorage.setItem(STORAGE_TREE_KEY, JSON.stringify(ids));
}
}
/**
* Expand a specified element in the sidebar.
* @param id - The expanded element's ID.
*/
2016-08-31 21:13:17 +08:00
function sessionExpandSidebarElement(project, id) {
2016-02-12 23:52:43 +08:00
var ids = sessionGetCollapsedSidebarElements();
var index = _.indexOf(ids, id);
if (index !== -1) {
2016-08-31 21:13:17 +08:00
2016-02-12 23:52:43 +08:00
ids.splice(index, 1);
2016-08-31 21:13:17 +08:00
var items = {};
items[project] = ids;
sessionStorage.setItem(STORAGE_TREE_KEY, JSON.stringify(items));
2016-02-12 23:52:43 +08:00
}
}
/**
* Get the session stored toggled boolean or null value if
* sidebar toggle state was not changed by user. It allow for
* automatic toggling for small devices.
*
* @return True if sidebar is toggled; false otherwise.
*/
function sessionIsSidebarToggled() {
var val = sessionStorage.getItem(STORAGE_TOGGLE_KEY);
if (val === null) {
return null;
}
return val === "toggled";
}
/**
* Store the sidebar toggled boolean to session storage.
*/
function sessionToggleSidebar() {
if (sessionIsSidebarToggled()) {
sessionStorage.setItem(STORAGE_TOGGLE_KEY, "not_toggled");
} else {
sessionStorage.setItem(STORAGE_TOGGLE_KEY, "toggled");
}
}
/**
* Setup the sidebar collapsing & expanding functionality.
*/
function setupSidebarTree() {
function toggleLi(el, collapse, animate) {
var children = el
.find(" > ul > li");
if (collapse) {
if (animate) {
children.hide("fast");
} else {
children.hide();
}
el
.find(" > span i")
.attr("title", "Expand this branch")
.removeClass("expanded");
} else {
if (animate) {
children.show("fast");
} else {
children.show();
}
el
.find(" > span i")
.attr("title", "Collapse this branch")
.addClass("expanded");
}
}
// Add triangle icons and titles to every parent node
$(".tree li:has(ul)")
.addClass("parent_li")
.find(" > span i")
.attr("title", "Collapse this branch");
$(".tree li.parent_li ")
.find("> span i")
.addClass("glyphicon glyphicon-triangle-right expanded");
// Add IDs to all parent <lis>
2016-08-31 14:24:59 +08:00
var i = 1;
_.each($('[data-parent="candidate"]'), function(el) {
2016-02-12 23:52:43 +08:00
$(el).attr("data-toggle-id", i++);
});
// Collapse session-stored elements
2016-08-31 21:13:17 +08:00
var project = $('#sidebar-project-name').text();
2016-02-12 23:52:43 +08:00
var collapsedIds = sessionGetCollapsedSidebarElements();
_.each($('li.parent_li[data-parent="candidate"]'), function(el) {
2016-08-31 14:24:59 +08:00
var id = $(el).data("toggle-id");
2016-02-12 23:52:43 +08:00
var li = $(".tree li.parent_li[data-toggle-id='" + id + "']");
2016-08-31 14:24:59 +08:00
if( li.hasClass("active") ){
console.log("-------------- active -------");
console.log(id);
console.log("-------------------------------");
2016-02-12 23:52:43 +08:00
// Only collapse element if it's descendants don't contain the currently
// active element
2016-08-31 14:24:59 +08:00
toggleLi(li,
false,
false);
2016-08-31 21:13:17 +08:00
sessionCollapseSidebarElement(project, id);
2016-08-31 14:24:59 +08:00
} else if ($.inArray( id, collapsedIds) === 0 ) {
console.log("-------------- expanded -------");
2016-08-31 14:24:59 +08:00
console.log(id);
console.log("-------------------------------");
2016-02-12 23:52:43 +08:00
toggleLi(li,
false,
2016-02-12 23:52:43 +08:00
false);
2016-08-31 21:13:17 +08:00
sessionExpandSidebarElement(project, id);
2016-02-12 23:52:43 +08:00
} else {
console.log("-------- colapsed -----");
2016-08-31 14:24:59 +08:00
console.log(id);
console.log("-------------------");
// collapse all element
toggleLi(li,
true,
false);
2016-08-31 21:13:17 +08:00
sessionCollapseSidebarElement(project, id);
2016-02-12 23:52:43 +08:00
}
});
// Add onclick callback to every triangle icon
$(".tree li.parent_li ")
.find("> span i")
.on("click", function (e) {
var el = $(this)
.parent("span")
.parent("li.parent_li");
if (el.find(" > ul > li").is(":visible")) {
toggleLi(el, true, true);
2016-08-31 21:13:17 +08:00
sessionCollapseSidebarElement(project, el.data("toggle-id"));
2016-02-12 23:52:43 +08:00
} else {
toggleLi(el, false, true);
2016-08-31 21:13:17 +08:00
sessionExpandSidebarElement(project, el.data("toggle-id"));
2016-02-12 23:52:43 +08:00
}
e.stopPropagation();
return false;
});
}
/**
* Initialize the show/hide toggling of sidebar.
*/
function initializeSidebarToggle() {
var wrapper = $("#wrapper");
var toggled = sessionIsSidebarToggled();
if (toggled || toggled === null && $(window).width() < SCREEN_SIZE_LARGE) {
wrapper.addClass("no-animation");
wrapper.addClass("toggled");
// Cause reflow of the wrapper element
wrapper[0].offsetHeight;
wrapper.removeClass("no-animation");
$(".navbar-secondary").addClass("navbar-without-sidebar");
}
$("#toggle-sidebar-link").on("click", function() {
$("#wrapper").toggleClass("toggled");
sessionToggleSidebar();
$(".navbar-secondary").toggleClass("navbar-without-sidebar", sessionIsSidebarToggled());
return false;
});
}
// Resize the sidebar to accomodate to the page size
function resizeSidebarContents() {
var wrapper = $("#wrapper");
var tree = $("#sidebar-wrapper .tree");
var toggled = sessionIsSidebarToggled();
if (tree.length && tree.length == 1) {
tree.css(
"height",
($(window).height() - tree.position().top - 50) + "px"
);
}
// Automatic toggling of sidebar for smaller devices
if (toggled === null) {
if ($(window).width() < SCREEN_SIZE_LARGE) {
wrapper.addClass("toggled");
} else {
wrapper.removeClass("toggled");
}
}
}
(function () {
// Initialize click listeners
setupSidebarTree();
initializeSidebarToggle();
// Actually display wrapper, which is, up to now,
// hidden
$("#wrapper").show();
// Resize the sidebar automatically
resizeSidebarContents();
// Bind onto window resize function
$(window).resize(function() {
resizeSidebarContents();
});
}());