DriveFile.js 7.6 KB

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