419 lines
14 KiB
JavaScript
Raw Normal View History

// Hack for bootstrap issue with IE10. https://github.com/twbs/bootstrap/issues/3672
if ($.browser.msie && $.browser.version > 9) {
$('.modal').removeClass('fade');
}
2012-05-23 21:54:03 -03:00
$.fn.dataTableExt.oApi.fnReloadAjax = function (oSettings, sNewSource, fnCallback, bStandingRedraw) {
if (typeof sNewSource != 'undefined' && sNewSource != null) {
oSettings.sAjaxSource = sNewSource;
}
this.oApi._fnProcessingDisplay(oSettings, true);
var that = this;
var iStart = oSettings._iDisplayStart;
var aData = [];
this.oApi._fnServerParams(oSettings, aData);
oSettings.fnServerData(oSettings.sAjaxSource, aData, function (json) {
2012-05-23 21:54:03 -03:00
/* Clear the old information from the table */
that.oApi._fnClearTable(oSettings);
/* Got the data - add it to the table */
var aData = (oSettings.sAjaxDataProp !== "") ?
that.oApi._fnGetObjectDataFn(oSettings.sAjaxDataProp)(json) : json;
for (var i = 0; i < aData.length; i++) {
that.oApi._fnAddData(oSettings, aData[i]);
}
oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
that.fnDraw();
if (typeof bStandingRedraw != 'undefined' && bStandingRedraw === true) {
oSettings._iDisplayStart = iStart;
that.fnDraw(false);
}
that.oApi._fnProcessingDisplay(oSettings, false);
/* Callback user function - for event handlers etc */
if (typeof fnCallback == 'function' && fnCallback != null) {
fnCallback(oSettings);
}
}, oSettings);
2012-05-29 22:56:14 -03:00
};
2012-05-23 21:54:03 -03:00
jQuery.fn.dataTableExt.selectAllMaps = function () {
2012-04-06 17:05:42 -03:00
var total = $('.select input:checkbox[id!="selectAll"]').size();
var selected = $('.select input:checked[id!="selectAll"]').size();
if (selected < total) {
$('.select input:!checked[id!="selectAll"]').each(function () {
2012-04-06 17:05:42 -03:00
$(this).prop("checked", true);
});
}
else {
$('.select input:!checked[id!="selectAll"]').each(function () {
2012-04-06 17:05:42 -03:00
$(this).prop("checked", false);
});
}
updateStatusToolbar();
2012-04-06 17:05:42 -03:00
};
jQuery.fn.dataTableExt.getSelectedMapsIds = function () {
2012-05-29 22:56:14 -03:00
var selectedRows = $('#mindmapListTable').dataTableExt.getSelectedRows();
var dataTable = $('#mindmapListTable').dataTable();
return selectedRows.map(function () {
2012-05-29 22:56:14 -03:00
return dataTable.fnGetData(this).id;
2012-04-06 17:05:42 -03:00
});
};
jQuery.fn.dataTableExt.getSelectedRows = function (oSettings) {
2012-04-07 20:05:50 -03:00
return $('.select input:checked[id!="selectAll"]').parent().parent();
};
jQuery.fn.dataTableExt.removeSelectedRows = function () {
2012-04-07 20:05:50 -03:00
var trs = this.getSelectedRows();
trs.each(function () {
2012-05-23 01:13:00 -03:00
$('#mindmapListTable').dataTable().fnDeleteRow(this);
2012-04-06 17:05:42 -03:00
});
updateStatusToolbar();
2012-04-06 17:05:42 -03:00
};
2012-04-07 20:05:50 -03:00
jQuery.fn.dialogForm = function (options) {
2012-04-07 20:05:50 -03:00
var containerId = this[0].id;
var url = options.url;
2012-05-19 21:36:34 -03:00
// Clear previous state ...
2012-05-23 20:05:16 -03:00
$("#" + containerId).find('.errorMessage').text("").removeClass("alert alert-error");
$("#" + containerId).find('.control-group').removeClass('error');
2012-05-19 21:36:34 -03:00
2012-05-23 20:05:16 -03:00
// Clear form values ...
if (options.clearForm == undefined || options.clearForm) {
$("#" + containerId).find('input').attr('value', '');
}
2012-04-07 20:05:50 -03:00
2012-05-23 21:54:03 -03:00
// Clear button "Saving..." state ...
2012-05-19 00:13:06 -03:00
var acceptBtn = $('#' + containerId + ' .btn-accept');
2012-05-23 20:05:16 -03:00
acceptBtn.button('reset');
acceptBtn.click(function () {
2012-04-07 20:05:50 -03:00
var formData = {};
$('#' + containerId + ' input').each(function (index, elem) {
2012-04-07 20:05:50 -03:00
formData[elem.name] = elem.value;
});
2014-02-01 22:48:01 -03:00
// Success actions ...
var onSuccess = function (jqXHR, textStatus, data) {
if (options.redirect) {
var resourceId = jqXHR.getResponseHeader("ResourceId");
var redirectUrl = options.redirect;
redirectUrl = redirectUrl.replace("{header.resourceId}", resourceId);
// Hack: IE ignore the base href tag ...
var baseUrl = window.location.href.substring(0, window.location.href.lastIndexOf("c/maps/"));
window.open(baseUrl + redirectUrl, '_self');
} else if (options.postUpdate) {
options.postUpdate(formData);
}
dialogElem.modal('hide');
2014-02-01 22:48:01 -03:00
};
// On error message
var onFailure = function (jqXHR, textStatus, data) {
var errors = JSON.parse(jqXHR.responseText);
// Mark fields with errors ...
var fieldErrors = errors.fieldErrors;
if (fieldErrors) {
for (var fieldName in fieldErrors) {
// Mark the field with errors ...
var message = fieldErrors[fieldName];
var inputField = $("#" + containerId + " input[name='" + fieldName + "']");
$("#" + containerId).find(".errorMessage").text(message).addClass("alert alert-error");
inputField.parent().addClass('error');
}
}
var acceptBtn = $('#' + containerId + ' .btn-accept');
acceptBtn.button('reset');
};
var onError = function (jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
console.log(jqXHR);
dialogElem.modal('hide');
$('#messagesPanel div div').text(errorThrown);
$('#messagesPanel').show()
var acceptBtn = $('#' + containerId + ' .btn-accept');
acceptBtn.button('reset');
2014-02-01 22:48:01 -03:00
};
2012-05-23 20:05:16 -03:00
$(acceptBtn).button('loading');
2012-05-23 01:13:00 -03:00
var dialogElem = this;
2012-04-07 20:05:50 -03:00
jQuery.ajax(url, {
async: false,
dataType: 'json',
data: JSON.stringify(formData),
type: options.type ? options.type : 'POST',
contentType: "application/json; charset=utf-8",
statusCode: {
2014-02-01 22:48:01 -03:00
200: onSuccess,
201: onSuccess,
204: onSuccess,
400: onFailure,
444: onError,
500: onError,
501: onError
2012-04-07 20:05:50 -03:00
}
});
}.bind(this));
2012-04-07 20:05:50 -03:00
$('#' + containerId + ' .btn-cancel').click(function () {
this.modal('hide');
}.bind(this));
2012-04-07 20:05:50 -03:00
// Open the modal dialog ...
2012-05-23 01:13:00 -03:00
this.modal();
2012-04-07 20:05:50 -03:00
};
// Update toolbar events ...
function updateStatusToolbar() {
2012-05-20 02:25:54 -03:00
2012-05-20 21:46:55 -03:00
// Mark column row selection values ...
2012-05-20 02:25:54 -03:00
$("#mindmapListTable tbody input:checked").parent().parent().addClass('row-selected');
$("#mindmapListTable tbody input:not(:checked)").parent().parent().removeClass('row-selected');
2012-09-10 23:51:53 -03:00
$('.buttonsToolbar').find('.act-single').hide().end().find('.act-multiple').hide();
2012-05-20 21:46:55 -03:00
var tableElem = $('#mindmapListTable');
var selectedRows = tableElem.dataTableExt.getSelectedRows();
2012-05-20 21:46:55 -03:00
if (selectedRows.length > 0) {
if (selectedRows.length == 1) {
2012-09-10 23:51:53 -03:00
$('.buttonsToolbar').find('.act-single').show().end().find('.act-multiple').show();
2012-05-20 21:46:55 -03:00
// Can be executed by the owner ?
var rowData = tableElem.dataTable().fnGetData(selectedRows[0]);
if ('owner' == rowData.role) {
$(".buttonsToolbar").find('#publishBtn').show().end().find('#shareBtn').show().end().find('#renameBtn').show();
} else {
2012-09-10 23:51:53 -03:00
$(".buttonsToolbar").find('#publishBtn').hide().end().find('#shareBtn').hide().end().find('#renameBtn').hide();
2012-05-20 21:46:55 -03:00
}
} else {
2012-09-10 23:51:53 -03:00
$(".buttonsToolbar .act-multiple").show();
}
}
}
// Update toolbar events ...
function updateStarred(spanElem) {
$(spanElem).removeClass('starredOff');
$(spanElem).addClass('starredOn');
// Retrieve row data ...
var tableElem = $('#mindmapListTable');
var trElem = $(spanElem).parent().parent();
var rowData = tableElem.dataTable().fnGetData(trElem[0]);
// Update status ...
var starred = !rowData.starred;
var mapId = rowData.id;
if (starred) {
$(spanElem).removeClass('starredOff');
$(spanElem).addClass('starredOn');
} else {
$(spanElem).removeClass('starredOn');
$(spanElem).addClass('starredOff');
}
jQuery.ajax("c/restful/maps/" + mapId + "/starred", {
async: false,
dataType: 'json',
data: "" + starred,
type: 'PUT',
contentType: "text/plain",
success: function () {
if (starred) {
$(spanElem).removeClass('starredOff');
$(spanElem).addClass('starredOn');
} else {
$(spanElem).removeClass('starredOn');
$(spanElem).addClass('starredOff');
}
},
error: function (jqXHR, textStatus, errorThrown) {
$('#messagesPanel div').text(errorThrown).parent().show();
}
});
// Finally update st
rowData.starred = starred;
}
2012-05-29 22:56:14 -03:00
function callbackOnTableInit() {
// Register starred events ...
$('#mindmapListTable .starredOff, #mindmapListTable .starredOn').click(function () {
2012-05-29 22:56:14 -03:00
updateStarred(this);
});
2012-06-06 01:42:24 -03:00
updateStatusToolbar();
2012-05-29 22:56:14 -03:00
}
$(function () {
2012-05-29 22:56:14 -03:00
// Creation buttons actions ...
$("#newBtn").click(
function () {
2012-05-29 22:56:14 -03:00
$("#new-dialog-modal").dialogForm({
redirect: "c/maps/{header.resourceId}/edit",
url: "c/restful/maps"
2012-05-29 22:56:14 -03:00
});
});
$("#duplicateBtn").click(function () {
2012-05-29 22:56:14 -03:00
// Map to be cloned ...
var tableElem = $('#mindmapListTable');
var rows = tableElem.dataTableExt.getSelectedRows();
if (rows.length > 0) {
// Obtain map name ...
var rowData = tableElem.dataTable().fnGetData(rows[0]);
$('#dupDialogTitle').text("Duplicate '" + rowData.title + "'");
// Obtains map id ...
var mapId = rowData.id;
// Initialize dialog ...
$("#duplicate-dialog-modal").dialogForm({
redirect: "c/maps/{header.resourceId}/edit",
url: "c/restful/maps/" + mapId
2012-05-29 22:56:14 -03:00
});
}
});
$("#renameBtn").click(function () {
2012-05-29 22:56:14 -03:00
// Map to be cloned ...
var tableElem = $('#mindmapListTable');
var rows = tableElem.dataTableExt.getSelectedRows();
if (rows.length > 0) {
// Obtain map name ...
var dataTable = tableElem.dataTable();
var rowData = dataTable.fnGetData(rows[0]);
// Fill dialog with default values ...
var mapId = rowData.id;
$("#rename-dialog-modal input[name='title']").attr('value', rowData.title);
$("#rename-dialog-modal input[name='description']").attr('value', rowData.description);
// Set title ...
$('#renameDialogTitle').text("Rename '" + rowData.title + "'");
// Initialize dialog ...
$("#rename-dialog-modal").dialogForm({
type: 'PUT',
clearForm: false,
postUpdate: function (reqBodyData) {
2012-05-29 22:56:14 -03:00
tableElem.dataTableExt.removeSelectedRows();
rowData.title = reqBodyData.title;
rowData.description = reqBodyData.description;
dataTable.fnAddData(JSON.parse(JSON.stringify(rowData)));
},
url: "c/restful/maps/" + mapId
2012-05-29 22:56:14 -03:00
});
}
});
$("#deleteBtn").click(function () {
2012-05-29 22:56:14 -03:00
var tableUI = $('#mindmapListTable');
var mapIds = tableUI.dataTableExt.getSelectedMapsIds();
2012-05-29 22:56:14 -03:00
if (mapIds.length > 0) {
// Initialize dialog ...
$("#delete-dialog-modal").dialogForm({
type: 'DELETE',
postUpdate: function () {
2012-05-29 22:56:14 -03:00
// Remove old entry ...
tableUI.dataTableExt.removeSelectedRows();
},
url: "c/restful/maps/batch?ids=" + jQuery.makeArray(mapIds).join(',')
2012-05-29 22:56:14 -03:00
});
}
});
$("#printBtn").click(function () {
2012-05-29 22:56:14 -03:00
var mapIds = $('#mindmapListTable').dataTableExt.getSelectedMapsIds();
if (mapIds.length > 0) {
// Hack: IE ignore the base href tag ...
var baseUrl = window.location.href.substring(0, window.location.href.lastIndexOf("c/maps/"));
window.open(baseUrl + 'c/maps/' + mapIds[0] + '/print');
2012-05-29 22:56:14 -03:00
}
});
$("#infoBtn").click(function () {
2012-07-01 22:34:01 -03:00
showEmbeddedDialog("c/maps/{mapId}/details", 'info-dialog-modal');
2012-05-29 22:56:14 -03:00
});
$("#historyBtn").click(function () {
2012-07-01 22:34:01 -03:00
showEmbeddedDialog("c/maps/{mapId}/history", 'history-dialog-modal');
2012-06-17 02:51:01 -03:00
});
$("#publishBtn").click(function () {
2012-07-01 22:34:01 -03:00
showEmbeddedDialog("c/maps/{mapId}/publish", "publish-dialog-modal");
2012-06-06 00:48:46 -03:00
});
$("#exportBtn").click(function () {
2012-07-01 22:34:01 -03:00
showEmbeddedDialog("c/maps/{mapId}/export", 'export-dialog-modal');
2012-06-06 00:48:46 -03:00
});
$("#importBtn").click(function () {
2012-07-01 22:34:01 -03:00
showEmbeddedDialog("c/maps/import", 'import-dialog-modal', true);
2012-06-06 00:48:46 -03:00
});
$("#shareBtn").click(function () {
2012-07-01 22:34:01 -03:00
showEmbeddedDialog("c/maps/{mapId}/share", 'share-dialog-modal', true);
});
var showEmbeddedDialog = function (urlTemplate, dialogElemId, ignore) {
2012-05-29 22:56:14 -03:00
var mapIds = $('#mindmapListTable').dataTableExt.getSelectedMapsIds();
2012-06-06 00:48:46 -03:00
if (mapIds.length > 0 || ignore) {
var mapId = mapIds[0];
$('#' + dialogElemId + ' .modal-body').load(urlTemplate.replace("{mapId}", mapId),
function () {
$('#' + dialogElemId + ' .btn-accept').unbind('click').click(function () {
2012-05-29 22:56:14 -03:00
submitDialogForm();
});
2012-06-06 00:48:46 -03:00
$('#' + dialogElemId).modal();
2012-05-29 22:56:14 -03:00
});
}
2012-06-06 00:48:46 -03:00
};
2012-05-29 22:56:14 -03:00
$('#foldersContainer li').click(function (event) {
2012-05-29 22:56:14 -03:00
// Deselect previous option ...
$('#foldersContainer li').removeClass('active');
$('#foldersContainer i').removeClass('icon-white');
// Select the new item ...
var dataTable = $('#mindmapListTable').dataTable();
$(this).addClass('active');
$('#foldersContainer .active i').addClass('icon-white');
// Reload the table data ...
dataTable.fnReloadAjax("c/restful/maps/?q=" + $(this).attr('data-filter'), callbackOnTableInit, true);
2012-05-29 22:56:14 -03:00
event.preventDefault();
});
});
// Register time update functions ....
setTimeout(function () {
2012-05-29 22:56:14 -03:00
jQuery("abbr.timeago").timeago()
}, 50000);