LocalFile.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. if (this.ui.isOfflineApp() || this.ui.isLocalFileSave())
  92. {
  93. this.ui.doSaveLocalFile(data, title, 'text/xml');
  94. }
  95. else
  96. {
  97. if (data.length < MAX_REQUEST_SIZE)
  98. {
  99. var dot = title.lastIndexOf('.');
  100. var format = (dot > 0) ? title.substring(dot + 1) : 'xml';
  101. // Do not update modified flag
  102. new mxXmlRequest(SAVE_URL, 'format=' + format + '&filename=' +
  103. encodeURIComponent(title) + '&xml=' +
  104. encodeURIComponent(data)).simulate(document, '_blank');
  105. }
  106. else
  107. {
  108. this.ui.handleError({message: mxResources.get('drawingTooLarge')}, mxResources.get('error'), mxUtils.bind(this, function()
  109. {
  110. mxUtils.popup(data);
  111. }));
  112. }
  113. }
  114. this.setModified(false);
  115. this.contentChanged();
  116. if (success != null)
  117. {
  118. success();
  119. }
  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. LocalFile.prototype.rename = function(title, success, error)
  128. {
  129. this.title = title;
  130. this.descriptorChanged();
  131. if (success != null)
  132. {
  133. success();
  134. }
  135. };
  136. /**
  137. * Returns the location as a new object.
  138. * @type mx.Point
  139. */
  140. LocalFile.prototype.open = function()
  141. {
  142. this.ui.setFileData(this.getData());
  143. // Only used to update the status when the file changes
  144. this.changeListener = mxUtils.bind(this, function(sender, eventObject)
  145. {
  146. this.setModified(true);
  147. this.addUnsavedStatus();
  148. });
  149. this.ui.editor.graph.model.addListener(mxEvent.CHANGE, this.changeListener);
  150. };