');
tabsList.append(tab);
tabsContainer.append(panel);
});
insertionPoint.el[insertionPoint.insert](tabsContainer);
tabsContainer.tabs();
tabs.push(tabsContainer);
});
return tabs;
}
function handleAddAnotherInline() {
// TODO: Refactor
$('.mt').parents('.inline-group').not('.tabular').find('.add-row a').click(function () {
var grouper = new TranslationFieldGrouper({
$fields: $(this).parent().prev().prev().find('.mt')
});
var tabs = createTabs(grouper.groupedTranslations);
// Update the main switch as it is not aware of the newly created tabs
MainSwitch.update(tabs);
// Activate the language tab selected in the main switch
MainSwitch.activateTab(tabs);
});
}
var TabularInlineGroup = function (options) {
this.id = options.id;
this.$id = null;
this.$table = null;
this.translationColumns = [];
// TODO: Make use of this to flag required tabs
this.requiredColumns = [];
this.init = function () {
this.$id = $('#' + this.id);
this.$table = $(this.$id).find('table');
};
this.getAllGroupedTranslations = function () {
var grouper = new TranslationFieldGrouper({
$fields: this.$table.find('.mt').filter(
'input:visible, textarea:visible, select:visible')
});
//this.requiredColumns = this.getRequiredColumns();
this.initTable();
return grouper.groupedTranslations;
};
this.getGroupedTranslations = function ($fields) {
var grouper = new TranslationFieldGrouper({
$fields: $fields
});
return grouper.groupedTranslations;
};
this.initTable = function () {
var self = this;
// The table header requires special treatment. In case an inline
// is declared with extra=0, the translation fields are not visible.
var thGrouper = new TranslationFieldGrouper({
$fields: this.$table.find('.mt').filter('input, textarea, select')
});
this.translationColumns = this.getTranslationColumns(thGrouper.groupedTranslations);
// The markup of tabular inlines is kinda weird. There is an additional
// leading td.original per row, so we have one td more than ths.
this.$table.find('th').each(function (idx) {
// Hide table heads from which translation fields have been moved out.
if($.inArray(idx + 1, self.translationColumns) !== -1) {
// FIXME: Why does this break when we use remove instead of hide?
$(this).hide();
}
// Remove language and brackets from table header,
// they are displayed in the tab already.
if ($(this).html() && $.inArray(idx + 1, self.translationColumns) === -1) {
$(this).html($(this).html().replace(/ \[.+\]/, ''));
}
});
};
this.getTranslationColumns = function (groupedTranslations) {
var translationColumns = [];
// Get table column indexes which have translation fields, but omit the first
// one per group, because that's where we insert our tab container.
$.each(groupedTranslations, function (groupId, lang) {
var i = 0;
$.each(lang, function (lang, el) {
var column = $(el).closest('td').prevAll().length;
if (i > 0 && $.inArray(column, translationColumns) === -1) {
translationColumns.push(column);
}
i += 1;
});
});
return translationColumns;
};
this.getRequiredColumns = function () {
var requiredColumns = [];
// Get table column indexes which have required fields, but omit the first
// one per group, because that's where we insert our tab container.
this.$table.find('th.required').each(function () {
requiredColumns.push($(this).index() + 1);
});
return requiredColumns;
};
this.init();
};
function handleTabularAddAnotherInline(tabularInlineGroup) {
tabularInlineGroup.$table.find('.add-row a').click(function () {
var tabs = createTabularTabs(
tabularInlineGroup.getGroupedTranslations(
$(this).parent().parent().prev().prev().find('.mt')));
// Update the main switch as it is not aware of the newly created tabs
MainSwitch.update(tabs);
// Activate the language tab selected in the main switch
MainSwitch.activateTab(tabs);
});
}
function createTabularTabs(groupedTranslations) {
var tabs = [];
$.each(groupedTranslations, function (groupId, lang) {
var tabsContainer = $('
'),
tabsList = $('
'),
insertionPoint;
tabsContainer.append(tabsList);
$.each(lang, function (lang, el) {
var $container = $(el).closest('td'),
$panel,
$tab,
tabId = 'tab_' + $(el).attr('id');
if (!insertionPoint) {
insertionPoint = {
'insert': $container.prev().length ? 'after' :
$container.next().length ? 'prepend' : 'append',
'el': $container.prev().length ? $container.prev() : $container.parent()
};
}
$panel = $('').append($container);
// Turn the moved tds into divs
var attrs = {};
$.each($container[0].attributes, function(idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
});
$container.replaceWith(function () {
return $('', attrs).append($(this).contents());
});
// TODO: Setting the required state based on the default field is naive.
// The user might have tweaked his admin. We somehow have to keep track of the
// column indexes _before_ the tds have been moved around.
$tab = $('