//iTop Designer combo box for icons
$(function()
{
// the widget definition, where "itop" is the namespace,
// "icon_select" the widget name
$.widget( "itop.icon_select",
{
// default options
options:
{
items: [],
current_idx: 0
},
// the constructor
_create: function()
{
var me = this;
var sLabel = '';
var sIcon = '';
if (this.options.items.length > 0)
{
sIcon = this.options.items[this.options.current_idx].icon;
sLabel = this.options.items[this.options.current_idx].label;
}
this.oImg = $('');
this.oLabel = $(''+sLabel+'');
this.oButton = $('');
this.oButton.prepend(this.oLabel).prepend(this.oImg);
this.element.after(this.oButton);
this.element.addClass( "itop-icon-select" ).button();
this.element.bind( "reverted.itop-icon-select", function(ev, data) {
var idx = me._find_item(data.previous_value);
if (idx != null)
{
me.oImg.attr('src', me.options.items[idx].icon);
me.oLabel.text(me.options.items[idx].label);
}
});
this._refresh();
},
// called when created, and later when changing options
_refresh: function()
{
if (this.options.items.length > 0)
{
this.element.val(this.options.items[this.options.current_idx].value);
this.oImg.attr('src', this.options.items[this.options.current_idx].icon);
this.oLabel.text(this.options.items[this.options.current_idx].label);
}
this._create_menu();
},
_create_menu: function()
{
var me = this;
var sMenu = '