ngx-smart-loader
Version:
Smart loader handler to manage loaders everywhere in Angular apps.
5 lines • 8.15 kB
JavaScript
!function(global,factory){"object"==typeof exports&&"undefined"!=typeof module?factory(exports,require("@angular/core"),require("@angular/common")):"function"==typeof define&&define.amd?define(["exports","@angular/core","@angular/common"],factory):factory((global.ng=global.ng||{},global.ng.ngxSmartLoader={}),global.ng.core,global.ng.common)}(this,function(exports,core,common){"use strict";/**
* @license ngx-smart-loader
* MIT license
*/
var NgxSmartLoaderService=function(){function NgxSmartLoaderService(){this._loaderStack=[],this._actions=[]}return NgxSmartLoaderService.prototype.addLoader=function(loaderInstance,force){if(force){var i=this._loaderStack.findIndex(function(o){return o.id===loaderInstance.id});i>-1?this._loaderStack[i].component=loaderInstance.component:this._loaderStack.push(loaderInstance)}else{if(this._getLoader(loaderInstance.id))throw new Error("Loader with "+loaderInstance.id+" identifier already exist");this._loaderStack.push(loaderInstance)}},NgxSmartLoaderService.prototype.removeLoader=function(id){this._loaderStack=this._loaderStack.filter(function(loader){return loader.id!==id}),this._removeAction(id,"*")},NgxSmartLoaderService.prototype.getLoaderStack=function(){return this._loaderStack},NgxSmartLoaderService.prototype.getLoaderStackCount=function(){return this._loaderStack.length},NgxSmartLoaderService.prototype.getOpenedLoaders=function(){return this._loaderStack.filter(function(loader){return loader.component.visible})},NgxSmartLoaderService.prototype.getActiveLoaders=function(){return this._loaderStack.filter(function(loader){return loader.component.loading})},NgxSmartLoaderService.prototype.getHigherIndex=function(){var index=this.getOpenedLoaders().map(function(loader){return loader.component.layerPosition});return Math.max.apply(Math,index)+1},NgxSmartLoaderService.prototype.start=function(id){var loader,_this=this;Array.isArray(id)?id.forEach(function(i){_this.start(i)}):(loader=this._getLoader(id))?(loader.component.start(),this._removeAction(id,"start")):this._addAction(id,"start")},NgxSmartLoaderService.prototype.startAll=function(){var _this=this;this._loaderStack.forEach(function(loader){return _this.start(loader.id)})},NgxSmartLoaderService.prototype.stop=function(id){var loader,_this=this;Array.isArray(id)?id.forEach(function(i){_this.stop(i)}):(loader=this._getLoader(id))?(loader.component.stop(),this._removeAction(id,"stop")):this._addAction(id,"stop")},NgxSmartLoaderService.prototype.stopAll=function(){var _this=this;this._loaderStack.forEach(function(loader){return _this.stop(loader.id)})},NgxSmartLoaderService.prototype.isLoading=function(id){var loader,_this=this;if(Array.isArray(id)){var tmp_1=[];return id.forEach(function(i){_this._loaderStack.forEach(function(load){load.id===i&&tmp_1.push(load.component.loading)})}),-1===tmp_1.indexOf(!1)}return!!(loader=this._getLoader(id))&&loader.component.loading},NgxSmartLoaderService.prototype.executeAction=function(id,action){if(this._actions.find(function(act){return act.identifier===id&&act.action===action}))switch(action){case"start":this.start(id);break;case"stop":this.stop(id)}},NgxSmartLoaderService.prototype._getLoader=function(id){return this._loaderStack.find(function(load){return load.id===id})||null},NgxSmartLoaderService.prototype._addAction=function(id,action){var _this=this;Array.isArray(id)?id.forEach(function(i){_this._addAction(i,action)}):this._actions.push({identifier:id,action:action})},NgxSmartLoaderService.prototype._removeAction=function(id,action){var _this=this;Array.isArray(id)?id.forEach(function(i){_this._removeAction(i,action)}):this._actions=this._actions.filter(function(act){return act.identifier!==id||act.action!==action&&"*"!==action})},NgxSmartLoaderService.decorators=[{type:core.Injectable}],NgxSmartLoaderService.ctorParameters=function(){return[]},NgxSmartLoaderService}(),LoaderInstance=function(){return function(component){this.id=component.identifier,this.component=component}}(),NgxSmartLoaderComponent=function(){function NgxSmartLoaderComponent(ngxSmartLoaderService,changeDetectorRef){this.ngxSmartLoaderService=ngxSmartLoaderService,this.changeDetectorRef=changeDetectorRef,this.identifier="",this.customClass="",this.force=!1,this.delayIn=0,this.delayOut=0,this.autostart=!1,this.onStart=new core.EventEmitter,this.onStop=new core.EventEmitter,this.onVisibleChange=new core.EventEmitter,this.loading=!1,this.visible=!1,this.layerPosition=999,this._isProcessing=!1,this._loaderBodyClass="loader-open",this._enterClass="enter",this._leaveClass="leave"}return NgxSmartLoaderComponent.prototype.ngOnInit=function(){try{var loader=new LoaderInstance(this);this.ngxSmartLoaderService.addLoader(loader,this.force),this.layerPosition+=this.ngxSmartLoaderService.getLoaderStackCount(),this.addCustomClass(this.identifier.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()),this.autostart?this.ngxSmartLoaderService.start(this.identifier):this.ngxSmartLoaderService.executeAction(this.identifier,"start")}catch(error){throw error}},NgxSmartLoaderComponent.prototype.ngOnDestroy=function(){this.ngxSmartLoaderService.removeLoader(this.identifier)},NgxSmartLoaderComponent.prototype.start=function(top){var _this=this;this._isProcessing=!0,clearInterval(this._debouncer),this.visible=!0,setTimeout(function(){_this.addCustomClass(_this._enterClass)}),this._debouncer=setTimeout(function(){top&&(_this.layerPosition=_this.ngxSmartLoaderService.getHigherIndex()),document.body.classList.contains(_this._loaderBodyClass)||document.body.classList.add(_this._loaderBodyClass),_this.loading=!0,_this.onStart.emit(_this),_this.onVisibleChange.emit(_this),_this.removeCustomClass(_this._enterClass),_this._isProcessing=!1},this.delayIn)},NgxSmartLoaderComponent.prototype.stop=function(){var _this=this;this._isProcessing&&(this.visible=!1,this.loading=!1),clearInterval(this._debouncer),this.addCustomClass(this._leaveClass),this.loading=!1,this._debouncer=setTimeout(function(){document.body.classList.contains(_this._loaderBodyClass)&&document.body.classList.remove(_this._loaderBodyClass),_this.visible=!1,_this.onStop.emit(_this),_this.onVisibleChange.emit(_this),_this.removeCustomClass(_this._leaveClass),setTimeout(function(){_this.changeDetectorRef.markForCheck()})},this.delayOut)},NgxSmartLoaderComponent.prototype.addCustomClass=function(className){this.customClass.length?-1===this.customClass.indexOf(className)&&(this.customClass+=" "+className):this.customClass=className},NgxSmartLoaderComponent.prototype.removeCustomClass=function(className){this.customClass=className?this.customClass.replace(className,"").trim():""},NgxSmartLoaderComponent.decorators=[{type:core.Component,args:[{selector:"ngx-smart-loader",template:'\n <div class="loader-container {{customClass}}" [ngClass]="{\'active\': loading}"\n [style.z-index]="layerPosition - 1" *ngIf="visible">\n <ng-content></ng-content>\n </div>\n '}]}],NgxSmartLoaderComponent.ctorParameters=function(){return[{type:NgxSmartLoaderService},{type:core.ChangeDetectorRef}]},NgxSmartLoaderComponent.propDecorators={identifier:[{type:core.Input}],customClass:[{type:core.Input}],force:[{type:core.Input}],delayIn:[{type:core.Input}],delayOut:[{type:core.Input}],autostart:[{type:core.Input}],onStart:[{type:core.Output}],onStop:[{type:core.Output}],onVisibleChange:[{type:core.Output}]},NgxSmartLoaderComponent}(),NgxSmartLoaderModule=function(){function NgxSmartLoaderModule(){}return NgxSmartLoaderModule.forRoot=function(){return{ngModule:NgxSmartLoaderModule,providers:[NgxSmartLoaderService]}},NgxSmartLoaderModule.forChild=function(){return{ngModule:NgxSmartLoaderModule,providers:[NgxSmartLoaderService]}},NgxSmartLoaderModule.decorators=[{type:core.NgModule,args:[{declarations:[NgxSmartLoaderComponent],exports:[NgxSmartLoaderComponent],imports:[common.CommonModule]}]}],NgxSmartLoaderModule.ctorParameters=function(){return[]},NgxSmartLoaderModule}();exports.NgxSmartLoaderService=NgxSmartLoaderService,exports.NgxSmartLoaderComponent=NgxSmartLoaderComponent,exports.NgxSmartLoaderModule=NgxSmartLoaderModule,Object.defineProperty(exports,"__esModule",{value:!0})});