123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747 |
- /**
- * Copyright (c) 2006-2017, JGraph Ltd
- * Copyright (c) 2006-2017, Gaudenz Alder
- */
- DropboxClient = function(editorUi)
- {
- mxEventSource.call(this);
-
- /**
- * Holds the x-coordinate of the point.
- * @type number
- * @default 0
- */
- this.ui = editorUi;
- /**
- * Holds the x-coordinate of the point.
- * @type number
- * @default 0
- */
- this.client = new Dropbox.Client(
- {
- key: App.DROPBOX_APPKEY,
- sandbox: true
- });
-
- /**
- * Holds the x-coordinate of the point.
- * @type number
- * @default 0
- */
- this.client.authDriver(new Dropbox.AuthDriver.Popup(
- {
- rememberUser: true,
- receiverUrl: 'https://' + window.location.host + '/dropbox.html'
- }));
- };
- // Extends mxEventSource
- mxUtils.extend(DropboxClient, mxEventSource);
- /**
- * FIXME: How to find name of app folder for current user. The Apps part of the
- * name is internationalized so this hardcoded check does not work everywhere.
- */
- DropboxClient.prototype.appPath = '/drawio/';
- /**
- * Executes the first step for connecting to Google Drive.
- */
- DropboxClient.prototype.extension = '.html';
- /**
- * Executes the first step for connecting to Google Drive.
- */
- DriveClient.prototype.maxRetries = 4;
- /**
- * Executes the first step for connecting to Google Drive.
- */
- DropboxClient.prototype.user = null;
- /**
- * Executes the first step for connecting to Google Drive.
- */
- DropboxClient.prototype.writingFile = false;
- /**
- * Authorizes the client, gets the userId and calls <open>.
- */
- DropboxClient.prototype.logout = function()
- {
- this.client.signOut(mxUtils.bind(this, function()
- {
- this.setUser(null);
- }));
- };
- /**
- * Authorizes the client, gets the userId and calls <open>.
- */
- DropboxClient.prototype.setUser = function(user)
- {
- this.user = user;
- this.fireEvent(new mxEventObject('userChanged'));
- };
- /**
- * Authorizes the client, gets the userId and calls <open>.
- */
- DropboxClient.prototype.getUser = function()
- {
- return this.user;
- };
- /**
- * Checks if the client is authorized and calls the next step.
- */
- DropboxClient.prototype.updateUser = function(success, error, remember)
- {
- this.client.getUserInfo(null, mxUtils.bind(this, function(error, info)
- {
- if (error == null)
- {
- this.setUser(new DrawioUser(info.uid, info.email, info.name));
- }
- else
- {
- this.setUser(null);
- }
- }));
- };
- /**
- * Authorizes the client, gets the userId and calls <open>.
- */
- DropboxClient.prototype.execute = function(fn)
- {
- if (this.client.isAuthenticated())
- {
- fn();
- }
- else
- {
- this.authorize(false, mxUtils.bind(this, function(error, client)
- {
- if (error != null)
- {
- this.ui.handleError(error);
- }
- else
- {
- if (this.client.isAuthenticated())
- {
- this.updateUser();
- fn();
- }
- else
- {
- this.ui.showAuthDialog(this, false, mxUtils.bind(this, function(remember, success)
- {
- this.authorize(true, mxUtils.bind(this, function(error2, client)
- {
- if (error2 != null)
- {
- this.ui.handleError(error2);
- }
- else if (this.client.isAuthenticated())
- {
- this.updateUser();
-
- if (success != null)
- {
- success();
- }
- fn();
- }
- }));
- }));
- }
- }
- }));
- }
- };
- /**
- * Authorizes the client, gets the userId and calls <open>.
- */
- DropboxClient.prototype.authorize = function(interactive, fn)
- {
- this.client.authenticate({interactive: interactive}, mxUtils.bind(this, function(error, client)
- {
- if (error != null)
- {
- if (window.console != null)
- {
- console.log(error);
- }
- }
- else
- {
- fn();
- }
- }));
- };
- /**
- * Checks if the client is authorized and calls the next step.
- */
- DropboxClient.prototype.getLibrary = function(path, success, error)
- {
- this.getFile(path, success, error, false, true);
- };
- /**
- * DenyConvert is ignored in this client, just added for API compatibility.
- */
- DropboxClient.prototype.getFile = function(path, success, error, denyConvert, asLibrary)
- {
- asLibrary = (asLibrary != null) ? asLibrary : false;
-
- var fn = mxUtils.bind(this, function()
- {
- this.execute(mxUtils.bind(this, function()
- {
- var acceptResponse = true;
-
- var timeoutThread = window.setTimeout(mxUtils.bind(this, function()
- {
- acceptResponse = false;
- error({code: App.ERROR_TIMEOUT, retry: fn});
- }), this.ui.timeout);
-
- var options = null;
-
- if (urlParams['rev'] != null)
- {
- options = {versionTag: urlParams['rev']};
- }
-
- this.client.readFile('/' + path, options, mxUtils.bind(this, function(err, data, stat)
- {
- try
- {
- window.clearTimeout(timeoutThread);
-
- if (acceptResponse)
- {
- if (err != null)
- {
- error(err)
- }
- else
- {
- if (asLibrary)
- {
- success(new DropboxLibrary(this.ui, data, stat));
- }
- else
- {
- success(new DropboxFile(this.ui, data, stat));
- }
- }
- }
- }
- catch (e)
- {
- error(e);
- }
- }));
- }));
- });
-
- fn();
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DropboxClient.prototype.checkExists = function(filename, fn)
- {
- this.client.stat(filename, mxUtils.bind(this, function(err, stat)
- {
- if ((err != null && err.status == 404) || (stat != null && stat.isRemoved))
- {
- fn(true);
- }
- else
- {
- this.ui.confirm(mxResources.get('replaceIt', [filename]), function()
- {
- fn(true);
- }, function()
- {
- fn(false);
- });
- }
- }));
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DropboxClient.prototype.renameFile = function(file, filename, success, error)
- {
- if (file != null && filename != null)
- {
- // Checks if file exists
- this.execute(mxUtils.bind(this, function()
- {
- this.checkExists(filename, mxUtils.bind(this, function(checked)
- {
- if (checked)
- {
- // Uses write and remove because move does not allow overwriting an existing target
- this.writeFile(filename, file.getData(), mxUtils.bind(this, function(stat)
- {
- this.client.remove(file.getTitle(), function(err2, stat2)
- {
- if (err2 != null)
- {
- error(err2)
- }
- else
- {
- success(stat);
- }
- });
- }), error);
- }
- else
- {
- error();
- }
- }));
- }));
- }
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DropboxClient.prototype.insertLibrary = function(filename, data, success, error)
- {
- this.insertFile(filename, data, success, error, true);
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DropboxClient.prototype.insertFile = function(filename, data, success, error, asLibrary)
- {
- asLibrary = (asLibrary != null) ? asLibrary : false;
-
- this.execute(mxUtils.bind(this, function()
- {
- this.checkExists(filename, mxUtils.bind(this, function(checked)
- {
- if (checked)
- {
- this.writeFile(filename, data, mxUtils.bind(this, function(stat)
- {
- if (asLibrary)
- {
- success(new DropboxLibrary(this.ui, data, stat));
- }
- else
- {
- success(new DropboxFile(this.ui, data, stat));
- }
- }), error);
- }
- else
- {
- error();
- }
- }));
- }));
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DropboxClient.prototype.saveFile = function(filename, data, success, error)
- {
- this.execute(mxUtils.bind(this, function()
- {
- this.writeFile(filename, data, 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.
- */
- DropboxClient.prototype.writeFile = function(filename, data, success, error)
- {
- if (/[\\\/:\?\*"\|]/.test(filename))
- {
- if (error != null)
- {
- error({message: mxResources.get('dropboxCharsNotAllowed')});
- }
- }
- else if (!this.writingFile)
- {
- var acceptResponse = true;
- var timeoutThread = null;
- this.writingFile = true;
- var retryCount = 0;
-
- // Cancels any pending requests
- if (this.requestThread != null)
- {
- window.clearTimeout(this.requestThread);
- }
-
- var fn = mxUtils.bind(this, function()
- {
- if (timeoutThread != null)
- {
- window.clearTimeout(timeoutThread);
- }
-
- timeoutThread = window.setTimeout(mxUtils.bind(this, function()
- {
- this.writingFile = false;
- acceptResponse = false;
-
- if (error != null)
- {
- error({code: App.ERROR_TIMEOUT, retry: fn});
- }
- }), this.ui.timeout);
-
- this.client.writeFile(filename, data, mxUtils.bind(this, function(err, stat)
- {
- window.clearTimeout(timeoutThread);
-
- if (acceptResponse)
- {
- if (err != null)
- {
- if (retryCount < this.maxRetries)
- {
- retryCount++;
- var jitter = 1 + 0.1 * (Math.random() - 0.5);
- this.requestThread = window.setTimeout(fn, Math.round(Math.pow(2, retryCount) * jitter * 1000));
- }
- else if (error != null)
- {
- this.writingFile = false;
- error(err);
- }
- }
- else
- {
- this.writingFile = false;
-
- if (success != null)
- {
- success(stat);
- }
- }
- }
- }));
- });
-
- fn();
- }
- else if (error != null)
- {
- error({code: App.ERROR_BUSY});
- }
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DropboxClient.prototype.pickLibrary = function(fn)
- {
- // Authentication will be carried out on open to make sure the
- // autosave does not show an auth dialog. Showing it here will
- // block the second dialog (the file picker) so it's too early.
- Dropbox.choose(
- {
- linkType : 'direct',
- cancel: mxUtils.bind(this, function()
- {
- // do nothing
- }),
- success : mxUtils.bind(this, function(files)
- {
- if (this.ui.spinner.spin(document.body, mxResources.get('loading')))
- {
- var error = mxUtils.bind(this, function(e)
- {
- this.ui.spinner.stop();
- this.ui.handleError(e);
- });
-
- var tmp = files[0].link.indexOf(this.appPath);
-
- if (tmp > 0)
- {
- // Checks if file is in app folder by loading file from there and comparing relative path and size
- // KNOWN: This check fails if a file is inside a drawio directory with same relative path and size
- this.execute(mxUtils.bind(this, function()
- {
- var rel = decodeURIComponent(files[0].link.substring(tmp + this.appPath.length - 1));
-
- this.client.readFile(rel, null, mxUtils.bind(this, function(err, data, stat)
- {
- if (stat != null && parseInt(files[0].bytes) === parseInt(stat.size) && rel === stat.path)
- {
- // No need to load file a second time
- try
- {
- this.ui.spinner.stop();
- fn(rel.substring(1), new DropboxLibrary(this.ui, data, stat));
- }
- catch (e)
- {
- this.ui.handleError(e);
- }
- }
- else
- {
- this.createLibrary(files[0], fn, error);
- }
- }));
- }));
- }
- else
- {
- this.execute(mxUtils.bind(this, function()
- {
- this.createLibrary(files[0], fn, error);
- }));
- }
- }
- })
- });
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DropboxClient.prototype.createLibrary = function(file, success, error)
- {
- this.ui.confirm(mxResources.get('note') + ': ' + mxResources.get('fileWillBeSavedInAppFolder', [file.name]), mxUtils.bind(this, function()
- {
- this.ui.loadUrl(file.link, mxUtils.bind(this, function(data)
- {
- this.insertFile(file.name, data, mxUtils.bind(this, function(newFile)
- {
- try
- {
- this.ui.spinner.stop();
- success(newFile.getHash().substring(1), newFile);
- }
- catch (e)
- {
- error(e);
- }
- }), error, true);
- }), error);
- }), mxUtils.bind(this, function()
- {
- this.ui.spinner.stop();
- }));
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DropboxClient.prototype.pickFile = function(fn, readOnly)
- {
- if (Dropbox.choose != null)
- {
- fn = (fn != null) ? fn : mxUtils.bind(this, function(path, file)
- {
- this.ui.loadFile('D' + encodeURIComponent(path), null, file);
- });
-
- // Authentication will be carried out on open to make sure the
- // autosave does not show an auth dialog. Showing it here will
- // block the second dialog (the file picker) so it's too early.
- Dropbox.choose(
- {
- linkType : 'direct',
- cancel: mxUtils.bind(this, function()
- {
- // do nothing
- }),
- success : mxUtils.bind(this, function(files)
- {
- if (this.ui.spinner.spin(document.body, mxResources.get('loading')))
- {
- // File used for read-only
- if (readOnly)
- {
- this.ui.spinner.stop();
- fn(files[0].link);
- }
- else
- {
- var error = mxUtils.bind(this, function(e)
- {
- this.ui.spinner.stop();
- this.ui.handleError(e);
- });
-
- var success = mxUtils.bind(this, function(path, file)
- {
- this.ui.spinner.stop();
- fn(path, file);
- });
-
- var tmp = files[0].link.indexOf(this.appPath);
-
- if (tmp > 0 && !/(\.png)$/i.test(files[0].name) && !/(\.vs?dx)$/i.test(files[0].name) && !/(\.gliffy)$/i.test(files[0].name))
- {
- // Checks if file is in app folder by loading file from there and comparing relative path and size
- // KNOWN: This check fails if a file is inside a drawio directory with same relative path and size
- this.execute(mxUtils.bind(this, function()
- {
- var rel = decodeURIComponent(files[0].link.substring(tmp + this.appPath.length - 1));
-
- this.client.readFile(rel, null, mxUtils.bind(this, function(err, data, stat)
- {
- if (stat != null && parseInt(files[0].bytes) === parseInt(stat.size) && rel === stat.path)
- {
- this.ui.spinner.stop();
-
- // No need to load file a second time
- fn(rel.substring(1), new DropboxFile(this.ui, data, stat));
- }
- else
- {
- this.createFile(files[0], success, error);
- }
- }));
- }));
- }
- else
- {
- this.execute(mxUtils.bind(this, function()
- {
- this.createFile(files[0], success, error);
- }));
- }
- }
- }
- })
- });
- }
- else
- {
- this.ui.handleError({message: mxResources.get('serviceUnavailableOrBlocked')});
- }
- };
- /**
- * Translates this point by the given vector.
- *
- * @param {number} dx X-coordinate of the translation.
- * @param {number} dy Y-coordinate of the translation.
- */
- DropboxClient.prototype.createFile = function(file, success, error)
- {
- var name = file.name;
-
- if (/(\.png)$/i.test(name) || /(\.vs?dx)$/i.test(name) || /(\.gliffy)$/i.test(name))
- {
- name = name.substring(0, name.lastIndexOf('.')) + this.extension;
- }
-
- var doInsert = mxUtils.bind(this, function(filename, data)
- {
- this.ui.confirm(mxResources.get('note') + ': ' + mxResources.get('fileWillBeSavedInAppFolder', [filename]), mxUtils.bind(this, function()
- {
- this.insertFile(filename, data, mxUtils.bind(this, function(newFile)
- {
- success(filename, newFile);
- }), error);
- }), mxUtils.bind(this, function()
- {
- this.ui.spinner.stop();
- }));
- });
-
- this.ui.loadUrl(file.link, mxUtils.bind(this, function(data)
- {
- if (/(\.vs?dx)$/i.test(file.name) || /(\.gliffy)$/i.test(file.name))
- {
- this.ui.parseFile(new Blob([data], {type: 'application/octet-stream'}), mxUtils.bind(this, function(xhr)
- {
- if (xhr.readyState == 4)
- {
- if (xhr.status >= 200 && xhr.status <= 299 && xhr.responseText.substring(0, 13) == '<mxGraphModel')
- {
- doInsert(name, xhr.responseText);
- }
- else
- {
- this.ui.spinner.stop();
-
- if (error != null)
- {
- error({message: mxResources.get('errorLoadingFile')});
- }
- }
- }
- }), file.name);
- }
- else
- {
- if (/(\.png)$/i.test(file.name))
- {
- data = this.ui.extractGraphModelFromPng(data);
- }
-
- if (data != null && data.length > 0)
- {
- doInsert(name, data);
- }
- else
- {
- this.ui.spinner.stop();
-
- if (error != null)
- {
- error({message: mxResources.get('errorLoadingFile')});
- }
- }
- }
- }), error, /(\.png)$/i.test(file.name));
- };
|