//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,
labels: {cancel: 'Cancel', pick_icon_file: 'Select an icon file to upload:', upload_dlg_title: 'Icon Upload...', upload: 'Upload...'},
post_upload_to: null
},
// the constructor
_create: function()
{
var me = this;
var sLabel = '';
var sIcon = '';
for(var i in this.options.items)
{
if(this.options.items[i].icon == '')
{
this.options.items[i].icon = GetAbsoluteUrlAppRoot()+'images/transparent_32_32.png';
}
}
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.oButton.click(function(event, ui) { me._on_button_clicked(event, ui); });
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);
}
});
if (this.options.post_upload_to != null)
{
this.oUploadBtn = $('');
this.oUploadBtn.click( function() { me._upload_dlg(); } );
this.oButton.after(this.oUploadBtn);
}
var id = this.element.attr('id');
$('#event_bus').bind('tabshow.itop-icon-select'+id, function(event) {
// Compute the offsetX the first time the 'element' becomes visible...
var bVisible = me.element.parent().is(':visible');
if ((me.options.offsetX == null) && (bVisible))
{
me._refresh();
}
});
this.oUploadDlg = null;
this._refresh();
},
// called when created, and later when changing options
_refresh: function()
{
if (!this.element.parent().is(':visible'))
{
this.options.offsetX = null; // Menu needs to be reconstructed when the button becomes visible
}
else
{
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 = '
'+this.options.labels['pick_icon_file']+'