ami/src/ami.js/sidebars.js

108 lines
3 KiB
JavaScript

#include utility.js
#include watcher.js
var AmiSidebars = function(container) {
var sidebars = new Map,
watchers = new AmiWatcherCollection,
active = undefined;
watchers.define(['show', 'hide']);
var createSidebar = function(sidebar) {
if(!('getName' in sidebar) || !('isWide' in sidebar) || !('getBody' in sidebar))
throw 'object does not contain expected functions';
var name = sidebar.getName(),
wide = sidebar.isWide(),
body = sidebar.getBody();
body.classList.add('body');
var classes = ['sidebar', 'hidden'];
if(wide) classes.push('widebar');
var titleElem = $e({ attrs: { className: 'top' } }),
elem = $e({
attrs: {
id: name,
classList: classes,
},
child: [titleElem, body],
});
var hide = function() {
elem.classList.add('hidden');
active = undefined;
};
var show = function() {
if(active !== undefined) {
if(active.getName() === name)
return;
active.hide();
}
active = sidebar;
elem.classList.remove('hidden');
};
sidebar.setTitle = function(title) { titleElem.textContent = (title || '').toString(); };
sidebar.isWide = function() { return elem.classList.contains('widebar'); };
sidebar.getElement = function() { return elem; };
sidebar.hide = hide;
sidebar.show = show;
if('apply' in sidebar)
sidebar.apply(sidebar);
if('reloadStrings' in sidebar)
sidebar.reloadStrings();
return sidebar;
};
var hide = function() {
if(active === undefined)
return;
watchers.call('hide', active, [active]);
active.hide();
};
var show = function(name) {
var sidebar = sidebars.get(name);
if(sidebar === undefined)
return;
sidebar.show();
watchers.call('show', sidebar, [sidebar]);
};
var toggle = function(name) {
if(active === undefined || active.getName() !== name)
show(name);
else
hide();
};
return {
create: function(name, wide, children) {
if(sidebars.has(name))
throw 'an icon with this name already exists';
var sidebar = createSidebar(name, wide, children);
sidebars.set(sidebar.getName(), sidebar);
container.appendChild(sidebar.getElement());
return sidebar;
},
has: function(name) {
return sidebars.has(name);
},
get: function(name) {
return sidebars.get(name);
},
hide: hide,
show: show,
toggle: toggle,
watch: function(name, watcher) {
watchers.watch(name, watcher);
},
unwatch: function(name, watcher) {
watchers.unwatch(name, watcher);
},
};
};