// jQuery UI style "widget" for managing the "xlsx-exporter"
$(function()
{
// the widget definition, where "itop" is the namespace,
// "tabularfieldsselector" the widget name
$.widget( "itop.tabularfieldsselector",
{
// default options
options:
{
fields: [],
value_holder: '#tabular_fields',
sample_data: [],
total_count: 0,
preview_limit: 3,
labels: {
preview_header: "Drag and drop the columns to change their order. Preview of %1$s lines. Total number of lines to export: %2$s",
empty_preview: "Select the columns to be exported from the list above",
columns_order: "Columns order",
columns_selection: 'Available columns from %1$s',
check_all: 'Check all',
uncheck_all: 'Uncheck all',
no_field_selected: 'Select at least one column to be exported'
}
},
// the constructor
_create: function()
{
var me = this;
this._flatten_fields(this.options.fields);
this.sId = this.element.attr('id');
this.element
.addClass('itop-tabularfieldsselector');
this.element.parent().bind('form-part-activate', function() { me._update_from_holder(); me._update_preview(); });
this.element.parent().bind('validate', function() { me.validate(); });
this.aSelected = [];
for(var i in this.options.fields)
{
var sContent = '
';
this.element.append(sContent);
}
sContent = '';
this.element.append(sContent);
this._update_from_holder();
$('body').on('click change', '.tfs_checkbox', function() {
var sInstanceId = $(this).attr('data-instance-id');
if (sInstanceId != me.sId) return;
me._on_click($(this));
});
var maxWidth = 0;
$('#'+this.sId+' .tfs_checkbox, #'+this.sId+' .tfs_checkbox_multi').each(function() {
maxWidth = Math.max(maxWidth, $(this).parent().width());
});
$('#'+this.sId+' .tfs_checkbox, #'+this.sId+' .tfs_checkbox_multi').each(function() {
$(this).parent().parent().width(maxWidth).css({display: 'inline-block'});
});
$('#'+this.sId+' .tfs_checkbox_multi').click(function() {
me._on_multi_click($(this).val(), this.checked);
});
$('#'+this.sId+' .check_all').click(function() {
me._on_check_all($(this).closest('fieldset'), true);
});
$('#'+this.sId+' .uncheck_all').click(function() {
me._on_check_all($(this).closest('fieldset'), false);
});
this._update_preview();
this._make_tooltips();
},
_on_click: function(jItemClicked)
{
var bChecked = jItemClicked.prop('checked');
var sValue = jItemClicked.val();
this._mark_as_selected(sValue, bChecked);
this._update_holder();
this._update_preview();
var sDataParent = jItemClicked.attr('data-parent');
if (sDataParent != '')
{
this._update_tristate(sDataParent+'_multi');
}
},
_on_multi_click: function(sMultiFieldCode, bChecked)
{
var oField = this._get_main_field_by_code(sMultiFieldCode);
if (oField != null)
{
var sPrefix = '#tfs_'+this.sId+'_';
for(var k in oField.subattr)
{
this._mark_as_selected(oField.subattr[k].code, bChecked);
// In case the tooltip is visible, also update the checkboxes
sElementId = (sPrefix+oField.subattr[k].code).replace('.', '_');
$(sElementId).prop('checked', bChecked);
}
this._update_holder();
this._update_preview();
}
},
_on_check_all: function(jSelector, bChecked)
{
var me = this;
jSelector.find('.tfs_checkbox').each(function() {
$(this).prop('checked', bChecked);
me._mark_as_selected($(this).val(), bChecked);
});
jSelector.find('.tfs_checkbox_multi').each(function() {
var oField = me._get_main_field_by_code($(this).val());
if (oField != null)
{
$(this).prop('checked', bChecked);
$(this).prop('indeterminate', false);
var sPrefix = '#tfs_'+this.sId+'_';
for(var k in oField.subattr)
{
me._mark_as_selected(oField.subattr[k].code, bChecked);
// In case the tooltip is visible, also update the checkboxes
sElementId = (sPrefix+oField.subattr[k].code).replace('.', '_');
$(sElementId).prop('checked', bChecked);
}
}
});
this._update_holder();
this._update_preview();
},
_update_tristate: function(sParentId)
{
// Check if the parent is checked, unchecked or indeterminate
var sParentId = sParentId.replace('.', '_');
var sAttCode = $('#'+sParentId).val();
var oField = this._get_main_field_by_code(sAttCode);
if (oField != null)
{
var iNbChecked = 0;
var aDebug = [];
for(var j in oField.subattr)
{
if ($.inArray(oField.subattr[j].code, this.aSelected) != -1)
{
aDebug.push(oField.subattr[j].code);
iNbChecked++;
}
}
if (iNbChecked == oField.subattr.length)
{
$('#'+sParentId).prop('checked', true);
$('#'+sParentId).prop('indeterminate', false);
}
else if (iNbChecked == 0)
{
$('#'+sParentId).prop('checked', false);
$('#'+sParentId).prop('indeterminate', false);
}
else
{
$('#'+sParentId).prop('checked', false);
$('#'+sParentId).prop('indeterminate', true);
}
}
},
_mark_as_selected: function(sValue, bSelected)
{
if(bSelected)
{
if ($.inArray(sValue, this.aSelected) == -1)
{
this.aSelected.push(sValue);
}
}
else
{
aSelected = [];
for(var k in this.aSelected)
{
if (this.aSelected[k] != sValue)
{
aSelected.push(this.aSelected[k]);
}
}
this.aSelected = aSelected;
}
},
_update_holder: function()
{
$(this.options.value_holder).val(this.aSelected.join(','));
},
_update_from_holder: function()
{
var sFields = $(this.options.value_holder).val();
var bAdvanced = parseInt($(this.options.advanced_holder).val(), 10);
if (sFields != '')
{
this.aSelected = sFields.split(',');
var safeSelected = [];
var me = this;
var bModified = false;
for(var k in this.aSelected)
{
var oField = this._get_field_by_code(this.aSelected[k])
if (oField == null)
{
// Invalid field code supplied, don't copy it
bModified = true;
}
else
{
safeSelected.push(this.aSelected[k]);
}
}
if (bModified)
{
this.aSelected = safeSelected;
this._update_holder();
}
$('#'+this.sId+' .tfs_checkbox').each(function() {
if ($.inArray($(this).val(), me.aSelected) != -1)
{
$(this).prop('checked', true);
}
else
{
$(this).prop('checked', false);
}
});
}
var me = this;
$('#'+this.sId+' .tfs_checkbox_multi').each(function() {
me._update_tristate($(this).attr('id'));
});
},
_update_preview: function()
{
var sHtml = '';
if(this.aSelected.length > 0)
{
sHtml += '
';
for(var k in this.aSelected)
{
var sField = this.aSelected[k];
if ($.inArray(sField, this.aSelected) != -1)
{
var sRemoveBtn = ' ×';
sHtml += '