123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- // $Id = DrawioFile.js,v 1.12 2010-01-02 09 =45 =14 gaudenz Exp $
- // Copyright (c) 2006-2014, JGraph Ltd
- /**
- * Constructs a new point for the optional x and y coordinates. If no
- * coordinates are given, then the default values for <x> and <y> are used.
- * @constructor
- * @class Implements a basic 2D point. Known subclassers = {@link mxRectangle}.
- * @param {number} x X-coordinate of the point.
- * @param {number} y Y-coordinate of the point.
- */
- DrawioFile = function(ui, data)
- {
- mxEventSource.call(this);
-
- /**
- * Holds the x-coordinate of the point.
- * @type number
- * @default 0
- */
- this.ui = ui;
-
- /**
- * Holds the x-coordinate of the point.
- * @type number
- * @default 0
- */
- this.data = data || '';
- };
- //Extends mxEventSource
- mxUtils.extend(DrawioFile, mxEventSource);
- /**
- * Sets the delay for autosave in milliseconds. Default is 1500.
- */
- DrawioFile.prototype.autosaveDelay = 1500;
- /**
- * Sets the delay for autosave in milliseconds. Default is 30000.
- */
- DrawioFile.prototype.maxAutosaveDelay = 30000;
- /**
- * Sets the delay for autosave in milliseconds. Default is 2000.
- */
- DrawioFile.prototype.autosaveThread = null;
- /**
- * Sets the delay for autosave in milliseconds. Default is 500.
- */
- DrawioFile.prototype.lastAutosave = null;
- /**
- * Sets the delay for autosave in milliseconds. Default is 1500.
- */
- DrawioFile.prototype.modified = false;
- /**
- * Specifies if the graph change listener is enabled. Default is true.
- */
- DrawioFile.prototype.changeListenerEnabled = true;
- /**
- * Sets the delay for autosave in milliseconds. Default is 1500.
- */
- DrawioFile.prototype.lastAutosaveRevision = null;
- /**
- * Sets the delay for autosave in milliseconds. Default is 1000.
- */
- DrawioFile.prototype.maxAutosaveRevisionDelay = 1800000;
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.descriptorChanged = function()
- {
- this.fireEvent(new mxEventObject('descriptorChanged'));
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.contentChanged = function()
- {
- this.fireEvent(new mxEventObject('contentChanged'));
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.save = function(revision, success, error, unloading)
- {
- this.updateFileData();
- this.clearAutosave();
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.updateFileData = function()
- {
- this.setData(this.ui.getFileData());
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.saveAs = function(filename, success, error) { };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.saveFile = function(title, revision, success, error) { };
- /**
- * Returns true if copy, export and print are not allowed for this file.
- */
- DrawioFile.prototype.isRestricted = function()
- {
- return false;
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.isModified = function()
- {
- return this.modified;
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.setModified = function(value)
- {
- this.modified = value;
- };
- /**
- * Specifies if the autosave checkbox should be shown in the document
- * properties dialog. Default is false.
- */
- DrawioFile.prototype.isAutosaveOptional = function()
- {
- return false;
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.isAutosave = function()
- {
- return this.ui.editor.autosave;
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.isRenamable = function()
- {
- return false;
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.rename = function(title, success, error) { };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.isMovable = function()
- {
- return false;
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.move = function(folderId, success, error) { };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.getHash = function()
- {
- return '';
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.getId = function()
- {
- return '';
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.isEditable = function()
- {
- return !this.ui.editor.chromeless;
- };
- /**
- * Returns the location as a new object.
- * @type mx.Point
- */
- DrawioFile.prototype.getUi = function()
- {
- return this.ui;
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DrawioFile.prototype.getTitle = function()
- {
- return '';
- };
- /**
- * Sets the location of this point.
- *
- * @param {number} x New X-coordinate of the point.
- * @param {number} y New Y-coordinate of the point.
- */
- DrawioFile.prototype.setData = function(data)
- {
- this.data = data;
- };
- /**
- * Returns the location as a new object.
- * @type mx.Point
- */
- DrawioFile.prototype.getData = function()
- {
- return this.data;
- };
- /**
- * Returns the location as a new object.
- * @type mx.Point
- */
- DrawioFile.prototype.open = function()
- {
- this.ui.setFileData(this.getData());
-
- this.changeListener = mxUtils.bind(this, function(sender, eventObject)
- {
- var edit = (eventObject != null) ? eventObject.getProperty('edit') : null;
-
- if (this.changeListenerEnabled && this.isEditable() &&
- (edit == null || !edit.ignoreEdit))
- {
- this.setModified(true);
-
- if (this.isAutosave())
- {
- this.ui.editor.setStatus(mxResources.get('saving') + '...');
-
- this.autosave(this.autosaveDelay, this.maxAutosaveDelay, mxUtils.bind(this, function(resp)
- {
- // Does not update status if another autosave was scheduled
- if (this.autosaveThread == null && this.ui.getCurrentFile() == this && !this.isModified())
- {
- this.ui.editor.setStatus(mxResources.get('allChangesSaved'));
- }
- }), mxUtils.bind(this, function(resp)
- {
- if (this.ui.getCurrentFile() == this)
- {
- this.addUnsavedStatus(resp);
- }
- }));
- }
- else
- {
- this.addUnsavedStatus();
- }
- }
- });
-
- this.ui.editor.graph.model.addListener(mxEvent.CHANGE, this.changeListener);
- // Some options trigger autosave
- this.ui.editor.graph.addListener('gridSizeChanged', this.changeListener);
- this.ui.editor.graph.addListener('shadowVisibleChanged', this.changeListener);
- this.ui.addListener('pageFormatChanged', this.changeListener);
- this.ui.addListener('pageScaleChanged', this.changeListener);
- this.ui.addListener('backgroundColorChanged', this.changeListener);
- this.ui.addListener('backgroundImageChanged', this.changeListener);
- this.ui.addListener('foldingEnabledChanged', this.changeListener);
- this.ui.addListener('mathEnabledChanged', this.changeListener);
- this.ui.addListener('gridEnabledChanged', this.changeListener);
- this.ui.addListener('guidesEnabledChanged', this.changeListener);
- this.ui.addListener('pageViewChanged', this.changeListener);
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.addUnsavedStatus = function(err)
- {
- if (err instanceof Error && err.message != null)
- {
- this.ui.editor.setStatus('<div class="geStatusAlert" style="cursor:pointer;">' +
- mxResources.get('unsavedChanges') + ' (' + err.message + ')</div>');
- }
- else
- {
- // FIXME: Handle multiple tabs
- // if (this.ui.mode == null && urlParams['splash'] == '0')
- // {
- // try
- // {
- // this.ui.updateDraft();
- // this.setModified(false);
- // }
- // catch (e)
- // {
- // // Keeps modified flag unchanged
- // }
- // }
-
- this.ui.editor.setStatus('<div class="geStatusMessage" style="cursor:pointer;">' +
- mxResources.get('unsavedChangesClickHereToSave') + '</div>');
-
- // Installs click handler for saving
- if (this.ui.statusContainer != null)
- {
- var links = this.ui.statusContainer.getElementsByTagName('div');
-
- if (links.length > 0)
- {
- mxEvent.addListener(links[0], 'click', mxUtils.bind(this, function()
- {
- this.ui.actions.get((this.ui.mode == null) ? 'saveAs' : 'save').funct();
- }));
- }
- }
- }
- };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.autosave = function(delay, maxDelay, success, error)
- {
- if (this.lastAutosave == null)
- {
- this.lastAutosave = new Date().getTime();
- }
-
- var tmp = (new Date().getTime() - this.lastAutosave < maxDelay) ? delay : 0;
- this.clearAutosave();
-
- // Starts new timer or executes immediately if not unsaved for maxDelay
- this.autosaveThread = window.setTimeout(mxUtils.bind(this, function()
- {
- this.autosaveThread = null;
- this.lastAutosave = null;
-
- // Workaround for duplicate save if UI is blocking
- // after save while pending autosave triggers
- if (this.isModified() && this.isAutosaveNow())
- {
- var rev = this.isAutosaveRevision();
-
- if (rev)
- {
- this.lastAutosaveRevision = new Date().getTime();
- }
-
- this.save(rev, mxUtils.bind(this, function(resp)
- {
- this.autosaveCompleted();
-
- if (success != null)
- {
- success(resp);
- }
- }), mxUtils.bind(this, function(resp)
- {
- if (error != null)
- {
- error(resp);
- }
- }));
- }
- else
- {
- if (success != null)
- {
- success(null);
- }
- }
- }), tmp);
- };
- /**
- * Returns true if an autosave is required at the time of execution.
- * This implementation returns true.
- */
- DrawioFile.prototype.isAutosaveNow = function()
- {
- return true;
- };
- /**
- * Hooks for subclassers after the autosave has completed.
- */
- DrawioFile.prototype.autosaveCompleted = function() { };
- /**
- * Adds the listener for automatically saving the diagram for local changes.
- */
- DrawioFile.prototype.clearAutosave = function()
- {
- if (this.autosaveThread != null)
- {
- window.clearTimeout(this.autosaveThread);
- this.autosaveThread = null;
- }
- };
- /**
- * Returns the location as a new object.
- * @type mx.Point
- */
- DrawioFile.prototype.isAutosaveRevision = function()
- {
- var now = new Date().getTime();
-
- return (this.lastAutosaveRevision == null) || (now - this.lastAutosaveRevision) > this.maxAutosaveRevisionDelay;
- };
- /**
- * Returns the location as a new object.
- */
- DrawioFile.prototype.close = function(unloading)
- {
- if (this.isAutosave() && this.isModified())
- {
- this.save(this.isAutosaveRevision(), null, null, unloading);
- }
-
- this.destroy();
- };
- /**
- * Returns the location as a new object.
- */
- DrawioFile.prototype.hasSameExtension = function(title, newTitle)
- {
- if (title != null && newTitle != null)
- {
- var dot = title.lastIndexOf('.');
- var ext = (dot > 0) ? title.substring(dot) : '';
- dot = newTitle.lastIndexOf('.');
- return ext === ((dot > 0) ? newTitle.substring(dot) : '');
- }
-
- return title == newTitle;
- };
- /**
- * Stops any pending autosaves and removes all listeners.
- */
- DrawioFile.prototype.destroy = function()
- {
- this.clearAutosave();
-
- if (this.changeListener != null)
- {
- this.ui.editor.graph.model.removeListener(this.changeListener);
- this.ui.editor.graph.removeListener(this.changeListener);
- this.ui.removeListener(this.changeListener);
- this.changeListener = null;
- }
- };
|