UNPKG

stz-scroll-into-view

Version:

Angular scroll to an element inside some view when ng-repeat is rendered or all images are loaded or both cases or scroll to static attribute

1 lines 4.34 kB
(function(){var e;e=angular.module("stz.scroll.into.view",[]),e.directive("stzLastRepeat",function(){return{restrict:"A",require:"^?stzScrollIntoView",link:function(e,t,n,r){e.$last&&(null!==r?"function"==typeof r.onStzNgRepeatFinished&&r.onStzNgRepeatFinished(t,n):e.$emit("stzNgRepeatFinished",t,n))}}}),e.directive("stzImageLoad",["$timeout",function(e){return{restrict:"A",require:"^?stzScrollIntoView",link:function(t,n,r,o){var a,i,l;l=r.stzImageLoad,a=function(){return null===o?e(function(){return t.$emit("stzNgSrcImageLoaded",n,l)},0):"function"==typeof o.onStzNgSrcImageLoaded?e(function(){return o.onStzNgSrcImageLoaded(n,l)},0):void 0},i=function(e){return e&&(n.unbind("load"),n.unbind("error")),"img"!==n[0].tagName.toLowerCase()?a():n[0].complete?a():(n.bind("load",function(){a()}),n.bind("error",function(){a()}))},r.ngSrc?r.$observe("ngSrc",function(){return i(!0)}):i(!1)}}}]),e.directive("stzScrollIntoView",["$timeout",function(e){var t,n;return n={IS_DEFAULT_WATCHER_PARAM:null,IS_IMAGES:null,IS_NG_REPEAT:null,SCROLL_TO_ELEMENT:null,PARENT_ID:null,SCOPE:null},t={imagesLoaded:0,ngRepeated:!1,imgLoaded:!1,defaultParamChanged:!1,allImages:0,watchers:{ngRepeatLoad:{on:angular.noop},imagesLoad:{on:angular.noop}},init:function(){switch(!1){case!!n.IS_DEFAULT_WATCHER_PARAM:switch(!1){case!n.IS_NG_REPEAT:n.IS_IMAGES&&t.initImagesLoadWatcher(function(){t.setImgLoaded(),t.ngRepeated&&t.defaultParamChanged&&t.scrollToElementView()}),t.initNgRepeatLoadWatcher(function(){n.IS_IMAGES&&t.setAllImagesLength(),t.setNgRepeated(),t.defaultParamChanged&&t.scrollToElementView()});break;case!n.IS_IMAGES:t.setAllImagesLength(),t.initImagesLoadWatcher(function(){t.setImgLoaded(),t.defaultParamChanged&&t.scrollToElementView()})}return t.initDefaultWatcher(function(){var e;return t.defaultParamChanged||t.setDefaultParamChanged(),e=function(){switch(!1){case!n.IS_NG_REPEAT:if(n.IS_IMAGES){if(t.ngRepeated&&t.imgLoaded)return t.scrollToElementView()}else if(t.ngRepeated)return t.scrollToElementView();break;case!n.IS_IMAGES:if(t.imgLoaded)return t.scrollToElementView();break;default:return t.scrollToElementView()}}()});case!n.IS_NG_REPEAT:return n.IS_IMAGES&&t.initImagesLoadWatcher(function(){return t.setImgLoaded(),t.scrollToElementView()}),t.initNgRepeatLoadWatcher(function(){return n.IS_IMAGES&&t.setAllImagesLength(),t.setNgRepeated()});case!n.IS_IMAGES:return t.setAllImagesLength(),t.initImagesLoadWatcher(function(){return t.setImgLoaded(),t.scrollToElementView()});default:return t.scrollToElementView()}},initImagesLoadWatcher:function(n){return t.watchers.imagesLoad.on=function(r,o){return e(function(){return"stz-s-i-v-ele"===o&&0!==+t.allImages&&++t.imagesLoaded===+t.allImages?n():void 0},0,!1)}},initNgRepeatLoadWatcher:function(n){return t.watchers.ngRepeatLoad.on=function(t,r){return e(function(){return n()},0,!1)}},initDefaultWatcher:function(e){return n.SCOPE.$watch("scrollParam",function(t){return angular.isDefined(t)?e():void 0})},setAllImagesLength:function(){return t.allImages=document.querySelectorAll("[stz-s-i-v-ele]").length},setNgRepeated:function(){return t.ngRepeated=!0},setImgLoaded:function(){return t.imgLoaded=!0},setDefaultParamChanged:function(){return t.defaultParamChanged=!0},scrollToElementView:function(){var e;return n.SCROLL_TO_ELEMENT=document.querySelector("[stz-s-i-v-ele='"+(n.IS_DEFAULT_WATCHER_PARAM?"true":n.SCOPE.scrollParam)+"']"),null!=n.SCROLL_TO_ELEMENT?(n.SCROLL_TO_ELEMENT.scrollIntoView(!0),e=function(){switch(!1){case"string"!=typeof n.PARENT_ID:return document.querySelector(n.PARENT_ID).scrollTop=0;case"object"!=typeof n.PARENT_ID:return"number"==typeof n.PARENT_ID.scrollTop?n.PARENT_ID.scrollTop=0:document.body.scrollTop=0;default:return document.body.scrollTop=0}}(),t.imagesLoaded=0):void 0}},{restrict:"A",scope:{scrollParam:"=stzScrollIntoView"},controller:["$scope",function(e){this.onStzNgRepeatFinished=function(e,n){return t.watchers.ngRepeatLoad.on(e,n)},this.onStzNgSrcImageLoaded=function(e,n){return t.watchers.imagesLoad.on(e,n)}}],link:function(e,r,o){n.IS_DEFAULT_WATCHER_PARAM="string"==typeof o.stzScrollIntoView&&""!==o.stzScrollIntoView?!1:!0,n.IS_IMAGES="undefined"!=typeof o.stzSIVImg?!0:!1,n.IS_NG_REPEAT="undefined"!=typeof o.stzSIVNgRepeat?!0:!1,n.PARENT_ID=o.stzSIVParentId||r[0].parentNode,n.SCOPE=e,t.init()}}}])}).call(this);