TrelloFile.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * Copyright (c) 2006-2017, JGraph Ltd
  3. * Copyright (c) 2006-2017, Gaudenz Alder
  4. */
  5. TrelloFile = function(ui, data, meta)
  6. {
  7. DrawioFile.call(this, ui, data);
  8. this.meta = meta;
  9. this.saveNeededCounter = 0;
  10. };
  11. //Extends mxEventSource
  12. mxUtils.extend(TrelloFile, DrawioFile);
  13. /**
  14. *
  15. */
  16. TrelloFile.prototype.getHash = function()
  17. {
  18. return 'T' + encodeURIComponent(this.meta.compoundId);
  19. };
  20. /**
  21. *
  22. */
  23. TrelloFile.prototype.getMode = function()
  24. {
  25. return App.MODE_TRELLO;
  26. };
  27. /**
  28. * Overridden to enable the autosave option in the document properties dialog.
  29. */
  30. TrelloFile.prototype.isAutosave = function()
  31. {
  32. return true;
  33. };
  34. /**
  35. *
  36. */
  37. TrelloFile.prototype.getTitle = function()
  38. {
  39. return this.meta.name;
  40. };
  41. /**
  42. *
  43. */
  44. TrelloFile.prototype.isRenamable = function()
  45. {
  46. return false;
  47. };
  48. /**
  49. * Specifies if notify events should be ignored.
  50. */
  51. TrelloFile.prototype.getSize = function()
  52. {
  53. return this.meta.bytes;
  54. };
  55. /**
  56. *
  57. */
  58. TrelloFile.prototype.save = function(revision, success, error)
  59. {
  60. this.doSave(this.getTitle(), success, error);
  61. };
  62. /**
  63. *
  64. */
  65. TrelloFile.prototype.saveAs = function(title, success, error)
  66. {
  67. this.doSave(title, success, error);
  68. };
  69. /**
  70. *
  71. */
  72. TrelloFile.prototype.doSave = function(title, success, error)
  73. {
  74. // Forces update of data for new extensions
  75. var prev = this.meta.name;
  76. this.meta.name = title;
  77. DrawioFile.prototype.save.apply(this, [null, mxUtils.bind(this, function()
  78. {
  79. this.meta.name = prev;
  80. this.saveFile(title, false, success, error);
  81. }), error]);
  82. };
  83. /**
  84. *
  85. */
  86. TrelloFile.prototype.saveFile = function(title, revision, success, error)
  87. {
  88. if (!this.isEditable())
  89. {
  90. if (success != null)
  91. {
  92. success();
  93. }
  94. }
  95. else if (!this.savingFile)
  96. {
  97. // Sets shadow modified state during save
  98. this.savingFileTime = new Date();
  99. this.setShadowModified(false);
  100. this.savingFile = true;
  101. if (this.getTitle() == title)
  102. {
  103. this.ui.trello.saveFile(this, mxUtils.bind(this, function(meta)
  104. {
  105. // Checks for changes during save
  106. this.setModified(this.getShadowModified());
  107. this.savingFile = false;
  108. this.meta = meta;
  109. this.contentChanged();
  110. if (success != null)
  111. {
  112. success();
  113. }
  114. if (this.saveNeededCounter > 0)
  115. {
  116. this.saveNeededCounter--;
  117. this.saveFile(title, revision, success, error);
  118. }
  119. }),
  120. mxUtils.bind(this, function(err)
  121. {
  122. this.savingFile = false;
  123. if (error != null)
  124. {
  125. error(err);
  126. }
  127. }));
  128. }
  129. else
  130. {
  131. this.ui.pickFolder(App.MODE_TRELLO, mxUtils.bind(this, function(cardId)
  132. {
  133. this.ui.trello.insertFile(title, this.getData(), mxUtils.bind(this, function(file)
  134. {
  135. this.savingFile = false;
  136. if (success != null)
  137. {
  138. success();
  139. }
  140. this.ui.fileLoaded(file);
  141. if (this.saveNeededCounter > 0)
  142. {
  143. this.saveNeededCounter--;
  144. this.saveFile(title, revision, success, error);
  145. }
  146. }), mxUtils.bind(this, function()
  147. {
  148. this.savingFile = false;
  149. if (error != null)
  150. {
  151. error();
  152. }
  153. }), false, cardId);
  154. }));
  155. }
  156. }
  157. else if (error != null)
  158. {
  159. this.saveNeededCounter++;
  160. error({code: App.ERROR_BUSY});
  161. }
  162. };