DriveFile.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /**
  2. * Copyright (c) 2006-2017, JGraph Ltd
  3. * Copyright (c) 2006-2017, Gaudenz Alder
  4. */
  5. DriveFile = function(ui, data, desc, doc)
  6. {
  7. DrawioFile.call(this, ui, data);
  8. this.desc = desc;
  9. if (doc != null && doc.getModel() != null && doc.getModel().getRoot() != null)
  10. {
  11. this.realtime = new DriveRealtime(this, doc);
  12. }
  13. };
  14. //Extends mxEventSource
  15. mxUtils.extend(DriveFile, DrawioFile);
  16. /**
  17. * Returns true if copy, export and print are not allowed for this file.
  18. */
  19. DriveFile.prototype.isRestricted = function()
  20. {
  21. return this.desc.userPermission != null && this.desc.labels != null &&
  22. this.desc.userPermission.role == 'reader' && this.desc.labels.restricted;
  23. };
  24. /**
  25. * Delay for last save in ms.
  26. */
  27. DriveFile.prototype.saveDelay = 0;
  28. /**
  29. * Translates this point by the given vector.
  30. *
  31. * @param {number} dx X-coordinate of the translation.
  32. * @param {number} dy Y-coordinate of the translation.
  33. */
  34. DriveFile.prototype.getMode = function()
  35. {
  36. return App.MODE_GOOGLE;
  37. };
  38. /**
  39. * Returns true if copy, export and print are not allowed for this file.
  40. */
  41. DriveFile.prototype.getPublicUrl = function(fn)
  42. {
  43. gapi.client.drive.permissions.list(
  44. {
  45. 'fileId': this.desc.id
  46. }).execute(mxUtils.bind(this, function(resp)
  47. {
  48. if (resp != null)
  49. {
  50. for (var i = 0; i < resp.items.length; i++)
  51. {
  52. if (resp.items[i].id === 'anyoneWithLink' ||
  53. resp.items[i].id === 'anyone')
  54. {
  55. fn(this.desc.webContentLink);
  56. return;
  57. }
  58. }
  59. }
  60. fn(null);
  61. }));
  62. };
  63. /**
  64. * Overridden to enable the autosave option in the document properties dialog
  65. * if realtime is not used.
  66. */
  67. DriveFile.prototype.isAutosaveOptional = function()
  68. {
  69. return true;
  70. };
  71. /**
  72. * Translates this point by the given vector.
  73. *
  74. * @param {number} dx X-coordinate of the translation.
  75. * @param {number} dy Y-coordinate of the translation.
  76. */
  77. DriveFile.prototype.isAutosave = function()
  78. {
  79. return this.ui.editor.autosave || this.isAutosaveRevision();
  80. };
  81. /**
  82. * Returns true if an autosave is required at the time of execution.
  83. * This implementation returns true.
  84. */
  85. DriveFile.prototype.isAutosaveNow = function()
  86. {
  87. if (this.realtime != null && this.realtime.root != null)
  88. {
  89. var backup = parseInt(this.realtime.root.get('backupDate'));
  90. var modified = parseInt(this.realtime.root.get('modifiedDate'));
  91. return isNaN(backup) || isNaN(modified) || backup < modified;
  92. }
  93. return true;
  94. };
  95. /**
  96. * Hooks for subclassers after the autosave has completed.
  97. */
  98. DriveFile.prototype.autosaveCompleted = function()
  99. {
  100. if (this.realtime != null && this.realtime.root != null)
  101. {
  102. this.realtime.root.set('backupDate', new Date().getTime());
  103. }
  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. DriveFile.prototype.isRenamable = function()
  112. {
  113. return this.isEditable() && DrawioFile.prototype.isEditable.apply(this, arguments);
  114. };
  115. /**
  116. * Translates this point by the given vector.
  117. *
  118. * @param {number} dx X-coordinate of the translation.
  119. * @param {number} dy Y-coordinate of the translation.
  120. */
  121. DriveFile.prototype.isMovable = function()
  122. {
  123. return this.isEditable();
  124. };
  125. /**
  126. * Translates this point by the given vector.
  127. *
  128. * @param {number} dx X-coordinate of the translation.
  129. * @param {number} dy Y-coordinate of the translation.
  130. */
  131. DriveFile.prototype.save = function(revision, success, error, unloading)
  132. {
  133. DrawioFile.prototype.save.apply(this, arguments);
  134. this.saveFile(null, revision, success, error, unloading);
  135. };
  136. /**
  137. * Translates this point by the given vector.
  138. *
  139. * @param {number} dx X-coordinate of the translation.
  140. * @param {number} dy Y-coordinate of the translation.
  141. */
  142. DriveFile.prototype.saveFile = function(title, revision, success, error, unloading)
  143. {
  144. if (!this.isEditable())
  145. {
  146. if (success != null)
  147. {
  148. success();
  149. }
  150. }
  151. else if (!this.savingFile)
  152. {
  153. this.savingFile = true;
  154. // Makes sure no changes get lost while the file is saved
  155. var prevModified = this.isModified;
  156. var modified = this.isModified();
  157. this.setModified(false);
  158. this.ui.drive.saveFile(this, revision, mxUtils.bind(this, function(resp)
  159. {
  160. this.savingFile = false;
  161. this.isModified = prevModified;
  162. // Handles special case where resp is false eg
  163. // if the old file was converted to realtime
  164. if (resp != false)
  165. {
  166. if (revision)
  167. {
  168. this.lastAutosaveRevision = new Date().getTime();
  169. }
  170. this.desc = resp;
  171. this.contentChanged();
  172. if (success != null)
  173. {
  174. success(resp);
  175. }
  176. }
  177. else
  178. {
  179. this.setModified(modified || this.isModified());
  180. if (error != null)
  181. {
  182. error();
  183. }
  184. }
  185. }), mxUtils.bind(this, function(resp)
  186. {
  187. this.savingFile = false;
  188. this.isModified = prevModified;
  189. this.setModified(modified || this.isModified());
  190. if (error != null)
  191. {
  192. error(resp);
  193. }
  194. }), unloading, unloading);
  195. }
  196. };
  197. /**
  198. * Translates this point by the given vector.
  199. *
  200. * @param {number} dx X-coordinate of the translation.
  201. * @param {number} dy Y-coordinate of the translation.
  202. */
  203. DriveFile.prototype.saveAs = function(filename, success, error)
  204. {
  205. this.ui.drive.copyFile(this.getId(), filename, success, error);
  206. };
  207. /**
  208. * Translates this point by the given vector.
  209. *
  210. * @param {number} dx X-coordinate of the translation.
  211. * @param {number} dy Y-coordinate of the translation.
  212. */
  213. DriveFile.prototype.rename = function(title, success, error)
  214. {
  215. this.ui.drive.renameFile(this.getId(), title, mxUtils.bind(this, function(resp)
  216. {
  217. if (!this.hasSameExtension(title, this.getTitle()))
  218. {
  219. this.desc = resp;
  220. this.save(true, success, error);
  221. }
  222. else
  223. {
  224. this.desc = resp;
  225. this.descriptorChanged();
  226. if (success != null)
  227. {
  228. success(resp);
  229. }
  230. }
  231. }), error);
  232. };
  233. /**
  234. * Translates this point by the given vector.
  235. *
  236. * @param {number} dx X-coordinate of the translation.
  237. * @param {number} dy Y-coordinate of the translation.
  238. */
  239. DriveFile.prototype.move = function(folderId, success, error)
  240. {
  241. this.ui.drive.moveFile(this.getId(), folderId, mxUtils.bind(this, function(resp)
  242. {
  243. this.desc = resp;
  244. this.descriptorChanged();
  245. if (success != null)
  246. {
  247. success(resp);
  248. }
  249. }), error);
  250. };
  251. /**
  252. * Translates this point by the given vector.
  253. *
  254. * @param {number} dx X-coordinate of the translation.
  255. * @param {number} dy Y-coordinate of the translation.
  256. */
  257. DriveFile.prototype.getTitle = function()
  258. {
  259. return this.desc.title;
  260. };
  261. /**
  262. * Translates this point by the given vector.
  263. *
  264. * @param {number} dx X-coordinate of the translation.
  265. * @param {number} dy Y-coordinate of the translation.
  266. */
  267. DriveFile.prototype.getHash = function()
  268. {
  269. return 'G' + this.getId();
  270. };
  271. /**
  272. * Translates this point by the given vector.
  273. *
  274. * @param {number} dx X-coordinate of the translation.
  275. * @param {number} dy Y-coordinate of the translation.
  276. */
  277. DriveFile.prototype.getId = function()
  278. {
  279. return this.desc.id;
  280. };
  281. /**
  282. * Translates this point by the given vector.
  283. *
  284. * @param {number} dx X-coordinate of the translation.
  285. * @param {number} dy Y-coordinate of the translation.
  286. */
  287. DriveFile.prototype.isEditable = function()
  288. {
  289. var editable = DrawioFile.prototype.isEditable.apply(this, arguments);
  290. if (this.realtime != null)
  291. {
  292. return editable && !this.realtime.rtModel.isReadOnly;
  293. }
  294. else
  295. {
  296. return editable && this.desc.editable;
  297. }
  298. };
  299. /**
  300. * Returns the location as a new object.
  301. */
  302. DriveFile.prototype.open = function()
  303. {
  304. if (this.realtime != null)
  305. {
  306. this.realtime.start();
  307. }
  308. else
  309. {
  310. DrawioFile.prototype.open.apply(this, arguments);
  311. }
  312. };
  313. /**
  314. * Returns the location as a new object.
  315. */
  316. DriveFile.prototype.close = function(unloading)
  317. {
  318. unloading = (unloading != null) ? unloading : false;
  319. DrawioFile.prototype.close.apply(this, arguments);
  320. if (this.realtime != null)
  321. {
  322. this.realtime.destroy(unloading);
  323. this.realtime = null;
  324. }
  325. };