deleteHandler.js 5.7 KB

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