simple_transaction.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Simple Transaction API, mainly designed for use in the ECMAScript console.
  3. */
  4. /**
  5. * Starts a transaction. This can increase performance when adding multiple entities.
  6. * Entities are added in one transaction when endTransaction is called.
  7. * \ingroup ecma_simple
  8. *
  9. * \param d RDocument or RDocumentInterface to apply the transaction to
  10. * (defaults to current document).
  11. *
  12. * \code
  13. * startTransaction();
  14. * for (...) {
  15. * addLine(...);
  16. * }
  17. * endTransaction();
  18. * \endcode
  19. */
  20. function startTransaction(d) {
  21. __simpleUseOp = true;
  22. if (isOfType(d, RDocument)) {
  23. __simpleDoc = d;
  24. __simpleDi = undefined;
  25. }
  26. if (isOfType(d, RDocumentInterface)) {
  27. __simpleDi = d;
  28. __simpleDoc = d.getDocument();
  29. }
  30. if (!isNull(__simpleOp)) {
  31. destr(__simpleOp);
  32. __simpleOp = undefined;
  33. }
  34. return true;
  35. }
  36. /**
  37. * Ends a transaction.
  38. * \see startTransaction
  39. * \ingroup ecma_simple
  40. *
  41. * \return RTransaction object containing information about the transaction.
  42. */
  43. function endTransaction() {
  44. var ret = undefined;
  45. if (!isNull(__simpleOp)) {
  46. // apply operation to document interface:
  47. if (!isNull(__simpleDi)) {
  48. __simpleDi.applyOperation(__simpleOp);
  49. __simpleDi = undefined;
  50. __simpleDoc = undefined;
  51. }
  52. // apply operation to off-screen document:
  53. else if (!isNull(__simpleDoc)) {
  54. ret = __simpleOp.apply(__simpleDoc);
  55. __simpleDoc = undefined;
  56. }
  57. // apply operation to current document:
  58. else {
  59. var di = getDocumentInterface();
  60. if (!isNull(di)) {
  61. ret = di.applyOperation(__simpleOp);
  62. }
  63. }
  64. __simpleOp = undefined;
  65. }
  66. __simpleUseOp = false;
  67. return ret;
  68. }
  69. /**
  70. * Returns the RDocument the current transaction applies to or the current document or undefined.
  71. * \ingroup ecma_simple
  72. */
  73. function getTransactionDocument() {
  74. if (!isNull(__simpleDoc)) {
  75. return __simpleDoc;
  76. }
  77. else {
  78. return getDocument();
  79. }
  80. }
  81. function getAssignTransactionDocument(fileType) {
  82. if (!isNull(__simpleDoc)) {
  83. return __simpleDoc;
  84. }
  85. else {
  86. return getAssignDocument(fileType);
  87. }
  88. }
  89. /**
  90. * Returns the RDocumentInterface the current transaction applies to or
  91. * the current document interface or undefined.
  92. * \ingroup ecma_simple
  93. */
  94. function getTransactionDocumentInterface() {
  95. if (!isNull(__simpleDi)) {
  96. return __simpleDi;
  97. }
  98. else {
  99. return getDocumentInterface();
  100. }
  101. }
  102. /**
  103. * Returns the current operation if we are in a transaction or a new operation.
  104. * \ingroup ecma_simple
  105. */
  106. function getOperation() {
  107. if (!isNull(__simpleOp)) {
  108. return __simpleOp;
  109. }
  110. return new RAddObjectsOperation();
  111. }