(function (ns) {
"use strict";
ns.EventTimeForm = function (options) {
qs.CustomForm.apply(this, arguments);
this.formPart = options.formPart;
this.get('addNewButton').on('click', _.bind(this.addNewOnClick, this));
this.notifyDatasetChanged();
this.initOptionButtons();
};
ns.EventTimeForm.prototype = Object.create(qs.CustomForm.prototype);
var fn = ns.EventTimeForm.prototype;
fn.notifyDatasetChanged = function () {
var self = this;
this.initOptionCells(this.getOptionCells());
this.getOptionItems().each(function() {
var item = $(this), index = 1;
self.initOptionCells(item.find('table [data-options-cell]'));
});
};
fn.initOptionButtons = function () {
this.node.find('[data-action-delete]').on('click', _.bind(this.onDeleteClick, this));
this.node.find('[data-action-up]').on('click', _.bind(this.onMoveUpClick, this));
this.node.find('[data-action-down]').on('click', _.bind(this.onMoveDownClick, this));
};
fn.addNewOnClick = function (event) {
var node = $(event.currentTarget);
var itemName = node.data('action-add');
var options = {
formPartAction: 'addNew',
formPartItemName: itemName
};
Qs_Form.updatePart(event.target.form, this.formPart, options);
};
fn.onDeleteClick = function (event) {
var element = $(event.currentTarget);
var row = element.parents('[data-collection-item]:first');
var table = row.parents('[data-collection-type]:first');
if (element.hasClass('disabled')) {
return false;
}
if (!confirm('Do you really want to delete this Date?')) {
return false;
}
var itemsCount = table.find('> tbody > [data-collection-item]').length;
if (1 == itemsCount) {
var addNewButton = table.find('> tbody > tr > td > [data-action-add]');
var elementContainer = $('
').attr('id', addNewButton.parents('td').attr('id'));
elementContainer.append(addNewButton);
table.get(0).parentNode.insertBefore(
elementContainer.get(0),
table.get(0)
);
table.remove();
} else {
row.remove();
}
this.notifyDatasetChanged();
return true;
};
fn.onMoveUpClick = function (event) {
var element = $(event.currentTarget), row = element.parents('[data-collection-item]:first');
if (element.is(':enabled')) {
row.insertBefore(row.prev('[data-collection-item]'));
this.notifyDatasetChanged();
}
};
fn.onMoveDownClick = function (event) {
var element = $(event.currentTarget), row = element.parents('[data-collection-item]:first');
if (element.is(':enabled')) {
row.insertAfter(row.next('[data-collection-item]'));
this.notifyDatasetChanged();
}
};
fn.getOptionItems = function () {
return this.node.find('[data-collection-type=eventTime] > tbody > [data-collection-item]');
};
fn.getOptionCells = function () {
return this.node.find('[data-collection-type=eventTime] > tbody > tr > [data-options-cell]');
};
fn.initOptionCells = function (cells) {
var row, length = cells.length;
if (length === 1) {
cells.find('[data-action-up]:enabled, [data-action-down]:enabled').prop('disabled', true);
} else if (length >= 2) {
row = cells.first();
row.find('[data-action-up]:enabled').prop('disabled', true);
row.find('[data-action-down]:disabled').prop('disabled', false);
row = cells.last();
row.find('[data-action-up]:disabled').prop('disabled', false);
row.find('[data-action-down]:enabled').prop('disabled', true);
if (length > 2) {
cells.slice(1, -1).find('[data-action-up]:disabled, [data-action-down]:disabled').prop('disabled', false);
}
}
}
})(qs.defineNS('app.event.admin.form'));