deleteHandler.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * Copyright (c) 2014, Arthur Schiwon <blizzz@owncloud.com>
  3. * This file is licensed under the Affero General Public License version 3 or later.
  4. * See the COPYING-README file.
  5. */
  6. /**
  7. * takes care of deleting things represented by an ID
  8. *
  9. * @class
  10. * @param {string} endpoint the corresponding ajax PHP script. Currently limited
  11. * to settings - ajax path.
  12. * @param {string} paramID the by the script expected parameter name holding the
  13. * ID of the object to delete
  14. * @param {markCallback} markCallback function to be called after successfully
  15. * marking the object for deletion.
  16. * @param {removeCallback} removeCallback the function to be called after
  17. * successful delete.
  18. */
  19. /* globals escapeHTML */
  20. function DeleteHandler(endpoint, paramID, markCallback, removeCallback) {
  21. this.oidToDelete = false;
  22. this.canceled = false;
  23. this.ajaxEndpoint = endpoint;
  24. this.ajaxParamID = paramID;
  25. this.markCallback = markCallback;
  26. this.removeCallback = removeCallback;
  27. this.undoCallback = false;
  28. this.notifier = false;
  29. this.notificationDataID = false;
  30. this.notificationMessage = false;
  31. this.notificationPlaceholder = '%oid';
  32. }
  33. /**
  34. * Number of milliseconds after which the operation is performed.
  35. */
  36. DeleteHandler.TIMEOUT_MS = 7000;
  37. /**
  38. * Timer after which the action will be performed anyway.
  39. */
  40. DeleteHandler.prototype._timeout = null;
  41. /**
  42. * The function to be called after successfully marking the object for deletion
  43. * @callback markCallback
  44. * @param {string} oid the ID of the specific user or group
  45. */
  46. /**
  47. * The function to be called after successful delete. The id of the object will
  48. * be passed as argument. Unsuccessful operations will display an error using
  49. * OC.dialogs, no callback is fired.
  50. * @callback removeCallback
  51. * @param {string} oid the ID of the specific user or group
  52. */
  53. /**
  54. * This callback is fired after "undo" was clicked so the consumer can update
  55. * the web interface
  56. * @callback undoCallback
  57. * @param {string} oid the ID of the specific user or group
  58. */
  59. /**
  60. * enabled the notification system. Required for undo UI.
  61. *
  62. * @param {object} notifier Usually OC.Notification
  63. * @param {string} dataID an identifier for the notifier, e.g. 'deleteuser'
  64. * @param {string} message the message that should be shown upon delete. %oid
  65. * will be replaced with the affected id of the item to be deleted
  66. * @param {undoCallback} undoCallback called after "undo" was clicked
  67. */
  68. DeleteHandler.prototype.setNotification = function(notifier, dataID, message, undoCallback) {
  69. this.notifier = notifier;
  70. this.notificationDataID = dataID;
  71. this.notificationMessage = message;
  72. this.undoCallback = undoCallback;
  73. var dh = this;
  74. $('#notification')
  75. .off('click.deleteHandler_' + dataID)
  76. .on('click.deleteHandler_' + dataID, '.undo', function () {
  77. if ($('#notification').data(dh.notificationDataID)) {
  78. var oid = dh.oidToDelete;
  79. dh.cancel();
  80. if(typeof dh.undoCallback !== 'undefined') {
  81. dh.undoCallback(oid);
  82. }
  83. }
  84. dh.notifier.hide();
  85. });
  86. };
  87. /**
  88. * shows the Undo Notification (if configured)
  89. */
  90. DeleteHandler.prototype.showNotification = function() {
  91. if(this.notifier !== false) {
  92. if(!this.notifier.isHidden()) {
  93. this.hideNotification();
  94. }
  95. $('#notification').data(this.notificationDataID, true);
  96. var msg = this.notificationMessage.replace(
  97. this.notificationPlaceholder, escapeHTML(this.oidToDelete));
  98. this.notifier.showHtml(msg);
  99. }
  100. };
  101. /**
  102. * hides the Undo Notification
  103. */
  104. DeleteHandler.prototype.hideNotification = function() {
  105. if(this.notifier !== false) {
  106. $('#notification').removeData(this.notificationDataID);
  107. this.notifier.hide();
  108. }
  109. };
  110. /**
  111. * initializes the delete operation for a given object id
  112. *
  113. * @param {string} oid the object id
  114. */
  115. DeleteHandler.prototype.mark = function(oid) {
  116. if(this.oidToDelete !== false) {
  117. // passing true to avoid hiding the notification
  118. // twice and causing the second notification
  119. // to disappear immediately
  120. this.deleteEntry(true);
  121. }
  122. this.oidToDelete = oid;
  123. this.canceled = false;
  124. this.markCallback(oid);
  125. this.showNotification();
  126. if (this._timeout) {
  127. clearTimeout(this._timeout);
  128. this._timeout = null;
  129. }
  130. if (DeleteHandler.TIMEOUT_MS > 0) {
  131. this._timeout = window.setTimeout(
  132. _.bind(this.deleteEntry, this),
  133. DeleteHandler.TIMEOUT_MS
  134. );
  135. }
  136. };
  137. /**
  138. * cancels a delete operation
  139. */
  140. DeleteHandler.prototype.cancel = function() {
  141. if (this._timeout) {
  142. clearTimeout(this._timeout);
  143. this._timeout = null;
  144. }
  145. this.canceled = true;
  146. this.oidToDelete = false;
  147. };
  148. /**
  149. * executes a delete operation. Requires that the operation has been
  150. * initialized by mark(). On error, it will show a message via
  151. * OC.dialogs.alert. On success, a callback is fired so that the client can
  152. * update the web interface accordingly.
  153. *
  154. * @param {boolean} [keepNotification] true to keep the notification, false to hide
  155. * it, defaults to false
  156. */
  157. DeleteHandler.prototype.deleteEntry = function(keepNotification) {
  158. var deferred = $.Deferred();
  159. if(this.canceled || this.oidToDelete === false) {
  160. return deferred.resolve().promise();
  161. }
  162. var dh = this;
  163. if(!keepNotification && $('#notification').data(this.notificationDataID) === true) {
  164. dh.hideNotification();
  165. }
  166. if (this._timeout) {
  167. clearTimeout(this._timeout);
  168. this._timeout = null;
  169. }
  170. var payload = {};
  171. payload[dh.ajaxParamID] = dh.oidToDelete;
  172. return $.ajax({
  173. type: 'DELETE',
  174. url: OC.generateUrl(dh.ajaxEndpoint+'/{oid}',{oid: this.oidToDelete}),
  175. // FIXME: do not use synchronous ajax calls as they block the browser !
  176. async: false,
  177. success: function (result) {
  178. // Remove undo option, & remove user from table
  179. //TODO: following line
  180. dh.removeCallback(dh.oidToDelete);
  181. dh.canceled = true;
  182. },
  183. error: function (jqXHR) {
  184. OC.dialogs.alert(jqXHR.responseJSON.data.message, t('settings', 'Unable to delete {objName}', {objName: dh.oidToDelete}));
  185. dh.undoCallback(dh.oidToDelete);
  186. }
  187. });
  188. };