').addClass('loading-overlay');
$loading.appendTo($listView);
performAction(args.data, {
ref: args.ref,
context: createFormContext,
$form: args.$form,
isHeader: isHeader,
complete: function(args) {
$loading.remove();
$listView.listView('refresh');
},
error: function(args) {
$loading.remove();
}
});
}
},
ref: listViewArgs.ref,
context: createFormContext
});
} else {
cloudStack.dialog.confirm({
message: messages.confirm(messageArgs),
action: function() {
var $newItem;
if (addRow && !action.isHeader) {
$newItem = $listView.listView('prependItem', {
data: [
$.extend(args.data, {
state: 'Creating',
status: 'Creating',
allocationstate: 'Creating'
})
]
});
} else if (action.isHeader) {
$newItem = $('
');
} else {
$newItem = $instanceRow;
}
performAction(args.data, {
ref: args.ref,
context: createFormContext,
$item: $newItem,
$form: args.$form
});
}
});
}
}
},
remove: function($instanceRow, args) {
uiActions.standard($instanceRow, args, {
complete: function(args, $newRow) {
$newRow.remove();
}
});
},
edit: function($instanceRow, args) {
var $td = $instanceRow.find('td.editable');
var $edit = $td.find('div.edit');
var $editInput = $edit.find('input');
var $label = $td.find('span');
var $listView = $instanceRow.closest('.list-view');
var listViewArgs = $listView.data('view-args');
// Hide label, show edit field
var showEditField = function() {
$edit.css({ opacity: 1 });
$label.fadeOut('fast', function() {
$edit.fadeIn();
$editInput.focus();
$instanceRow.closest('div.data-table').dataTable('refresh');
});
};
// Hide edit field, validate and save changes
var showLabel = function(val, options) {
if (!options) options = {};
var oldVal = $label.html();
$label.html(_s(val));
var data = {
id: $instanceRow.data('list-view-item-id'),
jsonObj: $instanceRow.data('jsonObj')
};
data[$td.data('list-view-item-field')] = $editInput.val();
var context = $.extend({}, listViewArgs.context);
context[
listViewArgs.activeSection
] = $instanceRow.data('jsonObj');
args.callback({
data: data,
context: context,
response: {
success: function(args) {
$edit.hide();
$label.fadeIn();
$instanceRow.closest('div.data-table').dataTable('refresh');
if (options.success) options.success(args);
},
error: function(message) {
if (message) {
cloudStack.dialog.notice({ message: message });
$edit.hide(),
$label.html(_s(oldVal)).fadeIn();
$instanceRow.closest('div.data-table').dataTable('refresh');
if (options.error) options.error(args);
}
}
}
});
};
if (args.cancel) {
showLabel();
return false;
}
if (!$editInput.is(':visible') || !(typeof(args.action) == 'undefined')) {
showEditField();
} else if ($editInput.val() != $label.html()) {
$edit.animate({ opacity: 0.5 });
var originalName = $label.html();
var newName = $editInput.val();
showLabel(newName, {
success: function() {
cloudStack.ui.notifications.add(
{
section: $instanceRow.closest('div.view').data('view-args').id,
desc: newName ?
_l('Set value of') +
' ' + $instanceRow.find('td.name span').html() +
' ' + _l('to') +
' ' + _s(newName) :
_l('Unset value for') +
' ' + $instanceRow.find('td.name span').html()
},
function(args) {},
[{ name: newName }]
);
}
});
} else {
showLabel();
}
return $instanceRow;
}
};
var rowActions = {
_std: function($tr, action) {
action();
$tr.closest('.data-table').dataTable('refresh');
setTimeout(function() {
$tr.closest('.data-table').dataTable('selectRow', $tr.index());
}, 0);
},
moveTop: function($tr) {
rowActions._std($tr, function() {
$tr.closest('tbody').prepend($tr);
$tr.closest('.list-view').animate({ scrollTop: 0 });
});
},
moveBottom: function($tr) {
rowActions._std($tr, function() {
$tr.closest('tbody').append($tr);
$tr.closest('.list-view').animate({ scrollTop: 0 });
});
},
moveUp: function($tr) {
rowActions._std($tr, function() {
$tr.prev().before($tr);
});
},
moveDown: function($tr) {
rowActions._std($tr, function() {
$tr.next().after($tr);
});
},
moveTo: function($tr, index, after) {
rowActions._std($tr, function() {
var $target = $tr.closest('tbody').find('tr').filter(function() {
return $(this).index() == index;
});
if ($target.index() > $tr.index()) $target.after($tr);
else $target.before($tr);
$tr.closest('.list-view').scrollTop($tr.position().top - $tr.height() * 2);
if (after)
setTimeout(function() {
after();
});
});
}
};
/**
* Edit field text
*
* @param $td {jQuery}
to put input field into
*/
var createEditField = function($td) {
$td.addClass('editable');
// Put | label into a span
var sanitizedValue = $td.html();
$('').html(sanitizedValue).appendTo($td.html(''));
var $editArea = $('').addClass('edit');
var $editField = $('').addClass('edit').attr({
type: 'text',
value: cloudStack.sanitizeReverse(sanitizedValue)
});
var $actionButton = $('').addClass('action');
var $saveButton = $actionButton.clone().addClass('save').attr({
'title': _l('Save')
});
var $cancelButton = $actionButton.clone().addClass('cancel').attr({
'title': _l('Cancel edit')
});
$([$editField, $saveButton, $cancelButton]).each(function() {
this.appendTo($editArea);
});
return $editArea.hide();
};
var renderActionCol = function(actions) {
return $.grep(
$.map(actions, function(value, key) {
return key;
}),
function(elem) { return elem != 'add'; }
).length;
};
var createHeader = function(preFilter, fields, $table, actions, options) {
if (!options) options = {};
var $thead = $('').prependTo($table).append($(''));
var reorder = options.reorder;
var hiddenFields = [];
if(preFilter != null)
hiddenFields = preFilter();
$.each(fields, function(key) {
if($.inArray(key, hiddenFields) != -1)
return true;
var field = this;
var $th = $('| ').addClass(key).appendTo($thead.find('tr'));
if ($th.index()) $th.addClass('reduced-hide');
$th.html(_l(field.label));
});
if (reorder) {
$thead.find('tr').append(
$(' | ').html(_l('label.order')).addClass('reorder-actions reduced-hide')
);
}
if (actions && renderActionCol(actions)) {
$thead.find('tr').append(
$(' | | ')
.html(_l('label.actions'))
.addClass('actions reduced-hide')
);
}
return $thead;
};
var createFilters = function($toolbar, filters) {
if (!filters) return false;
var $filters = $('').addClass('filters reduced-hide');
$filters.append($(' |