@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.
@@ -2,19 +2,19 @@ import * as i1 from 'apollo-angular';
2
2
  import { APOLLO_OPTIONS, gql, Apollo } from 'apollo-angular';
3
3
  import { HttpLink } from 'apollo-angular/http';
4
4
  import * as i0 from '@angular/core';
5
- import { InjectionToken, Injectable, Inject, Directive, ElementRef, Input, Output, HostBinding, HostListener, Pipe, NgModule } from '@angular/core';
5
+ import { InjectionToken, Injectable, Inject, Directive, ElementRef, Input, Output, HostBinding, NgZone, HostListener, Pipe, NgModule } from '@angular/core';
6
6
  import { BehaviorSubject, Observable, of, throwError } from 'rxjs';
7
7
  import { ApolloLink, split, concat } from '@apollo/client/core';
8
8
  import { InMemoryCache } from '@apollo/client/cache';
9
9
  import { WebSocketLink } from '@apollo/client/link/ws';
10
10
  import { getMainDefinition } from '@apollo/client/utilities';
11
+ import { __awaiter } from 'tslib';
11
12
  import * as i1$2 from '@angular/platform-browser';
12
13
  import { DomSanitizer, Title, Meta } from '@angular/platform-browser';
13
14
  import * as i1$1 from '@angular/router';
14
15
  import { Router } from '@angular/router';
15
16
  import { buildClientSchema, getIntrospectionQuery } from 'graphql';
16
17
  import { map } from 'rxjs/operators';
17
- import { __awaiter } from 'tslib';
18
18
 
19
19
  const BASE_MODULE_CONFIG = new InjectionToken('BASE_MODULE_CONFIG');
20
20
 
@@ -418,12 +418,125 @@ LazyLoadDirective.propDecorators = {
418
418
  src: [{ type: Input }]
419
419
  };
420
420
 
421
+ /**
422
+ * Image service
423
+ */
424
+ class ImageService {
425
+ /**
426
+ * Preaload images (for lazy handling)
427
+ *
428
+ * @param options
429
+ * @protected
430
+ */
431
+ preLoadImages(options) {
432
+ // Init configuration
433
+ const config = Object.assign({ doneFunnction: undefined, elements: document, regExp: undefined, timeout: undefined }, options);
434
+ return new Promise((resolve) => {
435
+ var _a;
436
+ // Init variables
437
+ let resolved = false;
438
+ let timeoutId;
439
+ // HTMLCollection to array
440
+ if (config.elements instanceof HTMLCollection) {
441
+ config.elements = Array.from(config.elements);
442
+ }
443
+ // Single element to array
444
+ if (!Array.isArray(config.elements)) {
445
+ config.elements = [config.elements];
446
+ }
447
+ // Init loading
448
+ const promises = [];
449
+ // Get all images
450
+ let images = [];
451
+ config.elements.forEach((element) => {
452
+ images = images.concat(Array.from(element.getElementsByTagName('img')));
453
+ });
454
+ // Prepare images
455
+ for (const image of images) {
456
+ const src = image.getAttribute('src');
457
+ // Check if changes are necessary and possible
458
+ if ((image.complete && image.naturalHeight !== 0) || // already laoded
459
+ image.getAttribute('loading') !== 'lazy' || // loading attribute !== lazy
460
+ !src || // Image has no src
461
+ ((_a = config.regExp) === null || _a === void 0 ? void 0 : _a.test(src)) // Test if src matches regular expression
462
+ ) {
463
+ continue;
464
+ }
465
+ // Replace lazy loading image with immediately loading image
466
+ const newImage = document.createElement('img');
467
+ Array.from(image.attributes).forEach((attr) => {
468
+ if (attr.name !== 'loading' && attr.name !== 'src') {
469
+ newImage.setAttribute(attr.name, attr.value);
470
+ }
471
+ });
472
+ image.replaceWith(newImage);
473
+ // Wait for laoding
474
+ promises.push(this.loadImage(newImage, src));
475
+ }
476
+ if (config.timeout) {
477
+ timeoutId = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
478
+ resolved = true;
479
+ if (config.doneFunnction) {
480
+ yield config.doneFunction();
481
+ }
482
+ resolve(undefined);
483
+ }));
484
+ }
485
+ // Wait until all images loaded
486
+ Promise.all(promises)
487
+ .catch((e) => console.error('Error during image loading', e))
488
+ .finally(() => __awaiter(this, void 0, void 0, function* () {
489
+ // Check if already resolved
490
+ if (resolved) {
491
+ return;
492
+ }
493
+ // Stop timeout
494
+ if (timeoutId) {
495
+ clearTimeout(timeoutId);
496
+ }
497
+ // Return result
498
+ let result;
499
+ if (config.doneFunnction) {
500
+ result = yield config.doneFunction();
501
+ }
502
+ resolve(result);
503
+ }));
504
+ });
505
+ }
506
+ /**
507
+ * Load image for image element
508
+ *
509
+ * @param imageElement
510
+ * @param src
511
+ * @protected
512
+ */
513
+ loadImage(imageElement, src) {
514
+ return __awaiter(this, void 0, void 0, function* () {
515
+ src = src || imageElement.src;
516
+ return new Promise((resolve, reject) => {
517
+ imageElement.onload = () => resolve(imageElement);
518
+ imageElement.onerror = reject;
519
+ imageElement.src = src;
520
+ });
521
+ });
522
+ }
523
+ }
524
+ ImageService.ɵprov = i0.ɵɵdefineInjectable({ factory: function ImageService_Factory() { return new ImageService(); }, token: ImageService, providedIn: "root" });
525
+ ImageService.decorators = [
526
+ { type: Injectable, args: [{
527
+ providedIn: 'root',
528
+ },] }
529
+ ];
530
+
421
531
  /**
422
532
  * Set same height (maximum) to different elements
423
533
  */
424
534
  class MatchHeightDirective {
425
- constructor(el) {
535
+ constructor(el, imageService, zone) {
426
536
  this.el = el;
537
+ this.imageService = imageService;
538
+ this.zone = zone;
539
+ this.loadLazyImages = true;
427
540
  }
428
541
  onResize() {
429
542
  this.matchHeight(this.el.nativeElement, this.ltMatchHeight);
@@ -432,33 +545,41 @@ class MatchHeightDirective {
432
545
  this.matchHeight(this.el.nativeElement, this.ltMatchHeight);
433
546
  }
434
547
  matchHeight(parent, target) {
435
- if (!parent) {
436
- return;
437
- }
438
- let classNames;
439
- if (!Array.isArray(target)) {
440
- classNames = [target];
441
- }
442
- else {
443
- classNames = target;
444
- }
445
- classNames.forEach((className) => {
446
- // find all child structureElements with the selected class name
447
- const children = parent.getElementsByClassName(className);
448
- if (!children) {
548
+ // Timeout is set to force Angular to wait for all actions and only continue once the process has run through.
549
+ // Otherwise, an infinite loop will occur.
550
+ setTimeout(() => __awaiter(this, void 0, void 0, function* () {
551
+ if (!parent) {
449
552
  return;
450
553
  }
451
- // reset all children height
452
- Array.from(children).forEach((x) => {
453
- x.style.height = 'initial';
454
- });
455
- // get all the child structureElements heights
456
- const itemHeights = Array.from(children).map((x) => x.getBoundingClientRect().height);
457
- // find out the tallest one
458
- const maxHeight = itemHeights.reduce((prev, curr) => (curr > prev ? curr : prev), 0);
459
- // update the child structureElements height to the tallest one
460
- Array.from(children).forEach((x) => (x.style.height = `${maxHeight}px`));
461
- });
554
+ let classNames;
555
+ if (!Array.isArray(target)) {
556
+ classNames = [target];
557
+ }
558
+ else {
559
+ classNames = target;
560
+ }
561
+ for (const className of classNames) {
562
+ // find all child structureElements with the selected class name
563
+ const children = parent.getElementsByClassName(className);
564
+ if (!children || children.length < 2) {
565
+ continue;
566
+ }
567
+ // Load images of children
568
+ if (this.loadLazyImages) {
569
+ yield this.imageService.preLoadImages({ elements: children });
570
+ }
571
+ // reset all children height
572
+ Array.from(children).forEach((element) => {
573
+ element.style.height = 'initial';
574
+ });
575
+ // get all the child structureElements heights
576
+ const itemHeights = Array.from(children).map((x) => x.getBoundingClientRect().height);
577
+ // find out the tallest one
578
+ const maxHeight = itemHeights.reduce((prev, curr) => (curr > prev ? curr : prev), 0);
579
+ // update the child structureElements height to the tallest one
580
+ Array.from(children).forEach((element) => (element.style.height = `${maxHeight}px`));
581
+ }
582
+ }), 1);
462
583
  }
463
584
  }
464
585
  MatchHeightDirective.decorators = [
@@ -467,10 +588,13 @@ MatchHeightDirective.decorators = [
467
588
  },] }
468
589
  ];
469
590
  MatchHeightDirective.ctorParameters = () => [
470
- { type: ElementRef }
591
+ { type: ElementRef },
592
+ { type: ImageService },
593
+ { type: NgZone }
471
594
  ];
472
595
  MatchHeightDirective.propDecorators = {
473
596
  ltMatchHeight: [{ type: Input }],
597
+ loadLazyImages: [{ type: Input }],
474
598
  onResize: [{ type: HostListener, args: ['window:resize',] }]
475
599
  };
476
600
 
@@ -2001,9 +2125,10 @@ class ScrollService {
2001
2125
  /**
2002
2126
  * Initializer
2003
2127
  */
2004
- constructor(router, moduleConfig) {
2128
+ constructor(router, moduleConfig, imageService) {
2005
2129
  this.router = router;
2006
2130
  this.moduleConfig = moduleConfig;
2131
+ this.imageService = imageService;
2007
2132
  /**
2008
2133
  * Is a scroll listener set
2009
2134
  *
@@ -2053,7 +2178,7 @@ class ScrollService {
2053
2178
  }
2054
2179
  // Load images before scrolling (once)
2055
2180
  if (config.loadImagesBefore) {
2056
- yield this.preLoadImages();
2181
+ yield this.imageService.preLoadImages();
2057
2182
  }
2058
2183
  // Get get bounding rect of target element
2059
2184
  const cords = el.getBoundingClientRect();
@@ -2164,87 +2289,8 @@ class ScrollService {
2164
2289
  // Return promise
2165
2290
  return this.scrollPromise;
2166
2291
  }
2167
- /**
2168
- * Preaload images (for lazy handling)
2169
- *
2170
- * @param options
2171
- * @protected
2172
- */
2173
- preLoadImages(options) {
2174
- // Init configuration
2175
- const config = Object.assign({ doneFunnction: undefined, timeout: undefined }, options);
2176
- return new Promise((resolve) => {
2177
- // Init variables
2178
- let resolved = false;
2179
- let timeoutId;
2180
- // Init loading
2181
- const images = Array.from(document.getElementsByTagName('img'));
2182
- const promises = [];
2183
- // Prepare images
2184
- for (const image of images) {
2185
- const src = image.getAttribute('src');
2186
- // Check if changes are necessary and possible
2187
- if ((image.complete && image.naturalHeight !== 0) || image.getAttribute('loading') !== 'lazy' || !src) {
2188
- continue;
2189
- }
2190
- const newImage = document.createElement('img');
2191
- Array.from(image.attributes).forEach((attr) => {
2192
- if (attr.name !== 'loading' && attr.name !== 'src') {
2193
- newImage.setAttribute(attr.name, attr.value);
2194
- }
2195
- });
2196
- image.replaceWith(newImage);
2197
- promises.push(this.loadImage(newImage, src));
2198
- }
2199
- if (config.timeout) {
2200
- timeoutId = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
2201
- resolved = true;
2202
- if (config.doneFunnction) {
2203
- yield config.doneFunction();
2204
- }
2205
- resolve(undefined);
2206
- }));
2207
- }
2208
- // Wait until all images loaded
2209
- Promise.all(promises)
2210
- .catch((e) => console.error('Error during image loading', e))
2211
- .finally(() => __awaiter(this, void 0, void 0, function* () {
2212
- // Check if already resolved
2213
- if (resolved) {
2214
- return;
2215
- }
2216
- // Stop timeout
2217
- if (timeoutId) {
2218
- clearTimeout(timeoutId);
2219
- }
2220
- // Return result
2221
- let result;
2222
- if (config.doneFunnction) {
2223
- result = yield config.doneFunction();
2224
- }
2225
- resolve(result);
2226
- }));
2227
- });
2228
- }
2229
- /**
2230
- * Load image for image element
2231
- *
2232
- * @param imageElement
2233
- * @param src
2234
- * @protected
2235
- */
2236
- loadImage(imageElement, src) {
2237
- return __awaiter(this, void 0, void 0, function* () {
2238
- src = src || imageElement.src;
2239
- return new Promise((resolve, reject) => {
2240
- imageElement.onload = () => resolve(imageElement);
2241
- imageElement.onerror = reject;
2242
- imageElement.src = src;
2243
- });
2244
- });
2245
- }
2246
2292
  }
2247
- ScrollService.ɵprov = i0.ɵɵdefineInjectable({ factory: function ScrollService_Factory() { return new ScrollService(i0.ɵɵinject(i1$1.Router), i0.ɵɵinject(BASE_MODULE_CONFIG)); }, token: ScrollService, providedIn: "root" });
2293
+ ScrollService.ɵprov = i0.ɵɵdefineInjectable({ factory: function ScrollService_Factory() { return new ScrollService(i0.ɵɵinject(i1$1.Router), i0.ɵɵinject(BASE_MODULE_CONFIG), i0.ɵɵinject(ImageService)); }, token: ScrollService, providedIn: "root" });
2248
2294
  ScrollService.decorators = [
2249
2295
  { type: Injectable, args: [{
2250
2296
  providedIn: 'root',
@@ -2252,7 +2298,8 @@ ScrollService.decorators = [
2252
2298
  ];
2253
2299
  ScrollService.ctorParameters = () => [
2254
2300
  { type: Router },
2255
- { type: undefined, decorators: [{ type: Inject, args: [BASE_MODULE_CONFIG,] }] }
2301
+ { type: undefined, decorators: [{ type: Inject, args: [BASE_MODULE_CONFIG,] }] },
2302
+ { type: ImageService }
2256
2303
  ];
2257
2304
 
2258
2305
  /**
@@ -2325,5 +2372,5 @@ function MustMatch(controlName, matchingControlName) {
2325
2372
  * Generated bundle index. Do not edit.
2326
2373
  */
2327
2374
 
2328
- export { AuthGuard, AuthService, BASE_MODULE_CONFIG, BaseModule, BasicUser, CanDeactivateGuard, ComparisonOperatorEnum, ContextMenuDirective, EllipsesPipe, FullscreenService, GraphQLEnum, GraphQLMeta, GraphQLMetaService, GraphQLPlusService, GraphQLRequestType, GraphQLService, GraphQLType, Helper, LazyLoadDirective, LoaderService, LogicalOperatorEnum, LogoutGuard, MatchHeightDirective, MustMatch, ResizableDirective, SEOService, SafeHtmlPipe, ScrollService, SortOrderEnum, Standard, StorageService, apolloOptionsFactory };
2375
+ export { AuthGuard, AuthService, BASE_MODULE_CONFIG, BaseModule, BasicUser, CanDeactivateGuard, ComparisonOperatorEnum, ContextMenuDirective, EllipsesPipe, FullscreenService, GraphQLEnum, GraphQLMeta, GraphQLMetaService, GraphQLPlusService, GraphQLRequestType, GraphQLService, GraphQLType, Helper, ImageService, LazyLoadDirective, LoaderService, LogicalOperatorEnum, LogoutGuard, MatchHeightDirective, MustMatch, ResizableDirective, SEOService, SafeHtmlPipe, ScrollService, SortOrderEnum, Standard, StorageService, apolloOptionsFactory };
2329
2376
  //# sourceMappingURL=lenne.tech-ng-base-src-lib-base.js.map