@lexriver/dome 1.2.16 → 1.4.0

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.
package/README.md CHANGED
@@ -1144,6 +1144,16 @@ Check if element is in DOM. Uses `document.body.contains(el)` internally, so it
1144
1144
 
1145
1145
  <br/>
1146
1146
 
1147
+ ## isOnScreen
1148
+
1149
+ ```typescript
1150
+ DomeManipulator.isOnScreen(el: ELement | undefined)
1151
+ ```
1152
+
1153
+ Check if element is on screen now.
1154
+
1155
+ <br/>
1156
+
1147
1157
  ## addCssClassAsync
1148
1158
 
1149
1159
  ```typescript
@@ -1597,6 +1607,7 @@ export class AnimatedListTest extends DomeComponent<Attrs>{
1597
1607
  getHtmlElement: (x:number) => { // render element
1598
1608
  return <div>the number is {x}</div>
1599
1609
  },
1610
+ emptyList:<div>Loading</div>
1600
1611
  })
1601
1612
 
1602
1613
  render(){
@@ -11,6 +11,7 @@ export declare class AnimatedArray<T> {
11
11
  getHtmlElement: (o: T) => HTMLElement;
12
12
  animationShow?: Animation;
13
13
  animationHide?: Animation;
14
+ emptyList?: HTMLElement;
14
15
  };
15
16
  arrayOfKeyToElement: KeyToElementPair[];
16
17
  constructor(params: {
@@ -20,6 +21,7 @@ export declare class AnimatedArray<T> {
20
21
  getHtmlElement: (o: T) => HTMLElement;
21
22
  animationShow?: Animation;
22
23
  animationHide?: Animation;
24
+ emptyList?: HTMLElement;
23
25
  });
24
26
  get size(): number;
25
27
  update(array: T[], parentElement?: HTMLElement | Element): void;
@@ -17,6 +17,13 @@ export class AnimatedArray {
17
17
  console.warn('AnimatedArray unable to update, no parentElement');
18
18
  return;
19
19
  }
20
+ if (array.length == 0 && this.params.emptyList) {
21
+ DomeManipulator.replaceAllChildrenAsync(parentElement, this.params.emptyList);
22
+ return;
23
+ }
24
+ if (array.length > 0 && this.params.emptyList) {
25
+ DomeManipulator.removeElementAsync(this.params.emptyList, this.params.animationHide);
26
+ }
20
27
  //let arrayOfKeyToElement:KeyToElementPair[] = []
21
28
  let newArrayOfKeys = array.map((item) => this.params.getKey(item));
22
29
  let oldArrayOfKeys = this.arrayOfKeyToElement.map(x => x.key);
@@ -18,6 +18,7 @@ export declare module DomeManipulator {
18
18
  function appendChildrenAsync(containerElement: Element, children: Element | Element[] | DocumentFragment | Text | string | null | undefined, animation?: Animation): Promise<void>;
19
19
  function replaceAllChildrenAsync(containerElement: Element, childrenToInsert: Element | Element[] | DocumentFragment | Text | string | null | undefined, animationForHide?: Animation, animationForShow?: Animation): Promise<void>;
20
20
  function isInDom(el: Element | undefined): boolean;
21
+ function isOnScreen(el: Element | undefined): boolean;
21
22
  function addCssClassAsync(element: Element, cssClassName: string, removeAfterMs?: number): Promise<void>;
22
23
  function addCssClassesAsync(element: Element, cssClassNames: string[], removeAfterMs?: number): Promise<void>;
23
24
  function removeCssClass(element: Element, cssClassName: string): void;
@@ -173,6 +173,14 @@ export var DomeManipulator;
173
173
  return document.body.contains(el);
174
174
  }
175
175
  DomeManipulator.isInDom = isInDom;
176
+ function isOnScreen(el) {
177
+ if (!el)
178
+ return false;
179
+ var rect = el.getBoundingClientRect();
180
+ var viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
181
+ return !(rect.bottom < 0 || rect.top - viewHeight >= 0);
182
+ }
183
+ DomeManipulator.isOnScreen = isOnScreen;
176
184
  async function addCssClassAsync(element, cssClassName, removeAfterMs) {
177
185
  if (element.nodeType !== Node.ELEMENT_NODE)
178
186
  return;
@@ -253,17 +261,15 @@ export var DomeManipulator;
253
261
  }
254
262
  DomeManipulator.hasFocus = hasFocus;
255
263
  function scrollIntoView(element, paddingFromTop = 100) {
256
- const positionBefore = getCurrentScrollPosition();
264
+ if (isOnScreen(element)) {
265
+ return; // no need to scroll
266
+ }
267
+ //const positionBefore = getCurrentScrollPosition()
257
268
  //element.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
258
269
  //const positionAfter = getCurrentScrollPosition()
259
270
  //console.log('before=', positionBefore, 'after=', positionAfter)
260
271
  let expectedPosition = element.getBoundingClientRect().top + window.pageYOffset;
261
- if (expectedPosition < positionBefore) {
262
- expectedPosition += paddingFromTop;
263
- }
264
- else {
265
- expectedPosition -= paddingFromTop;
266
- }
272
+ expectedPosition -= paddingFromTop;
267
273
  window.scrollTo({ top: expectedPosition, behavior: 'smooth' });
268
274
  }
269
275
  DomeManipulator.scrollIntoView = scrollIntoView;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "type": "module",
3
3
  "sideEffects": false,
4
4
  "name": "@lexriver/dome",
5
- "version": "1.2.16",
5
+ "version": "1.4.0",
6
6
  "description": "",
7
7
  "main": "out/index.js",
8
8
  "types": "out/index.d.ts",
@@ -17,6 +17,8 @@ export class AnimatedArray<T>{
17
17
  getHtmlElement:(o:T)=>HTMLElement,
18
18
  animationShow?:Animation
19
19
  animationHide?:Animation
20
+ //renderEmptyList?:()=>HTMLElement
21
+ emptyList?:HTMLElement
20
22
  }){
21
23
  if(params.array && params.parentElement){
22
24
  this.update(params.array)
@@ -34,6 +36,15 @@ export class AnimatedArray<T>{
34
36
  return
35
37
  }
36
38
 
39
+ if(array.length == 0 && this.params.emptyList){
40
+ DomeManipulator.replaceAllChildrenAsync(parentElement, this.params.emptyList)
41
+ return
42
+ }
43
+
44
+ if(array.length > 0 && this.params.emptyList){
45
+ DomeManipulator.removeElementAsync(this.params.emptyList, this.params.animationHide)
46
+ }
47
+
37
48
  //let arrayOfKeyToElement:KeyToElementPair[] = []
38
49
  let newArrayOfKeys = array.map((item:T) => this.params.getKey(item))
39
50
  let oldArrayOfKeys = this.arrayOfKeyToElement.map(x => x.key)
@@ -201,6 +201,13 @@ export module DomeManipulator {
201
201
  return document.body.contains(el)
202
202
  }
203
203
 
204
+ export function isOnScreen(el:Element|undefined){
205
+ if(!el) return false
206
+ var rect = el.getBoundingClientRect()
207
+ var viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight)
208
+ return !(rect.bottom < 0 || rect.top - viewHeight >= 0)
209
+ }
210
+
204
211
  export async function addCssClassAsync(element: Element, cssClassName: string, removeAfterMs?:number) {
205
212
  if(element.nodeType !== Node.ELEMENT_NODE) return
206
213
  element.classList.add(cssClassName)
@@ -285,17 +292,18 @@ export module DomeManipulator {
285
292
  }
286
293
 
287
294
  export function scrollIntoView(element: Element, paddingFromTop:number = 100) {
288
- const positionBefore = getCurrentScrollPosition()
295
+ if(isOnScreen(element)) {
296
+ return // no need to scroll
297
+ }
298
+ //const positionBefore = getCurrentScrollPosition()
289
299
  //element.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
290
300
  //const positionAfter = getCurrentScrollPosition()
291
301
  //console.log('before=', positionBefore, 'after=', positionAfter)
292
302
  let expectedPosition = element.getBoundingClientRect().top + window.pageYOffset;
293
- if(expectedPosition<positionBefore){
294
- expectedPosition += paddingFromTop
295
- } else {
296
- expectedPosition -= paddingFromTop
297
- }
303
+ expectedPosition -= paddingFromTop
298
304
  window.scrollTo({top: expectedPosition, behavior: 'smooth'});
305
+
306
+
299
307
 
300
308
  }
301
309