@zklogic/draw.io
Version:
Draw.IO - Graph Editor integration in Angular-cli
1 lines • 11.3 kB
JavaScript
!function(){var t=null;window.DropboxClient=function(t){DrawioClient.call(this,t,"dbauth"),this.client=new Dropbox({clientId:this.clientId})},mxUtils.extend(DropboxClient,DrawioClient),DropboxClient.prototype.appPath="/drawio/",DropboxClient.prototype.extension=".drawio",DropboxClient.prototype.writingFile=!1,DropboxClient.prototype.maxRetries=4,DropboxClient.prototype.clientId=window.DRAWIO_DROPBOX_ID,DropboxClient.prototype.redirectUri=window.location.protocol+"//"+window.location.host+"/dropbox",DropboxClient.prototype.logout=function(){this.ui.editor.loadUrl(this.redirectUri+"?doLogout=1&state="+encodeURIComponent("cId="+this.clientId+"&domain="+window.location.hostname)),this.clearPersistentToken(),this.setUser(null),t=null,this.client.authTokenRevoke().then(mxUtils.bind(this,(function(){this.client.setAccessToken(null)})))},DropboxClient.prototype.updateUser=function(i,e,n){var s=!0,o=window.setTimeout(mxUtils.bind(this,(function(){s=!1,e({code:App.ERROR_TIMEOUT})})),this.ui.timeout),l=this.client.usersGetCurrentAccount();l.then(mxUtils.bind(this,(function(t){window.clearTimeout(o),s&&(this.setUser(new DrawioUser(t.account_id,t.email,t.name.display_name)),i())}))),l.catch(mxUtils.bind(this,(function(l){window.clearTimeout(o),s&&(null==l||401!==l.status||n?e({message:mxResources.get("accessDenied")}):(this.setUser(null),this.client.setAccessToken(null),t=null,this.authenticate(mxUtils.bind(this,(function(){this.updateUser(i,e,!0)})),e)))})))},DropboxClient.prototype.authenticate=function(t,i){new mxXmlRequest(this.redirectUri+"?getState=1",null,"GET").send(mxUtils.bind(this,(function(e){e.getStatus()>=200&&e.getStatus()<=299?this.authenticateStep2(e.getText(),t,i):null!=i&&i(e)})),i)},DropboxClient.prototype.authenticateStep2=function(i,e,n){if(null==window.onDropboxCallback){var s=mxUtils.bind(this,(function(){var o=!0;null!=this.getPersistentToken(!0)?new mxXmlRequest(this.redirectUri+"?state="+encodeURIComponent("cId="+this.clientId+"&domain="+window.location.hostname+"&token="+i),null,"GET").send(mxUtils.bind(this,(function(i){i.getStatus()>=200&&i.getStatus()<=299?(t=JSON.parse(i.getText()).access_token,this.client.setAccessToken(t),this.setUser(null),e()):(this.clearPersistentToken(),this.setUser(null),t=null,this.client.setAccessToken(null),401==i.getStatus()?s():n({message:mxResources.get("accessDenied"),retry:s}))})),n):this.ui.showAuthDialog(this,!0,mxUtils.bind(this,(function(l,r){null!=window.open("https://www.dropbox.com/oauth2/authorize?client_id="+this.clientId+(l?"&token_access_type=offline":"")+"&redirect_uri="+encodeURIComponent(this.redirectUri)+"&response_type=code&state="+encodeURIComponent("cId="+this.clientId+"&domain="+window.location.hostname+"&token="+i),"dbauth")?window.onDropboxCallback=mxUtils.bind(this,(function(i,u){if(o){window.onDropboxCallback=null,o=!1;try{null==i?n({message:mxResources.get("accessDenied"),retry:s}):(null!=r&&r(),t=i.access_token,this.client.setAccessToken(t),this.setUser(null),l&&this.setPersistentToken("remembered"),e())}catch(t){n(t)}finally{null!=u&&u.close()}}else null!=u&&u.close()})):n({message:mxResources.get("serviceUnavailableOrBlocked"),retry:s})})),mxUtils.bind(this,(function(){o&&(window.onDropboxCallback=null,o=!1,n({message:mxResources.get("accessDenied"),retry:s}))})))}));s()}else n({code:App.ERROR_BUSY})},DropboxClient.prototype.executePromise=function(i,e,n){var s=mxUtils.bind(this,(function(l){var r=!0,u=window.setTimeout(mxUtils.bind(this,(function(){r=!1,n({code:App.ERROR_TIMEOUT,retry:o})})),this.ui.timeout),a=i();a.then(mxUtils.bind(this,(function(t){window.clearTimeout(u),r&&null!=e&&e(t)}))),a.catch(mxUtils.bind(this,(function(i){window.clearTimeout(u),r&&(null==i||500!=i.status&&400!=i.status&&401!=i.status?n({message:mxResources.get("error")+" "+i.status}):(this.setUser(null),this.client.setAccessToken(null),t=null,l?n({message:mxResources.get("accessDenied"),retry:mxUtils.bind(this,(function(){this.authenticate((function(){o(!0)}),n)}))}):this.authenticate((function(){s(!0)}),n)))})))})),o=mxUtils.bind(this,(function(t){null==this.user?this.updateUser((function(){o(!0)}),n,t):s(t)}));null==t?this.authenticate((function(){o(!0)}),n):o(!1)},DropboxClient.prototype.getLibrary=function(t,i,e){this.getFile(t,i,e,!0)},DropboxClient.prototype.getFile=function(i,e,n,s){s=null!=s&&s;var o=/\.png$/i.test(i);if(/^https:\/\//i.test(i)||/\.v(dx|sdx?)$/i.test(i)||/\.gliffy$/i.test(i)||/\.pdf$/i.test(i)||!this.ui.useCanvasForExport&&o){var l=mxUtils.bind(this,(function(){var t=i.split("/"),s=t.length>0?t[t.length-1]:i;this.ui.convertFile(i,s,null,this.extension,e,n)}));null!=t?l():this.authenticate(l,n)}else{var r={path:"/"+i};null!=urlParams.rev&&(r.rev=urlParams.rev),this.readFile(r,mxUtils.bind(this,(function(t,n){var l=null;if((o?t.lastIndexOf(","):-1)>0){var r=this.ui.extractGraphModelFromPng(t);null!=r&&r.length>0?t=r:l=new LocalFile(this,t,i,!0)}e(null!=l?l:s?new DropboxLibrary(this.ui,t,n):new DropboxFile(this.ui,t,n))})),n,o)}},DropboxClient.prototype.readFile=function(i,e,n,s){var o=mxUtils.bind(this,(function(r){var u=!0,a=window.setTimeout(mxUtils.bind(this,(function(){u=!1,n({code:App.ERROR_TIMEOUT})})),this.ui.timeout),c=this.client.filesGetMetadata({path:"/"+i.path.substring(1),include_deleted:!1});c.then(mxUtils.bind(this,(function(t){}))),c.catch((function(t){window.clearTimeout(a),u&&null!=t&&409==t.status&&(u=!1,n({message:mxResources.get("fileNotFound")}))}));var h=this.client.filesDownload(i);h.then(mxUtils.bind(this,(function(t){if(window.clearTimeout(a),u){u=!1;try{var i=new FileReader;i.onload=mxUtils.bind(this,(function(n){e(i.result,t)})),s?i.readAsDataURL(t.fileBlob):i.readAsText(t.fileBlob)}catch(t){n(t)}}}))),h.catch(mxUtils.bind(this,(function(i){window.clearTimeout(a),u&&(u=!1,null==i||500!=i.status&&400!=i.status&&401!=i.status?n({message:mxResources.get("error")+" "+i.status}):(this.client.setAccessToken(null),this.setUser(null),t=null,r?n({message:mxResources.get("accessDenied"),retry:mxUtils.bind(this,(function(){this.authenticate((function(){l(!0)}),n)}))}):this.authenticate((function(){o(!0)}),n)))})))})),l=mxUtils.bind(this,(function(t){null==this.user?this.updateUser((function(){l(!0)}),n,t):o(t)}));null==t?this.authenticate((function(){l(!0)}),n):l(!1)},DropboxClient.prototype.checkExists=function(t,i,e){var n=mxUtils.bind(this,(function(){return this.client.filesGetMetadata({path:"/"+t.toLowerCase(),include_deleted:!1})}));this.executePromise(n,mxUtils.bind(this,(function(n){e?i(!1,!0,n):this.ui.confirm(mxResources.get("replaceIt",[t]),(function(){i(!0,!0,n)}),(function(){i(!1,!0,n)}))})),(function(t){i(!0,!1)}))},DropboxClient.prototype.renameFile=function(t,i,e,n){if(/[\\\/:\?\*"\|]/.test(i))n({message:mxResources.get("dropboxCharsNotAllowed")});else{if(null!=t&&null!=i){var s=t.stat.path_display.substring(1),o=s.lastIndexOf("/");o>0&&(i=s.substring(0,o+1)+i)}null!=t&&null!=i&&t.stat.path_lower.substring(1)!==i.toLowerCase()?this.checkExists(i,mxUtils.bind(this,(function(s,o,l){if(s){var r=mxUtils.bind(this,(function(s){var o=mxUtils.bind(this,(function(){return this.client.filesMove({from_path:t.stat.path_display,to_path:"/"+i,autorename:!1})}));this.executePromise(o,e,n)}));if(o&&l.path_lower.substring(1)!==i.toLowerCase()){var u=mxUtils.bind(this,(function(){return this.client.filesDelete({path:"/"+i.toLowerCase()})}));this.executePromise(u,r,n)}else r()}else n()}))):n({message:mxResources.get("invalidName")})}},DropboxClient.prototype.insertLibrary=function(t,i,e,n){this.insertFile(t,i,e,n,!0)},DropboxClient.prototype.insertFile=function(t,i,e,n,s){s=null!=s&&s,this.checkExists(t,mxUtils.bind(this,(function(o){o?this.saveFile(t,i,mxUtils.bind(this,(function(t){e(s?new DropboxLibrary(this.ui,i,t):new DropboxFile(this.ui,i,t))})),n):n()})))},DropboxClient.prototype.saveFile=function(t,i,e,n,s){if(/[\\\/:\?\*"\|]/.test(t))n({message:mxResources.get("dropboxCharsNotAllowed")});else if(i.length>=15e7)n({message:mxResources.get("drawingTooLarge")+" ("+this.ui.formatFileSize(i.length)+" / 150 MB)"});else{s=null!=s?s:"";var o=mxUtils.bind(this,(function(){return this.client.filesUpload({path:"/"+s+t,mode:{".tag":"overwrite"},mute:!0,contents:new Blob([i],{type:"text/plain"})})}));this.executePromise(o,e,n)}},DropboxClient.prototype.pickLibrary=function(t){Dropbox.choose({linkType:"direct",cancel:mxUtils.bind(this,(function(){})),success:mxUtils.bind(this,(function(i){if(this.ui.spinner.spin(document.body,mxResources.get("loading"))){var e=mxUtils.bind(this,(function(t){this.ui.spinner.stop(),this.ui.handleError(t)})),n=i[0].link.indexOf(this.appPath);if(n>0){var s=decodeURIComponent(i[0].link.substring(n+this.appPath.length-1));this.readFile({path:s},mxUtils.bind(this,(function(n,o){if(null!=o&&o.id==i[0].id)try{this.ui.spinner.stop(),t(s.substring(1),new DropboxLibrary(this.ui,n,o))}catch(t){this.ui.handleError(t)}else this.createLibrary(i[0],t,e)})),e)}else this.createLibrary(i[0],t,e)}}))})},DropboxClient.prototype.createLibrary=function(t,i,e){this.ui.confirm(mxResources.get("note")+": "+mxResources.get("fileWillBeSavedInAppFolder",[t.name]),mxUtils.bind(this,(function(){this.ui.editor.loadUrl(t.link,mxUtils.bind(this,(function(n){this.insertFile(t.name,n,mxUtils.bind(this,(function(t){try{this.ui.spinner.stop(),i(t.getHash().substring(1),t)}catch(t){e(t)}})),e,!0)})),e)})),mxUtils.bind(this,(function(){this.ui.spinner.stop()})))},DropboxClient.prototype.pickFile=function(t,i){null!=Dropbox.choose?(t=null!=t?t:mxUtils.bind(this,(function(t,i){this.ui.loadFile(null!=t?"D"+encodeURIComponent(t):i.getHash(),null,i)})),Dropbox.choose({linkType:"direct",cancel:mxUtils.bind(this,(function(){})),success:mxUtils.bind(this,(function(e){if(this.ui.spinner.spin(document.body,mxResources.get("loading")))if(i)this.ui.spinner.stop(),t(e[0].link);else{var n=mxUtils.bind(this,(function(t){this.ui.spinner.stop(),this.ui.handleError(t)})),s=mxUtils.bind(this,(function(i,e){this.ui.spinner.stop(),t(i,e)})),o=/\.png$/i.test(e[0].name);if(/\.vsdx$/i.test(e[0].name)||/\.gliffy$/i.test(e[0].name)||!this.ui.useCanvasForExport&&o)s(e[0].link);else{var l=e[0].link.indexOf(this.appPath);if(l>0){var r=decodeURIComponent(e[0].link.substring(l+this.appPath.length-1));this.readFile({path:r},mxUtils.bind(this,(function(i,l){if(null!=l&&l.id==e[0].id){var u=o?i.lastIndexOf(","):-1;this.ui.spinner.stop();var a=null;if(u>0){var c=this.ui.extractGraphModelFromPng(i);null!=c&&c.length>0?i=c:a=new LocalFile(this,i,r,!0)}t(r.substring(1),null!=a?a:new DropboxFile(this.ui,i,l))}else this.createFile(e[0],s,n)})),n,o)}else this.createFile(e[0],s,n)}}}))})):this.ui.handleError({message:mxResources.get("serviceUnavailableOrBlocked")})},DropboxClient.prototype.createFile=function(t,i,e){var n=/(\.png)$/i.test(t.name);this.ui.editor.loadUrl(t.link,mxUtils.bind(this,(function(s){null!=s&&s.length>0?this.ui.confirm(mxResources.get("note")+": "+mxResources.get("fileWillBeSavedInAppFolder",[t.name]),mxUtils.bind(this,(function(){var o=n?s.lastIndexOf(","):-1;if(o>0){var l=this.ui.extractGraphModelFromPng(s.substring(o+1));null!=l&&l.length>0&&(s=l)}this.insertFile(t.name,s,mxUtils.bind(this,(function(e){i(t.name,e)})),e)})),mxUtils.bind(this,(function(){this.ui.spinner.stop()}))):(this.ui.spinner.stop(),e({message:mxResources.get("errorLoadingFile")}))})),e,n)}}();