GitHubFile.js 5.1 KB

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