DropboxFile.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /**
  2. * Copyright (c) 2006-2017, JGraph Ltd
  3. * Copyright (c) 2006-2017, Gaudenz Alder
  4. */
  5. DropboxFile = function(ui, data, stat)
  6. {
  7. DrawioFile.call(this, ui, data);
  8. this.stat = stat;
  9. };
  10. //Extends mxEventSource
  11. mxUtils.extend(DropboxFile, 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. DropboxFile.prototype.getId = function()
  19. {
  20. return this.stat.path_display.substring(1);
  21. };
  22. /**
  23. * Translates this point by the given vector.
  24. *
  25. * @param {number} dx X-coordinate of the translation.
  26. * @param {number} dy Y-coordinate of the translation.
  27. */
  28. DropboxFile.prototype.getHash = function()
  29. {
  30. return 'D' + encodeURIComponent(this.getId());
  31. };
  32. /**
  33. * Translates this point by the given vector.
  34. *
  35. * @param {number} dx X-coordinate of the translation.
  36. * @param {number} dy Y-coordinate of the translation.
  37. */
  38. DropboxFile.prototype.getMode = function()
  39. {
  40. return App.MODE_DROPBOX;
  41. };
  42. /**
  43. * Overridden to enable the autosave option in the document properties dialog.
  44. */
  45. DropboxFile.prototype.isAutosaveOptional = function()
  46. {
  47. return true;
  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. DropboxFile.prototype.getTitle = function()
  56. {
  57. return this.stat.name;
  58. };
  59. /**
  60. * Translates this point by the given vector.
  61. *
  62. * @param {number} dx X-coordinate of the translation.
  63. * @param {number} dy Y-coordinate of the translation.
  64. */
  65. DropboxFile.prototype.isRenamable = function()
  66. {
  67. return true;
  68. };
  69. /**
  70. * Specifies if notify events should be ignored.
  71. */
  72. DropboxFile.prototype.getSize = function()
  73. {
  74. return this.stat.size;
  75. };
  76. /**
  77. * Hook for subclassers.
  78. */
  79. DropboxFile.prototype.isRevisionHistorySupported = function()
  80. {
  81. return true;
  82. };
  83. /**
  84. * Returns true if copy, export and print are not allowed for this file.
  85. */
  86. DropboxFile.prototype.getFileUrl = function()
  87. {
  88. return 'https://www.dropbox.com/home/Apps' + this.ui.dropbox.appPath + this.stat.path_display;
  89. };
  90. /**
  91. * Returns true if copy, export and print are not allowed for this file.
  92. */
  93. DropboxFile.prototype.getFolderUrl = function()
  94. {
  95. var url = this.getFileUrl();
  96. return url.substring(0, url.lastIndexOf('/'));
  97. };
  98. /**
  99. * Hook for subclassers.
  100. */
  101. DropboxFile.prototype.getRevisions = function(success, error)
  102. {
  103. var promise = this.ui.dropbox.client.filesListRevisions({path: this.stat.path_lower, limit: 100});
  104. promise.then(mxUtils.bind(this, function(resp)
  105. {
  106. try
  107. {
  108. var revs = [];
  109. for (var i = resp.entries.length - 1; i >= 0; i--)
  110. {
  111. (mxUtils.bind(this, function(stat)
  112. {
  113. revs.push({modifiedDate: stat.client_modified, fileSize: stat.size,
  114. getXml: mxUtils.bind(this, function(itemSuccess, itemError)
  115. {
  116. this.ui.dropbox.readFile({path: this.stat.path_lower, rev: stat.rev},
  117. itemSuccess, itemError);
  118. }), getUrl: mxUtils.bind(this, function(page)
  119. {
  120. return this.ui.getUrl(window.location.pathname + '?rev=' +
  121. stat.rev + '&chrome=0&nav=1&layers=1&edit=_blank' + ((page != null) ?
  122. '&page=' + page : '')) + window.location.hash;
  123. })});
  124. }))(resp.entries[i]);
  125. }
  126. success(revs);
  127. }
  128. catch (e)
  129. {
  130. error(e);
  131. }
  132. }));
  133. // Workaround for IE8/9 support with catch function
  134. promise['catch'](function(err)
  135. {
  136. error(err);
  137. });
  138. };
  139. /**
  140. * Adds the listener for automatically saving the diagram for local changes.
  141. */
  142. DropboxFile.prototype.getLatestVersion = function(success, error)
  143. {
  144. this.ui.dropbox.getFile(this.getId(), success, error);
  145. };
  146. /**
  147. * Updates the descriptor of this file with the one from the given file.
  148. */
  149. DropboxFile.prototype.updateDescriptor = function(newFile)
  150. {
  151. this.stat = newFile.stat;
  152. };
  153. /**
  154. * Translates this point by the given vector.
  155. *
  156. * @param {number} dx X-coordinate of the translation.
  157. * @param {number} dy Y-coordinate of the translation.
  158. */
  159. DropboxFile.prototype.save = function(revision, success, error, unloading, overwrite)
  160. {
  161. this.doSave(this.getTitle(), revision, success, error, unloading, overwrite);
  162. };
  163. /**
  164. * Translates this point by the given vector.
  165. *
  166. * @param {number} dx X-coordinate of the translation.
  167. * @param {number} dy Y-coordinate of the translation.
  168. */
  169. DropboxFile.prototype.saveAs = function(title, success, error)
  170. {
  171. this.doSave(title, false, success, error);
  172. };
  173. /**
  174. * Translates this point by the given vector.
  175. *
  176. * @param {number} dx X-coordinate of the translation.
  177. * @param {number} dy Y-coordinate of the translation.
  178. */
  179. DropboxFile.prototype.doSave = function(title, revision, success, error, unloading, overwrite)
  180. {
  181. // Forces update of data for new extensions
  182. var prev = this.stat.name;
  183. this.stat.name = title;
  184. DrawioFile.prototype.save.apply(this, [null, mxUtils.bind(this, function()
  185. {
  186. this.stat.name = prev;
  187. this.saveFile(title, revision, success, error, unloading, overwrite);
  188. }), error, unloading, overwrite]);
  189. };
  190. /**
  191. * Translates this point by the given vector.
  192. *
  193. * @param {number} dx X-coordinate of the translation.
  194. * @param {number} dy Y-coordinate of the translation.
  195. */
  196. DropboxFile.prototype.saveFile = function(title, revision, success, error)
  197. {
  198. if (!this.isEditable())
  199. {
  200. if (success != null)
  201. {
  202. success();
  203. }
  204. }
  205. else if (!this.savingFile)
  206. {
  207. var fn = mxUtils.bind(this, function(checked)
  208. {
  209. if (checked)
  210. {
  211. try
  212. {
  213. // Sets shadow modified state during save
  214. this.savingFileTime = new Date();
  215. this.setShadowModified(false);
  216. this.savingFile = true;
  217. var doSave = mxUtils.bind(this, function(data)
  218. {
  219. var index = this.stat.path_display.lastIndexOf('/');
  220. var folder = (index > 1) ? this.stat.path_display.substring(1, index + 1) : null;
  221. this.ui.dropbox.saveFile(title, data, mxUtils.bind(this, function(stat)
  222. {
  223. // Checks for changes during save
  224. this.setModified(this.getShadowModified());
  225. this.savingFile = false;
  226. this.stat = stat;
  227. this.contentChanged();
  228. if (success != null)
  229. {
  230. success();
  231. }
  232. }), mxUtils.bind(this, function(err)
  233. {
  234. this.savingFile = false;
  235. if (error != null)
  236. {
  237. error(err);
  238. }
  239. }), folder);
  240. });
  241. if (this.ui.useCanvasForExport && /(\.png)$/i.test(this.getTitle()))
  242. {
  243. var p = this.ui.getPngFileProperties(this.ui.fileNode);
  244. this.ui.getEmbeddedPng(mxUtils.bind(this, function(data)
  245. {
  246. doSave(this.ui.base64ToBlob(data, 'image/png'));
  247. }), error, (this.ui.getCurrentFile() != this) ?
  248. this.getData() : null, p.scale, p.border);
  249. }
  250. else
  251. {
  252. doSave(this.getData());
  253. }
  254. }
  255. catch (e)
  256. {
  257. this.savingFile = false;
  258. if (error != null)
  259. {
  260. error(e);
  261. }
  262. else
  263. {
  264. throw e;
  265. }
  266. }
  267. }
  268. else if (error != null)
  269. {
  270. error();
  271. }
  272. });
  273. if (this.getTitle() == title)
  274. {
  275. fn(true);
  276. }
  277. else
  278. {
  279. this.ui.dropbox.checkExists(title, fn);
  280. }
  281. }
  282. else if (error != null)
  283. {
  284. error({code: App.ERROR_BUSY});
  285. }
  286. };
  287. /**
  288. * Translates this point by the given vector.
  289. *
  290. * @param {number} dx X-coordinate of the translation.
  291. * @param {number} dy Y-coordinate of the translation.
  292. */
  293. DropboxFile.prototype.rename = function(title, success, error)
  294. {
  295. this.ui.dropbox.renameFile(this, title, mxUtils.bind(this, function(stat)
  296. {
  297. if (!this.hasSameExtension(title, this.getTitle()))
  298. {
  299. this.stat = stat;
  300. // Required in this case to update hash tag in page
  301. // before saving so that the edit link is correct
  302. this.descriptorChanged();
  303. this.save(true, success, error);
  304. }
  305. else
  306. {
  307. this.stat = stat;
  308. this.descriptorChanged();
  309. if (success != null)
  310. {
  311. success();
  312. }
  313. }
  314. }), error);
  315. };