LocalFile.js 3.9 KB

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