161 lines
4.6 KiB
JavaScript
161 lines
4.6 KiB
JavaScript
jQuery(function($)
|
|
{
|
|
$.fn.stTableRecordManager = function(options)
|
|
{
|
|
function getRows(subject) {
|
|
return subject.find('tbody tr');
|
|
}
|
|
|
|
function initialize(subject) {
|
|
let body = subject.find('tbody');
|
|
|
|
subject.find('.template .actions .create').click(add);
|
|
|
|
subject.find('.template input, .template select, .template textarea').keypress(function(event) {
|
|
return event.keyCode != 13;
|
|
}).keyup(function(event) {
|
|
if (event.keyCode == 13)
|
|
{
|
|
$(this).change();
|
|
return add(event);
|
|
}
|
|
|
|
return true;
|
|
});
|
|
|
|
subject.find('.tbody .actions .remove').click(remove);
|
|
|
|
body.find('td.actions a').click(actionDispatcher);
|
|
|
|
let index = body.children().size();
|
|
|
|
function add(event)
|
|
{
|
|
let row = $('<tr></tr>');
|
|
|
|
let fields = {};
|
|
|
|
subject.find('.template th').each(function() {
|
|
current = $(this);
|
|
|
|
let td = $('<td></td>');
|
|
|
|
td.attr('class', current.attr('class'));
|
|
|
|
if (current.hasClass('actions'))
|
|
{
|
|
current.find('a').each(function(){
|
|
if (!$(this).hasClass('create'))
|
|
{
|
|
$(this).clone().click(actionDispatcher).appendTo(td);
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
current.find('input:not(.chosen-search-input), select, textarea').each(function() {
|
|
let current = $(this);
|
|
let parent = current.parent();
|
|
let container;
|
|
let field;
|
|
|
|
if (current.is(':checkbox') && parent.hasClass('pretty'))
|
|
{
|
|
container = parent.clone();
|
|
field = parent.children('input');
|
|
}
|
|
else
|
|
{
|
|
field = container = current.clone();
|
|
}
|
|
|
|
field.attr('name', options.namespace + '[' + index + '][' + current.attr('name') + ']');
|
|
|
|
field.attr('id', field.attr('name').replace(/\[/g, '_').replace(/\]/g, ''));
|
|
|
|
// clone issue workaround
|
|
if (current.is('select'))
|
|
{
|
|
field.val(current.val());
|
|
}
|
|
|
|
field.attr('prev-name', current.attr('name'));
|
|
|
|
container.appendTo(td);
|
|
|
|
fields[current.attr('name')] = field.get(0);
|
|
});
|
|
}
|
|
|
|
td.appendTo(row);
|
|
});
|
|
|
|
row.appendTo(body);
|
|
|
|
row.find('select').show().selectBox();
|
|
row.find('input[data-format]').adminInputFormat();
|
|
|
|
subject.find('.template th input, .template th select, .template th textarea').each(function() {
|
|
let field = $(this);
|
|
|
|
if (field.is('select')) {
|
|
field.children().prop('selected', function() {
|
|
return this.defaultSelected;
|
|
});
|
|
field.selectBox('update');
|
|
} else if (field.is(':checkbox')) {
|
|
field.prop("checked", this.defaultChecked);
|
|
} else if (field.is(':hidden')) {
|
|
field.val('');
|
|
} else {
|
|
field.val(this.defaultValue);
|
|
}
|
|
});
|
|
|
|
subject.trigger('postAdd', [row, fields, index, options.namespace]);
|
|
|
|
index++;
|
|
|
|
return false;
|
|
}
|
|
|
|
function remove(event, target)
|
|
{
|
|
if (window.confirm(options.confirmMsg ? options.confirmMsg : 'Are you sure?'))
|
|
{
|
|
let parent = target.parents('tr');
|
|
|
|
subject.trigger('preRemove', [parent]);
|
|
|
|
parent.remove();
|
|
|
|
subject.trigger('postRemove');
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function actionDispatcher(event)
|
|
{
|
|
let current = $(this);
|
|
|
|
if (current.hasClass('remove'))
|
|
{
|
|
return remove(event, current);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
if ('string' === typeof options) {
|
|
switch (options) {
|
|
case 'rows':
|
|
return getRows(this);
|
|
break;
|
|
}
|
|
} else {
|
|
initialize(this);
|
|
}
|
|
}
|
|
}); |