TrelloFile.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. };
  10. //Extends mxEventSource
  11. mxUtils.extend(TrelloFile, DrawioFile);
  12. /**
  13. *
  14. */
  15. TrelloFile.prototype.getHash = function()
  16. {
  17. return 'T' + encodeURIComponent(this.meta.compoundId);
  18. };
  19. /**
  20. *
  21. */
  22. TrelloFile.prototype.getMode = function()
  23. {
  24. return App.MODE_TRELLO;
  25. };
  26. /**
  27. * Overridden to enable the autosave option in the document properties dialog.
  28. */
  29. TrelloFile.prototype.isAutosaveOptional = function()
  30. {
  31. return true;
  32. };
  33. /**
  34. *
  35. */
  36. TrelloFile.prototype.getTitle = function()
  37. {
  38. return this.meta.name;
  39. };
  40. /**
  41. *
  42. */
  43. TrelloFile.prototype.isRenamable = function()
  44. {
  45. return false;
  46. };
  47. /**
  48. *
  49. */
  50. TrelloFile.prototype.save = function(revision, success, error)
  51. {
  52. this.doSave(this.getTitle(), success, error);
  53. };
  54. /**
  55. *
  56. */
  57. TrelloFile.prototype.saveAs = function(title, success, error)
  58. {
  59. this.doSave(title, success, error);
  60. };
  61. /**
  62. *
  63. */
  64. TrelloFile.prototype.doSave = function(title, success, error)
  65. {
  66. // Forces update of data for new extensions
  67. var prev = this.meta.name;
  68. this.meta.name = title;
  69. DrawioFile.prototype.save.apply(this, arguments);
  70. this.meta.name = prev;
  71. this.saveFile(title, false, success, error);
  72. };
  73. /**
  74. *
  75. */
  76. TrelloFile.prototype.saveFile = function(title, revision, success, error)
  77. {
  78. if (!this.isEditable())
  79. {
  80. if (success != null)
  81. {
  82. success();
  83. }
  84. }
  85. else if (!this.savingFile)
  86. {
  87. this.savingFile = true;
  88. if (this.getTitle() == title)
  89. {
  90. // Makes sure no changes get lost while the file is saved
  91. var prevModified = this.isModified;
  92. var modified = this.isModified();
  93. var prepare = mxUtils.bind(this, function()
  94. {
  95. this.setModified(false);
  96. this.isModified = function()
  97. {
  98. return modified;
  99. };
  100. });
  101. prepare();
  102. this.ui.trello.saveFile(this, mxUtils.bind(this, function(meta)
  103. {
  104. this.savingFile = false;
  105. this.isModified = prevModified;
  106. this.meta = meta;
  107. this.contentChanged();
  108. if (success != null)
  109. {
  110. success();
  111. }
  112. }),
  113. mxUtils.bind(this, function(err)
  114. {
  115. this.savingFile = false;
  116. this.isModified = prevModified;
  117. this.setModified(modified || this.isModified());
  118. if (error != null)
  119. {
  120. // Handles modified state for retries
  121. if (err != null && err.retry != null)
  122. {
  123. var retry = err.retry;
  124. err.retry = function()
  125. {
  126. prepare();
  127. retry();
  128. };
  129. }
  130. error(err);
  131. }
  132. }));
  133. }
  134. else
  135. {
  136. this.ui.pickFolder(App.MODE_TRELLO, mxUtils.bind(this, function(cardId)
  137. {
  138. this.ui.trello.insertFile(title, this.getData(), mxUtils.bind(this, function(file)
  139. {
  140. this.savingFile = false;
  141. if (success != null)
  142. {
  143. success();
  144. }
  145. this.ui.fileLoaded(file);
  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. error({code: App.ERROR_BUSY});
  160. }
  161. };