var Qs_Form_Element_MultiText = {
message: new qs.Message({
'en': {
'add': 'add',
'up': 'up',
'down': 'down',
'delete': 'delete',
'confirmDelete': 'Do you really want to delete this {itemName}?'
},
'es': {
'add': 'añadir',
'up': 'ascendente',
'down': 'de abajo',
'delete': 'eliminar',
'confirmDelete': 'Esta seguro que quiere eliminar esta fila?'
},
'uk': {
'add': 'додати',
'up': 'вгору',
'down': 'вниз',
'delete': 'видалити',
'confirmDelete': 'Ви дійсно хочете видалити цей {itemName}?'
}
}),
options: {},
elementOptions: {},
init: function (id, options)
{
Qs_Form_Element_MultiText.setElementOptions(id, options);
this.options = options;
if (options.hasAddLink !== true) {
Qs_Form_Element_MultiText.hideInputs(id);
$('#' + id).find('tfoot').remove();
}
Qs_Form_Element_MultiText.updateOptions(id);
Qs_Form_Element_MultiText.addInCount(id, options.itemsCount);
},
getElementOptions: function (id)
{
if (typeof Qs_Form_Element_MultiText.elementOptions[id] == 'undefined') {
return null;
}
return Qs_Form_Element_MultiText.elementOptions[id];
},
getElementOption: function (id, name)
{
var options = Qs_Form_Element_MultiText.getElementOptions(id);
if (options) {
if (typeof options[name] != 'undefined') {
return options[name];
}
}
return null;
},
setElementOption: function (id, name, value)
{
var options = Qs_Form_Element_MultiText.getElementOptions(id);
if (options) {
options[name] = value;
} else {
Qs_Form_Element_MultiText.elementOptions[id] = {name: value};
}
},
setElementOptions: function (id, options)
{
Qs_Form_Element_MultiText.elementOptions[id] = options;
},
_renderInput: function(type, name, attribs, options)
{
var value = '';
var className = '';
attribs = attribs || {};
attribs.value = '';
attribs.name = name;
attribs.type = type;
if (attribs) {
id = attribs.id;
className = attribs['class'];
}
if (!className) {
className = type;
}
var html = '';
return html;
},
add: function (id)
{
var isNumering = Qs_Form_Element_MultiText.getElementOption(id, 'isNumering');
var isSortable = Qs_Form_Element_MultiText.getElementOption(id, 'isSortable');
var numeringFormat = Qs_Form_Element_MultiText.getElementOption(id, 'numeringFormat');
var itemName = Qs_Form_Element_MultiText.getElementOption(id, 'itemName');
var name = Qs_Form_Element_MultiText.getElementOption(id, 'name');
var table = document.getElementById(id);
var row = '
';
if (isNumering) {
row += '';
row += numeringFormat.replace(/%number%/, '' + ($('tbody > tr', table).size() + 1) + '');
row += ' | ';
}
row += ''
+ Qs_Form_Element_MultiText._renderInput('text', name, {'class': 'text', 'aria-labelledby' : id + 'Label'});
+ ' | ';
row += '';
if (Qs_Form_Element_MultiText.getElementOption(id, 'isSortable')) {
row += ''
+ Qs_Form_Element_MultiText.message.get('up') + ' | ';
row += ''
+ Qs_Form_Element_MultiText.message.get('down') + ' | ';
}
row += ''
+ Qs_Form_Element_MultiText.message.get('delete') + "";
row += ' | ';
$('#' + id + ' tbody').append(row);
row += '
';
Qs_Form_Element_MultiText.updateOptions(id);
return false;
},
hideInputs: function (id)
{
$('#' + id + '-label').hide();
$('#' + id + '-element').hide();
},
showInputs: function (id)
{
$('#' + id + '-label').show();
$('#' + id + '-element').show();
},
addInCount: function (id, count)
{
var items = $('#' + id).find('tbody').find('tr'), itemsCount = items.length;
count = parseInt(count);
if (count < 0) {
return false;
}
if (count == 0) {
Qs_Form_Element_MultiText.hideInputs(id);
items.remove();
return false;
}
Qs_Form_Element_MultiText.showInputs(id);
if (itemsCount > count) {
while (itemsCount > count) {
items.last().remove();
--itemsCount;
items = $('#' + id).find('tbody').find('tr');
}
} else {
while (count > itemsCount) {
Qs_Form_Element_MultiText.add(id);
count--;
}
}
return false;
},
up: function (a, id)
{
var row = qs.dom.getParentTag(a, 'TR');
var topRow = qs.dom.getPreviousTag(row, 'TR');
if (topRow) {
var tbody = qs.dom.getParentTag(topRow, 'TBODY');
tbody.insertBefore(row, topRow);
Qs_Form_Element_MultiText.blink(id, row);
Qs_Form_Element_MultiText.updateNumeration(id);
Qs_Form_Element_MultiText.updateOptions(id);
}
return false;
},
down: function (a, id)
{
var row = qs.dom.getParentTag(a, 'TR');
var bottomRow = qs.dom.getNextTag(row, 'TR');
if (bottomRow) {
var tbody = qs.dom.getParentTag(row, 'TBODY');
tbody.insertBefore(bottomRow, row);
Qs_Form_Element_MultiText.blink(id, row);
Qs_Form_Element_MultiText.updateNumeration(id);
Qs_Form_Element_MultiText.updateOptions(id);
}
return false;
},
blink: function (id, row)
{
var idTimer = Qs_Form_Element_MultiText.getElementOption(id, 'blinkTimer');
if (idTimer) {
clearTimeout(idTimer);
}
row.className = 'moved';
idTimer = setTimeout(function(){row.className = '';}, 1000);
Qs_Form_Element_MultiText.setElementOption(id, 'blinkTimer', idTimer);
},
del: function (a, id)
{
var itemName = Qs_Form_Element_MultiText.getElementOption(id, 'itemName');
var msg = _.str.tpl(Qs_Form_Element_MultiText.message.get('confirmDelete'), {itemName: itemName});
if (confirm(msg)) {
var row = qs.dom.getParentTag(a, 'TR');
if (row) {
$(row).remove();
}
Qs_Form_Element_MultiText.updateNumeration(id);
Qs_Form_Element_MultiText.updateOptions(id);
var itemsCount = $('#' + id).find('tbody').find('tr').length;
Qs_Form_Element_MultiText.addInCount(id, itemsCount);
$('#' + id + 'Count').val(itemsCount);
}
return false;
},
updateNumeration: function (id)
{
$('#' + id + ' tbody > tr > td.no > span.row-number').each(function(i, span){
$(span).text((i+1));
});
},
hideLink: function (link)
{
$(link).hide();
$(link).each(function(i, el){
if (el.nextSibling && el.nextSibling.tagName == 'SPAN') {
$(el.nextSibling).hide();
}
});
},
showLink: function (link)
{
$(link).show();
$(link).each(function(i, el){
if (el.nextSibling && el.nextSibling.tagName == 'SPAN') {
$(el.nextSibling).show();
}
});
},
updateOptions: function (id)
{
var selector = '#' + id + ' a';
var del = selector + '.del';
var up = selector + '.up';
var down = selector + '.down';
var rows = $(del).size();
Qs_Form_Element_MultiText.showLink(del);
Qs_Form_Element_MultiText.showLink(up);
Qs_Form_Element_MultiText.showLink(down);
if (rows == 1) {
if (this.options.hasAddLink === true) {
Qs_Form_Element_MultiText.hideLink(del);
}
Qs_Form_Element_MultiText.hideLink(up);
Qs_Form_Element_MultiText.hideLink(down);
} else if (rows > 1) {
Qs_Form_Element_MultiText.hideLink(up + ':first');
Qs_Form_Element_MultiText.hideLink(down + ':last');
}
}
}