2014-02-26 21:04:55 +04:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2014, Arthur Schiwon <blizzz@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-05-13 18:43:14 +04:00
|
|
|
* takes care of deleting things represented by an ID
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @param {string} endpoint the corresponding ajax PHP script. Currently limited
|
2014-02-26 21:04:55 +04:00
|
|
|
* to settings - ajax path.
|
2014-05-13 18:43:14 +04:00
|
|
|
* @param {string} paramID the by the script expected parameter name holding the
|
2014-02-26 21:04:55 +04:00
|
|
|
* ID of the object to delete
|
2014-05-13 18:43:14 +04:00
|
|
|
* @param {markCallback} markCallback function to be called after successfully
|
2014-02-26 21:04:55 +04:00
|
|
|
* marking the object for deletion.
|
2014-05-13 18:43:14 +04:00
|
|
|
* @param {removeCallback} removeCallback the function to be called after
|
|
|
|
* successful delete.
|
2014-02-26 21:04:55 +04:00
|
|
|
*/
|
|
|
|
function DeleteHandler(endpoint, paramID, markCallback, removeCallback) {
|
|
|
|
this.oidToDelete = false;
|
|
|
|
this.canceled = false;
|
|
|
|
|
|
|
|
this.ajaxEndpoint = endpoint;
|
|
|
|
this.ajaxParamID = paramID;
|
|
|
|
|
|
|
|
this.markCallback = markCallback;
|
|
|
|
this.removeCallback = removeCallback;
|
|
|
|
this.undoCallback = false;
|
|
|
|
|
|
|
|
this.notifier = false;
|
|
|
|
this.notificationDataID = false;
|
|
|
|
this.notificationMessage = false;
|
|
|
|
this.notificationPlaceholder = '%oid';
|
|
|
|
}
|
|
|
|
|
2014-09-11 17:56:00 +04:00
|
|
|
/**
|
|
|
|
* Number of milliseconds after which the operation is performed.
|
|
|
|
*/
|
|
|
|
DeleteHandler.TIMEOUT_MS = 7000;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Timer after which the action will be performed anyway.
|
|
|
|
*/
|
|
|
|
DeleteHandler.prototype._timeout = null;
|
|
|
|
|
2014-02-26 21:04:55 +04:00
|
|
|
/**
|
2014-05-13 18:43:14 +04:00
|
|
|
* The function to be called after successfully marking the object for deletion
|
|
|
|
* @callback markCallback
|
|
|
|
* @param {string} oid the ID of the specific user or group
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The function to be called after successful delete. The id of the object will
|
|
|
|
* be passed as argument. Unsuccessful operations will display an error using
|
|
|
|
* OC.dialogs, no callback is fired.
|
|
|
|
* @callback removeCallback
|
|
|
|
* @param {string} oid the ID of the specific user or group
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This callback is fired after "undo" was clicked so the consumer can update
|
|
|
|
* the web interface
|
|
|
|
* @callback undoCallback
|
|
|
|
* @param {string} oid the ID of the specific user or group
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* enabled the notification system. Required for undo UI.
|
|
|
|
*
|
|
|
|
* @param {object} notifier Usually OC.Notification
|
|
|
|
* @param {string} dataID an identifier for the notifier, e.g. 'deleteuser'
|
|
|
|
* @param {string} message the message that should be shown upon delete. %oid
|
2014-02-26 21:04:55 +04:00
|
|
|
* will be replaced with the affected id of the item to be deleted
|
2014-05-13 18:43:14 +04:00
|
|
|
* @param {undoCallback} undoCallback called after "undo" was clicked
|
2014-02-26 21:04:55 +04:00
|
|
|
*/
|
2014-04-16 20:57:35 +04:00
|
|
|
DeleteHandler.prototype.setNotification = function(notifier, dataID, message, undoCallback) {
|
2014-02-26 21:04:55 +04:00
|
|
|
this.notifier = notifier;
|
|
|
|
this.notificationDataID = dataID;
|
|
|
|
this.notificationMessage = message;
|
2014-04-16 20:57:35 +04:00
|
|
|
this.undoCallback = undoCallback;
|
2014-02-26 21:04:55 +04:00
|
|
|
|
2014-04-16 20:57:35 +04:00
|
|
|
var dh = this;
|
2014-02-26 21:04:55 +04:00
|
|
|
|
2014-09-11 17:56:00 +04:00
|
|
|
$('#notification')
|
|
|
|
.off('click.deleteHandler_' + dataID)
|
|
|
|
.on('click.deleteHandler_' + dataID, '.undo', function () {
|
2014-02-26 21:04:55 +04:00
|
|
|
if ($('#notification').data(dh.notificationDataID)) {
|
2014-04-16 20:57:35 +04:00
|
|
|
var oid = dh.oidToDelete;
|
2014-05-09 20:58:50 +04:00
|
|
|
dh.cancel();
|
2014-02-26 21:04:55 +04:00
|
|
|
if(typeof dh.undoCallback !== 'undefined') {
|
|
|
|
dh.undoCallback(oid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dh.notifier.hide();
|
|
|
|
});
|
2014-04-16 20:57:35 +04:00
|
|
|
};
|
2014-02-26 21:04:55 +04:00
|
|
|
|
|
|
|
/**
|
2014-05-13 18:43:14 +04:00
|
|
|
* shows the Undo Notification (if configured)
|
2014-02-26 21:04:55 +04:00
|
|
|
*/
|
|
|
|
DeleteHandler.prototype.showNotification = function() {
|
|
|
|
if(this.notifier !== false) {
|
|
|
|
if(!this.notifier.isHidden()) {
|
|
|
|
this.hideNotification();
|
|
|
|
}
|
|
|
|
$('#notification').data(this.notificationDataID, true);
|
2014-06-16 15:46:31 +04:00
|
|
|
var msg = this.notificationMessage.replace(
|
|
|
|
this.notificationPlaceholder, escapeHTML(this.oidToDelete));
|
2014-02-26 21:04:55 +04:00
|
|
|
this.notifier.showHtml(msg);
|
|
|
|
}
|
2014-04-16 20:57:35 +04:00
|
|
|
};
|
2014-02-26 21:04:55 +04:00
|
|
|
|
|
|
|
/**
|
2014-05-13 18:43:14 +04:00
|
|
|
* hides the Undo Notification
|
2014-02-26 21:04:55 +04:00
|
|
|
*/
|
|
|
|
DeleteHandler.prototype.hideNotification = function() {
|
|
|
|
if(this.notifier !== false) {
|
|
|
|
$('#notification').removeData(this.notificationDataID);
|
|
|
|
this.notifier.hide();
|
|
|
|
}
|
2014-04-16 20:57:35 +04:00
|
|
|
};
|
2014-02-26 21:04:55 +04:00
|
|
|
|
|
|
|
/**
|
2014-05-13 18:43:14 +04:00
|
|
|
* initializes the delete operation for a given object id
|
|
|
|
*
|
|
|
|
* @param {string} oid the object id
|
2014-02-26 21:04:55 +04:00
|
|
|
*/
|
|
|
|
DeleteHandler.prototype.mark = function(oid) {
|
|
|
|
if(this.oidToDelete !== false) {
|
2014-09-11 17:56:00 +04:00
|
|
|
// passing true to avoid hiding the notification
|
|
|
|
// twice and causing the second notification
|
|
|
|
// to disappear immediately
|
|
|
|
this.deleteEntry(true);
|
2014-02-26 21:04:55 +04:00
|
|
|
}
|
|
|
|
this.oidToDelete = oid;
|
|
|
|
this.canceled = false;
|
|
|
|
this.markCallback(oid);
|
|
|
|
this.showNotification();
|
2014-09-11 17:56:00 +04:00
|
|
|
if (this._timeout) {
|
|
|
|
clearTimeout(this._timeout);
|
|
|
|
this._timeout = null;
|
|
|
|
}
|
|
|
|
if (DeleteHandler.TIMEOUT_MS > 0) {
|
|
|
|
this._timeout = window.setTimeout(
|
|
|
|
_.bind(this.deleteEntry, this),
|
|
|
|
DeleteHandler.TIMEOUT_MS
|
|
|
|
);
|
|
|
|
}
|
2014-04-16 20:57:35 +04:00
|
|
|
};
|
2014-02-26 21:04:55 +04:00
|
|
|
|
|
|
|
/**
|
2014-05-13 18:43:14 +04:00
|
|
|
* cancels a delete operation
|
2014-02-26 21:04:55 +04:00
|
|
|
*/
|
2014-04-16 20:57:35 +04:00
|
|
|
DeleteHandler.prototype.cancel = function() {
|
2014-09-11 17:56:00 +04:00
|
|
|
if (this._timeout) {
|
|
|
|
clearTimeout(this._timeout);
|
|
|
|
this._timeout = null;
|
|
|
|
}
|
|
|
|
|
2014-02-26 21:04:55 +04:00
|
|
|
this.canceled = true;
|
|
|
|
this.oidToDelete = false;
|
2014-04-16 20:57:35 +04:00
|
|
|
};
|
2014-02-26 21:04:55 +04:00
|
|
|
|
|
|
|
/**
|
2014-05-13 18:43:14 +04:00
|
|
|
* executes a delete operation. Requires that the operation has been
|
2014-04-16 20:57:35 +04:00
|
|
|
* initialized by mark(). On error, it will show a message via
|
2014-02-26 21:04:55 +04:00
|
|
|
* OC.dialogs.alert. On success, a callback is fired so that the client can
|
|
|
|
* update the web interface accordingly.
|
2014-09-11 17:56:00 +04:00
|
|
|
*
|
|
|
|
* @param {boolean} [keepNotification] true to keep the notification, false to hide
|
|
|
|
* it, defaults to false
|
2014-02-26 21:04:55 +04:00
|
|
|
*/
|
2014-09-11 17:56:00 +04:00
|
|
|
DeleteHandler.prototype.deleteEntry = function(keepNotification) {
|
2014-02-26 21:04:55 +04:00
|
|
|
if(this.canceled || this.oidToDelete === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-16 20:57:35 +04:00
|
|
|
var dh = this;
|
2014-09-11 17:56:00 +04:00
|
|
|
if(!keepNotification && $('#notification').data(this.notificationDataID) === true) {
|
2014-02-26 21:04:55 +04:00
|
|
|
dh.hideNotification();
|
|
|
|
}
|
|
|
|
|
2014-09-11 17:56:00 +04:00
|
|
|
if (this._timeout) {
|
|
|
|
clearTimeout(this._timeout);
|
|
|
|
this._timeout = null;
|
|
|
|
}
|
|
|
|
|
2014-04-16 20:57:35 +04:00
|
|
|
var payload = {};
|
|
|
|
payload[dh.ajaxParamID] = dh.oidToDelete;
|
2014-02-26 21:04:55 +04:00
|
|
|
$.ajax({
|
2014-12-04 16:15:55 +03:00
|
|
|
type: 'DELETE',
|
|
|
|
url: OC.generateUrl(dh.ajaxEndpoint+'/'+this.oidToDelete),
|
2014-09-11 17:56:00 +04:00
|
|
|
// FIXME: do not use synchronous ajax calls as they block the browser !
|
2014-02-26 21:04:55 +04:00
|
|
|
async: false,
|
|
|
|
success: function (result) {
|
|
|
|
if (result.status === 'success') {
|
|
|
|
// Remove undo option, & remove user from table
|
|
|
|
|
|
|
|
//TODO: following line
|
|
|
|
dh.removeCallback(dh.oidToDelete);
|
|
|
|
dh.canceled = true;
|
|
|
|
} else {
|
2014-06-05 22:30:20 +04:00
|
|
|
OC.dialogs.alert(result.data.message, t('settings', 'Unable to delete {objName}', {objName: escapeHTML(dh.oidToDelete)}));
|
2014-02-26 21:04:55 +04:00
|
|
|
dh.undoCallback(dh.oidToDelete);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-04-16 20:57:35 +04:00
|
|
|
};
|