68 lines
2 KiB
JavaScript
68 lines
2 KiB
JavaScript
#include utility.js
|
|
|
|
var AmiOptionIcons = function(parent) {
|
|
var container = $e({ attrs: { id: 'optionsContainer' } }),
|
|
icons = new Map;
|
|
parent.appendChild(container);
|
|
|
|
var createIcon = function(name, title, icon, onclick, enabled) {
|
|
var pub = {};
|
|
var elem = $e({
|
|
tag: 'a',
|
|
attrs: {
|
|
href: 'javascript:void(0);',
|
|
title: title,
|
|
className: 'optIcon sprite sprite-' + icon,
|
|
onclick: function(ev) { performClick(ev); },
|
|
},
|
|
});
|
|
|
|
var hasState = typeof enabled === 'boolean';
|
|
var setState = function(state) {
|
|
if(!hasState) return;
|
|
elem.classList[state ? 'add' : 'remove']('on');
|
|
elem.classList[state ? 'remove' : 'add']('off');
|
|
};
|
|
|
|
if(hasState)
|
|
setState(enabled);
|
|
|
|
var performClick = function(ev) {
|
|
onclick.call(pub, pub, ev);
|
|
};
|
|
|
|
pub.getName = function() { return name; };
|
|
pub.getTitle = function() { return title; };
|
|
pub.getElement = function() { return elem; };
|
|
pub.setState = setState;
|
|
pub.performClick = performClick;
|
|
|
|
return pub;
|
|
};
|
|
|
|
return {
|
|
create: function(name, title, icon, onclick, enabled) {
|
|
if(icons.has(name))
|
|
throw 'an icon with this name already exists';
|
|
var iconInfo = createIcon(name, title, icon, onclick, enabled);
|
|
icons.set(iconInfo.getName(), iconInfo);
|
|
container.appendChild(iconInfo.getElement());
|
|
return iconInfo;
|
|
},
|
|
get: function(name) {
|
|
return icons.get(name);
|
|
},
|
|
remove: function(name) {
|
|
if(!icons.has(name))
|
|
return;
|
|
var iconInfo = icons.get(name);
|
|
container.removeChild(iconInfo.getElement());
|
|
icons.delete(name);
|
|
},
|
|
clear: function() {
|
|
icons.clear();
|
|
$rc(container);
|
|
},
|
|
};
|
|
};
|