Manager.js
上传用户:kimgenplus
上传日期:2016-06-05
资源大小:20877k
文件大小:4k
源码类别:

OA系统

开发平台:

Java

  1. /*
  2. Copyright (c) 2004-2006, The Dojo Foundation
  3. All Rights Reserved.
  4. Licensed under the Academic Free License version 2.1 or above OR the
  5. modified BSD license. For more information on Dojo licensing, see:
  6. http://dojotoolkit.org/community/licensing.shtml
  7. */
  8. dojo.provide("dojo.undo.Manager");
  9. dojo.require("dojo.lang.common");
  10. dojo.undo.Manager = function (parent) {
  11. this.clear();
  12. this._parent = parent;
  13. };
  14. dojo.extend(dojo.undo.Manager, {_parent:null, _undoStack:null, _redoStack:null, _currentManager:null, canUndo:false, canRedo:false, isUndoing:false, isRedoing:false, onUndo:function (manager, item) {
  15. }, onRedo:function (manager, item) {
  16. }, onUndoAny:function (manager, item) {
  17. }, onRedoAny:function (manager, item) {
  18. }, _updateStatus:function () {
  19. this.canUndo = this._undoStack.length > 0;
  20. this.canRedo = this._redoStack.length > 0;
  21. }, clear:function () {
  22. this._undoStack = [];
  23. this._redoStack = [];
  24. this._currentManager = this;
  25. this.isUndoing = false;
  26. this.isRedoing = false;
  27. this._updateStatus();
  28. }, undo:function () {
  29. if (!this.canUndo) {
  30. return false;
  31. }
  32. this.endAllTransactions();
  33. this.isUndoing = true;
  34. var top = this._undoStack.pop();
  35. if (top instanceof dojo.undo.Manager) {
  36. top.undoAll();
  37. } else {
  38. top.undo();
  39. }
  40. if (top.redo) {
  41. this._redoStack.push(top);
  42. }
  43. this.isUndoing = false;
  44. this._updateStatus();
  45. this.onUndo(this, top);
  46. if (!(top instanceof dojo.undo.Manager)) {
  47. this.getTop().onUndoAny(this, top);
  48. }
  49. return true;
  50. }, redo:function () {
  51. if (!this.canRedo) {
  52. return false;
  53. }
  54. this.isRedoing = true;
  55. var top = this._redoStack.pop();
  56. if (top instanceof dojo.undo.Manager) {
  57. top.redoAll();
  58. } else {
  59. top.redo();
  60. }
  61. this._undoStack.push(top);
  62. this.isRedoing = false;
  63. this._updateStatus();
  64. this.onRedo(this, top);
  65. if (!(top instanceof dojo.undo.Manager)) {
  66. this.getTop().onRedoAny(this, top);
  67. }
  68. return true;
  69. }, undoAll:function () {
  70. while (this._undoStack.length > 0) {
  71. this.undo();
  72. }
  73. }, redoAll:function () {
  74. while (this._redoStack.length > 0) {
  75. this.redo();
  76. }
  77. }, push:function (undo, redo, description) {
  78. if (!undo) {
  79. return;
  80. }
  81. if (this._currentManager == this) {
  82. this._undoStack.push({undo:undo, redo:redo, description:description});
  83. } else {
  84. this._currentManager.push.apply(this._currentManager, arguments);
  85. }
  86. this._redoStack = [];
  87. this._updateStatus();
  88. }, concat:function (manager) {
  89. if (!manager) {
  90. return;
  91. }
  92. if (this._currentManager == this) {
  93. for (var x = 0; x < manager._undoStack.length; x++) {
  94. this._undoStack.push(manager._undoStack[x]);
  95. }
  96. if (manager._undoStack.length > 0) {
  97. this._redoStack = [];
  98. }
  99. this._updateStatus();
  100. } else {
  101. this._currentManager.concat.apply(this._currentManager, arguments);
  102. }
  103. }, beginTransaction:function (description) {
  104. if (this._currentManager == this) {
  105. var mgr = new dojo.undo.Manager(this);
  106. mgr.description = description ? description : "";
  107. this._undoStack.push(mgr);
  108. this._currentManager = mgr;
  109. return mgr;
  110. } else {
  111. this._currentManager = this._currentManager.beginTransaction.apply(this._currentManager, arguments);
  112. }
  113. }, endTransaction:function (flatten) {
  114. if (this._currentManager == this) {
  115. if (this._parent) {
  116. this._parent._currentManager = this._parent;
  117. if (this._undoStack.length == 0 || flatten) {
  118. var idx = dojo.lang.find(this._parent._undoStack, this);
  119. if (idx >= 0) {
  120. this._parent._undoStack.splice(idx, 1);
  121. if (flatten) {
  122. for (var x = 0; x < this._undoStack.length; x++) {
  123. this._parent._undoStack.splice(idx++, 0, this._undoStack[x]);
  124. }
  125. this._updateStatus();
  126. }
  127. }
  128. }
  129. return this._parent;
  130. }
  131. } else {
  132. this._currentManager = this._currentManager.endTransaction.apply(this._currentManager, arguments);
  133. }
  134. }, endAllTransactions:function () {
  135. while (this._currentManager != this) {
  136. this.endTransaction();
  137. }
  138. }, getTop:function () {
  139. if (this._parent) {
  140. return this._parent.getTop();
  141. } else {
  142. return this;
  143. }
  144. }});