ngx-doc-viewer2
Version:
Angular document viewer.
16 lines (14 loc) • 9.56 kB
JavaScript
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@angular/core"),require("@angular/platform-browser"),require("rxjs/operators"),require("rxjs"),require("@angular/common")):"function"==typeof define&&define.amd?define("ngx-doc-viewer2",["exports","@angular/core","@angular/platform-browser","rxjs/operators","rxjs","@angular/common"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self)["ngx-doc-viewer2"]={},e.ng.core,e.ng.platformBrowser,e.rxjs.operators,e.rxjs,e.ng.common)}(this,(function(e,r,t,n,i,o){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */function l(e,r,t,n){return new(t||(t=Promise))((function(i,o){function l(e){try{a(n.next(e))}catch(e){o(e)}}function s(e){try{a(n.throw(e))}catch(e){o(e)}}function a(e){var r;e.done?i(e.value):(r=e.value,r instanceof t?r:new t((function(e){e(r)}))).then(l,s)}a((n=n.apply(e,r||[])).next())}))}function s(e,r){var t,n,i,o,l={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(t)throw new TypeError("Generator is already executing.");for(;l;)try{if(t=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return l.label++,{value:o[1],done:!1};case 5:l.label++,n=o[1],o=[0];continue;case 7:o=l.ops.pop(),l.trys.pop();continue;default:if(!(i=l.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){l=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){l.label=o[1];break}if(6===o[0]&&l.label<i[1]){l.label=i[1],i=o;break}if(i&&l.label<i[2]){l.label=i[2],l.ops.push(o);break}i[2]&&l.ops.pop(),l.trys.pop();continue}o=r.call(e,l)}catch(e){o=[6,e],n=0}finally{t=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}}Object.create;Object.create;var a=function(){function e(e,t){this.domSanitizer=e,this.ngZone=t,this.loaded=new r.EventEmitter,this.url="",this.queryParams="",this.viewerUrl="",this.googleCheckInterval=3e3,this.disableContent="none",this.googleCheckContentLoaded=!0,this.fullUrl=null,this.externalViewer=!1,this.docHtml="",this.configuredViewer="google",this.checkIFrameSubscription=null}return e.prototype.ngOnDestroy=function(){this.checkIFrameSubscription&&this.checkIFrameSubscription.unsubscribe()},e.prototype.ngOnChanges=function(e){return l(this,void 0,void 0,(function(){var r,t,o,l,a=this;return s(this,(function(s){switch(s.label){case 0:if(e&&e.viewer&&(e.viewer.isFirstChange||e.viewer.currentValue!==e.viewer.previousValue)&&("google"!==this.viewer&&"office"!==this.viewer&&"mammoth"!==this.viewer&&"pdf"!==this.viewer&&"url"!==this.viewer&&console.error("Unsupported viewer: '"+this.viewer+"'. Supported viewers: google, office, mammoth and pdf"),"mammoth"===this.viewer&&null===mammoth&&console.error("please install mammoth when using local viewer"),this.configuredViewer=this.viewer),"none"!==this.disableContent&&this.viewer,!(e.url&&e.url.currentValue!==e.url.previousValue||e.viewer&&e.viewer.currentValue!==e.viewer.previousValue||e.viewerUrl&&e.viewerUrl.currentValue!==e.viewerUrl.previousValue))return[3,4];switch(e.viewerUrl||(this.viewerUrl=null),this.configuredViewer){case"google":this.viewerUrl="https://docs.google.com/gview?url=%URL%&embedded=true";break;case"office":this.viewerUrl="https://view.officeapps.live.com/op/embed.aspx?src=%URL%";break;case"pdf":this.viewerUrl=null}return this.docHtml="",this.externalViewer="google"===this.configuredViewer||"office"===this.configuredViewer||"url"===this.configuredViewer,this.checkIFrameSubscription&&this.checkIFrameSubscription.unsubscribe(),this.url?[3,1]:(this.fullUrl=null,[3,4]);case 1:return"office"!==this.configuredViewer&&"google"!==this.configuredViewer&&"pdf"!==this.configuredViewer&&"url"!==this.configuredViewer?[3,2]:(r=this.url.indexOf("/")?encodeURIComponent(this.url):this.url,t=this.viewerUrl?this.viewerUrl.replace("%URL%",r):this.url,this.queryParams&&"pdf"!==this.configuredViewer&&(o=this.queryParams.startsWith("&")?"":"&",t=""+t+o+this.queryParams),this.fullUrl=this.domSanitizer.bypassSecurityTrustResourceUrl(t),"google"===this.configuredViewer&&this.googleCheckContentLoaded&&this.ngZone.runOutsideAngular((function(){console.log("NGDOC:>>outside"),a.checkIFrameSubscription=i.timer(500,a.googleCheckInterval).pipe(n.take(Math.round(0===a.googleCheckInterval?0:2e4/a.googleCheckInterval))).subscribe((function(){var e,r;console.log("NGDOC:>>checking again");var t=null===(r=null===(e=a.iframes)||void 0===e?void 0:e.first)||void 0===r?void 0:r.nativeElement;a.reloadIFrame(t)}))})),[3,4]);case 2:return"mammoth"!==this.configuredViewer?[3,4]:(mammoth||console.error("Please install mammoth and make sure mammoth.browser.min.js is loaded."),l=this,[4,this.getDocxToHtml(this.url)]);case 3:l.docHtml=s.sent(),s.label=4;case 4:return[2]}}))}))},e.prototype.iframeLoaded=function(){var e,r;(this.loaded.emit(null),this.checkIFrameSubscription)&&((null===(r=null===(e=this.iframes)||void 0===e?void 0:e.first)||void 0===r?void 0:r.nativeElement)&&(this.loaded.emit(!0),this.checkIFrameSubscription.unsubscribe()))},e.prototype.reloadIFrame=function(e){if(e){var r=e.src;e.src=e.src,setTimeout((function(){e.src=r}),100)}},e.prototype.getDocxToHtml=function(e){return l(this,void 0,void 0,(function(){var r;return s(this,(function(t){switch(t.label){case 0:return[4,this.fileToArray(e)];case 1:return r=t.sent(),[4,mammoth.convertToHtml({arrayBuffer:r})];case 2:return[2,t.sent().value]}}))}))},e.prototype.fileToArray=function(e){return new Promise((function(r,t){try{var n=new XMLHttpRequest;n.open("GET",e,!0),n.responseType="blob",n.onload=function(){var e=new FileReader;e.readAsArrayBuffer(n.response),e.onloadend=function(t){r(e.result)}},n.send()}catch(r){t("error while retrieving file "+e+".")}}))},e}();a.decorators=[{type:r.Component,args:[{selector:"ngx-doc-viewer",template:'<ng-container *ngIf="!externalViewer">\r\n <div *ngIf="configuredViewer !== \'pdf\'" [innerHtml]="docHtml"></div>\r\n <object *ngIf="fullUrl && configuredViewer === \'pdf\'" [data]="fullUrl"\r\n type="application/pdf" width="100%" height="100%">\r\n <p>Your browser does not support PDFs.\r\n <a [href]="fullUrl">Download the PDF</a>.</p>\r\n </object>\r\n</ng-container>\r\n<ng-container *ngIf="externalViewer">\r\n <iframe (load)="iframeLoaded()" *ngIf="fullUrl && disableContent === \'none\'" #iframe id="iframe" frameBorder="0" [src]="fullUrl"></iframe>\r\n <div class="container" *ngIf="disableContent !== \'none\'">\r\n <div [class.overlay-full]="disableContent === \'all\'"\r\n [class.overlay-popout-google]="configuredViewer ===\'google\' && (disableContent === \'popout\' || disableContent === \'popout-hide\')"\r\n [class.overlay-popout-office]="configuredViewer ===\'office\' && (disableContent === \'popout\' || disableContent === \'popout-hide\')"\r\n [style.background-color]="disableContent === \'popout-hide\' ? \'#fff\': \'transparent\'">\r\n </div>\r\n <iframe (load)="iframeLoaded()" *ngIf="fullUrl" #iframe id="iframe" frameBorder="0" [src]="fullUrl"></iframe>\r\n </div>\r\n</ng-container>',styles:[":host {\n display: block;\n }\n .container {\n width: 100%;\n height: 100%;\n position: relative;\n }\n .overlay-popout-google {\n width: 40px;\n height: 40px;\n right: 26px;\n top: 11.5px;\n position: absolute;\n z-index: 1000;\n }\n .overlay-popout-office {\n width: 100px;\n height: 20px;\n right: 0;\n bottom: 0;\n position: absolute;\n z-index: 1000;\n }\n .overlay-full {\n width: 100%;\n height: 100%;\n right: 0;\n top: 0;\n position: absolute;\n z-index: 1000;\n }\n iframe {\n width: 100%;\n height: 100%;\n }\n "]}]}],a.ctorParameters=function(){return[{type:t.DomSanitizer},{type:r.NgZone}]},a.propDecorators={loaded:[{type:r.Output}],url:[{type:r.Input}],queryParams:[{type:r.Input}],viewerUrl:[{type:r.Input}],googleCheckInterval:[{type:r.Input}],disableContent:[{type:r.Input}],googleCheckContentLoaded:[{type:r.Input}],viewer:[{type:r.Input}],iframes:[{type:r.ViewChildren,args:["iframe"]}]};var u=function(){};u.decorators=[{type:r.NgModule,args:[{imports:[o.CommonModule],declarations:[a],exports:[a]}]}],e.NgxDocViewerComponent=a,e.NgxDocViewerModule=u,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=ngx-doc-viewer2.umd.min.js.map