TrelloFile.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. *
  50. */
  51. TrelloFile.prototype.save = function(revision, success, error)
  52. {
  53. this.doSave(this.getTitle(), success, error);
  54. };
  55. /**
  56. *
  57. */
  58. TrelloFile.prototype.saveAs = function(title, success, error)
  59. {
  60. this.doSave(title, success, error);
  61. };
  62. /**
  63. *
  64. */
  65. TrelloFile.prototype.doSave = function(title, success, error)
  66. {
  67. // Forces update of data for new extensions
  68. var prev = this.meta.name;
  69. this.meta.name = title;
  70. DrawioFile.prototype.save.apply(this, arguments);
  71. this.meta.name = prev;
  72. this.saveFile(title, false, success, error);
  73. };
  74. /**
  75. *
  76. */
  77. TrelloFile.prototype.saveFile = function(title, revision, success, error)
  78. {
  79. if (!this.isEditable())
  80. {
  81. if (success != null)
  82. {
  83. success();
  84. }
  85. }
  86. else if (!this.savingFile)
  87. {
  88. this.savingFile = true;
  89. if (this.getTitle() == title)
  90. {
  91. // Makes sure no changes get lost while the file is saved
  92. var prevModified = this.isModified;
  93. var modified = this.isModified();
  94. var prepare = mxUtils.bind(this, function()
  95. {
  96. this.setModified(false);
  97. this.isModified = function()
  98. {
  99. return modified;
  100. };
  101. });
  102. prepare();
  103. this.ui.trello.saveFile(this, mxUtils.bind(this, function(meta)
  104. {
  105. this.savingFile = false;
  106. this.isModified = prevModified;
  107. this.meta = meta;
  108. this.contentChanged();
  109. if (success != null)
  110. {
  111. success();
  112. }
  113. if (this.saveNeededCounter > 0) {
  114. this.saveNeededCounter--;
  115. this.saveFile(title, revision, success, error);
  116. }
  117. }),
  118. mxUtils.bind(this, function(err)
  119. {
  120. this.savingFile = false;
  121. this.isModified = prevModified;
  122. this.setModified(modified || this.isModified());
  123. if (error != null)
  124. {
  125. // Handles modified state for retries
  126. if (err != null && err.retry != null)
  127. {
  128. var retry = err.retry;
  129. err.retry = function()
  130. {
  131. prepare();
  132. retry();
  133. };
  134. }
  135. error(err);
  136. }
  137. }));
  138. }
  139. else
  140. {
  141. this.ui.pickFolder(App.MODE_TRELLO, mxUtils.bind(this, function(cardId)
  142. {
  143. this.ui.trello.insertFile(title, this.getData(), mxUtils.bind(this, function(file)
  144. {
  145. this.savingFile = false;
  146. if (success != null)
  147. {
  148. success();
  149. }
  150. this.ui.fileLoaded(file);
  151. if (this.saveNeededCounter > 0) {
  152. this.saveNeededCounter--;
  153. this.saveFile(title, revision, success, error);
  154. }
  155. }), mxUtils.bind(this, function()
  156. {
  157. this.savingFile = false;
  158. if (error != null)
  159. {
  160. error();
  161. }
  162. }), false, cardId);
  163. }));
  164. }
  165. }
  166. else if (error != null)
  167. {
  168. this.saveNeededCounter++;
  169. error({code: App.ERROR_BUSY});
  170. }
  171. };