@lenne.tech/ng-base 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -785,12 +785,162 @@
785
785
  src: [{ type: i0.Input }]
786
786
  };
787
787
 
788
+ /**
789
+ * Image service
790
+ */
791
+ var ImageService = /** @class */ (function () {
792
+ function ImageService() {
793
+ }
794
+ /**
795
+ * Preaload images (for lazy handling)
796
+ *
797
+ * @param options
798
+ * @protected
799
+ */
800
+ ImageService.prototype.preLoadImages = function (options) {
801
+ var _this = this;
802
+ // Init configuration
803
+ var config = Object.assign({ doneFunnction: undefined, elements: document, regExp: undefined, timeout: undefined }, options);
804
+ return new Promise(function (resolve) {
805
+ var e_1, _b;
806
+ var _a;
807
+ // Init variables
808
+ var resolved = false;
809
+ var timeoutId;
810
+ // HTMLCollection to array
811
+ if (config.elements instanceof HTMLCollection) {
812
+ config.elements = Array.from(config.elements);
813
+ }
814
+ // Single element to array
815
+ if (!Array.isArray(config.elements)) {
816
+ config.elements = [config.elements];
817
+ }
818
+ // Init loading
819
+ var promises = [];
820
+ // Get all images
821
+ var images = [];
822
+ config.elements.forEach(function (element) {
823
+ images = images.concat(Array.from(element.getElementsByTagName('img')));
824
+ });
825
+ var _loop_1 = function (image) {
826
+ var src = image.getAttribute('src');
827
+ // Check if changes are necessary and possible
828
+ if ((image.complete && image.naturalHeight !== 0) || // already laoded
829
+ image.getAttribute('loading') !== 'lazy' || // loading attribute !== lazy
830
+ !src || // Image has no src
831
+ ((_a = config.regExp) === null || _a === void 0 ? void 0 : _a.test(src)) // Test if src matches regular expression
832
+ ) {
833
+ return "continue";
834
+ }
835
+ // Replace lazy loading image with immediately loading image
836
+ var newImage = document.createElement('img');
837
+ Array.from(image.attributes).forEach(function (attr) {
838
+ if (attr.name !== 'loading' && attr.name !== 'src') {
839
+ newImage.setAttribute(attr.name, attr.value);
840
+ }
841
+ });
842
+ image.replaceWith(newImage);
843
+ // Wait for laoding
844
+ promises.push(_this.loadImage(newImage, src));
845
+ };
846
+ try {
847
+ // Prepare images
848
+ for (var images_1 = __values(images), images_1_1 = images_1.next(); !images_1_1.done; images_1_1 = images_1.next()) {
849
+ var image = images_1_1.value;
850
+ _loop_1(image);
851
+ }
852
+ }
853
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
854
+ finally {
855
+ try {
856
+ if (images_1_1 && !images_1_1.done && (_b = images_1.return)) _b.call(images_1);
857
+ }
858
+ finally { if (e_1) throw e_1.error; }
859
+ }
860
+ if (config.timeout) {
861
+ timeoutId = setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
862
+ return __generator(this, function (_b) {
863
+ switch (_b.label) {
864
+ case 0:
865
+ resolved = true;
866
+ if (!config.doneFunnction) return [3 /*break*/, 2];
867
+ return [4 /*yield*/, config.doneFunction()];
868
+ case 1:
869
+ _b.sent();
870
+ _b.label = 2;
871
+ case 2:
872
+ resolve(undefined);
873
+ return [2 /*return*/];
874
+ }
875
+ });
876
+ }); });
877
+ }
878
+ // Wait until all images loaded
879
+ Promise.all(promises)
880
+ .catch(function (e) { return console.error('Error during image loading', e); })
881
+ .finally(function () { return __awaiter(_this, void 0, void 0, function () {
882
+ var result;
883
+ return __generator(this, function (_b) {
884
+ switch (_b.label) {
885
+ case 0:
886
+ // Check if already resolved
887
+ if (resolved) {
888
+ return [2 /*return*/];
889
+ }
890
+ // Stop timeout
891
+ if (timeoutId) {
892
+ clearTimeout(timeoutId);
893
+ }
894
+ if (!config.doneFunnction) return [3 /*break*/, 2];
895
+ return [4 /*yield*/, config.doneFunction()];
896
+ case 1:
897
+ result = _b.sent();
898
+ _b.label = 2;
899
+ case 2:
900
+ resolve(result);
901
+ return [2 /*return*/];
902
+ }
903
+ });
904
+ }); });
905
+ });
906
+ };
907
+ /**
908
+ * Load image for image element
909
+ *
910
+ * @param imageElement
911
+ * @param src
912
+ * @protected
913
+ */
914
+ ImageService.prototype.loadImage = function (imageElement, src) {
915
+ return __awaiter(this, void 0, void 0, function () {
916
+ return __generator(this, function (_b) {
917
+ src = src || imageElement.src;
918
+ return [2 /*return*/, new Promise(function (resolve, reject) {
919
+ imageElement.onload = function () { return resolve(imageElement); };
920
+ imageElement.onerror = reject;
921
+ imageElement.src = src;
922
+ })];
923
+ });
924
+ });
925
+ };
926
+ return ImageService;
927
+ }());
928
+ ImageService.ɵprov = i0__namespace.ɵɵdefineInjectable({ factory: function ImageService_Factory() { return new ImageService(); }, token: ImageService, providedIn: "root" });
929
+ ImageService.decorators = [
930
+ { type: i0.Injectable, args: [{
931
+ providedIn: 'root',
932
+ },] }
933
+ ];
934
+
788
935
  /**
789
936
  * Set same height (maximum) to different elements
790
937
  */
791
938
  var MatchHeightDirective = /** @class */ (function () {
792
- function MatchHeightDirective(el) {
939
+ function MatchHeightDirective(el, imageService, zone) {
793
940
  this.el = el;
941
+ this.imageService = imageService;
942
+ this.zone = zone;
943
+ this.loadLazyImages = true;
794
944
  }
795
945
  MatchHeightDirective.prototype.onResize = function () {
796
946
  this.matchHeight(this.el.nativeElement, this.ltMatchHeight);
@@ -799,33 +949,82 @@
799
949
  this.matchHeight(this.el.nativeElement, this.ltMatchHeight);
800
950
  };
801
951
  MatchHeightDirective.prototype.matchHeight = function (parent, target) {
802
- if (!parent) {
803
- return;
804
- }
805
- var classNames;
806
- if (!Array.isArray(target)) {
807
- classNames = [target];
808
- }
809
- else {
810
- classNames = target;
811
- }
812
- classNames.forEach(function (className) {
813
- // find all child structureElements with the selected class name
814
- var children = parent.getElementsByClassName(className);
815
- if (!children) {
816
- return;
817
- }
818
- // reset all children height
819
- Array.from(children).forEach(function (x) {
820
- x.style.height = 'initial';
952
+ var _this = this;
953
+ // Timeout is set to force Angular to wait for all actions and only continue once the process has run through.
954
+ // Otherwise, an infinite loop will occur.
955
+ setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
956
+ var classNames, _loop_1, this_1, classNames_1, classNames_1_1, className, e_1_1;
957
+ var e_1, _a;
958
+ return __generator(this, function (_b) {
959
+ switch (_b.label) {
960
+ case 0:
961
+ if (!parent) {
962
+ return [2 /*return*/];
963
+ }
964
+ if (!Array.isArray(target)) {
965
+ classNames = [target];
966
+ }
967
+ else {
968
+ classNames = target;
969
+ }
970
+ _loop_1 = function (className) {
971
+ var children, itemHeights, maxHeight;
972
+ return __generator(this, function (_c) {
973
+ switch (_c.label) {
974
+ case 0:
975
+ children = parent.getElementsByClassName(className);
976
+ if (!children || children.length < 2) {
977
+ return [2 /*return*/, "continue"];
978
+ }
979
+ if (!this_1.loadLazyImages) return [3 /*break*/, 2];
980
+ return [4 /*yield*/, this_1.imageService.preLoadImages({ elements: children })];
981
+ case 1:
982
+ _c.sent();
983
+ _c.label = 2;
984
+ case 2:
985
+ // reset all children height
986
+ Array.from(children).forEach(function (element) {
987
+ element.style.height = 'initial';
988
+ });
989
+ itemHeights = Array.from(children).map(function (x) { return x.getBoundingClientRect().height; });
990
+ maxHeight = itemHeights.reduce(function (prev, curr) { return (curr > prev ? curr : prev); }, 0);
991
+ // update the child structureElements height to the tallest one
992
+ Array.from(children).forEach(function (element) { return (element.style.height = maxHeight + "px"); });
993
+ return [2 /*return*/];
994
+ }
995
+ });
996
+ };
997
+ this_1 = this;
998
+ _b.label = 1;
999
+ case 1:
1000
+ _b.trys.push([1, 6, 7, 8]);
1001
+ classNames_1 = __values(classNames), classNames_1_1 = classNames_1.next();
1002
+ _b.label = 2;
1003
+ case 2:
1004
+ if (!!classNames_1_1.done) return [3 /*break*/, 5];
1005
+ className = classNames_1_1.value;
1006
+ return [5 /*yield**/, _loop_1(className)];
1007
+ case 3:
1008
+ _b.sent();
1009
+ _b.label = 4;
1010
+ case 4:
1011
+ classNames_1_1 = classNames_1.next();
1012
+ return [3 /*break*/, 2];
1013
+ case 5: return [3 /*break*/, 8];
1014
+ case 6:
1015
+ e_1_1 = _b.sent();
1016
+ e_1 = { error: e_1_1 };
1017
+ return [3 /*break*/, 8];
1018
+ case 7:
1019
+ try {
1020
+ if (classNames_1_1 && !classNames_1_1.done && (_a = classNames_1.return)) _a.call(classNames_1);
1021
+ }
1022
+ finally { if (e_1) throw e_1.error; }
1023
+ return [7 /*endfinally*/];
1024
+ case 8: return [2 /*return*/];
1025
+ }
821
1026
  });
822
- // get all the child structureElements heights
823
- var itemHeights = Array.from(children).map(function (x) { return x.getBoundingClientRect().height; });
824
- // find out the tallest one
825
- var maxHeight = itemHeights.reduce(function (prev, curr) { return (curr > prev ? curr : prev); }, 0);
826
- // update the child structureElements height to the tallest one
827
- Array.from(children).forEach(function (x) { return (x.style.height = maxHeight + "px"); });
828
- });
1027
+ }); }, 1);
829
1028
  };
830
1029
  return MatchHeightDirective;
831
1030
  }());
@@ -835,10 +1034,13 @@
835
1034
  },] }
836
1035
  ];
837
1036
  MatchHeightDirective.ctorParameters = function () { return [
838
- { type: i0.ElementRef }
1037
+ { type: i0.ElementRef },
1038
+ { type: ImageService },
1039
+ { type: i0.NgZone }
839
1040
  ]; };
840
1041
  MatchHeightDirective.propDecorators = {
841
1042
  ltMatchHeight: [{ type: i0.Input }],
1043
+ loadLazyImages: [{ type: i0.Input }],
842
1044
  onResize: [{ type: i0.HostListener, args: ['window:resize',] }]
843
1045
  };
844
1046
 
@@ -2602,9 +2804,10 @@
2602
2804
  /**
2603
2805
  * Initializer
2604
2806
  */
2605
- function ScrollService(router, moduleConfig) {
2807
+ function ScrollService(router, moduleConfig, imageService) {
2606
2808
  this.router = router;
2607
2809
  this.moduleConfig = moduleConfig;
2810
+ this.imageService = imageService;
2608
2811
  /**
2609
2812
  * Is a scroll listener set
2610
2813
  *
@@ -2655,7 +2858,7 @@
2655
2858
  undoOverflowChanges = true;
2656
2859
  }
2657
2860
  if (!config.loadImagesBefore) return [3 /*break*/, 4];
2658
- return [4 /*yield*/, this.preLoadImages()];
2861
+ return [4 /*yield*/, this.imageService.preLoadImages()];
2659
2862
  case 3:
2660
2863
  _a.sent();
2661
2864
  _a.label = 4;
@@ -2785,122 +2988,9 @@
2785
2988
  // Return promise
2786
2989
  return this.scrollPromise;
2787
2990
  };
2788
- /**
2789
- * Preaload images (for lazy handling)
2790
- *
2791
- * @param options
2792
- * @protected
2793
- */
2794
- ScrollService.prototype.preLoadImages = function (options) {
2795
- var _this = this;
2796
- // Init configuration
2797
- var config = Object.assign({ doneFunnction: undefined, timeout: undefined }, options);
2798
- return new Promise(function (resolve) {
2799
- var e_1, _a;
2800
- // Init variables
2801
- var resolved = false;
2802
- var timeoutId;
2803
- // Init loading
2804
- var images = Array.from(document.getElementsByTagName('img'));
2805
- var promises = [];
2806
- var _loop_1 = function (image) {
2807
- var src = image.getAttribute('src');
2808
- // Check if changes are necessary and possible
2809
- if ((image.complete && image.naturalHeight !== 0) || image.getAttribute('loading') !== 'lazy' || !src) {
2810
- return "continue";
2811
- }
2812
- var newImage = document.createElement('img');
2813
- Array.from(image.attributes).forEach(function (attr) {
2814
- if (attr.name !== 'loading' && attr.name !== 'src') {
2815
- newImage.setAttribute(attr.name, attr.value);
2816
- }
2817
- });
2818
- image.replaceWith(newImage);
2819
- promises.push(_this.loadImage(newImage, src));
2820
- };
2821
- try {
2822
- // Prepare images
2823
- for (var images_1 = __values(images), images_1_1 = images_1.next(); !images_1_1.done; images_1_1 = images_1.next()) {
2824
- var image = images_1_1.value;
2825
- _loop_1(image);
2826
- }
2827
- }
2828
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
2829
- finally {
2830
- try {
2831
- if (images_1_1 && !images_1_1.done && (_a = images_1.return)) _a.call(images_1);
2832
- }
2833
- finally { if (e_1) throw e_1.error; }
2834
- }
2835
- if (config.timeout) {
2836
- timeoutId = setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
2837
- return __generator(this, function (_a) {
2838
- switch (_a.label) {
2839
- case 0:
2840
- resolved = true;
2841
- if (!config.doneFunnction) return [3 /*break*/, 2];
2842
- return [4 /*yield*/, config.doneFunction()];
2843
- case 1:
2844
- _a.sent();
2845
- _a.label = 2;
2846
- case 2:
2847
- resolve(undefined);
2848
- return [2 /*return*/];
2849
- }
2850
- });
2851
- }); });
2852
- }
2853
- // Wait until all images loaded
2854
- Promise.all(promises)
2855
- .catch(function (e) { return console.error('Error during image loading', e); })
2856
- .finally(function () { return __awaiter(_this, void 0, void 0, function () {
2857
- var result;
2858
- return __generator(this, function (_a) {
2859
- switch (_a.label) {
2860
- case 0:
2861
- // Check if already resolved
2862
- if (resolved) {
2863
- return [2 /*return*/];
2864
- }
2865
- // Stop timeout
2866
- if (timeoutId) {
2867
- clearTimeout(timeoutId);
2868
- }
2869
- if (!config.doneFunnction) return [3 /*break*/, 2];
2870
- return [4 /*yield*/, config.doneFunction()];
2871
- case 1:
2872
- result = _a.sent();
2873
- _a.label = 2;
2874
- case 2:
2875
- resolve(result);
2876
- return [2 /*return*/];
2877
- }
2878
- });
2879
- }); });
2880
- });
2881
- };
2882
- /**
2883
- * Load image for image element
2884
- *
2885
- * @param imageElement
2886
- * @param src
2887
- * @protected
2888
- */
2889
- ScrollService.prototype.loadImage = function (imageElement, src) {
2890
- return __awaiter(this, void 0, void 0, function () {
2891
- return __generator(this, function (_a) {
2892
- src = src || imageElement.src;
2893
- return [2 /*return*/, new Promise(function (resolve, reject) {
2894
- imageElement.onload = function () { return resolve(imageElement); };
2895
- imageElement.onerror = reject;
2896
- imageElement.src = src;
2897
- })];
2898
- });
2899
- });
2900
- };
2901
2991
  return ScrollService;
2902
2992
  }());
2903
- ScrollService.ɵprov = i0__namespace.ɵɵdefineInjectable({ factory: function ScrollService_Factory() { return new ScrollService(i0__namespace.ɵɵinject(i1__namespace$1.Router), i0__namespace.ɵɵinject(BASE_MODULE_CONFIG)); }, token: ScrollService, providedIn: "root" });
2993
+ ScrollService.ɵprov = i0__namespace.ɵɵdefineInjectable({ factory: function ScrollService_Factory() { return new ScrollService(i0__namespace.ɵɵinject(i1__namespace$1.Router), i0__namespace.ɵɵinject(BASE_MODULE_CONFIG), i0__namespace.ɵɵinject(ImageService)); }, token: ScrollService, providedIn: "root" });
2904
2994
  ScrollService.decorators = [
2905
2995
  { type: i0.Injectable, args: [{
2906
2996
  providedIn: 'root',
@@ -2908,7 +2998,8 @@
2908
2998
  ];
2909
2999
  ScrollService.ctorParameters = function () { return [
2910
3000
  { type: i1$2.Router },
2911
- { type: undefined, decorators: [{ type: i0.Inject, args: [BASE_MODULE_CONFIG,] }] }
3001
+ { type: undefined, decorators: [{ type: i0.Inject, args: [BASE_MODULE_CONFIG,] }] },
3002
+ { type: ImageService }
2912
3003
  ]; };
2913
3004
 
2914
3005
  /**
@@ -2998,6 +3089,7 @@
2998
3089
  exports.GraphQLService = GraphQLService;
2999
3090
  exports.GraphQLType = GraphQLType;
3000
3091
  exports.Helper = Helper;
3092
+ exports.ImageService = ImageService;
3001
3093
  exports.LazyLoadDirective = LazyLoadDirective;
3002
3094
  exports.LoaderService = LoaderService;
3003
3095
  exports.LogoutGuard = LogoutGuard;