/*
* @copyright Copyright (C) 2010-2021 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
// 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 = `
';
for (var i = 0; i < Math.min(this.options.preview_limit, this.options.total_count); i++) {
sHtml += '
';
for (var k in this.aSelected) {
var sField = this.aSelected[k];
sHtml += '
'+this.options.sample_data[i][sField]+'
';
}
sHtml += '
';
}
sHtml += '
';
$('#'+this.sId+' .preview_header').show();
$('#'+this.sId+' .ibo-table-preview').html(sHtml);
var me = this;
$('#'+this.sId+' .ibo-table-preview table').dragtable({
persistState: function (table) {
me._on_drag_columns(table);
}, dragHandle: '.drag-handle'
});
$('#'+this.sId+' .ibo-table-preview table .export-field-close').on('click', function (event) {
me._on_remove_column($(this).attr('data-attcode'));
event.preventDefault();
return false;
});
} else {
$('#'+this.sId+' .preview_header').hide();
$('#'+this.sId+' .ibo-table-preview').html('
'+this.options.labels.empty_preview+'
');
}
$('.form_part:visible').trigger('preview_updated');
},
_get_field_by_code: function (sFieldCode) {
for (var k in this.aFieldsByCode) {
if (k == sFieldCode) {
return this.aFieldsByCode[k];
}
}
return null;
},
_get_main_field_by_code: function (sFieldCode) {
for (var i in this.options.fields) {
for (var j in this.options.fields[i]) {
if (this.options.fields[i][j].code == sFieldCode) {
return this.options.fields[i][j];
}
}
}
return null;
},
_on_drag_columns: function (table) {
var me = this;
me.aSelected = [];
table.el.find('th').each(function (i) {
me.aSelected.push($(this).attr('data-attcode'));
});
this._update_holder();
},
_on_remove_column: function (sField) {
var sElementId = this.sId+'_'+sField;
sElementId = '#tfs_'+sElementId.replace('.', '_');
$(sElementId).prop('checked', false);
this._mark_as_selected(sField, false);
this._update_holder();
this._update_preview();
var me = this;
$('#'+this.sId+' .tfs_checkbox_multi').each(function () {
me._update_tristate($(this).attr('id'));
me._update_tooltips($(this).val());
});
},
_format: function () {
var s = arguments[0];
for (var i = 0; i < arguments.length-1; i++) {
var reg = new RegExp("%"+(i+1)+"\\$s", "gm");
s = s.replace(reg, arguments[i+1]);
}
return s;
},
validate: function () {
if (this.aSelected.length == 0) {
var aMessages = $('#export-form').data('validation_messages');
aMessages.push(this.options.labels.no_field_selected);
$('#export-form').data('validation_messages', aMessages);
}
},
// events bound via _bind are removed automatically
// revert other modifications here
destroy: function () {
this.element
.removeClass('itop-tabularfieldsselector');
this.element.parent().off('activate');
this.element.parent().off('validate');
},
// _setOptions is called with a hash of all options that are changing
_setOptions: function () {
this._superApply(arguments);
},
// _setOption is called for each individual option that is changing
_setOption: function (key, value) {
if (key == 'fields') {
this._flatten_fields(value);
}
this._superApply(arguments);
},
_flatten_fields: function (aFields) {
// Update the "flattened" via of the fields
this.aFieldsByCode = {}; // Must be an object since indexes are non-numeric
for (var k in aFields) {
for (var i in aFields[k]) {
this.aFieldsByCode[aFields[k][i].code] = aFields[k][i];
for (var j in aFields[k][i].subattr) {
this.aFieldsByCode[aFields[k][i].subattr[j].code] = aFields[k][i].subattr[j];
}
}
}
},
_make_tooltips: function () {
var me = this;
$('#'+this.sId+' .tfs_advanced').each(function (index, elt) {
var sDataAttcode = $(elt).attr('data-attcode');
var sTooltipContent = me._get_tooltip_content(sDataAttcode);
tippy(elt, {
appendTo: document.body,
'content': sTooltipContent,
allowHTML: true,
interactive: true,
zIndex: 9999,
role: 'tooltip',
popperOptions: {
strategy: 'fixed'
}
});
});
},
_update_tooltips: function (sDataAttCode) {
let sTooltipContent = this._get_tooltip_content(sDataAttCode);
$('#'+this.sId+' label[data-attcode="'+sDataAttCode+'"]').each(function (index, elt) {
let tippyInstance = elt._tippy;
tippyInstance.setContent(sTooltipContent);
});
},
_get_tooltip_content: function (sDataAttCode) {
var oField = this._get_main_field_by_code(sDataAttCode);
var sContent = '';
if (oField != null) {
sContent += '
'+oField.label+'
';
for (var k in oField.subattr) {
bChecked = ($.inArray(oField.subattr[k].code, this.aSelected) != -1);
sContent += this._get_field_checkbox(oField.subattr[k].code, oField.subattr[k].label, false, bChecked, sDataAttCode);
}
}
return sContent;
},
_get_field_checkbox: function (sCode, sLabel, bHasTooltip, bChecked, sParentId) {
var sPrefix = 'tfs_'+this.sId+'_';
sParentId = (sPrefix+sParentId).replace('.', '_');
sElementId = (sPrefix+sCode).replace('.', '_');
var aClasses = [];
if (bHasTooltip) {
aClasses.push('tfs_advanced');
sLabel += ' [+]';
}
var sChecked = '';
if (bChecked) {
sChecked = ' checked ';
}
var sDataParent = '';
if (sParentId != null) {
sDataParent = ' data-parent="'+sParentId+'" ';
}
if (bHasTooltip) {
sContent = '