DrillThroughManageUndoRedo.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Watson Analytics (C) Copyright IBM Corp. 2018
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. *
  7. */
  8. define(['../../lib/@waca/core-client/js/core-client/ui/core/Class', '../../lib/@waca/core-client/js/core-client/utils/UniqueId'], function (Class, UniqueId) {
  9. /**
  10. * Handles undo/redo and rollback drill through model if users cancel editing in the manage dialog
  11. */
  12. var DrillThroughUndoRedoTransaction = Class.extend({
  13. init: function init(options) {
  14. options = options || {};
  15. this._drillThroughModelApi = options.drillThroughModelApi;
  16. },
  17. /**
  18. * silently apply the undo/redo payload to the model to restore a previous state
  19. * @param undoRedoValue - the value passed to onModelChange when an undo or redo occurs.
  20. */
  21. applyUndoRedo: function applyUndoRedo(payload, prevValue, sender, name, context) {
  22. if (sender === 'UndoRedoController' && name === 'reset') {
  23. var value = context.undoRedoTransactionType === 'undo' ? payload.prevValue : payload.value;
  24. this._drillThroughModelApi.applyModelChange(value, { silent: true });
  25. }
  26. },
  27. rollback: function rollback(transactionInfo) {
  28. if (transactionInfo.transactionId === this._mainTransaction) {
  29. transactionInfo.options.silent = true;
  30. this._drillThroughModelApi.applyModelChange(this.callbackPayload.prevValue, transactionInfo.options);
  31. }
  32. },
  33. startTransaction: function startTransaction(prefix, options, transactionArgs, transactionType) {
  34. var startStart = Date.now();
  35. this._transactionType = transactionType || this.TRANSACTION_TYPE_DEFAULT;
  36. this._transactionArgs = transactionArgs || {};
  37. // If a transaction id provided in the transactionArgs, then use it.
  38. var transactionId = transactionArgs && transactionArgs.transactionId || UniqueId.get(prefix);
  39. if (!this._mainTransaction && !this._transactionInProgress(options)) {
  40. options = options || {};
  41. options.payloadData = options.payloadData || {};
  42. this._mainTransaction = transactionId;
  43. options.payloadData.undoRedoTransactionId = this._mainTransaction;
  44. options.silent = true;
  45. this.callbackPayload = {
  46. //Make a clone
  47. prevValue: JSON.parse(JSON.stringify(this._drillThroughModelApi.toJSON()))
  48. };
  49. } else {
  50. options.silent = true;
  51. }
  52. return {
  53. startStart: startStart,
  54. startEnd: Date.now(),
  55. options: options,
  56. transactionId: transactionId
  57. };
  58. },
  59. _transactionInProgress: function _transactionInProgress(options) {
  60. return options && options.payloadData && options.payloadData.undoRedoTransactionId;
  61. },
  62. endTransaction: function endTransaction(transactionInfo) {
  63. if (transactionInfo.transactionId === this._mainTransaction) {
  64. //var endStart=Date.now();
  65. transactionInfo.options.silent = false;
  66. var newValue = this._drillThroughModelApi.toJSON();
  67. this.callbackPayload.value = newValue;
  68. transactionInfo.options = transactionInfo.options || {};
  69. transactionInfo.options.sender = 'DrillThroughTransaction';
  70. transactionInfo.options.senderContext = {
  71. applyFn: this.applyUndoRedo.bind(this, this.callbackPayload)
  72. };
  73. this._drillThroughModelApi.applyModelChange(newValue, transactionInfo.options);
  74. }
  75. }
  76. });
  77. return DrillThroughUndoRedoTransaction;
  78. });
  79. //# sourceMappingURL=DrillThroughManageUndoRedo.js.map