').addClass('loading'));
$instanceRow.addClass('loading');
if (options.$item) $instanceRow.data('list-view-new-item', true);
// Disable any clicking/actions for row
$instanceRow.bind('click', function() { return false; });
notification._custom = args._custom;
if (additional && additional.success) additional.success(args);
addNotification(
notification,
// Success
function(args) {
if (!args) args = {};
var actionFilter = args.actionFilter ?
args.actionFilter : $instanceRow.data('list-view-action-filter');
if ($instanceRow.is(':visible')) {
if (args.data) {
$newRow = replaceItem(
$instanceRow,
$.extend($instanceRow.data('json-obj'), args.data),
actionFilter
);
}
else {
// Nothing new, so just put in existing data
$newRow = replaceItem(
$instanceRow,
$instanceRow.data('json-obj'),
actionFilter
);
}
if (additional && additional.complete)
additional.complete(args, $newRow);
}
if (messages.complete) {
cloudStack.dialog.notice({
message: messages.complete(args.data)
});
}
},
{},
// Error
function(args) {
if ($instanceRow.data('list-view-new-item')) {
// For create forms
$instanceRow.remove();
} else {
// For standard actions
replaceItem(
$instanceRow,
$.extend($instanceRow.data('json-obj'), args.data),
args.actionFilter ?
args.actionFilter :
$instanceRow.data('list-view-action-filter')
);
}
}
);
},
error: function(message) {
if ($.isPlainObject(args.action.createForm)
&& args.action.addRow != 'false') {
$instanceRow.remove();
}
if (message) cloudStack.dialog.notice({ message: message });
}
}
};
if (action.custom && action.noAdd) {
action.custom({
data: data,
ref: options.ref,
context: context,
$instanceRow: $instanceRow,
complete: actionArgs.response.success
});
} else {
action(actionArgs);
}
}
};
var context = $.extend({}, listViewArgs.context);
context[
listViewArgs.activeSection
] = [$instanceRow.data('jsonObj')];
if (!args.action.createForm &&
args.action.addRow != 'true' &&
!action.custom && !action.uiCustom)
cloudStack.dialog.confirm({
message: messages.confirm(messageArgs),
action: function() {
performAction({
id: $instanceRow.data('list-view-item-id')
}, {
context: context
});
}
});
else if (action.custom || action.uiCustom)
performAction();
else {
var addRow = args.action.addRow == "false" ? false : true;
var isHeader = args.action.isHeader;
var createFormContext = $.extend({}, context);
if (args.action.createForm) {
cloudStack.dialog.createForm({
form: args.action.createForm,
after: function(args) {
var $newItem;
if (!isHeader) {
if (addRow != false) {
$newItem = $listView.listView('prependItem', {
data: [
$.extend(args.data, {
state: 'Creating',
status: 'Creating',
allocationstate: 'Creating'
})
]
});
} else {
$newItem = $instanceRow;
}
performAction(args.data, {
ref: args.ref,
context: createFormContext,
$item: $newItem,
$form: args.$form
});
} else {
performAction(args.data, {
ref: args.ref,
context: createFormContext,
$form: args.$form
});
}
},
ref: listViewArgs.ref,
context: createFormContext
});
} else {
cloudStack.dialog.confirm({
message: messages.confirm(messageArgs),
action: function() {
var $newItem;
if (addRow != false && !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
});
}
});
}
}
},
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(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(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')) {
showEditField();
} else if ($editInput.val() != $label.html()) {
$edit.animate({ opacity: 0.5 });
var originalName = $label.html();
var newName = $editInput.val();
showLabel(newName, {
success: function() {
addNotification(
{
section: $instanceRow.closest('div.view').data('view-args').id,
desc: newName ? 'Set value of ' + $instanceRow.find('td.name span').html() + ' to ' + newName :
'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 value = $td.html();
$('').html(value).appendTo($td.html(''));
var $editArea = $('').addClass('edit');
var $editField = $('').addClass('edit').attr({
type: 'text',
value: value
});
var $actionButton = $('').addClass('action');
var $saveButton = $actionButton.clone().addClass('save').attr({
'title': 'Save'
});
var $cancelButton = $actionButton.clone().addClass('cancel').attr({
'title': '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(fields, $table, actions, options) {
if (!options) options = {};
var $thead = $('').appendTo($table);
var reorder = options.reorder;
$.each(fields, function(key) {
var field = this;
var $th = $('| ').appendTo($thead);
if ($th.index()) $th.addClass('reduced-hide');
$th.html(field.label);
});
if (actions && renderActionCol(actions)) {
$thead.append(
$(' | | ')
.html('Actions')
.addClass('actions reduced-hide')
);
}
if (reorder) {
$thead.append(
$('').html('Order').addClass('reorder-actions reduced-hide')
);
}
return $thead;
};
var createFilters = function($toolbar, filters) {
if (!filters) return false;
var $filters = $('').addClass('filters reduced-hide');
$filters.append('');
var $filterSelect = $('').appendTo($filters);
if (filters)
$.each(filters, function(key) {
var $option = $(' | |