LocalFile.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // $Id = LocalFile.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. LocalFile = function(ui, data, title, temp)
  12. {
  13. DrawioFile.call(this, ui, data);
  14. this.title = title;
  15. this.mode = (temp) ? null : App.MODE_DEVICE;
  16. };
  17. //Extends mxEventSource
  18. mxUtils.extend(LocalFile, DrawioFile);
  19. /**
  20. * Translates this point by the given vector.
  21. *
  22. * @param {number} dx X-coordinate of the translation.
  23. * @param {number} dy Y-coordinate of the translation.
  24. */
  25. LocalFile.prototype.isAutosave = function()
  26. {
  27. return false;
  28. };
  29. /**
  30. * Translates this point by the given vector.
  31. *
  32. * @param {number} dx X-coordinate of the translation.
  33. * @param {number} dy Y-coordinate of the translation.
  34. */
  35. LocalFile.prototype.getMode = function()
  36. {
  37. return this.mode;
  38. };
  39. /**
  40. * Translates this point by the given vector.
  41. *
  42. * @param {number} dx X-coordinate of the translation.
  43. * @param {number} dy Y-coordinate of the translation.
  44. */
  45. LocalFile.prototype.getTitle = function()
  46. {
  47. return this.title;
  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. LocalFile.prototype.isRenamable = function()
  56. {
  57. return true;
  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. LocalFile.prototype.save = function(revision, success, error)
  66. {
  67. this.saveAs(this.title, success, error);
  68. };
  69. /**
  70. * Translates this point by the given vector.
  71. *
  72. * @param {number} dx X-coordinate of the translation.
  73. * @param {number} dy Y-coordinate of the translation.
  74. */
  75. LocalFile.prototype.saveAs = function(title, success, error)
  76. {
  77. this.saveFile(title, false, success, error);
  78. };
  79. /**
  80. * Translates this point by the given vector.
  81. *
  82. * @param {number} dx X-coordinate of the translation.
  83. * @param {number} dy Y-coordinate of the translation.
  84. */
  85. LocalFile.prototype.saveFile = function(title, revision, success, error)
  86. {
  87. this.title = title;
  88. // Updates data after changing file name
  89. this.updateFileData();
  90. var data = this.getData();
  91. var binary = this.ui.useCanvasForExport && /(\.png)$/i.test(this.getTitle());
  92. var doSave = mxUtils.bind(this, function(data)
  93. {
  94. if (this.ui.isOfflineApp() || this.ui.isLocalFileSave())
  95. {
  96. this.ui.doSaveLocalFile(data, title, (binary) ?
  97. 'image/png' : 'text/xml', binary);
  98. }
  99. else
  100. {
  101. if (data.length < MAX_REQUEST_SIZE)
  102. {
  103. var dot = title.lastIndexOf('.');
  104. var format = (dot > 0) ? title.substring(dot + 1) : 'xml';
  105. // Do not update modified flag
  106. new mxXmlRequest(SAVE_URL, 'format=' + format +
  107. '&xml=' + encodeURIComponent(data) +
  108. '&filename=' + encodeURIComponent(title) +
  109. ((binary) ? '&binary=1' : '')).
  110. simulate(document, '_blank');
  111. }
  112. else
  113. {
  114. this.ui.handleError({message: mxResources.get('drawingTooLarge')}, mxResources.get('error'), mxUtils.bind(this, function()
  115. {
  116. mxUtils.popup(data);
  117. }));
  118. }
  119. }
  120. this.setModified(false);
  121. this.contentChanged();
  122. if (success != null)
  123. {
  124. success();
  125. }
  126. });
  127. if (binary)
  128. {
  129. this.ui.getEmbeddedPng(mxUtils.bind(this, function(imageData)
  130. {
  131. doSave(imageData);
  132. }), error, (this.ui.getCurrentFile() != this) ? this.getData() : null);
  133. }
  134. else
  135. {
  136. doSave(data);
  137. }
  138. };
  139. /**
  140. * Translates this point by the given vector.
  141. *
  142. * @param {number} dx X-coordinate of the translation.
  143. * @param {number} dy Y-coordinate of the translation.
  144. */
  145. LocalFile.prototype.rename = function(title, success, error)
  146. {
  147. this.title = title;
  148. this.descriptorChanged();
  149. if (success != null)
  150. {
  151. success();
  152. }
  153. };
  154. /**
  155. * Returns the location as a new object.
  156. * @type mx.Point
  157. */
  158. LocalFile.prototype.open = function()
  159. {
  160. this.ui.setFileData(this.getData());
  161. // Only used to update the status when the file changes
  162. this.changeListener = mxUtils.bind(this, function(sender, eventObject)
  163. {
  164. this.setModified(true);
  165. this.addUnsavedStatus();
  166. });
  167. this.ui.editor.graph.model.addListener(mxEvent.CHANGE, this.changeListener);
  168. };