GitHubFile.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * Copyright (c) 2006-2017, JGraph Ltd
  3. * Copyright (c) 2006-2017, Gaudenz Alder
  4. */
  5. GitHubFile = function(ui, data, meta)
  6. {
  7. DrawioFile.call(this, ui, data);
  8. this.meta = meta;
  9. };
  10. //Extends mxEventSource
  11. mxUtils.extend(GitHubFile, DrawioFile);
  12. /**
  13. * Translates this point by the given vector.
  14. *
  15. * @param {number} dx X-coordinate of the translation.
  16. * @param {number} dy Y-coordinate of the translation.
  17. */
  18. GitHubFile.prototype.getHash = function()
  19. {
  20. return encodeURIComponent('H' + encodeURIComponent(this.meta.org) + '/' +
  21. ((this.meta.repo != null) ?
  22. encodeURIComponent(this.meta.repo) + '/' +
  23. ((this.meta.ref != null) ?
  24. this.meta.ref + ((this.meta.path != null) ?
  25. '/' + this.meta.path : '') : '') : ''));
  26. };
  27. /**
  28. * Returns true if copy, export and print are not allowed for this file.
  29. */
  30. GitHubFile.prototype.getPublicUrl = function(fn)
  31. {
  32. // LATER: Check if download_url is always null for private repos
  33. if (this.meta.download_url != null)
  34. {
  35. mxUtils.get(this.meta.download_url, mxUtils.bind(this, function(req)
  36. {
  37. fn((req.getStatus() >= 200 && req.getStatus() <= 299) ? this.meta.download_url : null);
  38. }), mxUtils.bind(this, function()
  39. {
  40. fn(null);
  41. }));
  42. }
  43. else
  44. {
  45. fn(null);
  46. }
  47. };
  48. /**
  49. * Translates this point by the given vector.
  50. *
  51. * @param {number} dx X-coordinate of the translation.
  52. * @param {number} dy Y-coordinate of the translation.
  53. */
  54. GitHubFile.prototype.getMode = function()
  55. {
  56. return App.MODE_GITHUB;
  57. };
  58. /**
  59. * Overridden to enable the autosave option in the document properties dialog.
  60. */
  61. GitHubFile.prototype.isAutosave = function()
  62. {
  63. return false;
  64. };
  65. /**
  66. * Translates this point by the given vector.
  67. *
  68. * @param {number} dx X-coordinate of the translation.
  69. * @param {number} dy Y-coordinate of the translation.
  70. */
  71. GitHubFile.prototype.getTitle = function()
  72. {
  73. return this.meta.name;
  74. };
  75. /**
  76. * Translates this point by the given vector.
  77. *
  78. * @param {number} dx X-coordinate of the translation.
  79. * @param {number} dy Y-coordinate of the translation.
  80. */
  81. GitHubFile.prototype.isRenamable = function()
  82. {
  83. return false;
  84. };
  85. /**
  86. * Translates this point by the given vector.
  87. *
  88. * @param {number} dx X-coordinate of the translation.
  89. * @param {number} dy Y-coordinate of the translation.
  90. */
  91. GitHubFile.prototype.save = function(revision, success, error)
  92. {
  93. this.doSave(this.getTitle(), success, error);
  94. };
  95. /**
  96. * Translates this point by the given vector.
  97. *
  98. * @param {number} dx X-coordinate of the translation.
  99. * @param {number} dy Y-coordinate of the translation.
  100. */
  101. GitHubFile.prototype.saveAs = function(title, success, error)
  102. {
  103. this.doSave(title, success, error);
  104. };
  105. /**
  106. * Translates this point by the given vector.
  107. *
  108. * @param {number} dx X-coordinate of the translation.
  109. * @param {number} dy Y-coordinate of the translation.
  110. */
  111. GitHubFile.prototype.doSave = function(title, success, error)
  112. {
  113. // Forces update of data for new extensions
  114. var prev = this.meta.name;
  115. this.meta.name = title;
  116. DrawioFile.prototype.save.apply(this, arguments);
  117. this.meta.name = prev;
  118. this.saveFile(title, false, success, error);
  119. };
  120. /**
  121. * Translates this point by the given vector.
  122. *
  123. * @param {number} dx X-coordinate of the translation.
  124. * @param {number} dy Y-coordinate of the translation.
  125. */
  126. GitHubFile.prototype.saveFile = function(title, revision, success, error)
  127. {
  128. if (!this.isEditable())
  129. {
  130. if (success != null)
  131. {
  132. success();
  133. }
  134. }
  135. else if (!this.savingFile)
  136. {
  137. this.savingFile = true;
  138. if (this.getTitle() == title)
  139. {
  140. // Makes sure no changes get lost while the file is saved
  141. var prevModified = this.isModified;
  142. var modified = this.isModified();
  143. var prepare = mxUtils.bind(this, function()
  144. {
  145. this.setModified(false);
  146. this.isModified = function()
  147. {
  148. return modified;
  149. };
  150. });
  151. prepare();
  152. this.ui.gitHub.saveFile(this, mxUtils.bind(this, function(commit)
  153. {
  154. this.savingFile = false;
  155. this.isModified = prevModified;
  156. this.meta.sha = commit.content.sha;
  157. this.meta.html_url = commit.content.html_url;
  158. this.meta.download_url = commit.content.download_url;
  159. this.contentChanged();
  160. if (success != null)
  161. {
  162. success();
  163. }
  164. }),
  165. mxUtils.bind(this, function(err)
  166. {
  167. this.savingFile = false;
  168. this.isModified = prevModified;
  169. this.setModified(modified || this.isModified());
  170. if (this.isModified())
  171. {
  172. this.addUnsavedStatus();
  173. }
  174. if (error != null)
  175. {
  176. // Handles modified state for retries
  177. if (err != null && err.retry != null)
  178. {
  179. var retry = err.retry;
  180. err.retry = function()
  181. {
  182. prepare();
  183. retry();
  184. };
  185. }
  186. error(err);
  187. }
  188. }));
  189. }
  190. else
  191. {
  192. this.ui.pickFolder(App.MODE_GITHUB, mxUtils.bind(this, function(folderId)
  193. {
  194. this.ui.gitHub.insertFile(title, this.getData(), mxUtils.bind(this, function(file)
  195. {
  196. this.savingFile = false;
  197. if (success != null)
  198. {
  199. success();
  200. }
  201. this.ui.fileLoaded(file);
  202. }), mxUtils.bind(this, function()
  203. {
  204. this.savingFile = false;
  205. if (error != null)
  206. {
  207. error();
  208. }
  209. }), false, folderId);
  210. }));
  211. }
  212. }
  213. else if (error != null)
  214. {
  215. error({code: App.ERROR_BUSY});
  216. }
  217. };