@creative-web-solution/front-library 7.1.47 → 7.1.48

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.
@@ -1,6 +1,5 @@
1
- import { extend } from '../Helpers/Extend';
2
- import { slice } from '../Helpers/Slice';
3
-
1
+ import { extend } from "../Helpers/Extend";
2
+ import { slice } from "../Helpers/Slice";
4
3
 
5
4
  /**
6
5
  * Manage media queries events
@@ -70,6 +69,10 @@ import { slice } from '../Helpers/Slice';
70
69
  * myBreakpoint = mquery.currentBreakpoint; // => { name, query, ... } or false
71
70
  * myBreakpoint.is( 'mob' ); // => true|false
72
71
  * myBreakpoint.in( ['mob', 'tab'] ); // => true|false
72
+ * myBreakpoint.isBefore('tablet'); // => true|false
73
+ * myBreakpoint.isBeforeOrSame('tablet'); // => true|false
74
+ * myBreakpoint.isAfter('tablet'); // => true|false
75
+ * myBreakpoint.isAfterOrSame('tablet'); // => true|false
73
76
  *
74
77
  * // List of all breakpoints
75
78
  * array = mquery.list;
@@ -85,17 +88,21 @@ import { slice } from '../Helpers/Slice';
85
88
  * ```
86
89
  */
87
90
  export default class MediaQueriesEvents {
88
-
89
- #breakpointsList: FLib.Events.MediaqueriesEvents.Breakpoint[];
91
+ #breakpointsList: FLib.Events.MediaqueriesEvents.Breakpoint[];
90
92
  #currentBreakpoint: FLib.Events.MediaqueriesEvents.Breakpoint | undefined;
91
- #functionHash: Record<string, FLib.Events.MediaqueriesEvents.InternalCallbackType[]>;
92
- #isSuspended = false;
93
- #options: FLib.Events.MediaqueriesEvents.Options;
93
+ #functionHash: Record<
94
+ string,
95
+ FLib.Events.MediaqueriesEvents.InternalCallbackType[]
96
+ >;
97
+ #isSuspended = false;
98
+ #options: FLib.Events.MediaqueriesEvents.Options;
94
99
 
95
- #globalHashName = '__globalHashName';
100
+ #globalHashName = "__globalHashName";
96
101
 
97
102
  /** Current active breakpoint */
98
- get currentBreakpoint(): FLib.Events.MediaqueriesEvents.Breakpoint | undefined {
103
+ get currentBreakpoint():
104
+ | FLib.Events.MediaqueriesEvents.Breakpoint
105
+ | undefined {
99
106
  return this.#currentBreakpoint;
100
107
  }
101
108
 
@@ -104,52 +111,67 @@ export default class MediaQueriesEvents {
104
111
  return this.#breakpointsList;
105
112
  }
106
113
 
107
-
108
- constructor( breakpointsList: FLib.Events.MediaqueriesEvents.ListOptions[], userOptions: FLib.Events.MediaqueriesEvents.Options ) {
114
+ constructor(
115
+ breakpointsList: FLib.Events.MediaqueriesEvents.ListOptions[],
116
+ userOptions: FLib.Events.MediaqueriesEvents.Options,
117
+ ) {
109
118
  const DEFAULT_OPTIONS = {
110
- "unit": "px"
119
+ unit: "px",
111
120
  };
112
121
 
113
- this.#options = extend(
114
- {},
115
- DEFAULT_OPTIONS,
116
- userOptions
117
- );
122
+ this.#options = extend({}, DEFAULT_OPTIONS, userOptions);
118
123
 
119
124
  this.#functionHash = {
120
- [ `${ this.#globalHashName }` ]: []
125
+ [`${this.#globalHashName}`]: [],
121
126
  };
122
127
 
123
128
  // Handlers
124
- this.#breakpointsList = breakpointsList.map( breakpointItem => {
129
+ this.#breakpointsList = breakpointsList.map((breakpointItem, index) => {
125
130
  const breakpoint: FLib.Events.MediaqueriesEvents.Breakpoint = {
126
131
  ...breakpointItem,
127
- "query": breakpointItem.query || this.#createQuery( breakpointItem ),
128
- "handler": mql => {
129
- if ( mql.matches ) {
132
+ index,
133
+ query:
134
+ breakpointItem.query || this.#createQuery(breakpointItem),
135
+ handler: (mql) => {
136
+ if (mql.matches) {
130
137
  this.#currentBreakpoint = breakpoint;
131
138
  }
132
139
 
133
- if ( !this.#isSuspended ) {
134
- this.#update( breakpoint, mql.matches );
140
+ if (!this.#isSuspended) {
141
+ this.#update(breakpoint, mql.matches);
135
142
  }
136
143
  },
137
- "in": breakpointNameList => {
138
- if ( !breakpointNameList || !breakpointNameList.length ) {
144
+ in: (breakpointNameList) => {
145
+ if (!breakpointNameList || !breakpointNameList.length) {
139
146
  return false;
140
147
  }
141
- return breakpointNameList.includes( breakpoint.name );
148
+ return breakpointNameList.includes(breakpoint.name);
142
149
  },
143
- "is": breakpointName => breakpoint.name === breakpointName
150
+ is: (breakpointName) => breakpoint.name === breakpointName,
151
+ isBefore: (breakpointName: string) =>
152
+ this.#checkPosition(breakpointName, "before", breakpoint),
153
+ isBeforeOrSame: (breakpointName: string) =>
154
+ this.#checkPosition(
155
+ breakpointName,
156
+ "beforeSame",
157
+ breakpoint,
158
+ ),
159
+ isAfter: (breakpointName: string) =>
160
+ this.#checkPosition(breakpointName, "after", breakpoint),
161
+ isAfterOrSame: (breakpointName: string) =>
162
+ this.#checkPosition(
163
+ breakpointName,
164
+ "afterSame",
165
+ breakpoint,
166
+ ),
144
167
  };
145
168
 
146
- if ( breakpoint.query.addEventListener ) {
147
- breakpoint.query.addEventListener( "change", breakpoint.handler );
169
+ if (breakpoint.query.addEventListener) {
170
+ breakpoint.query.addEventListener("change", breakpoint.handler);
171
+ } else {
172
+ breakpoint.query.addListener(breakpoint.handler);
148
173
  }
149
- else {
150
- breakpoint.query.addListener( breakpoint.handler );
151
- }
152
- this.#functionHash[ breakpoint.name ] = [];
174
+ this.#functionHash[breakpoint.name] = [];
153
175
 
154
176
  return breakpoint;
155
177
  });
@@ -157,76 +179,146 @@ export default class MediaQueriesEvents {
157
179
  this.#currentBreakpoint = this.#getCurrentBreakpoint();
158
180
  }
159
181
 
182
+ isBefore(breakpointName: string): boolean {
183
+ return this.#checkPosition(
184
+ breakpointName,
185
+ "before",
186
+ this.#currentBreakpoint,
187
+ );
188
+ }
160
189
 
161
- // TOOLS
190
+ isBeforeOrSame(breakpointName: string): boolean {
191
+ return this.#checkPosition(
192
+ breakpointName,
193
+ "beforeSame",
194
+ this.#currentBreakpoint,
195
+ );
196
+ }
162
197
 
198
+ isAfter(breakpointName: string): boolean {
199
+ return this.#checkPosition(
200
+ breakpointName,
201
+ "after",
202
+ this.#currentBreakpoint,
203
+ );
204
+ }
163
205
 
164
- #processList = ( list: FLib.Events.MediaqueriesEvents.InternalCallbackType[], breakpoint: FLib.Events.MediaqueriesEvents.Breakpoint, isMatching: boolean ): void => {
165
- list.forEach( obj => {
166
- if (
167
- obj.type === 'both' ||
168
- isMatching && obj.type === 'enter' ||
169
- !isMatching && obj.type === 'leave'
170
- ) {
171
- obj.callback( breakpoint, isMatching )
172
- }
173
- } );
206
+ isAfterOrSame(breakpointName: string): boolean {
207
+ return this.#checkPosition(
208
+ breakpointName,
209
+ "afterSame",
210
+ this.#currentBreakpoint,
211
+ );
174
212
  }
175
213
 
214
+ // TOOLS
176
215
 
177
- // Call each registered function
178
- #update = ( breakpoint: FLib.Events.MediaqueriesEvents.Breakpoint, isMatching: boolean ): void => {
179
- if ( this.#functionHash[ breakpoint.name ] && this.#functionHash[ breakpoint.name ].length ) {
180
- this.#processList( this.#functionHash[ breakpoint.name ], breakpoint, isMatching );
216
+ #checkPosition(
217
+ breakpointName: string,
218
+ type: "before" | "beforeSame" | "after" | "afterSame" | "same",
219
+ targetBreakpoint?: FLib.Events.MediaqueriesEvents.Breakpoint,
220
+ ): boolean {
221
+ const breakpoint = this.#getBreakpoint(breakpointName);
222
+ if (!targetBreakpoint || !breakpoint) {
223
+ return false;
181
224
  }
182
225
 
183
- if ( this.#functionHash[ this.#globalHashName ].length ) {
184
- this.#processList( this.#functionHash[ this.#globalHashName ], breakpoint, isMatching );
226
+ if (type === "same") {
227
+ return targetBreakpoint.index === breakpoint.index;
228
+ } else if (type === "before") {
229
+ return targetBreakpoint.index < breakpoint.index;
230
+ } else if (type === "beforeSame") {
231
+ return targetBreakpoint.index <= breakpoint.index;
232
+ } else if (type === "after") {
233
+ return targetBreakpoint.index > breakpoint.index;
234
+ } else if (type === "afterSame") {
235
+ return targetBreakpoint.index >= breakpoint.index;
185
236
  }
186
- }
187
-
188
237
 
189
- #getBreakpoint = ( breakpointName: string ): FLib.Events.MediaqueriesEvents.Breakpoint | undefined => {
190
- return this.#breakpointsList.find( bp => bp.name === breakpointName );
238
+ return false;
191
239
  }
192
240
 
241
+ #processList = (
242
+ list: FLib.Events.MediaqueriesEvents.InternalCallbackType[],
243
+ breakpoint: FLib.Events.MediaqueriesEvents.Breakpoint,
244
+ isMatching: boolean,
245
+ ): void => {
246
+ list.forEach((obj) => {
247
+ if (
248
+ obj.type === "both" ||
249
+ (isMatching && obj.type === "enter") ||
250
+ (!isMatching && obj.type === "leave")
251
+ ) {
252
+ obj.callback(breakpoint, isMatching);
253
+ }
254
+ });
255
+ };
193
256
 
194
- #getCurrentBreakpoint = (): FLib.Events.MediaqueriesEvents.Breakpoint | undefined => {
195
- return this.#breakpointsList.find( bp => bp.query.matches );
196
- }
197
-
257
+ // Call each registered function
258
+ #update = (
259
+ breakpoint: FLib.Events.MediaqueriesEvents.Breakpoint,
260
+ isMatching: boolean,
261
+ ): void => {
262
+ if (
263
+ this.#functionHash[breakpoint.name] &&
264
+ this.#functionHash[breakpoint.name].length
265
+ ) {
266
+ this.#processList(
267
+ this.#functionHash[breakpoint.name],
268
+ breakpoint,
269
+ isMatching,
270
+ );
271
+ }
198
272
 
199
- #createQuery = ( breakpoint: FLib.Events.MediaqueriesEvents.ListOptions ): MediaQueryList => {
273
+ if (this.#functionHash[this.#globalHashName].length) {
274
+ this.#processList(
275
+ this.#functionHash[this.#globalHashName],
276
+ breakpoint,
277
+ isMatching,
278
+ );
279
+ }
280
+ };
281
+
282
+ #getBreakpoint = (
283
+ breakpointName: string,
284
+ ): FLib.Events.MediaqueriesEvents.Breakpoint | undefined => {
285
+ return this.#breakpointsList.find((bp) => bp.name === breakpointName);
286
+ };
287
+
288
+ #getCurrentBreakpoint = ():
289
+ | FLib.Events.MediaqueriesEvents.Breakpoint
290
+ | undefined => {
291
+ return this.#breakpointsList.find((bp) => bp.query.matches);
292
+ };
293
+
294
+ #createQuery = (
295
+ breakpoint: FLib.Events.MediaqueriesEvents.ListOptions,
296
+ ): MediaQueryList => {
200
297
  let minQuery, maxQuery, query;
201
298
 
202
- if ( typeof breakpoint.min === 'number' ) {
203
- minQuery = `(min-width:${ breakpoint.min }${ this.#options.unit })`;
299
+ if (typeof breakpoint.min === "number") {
300
+ minQuery = `(min-width:${breakpoint.min}${this.#options.unit})`;
204
301
  }
205
302
 
206
- if ( typeof breakpoint.max === 'number' ) {
207
- maxQuery = `(max-width:${ breakpoint.max }${ this.#options.unit })`;
303
+ if (typeof breakpoint.max === "number") {
304
+ maxQuery = `(max-width:${breakpoint.max}${this.#options.unit})`;
208
305
  }
209
306
 
210
- if ( minQuery && maxQuery ) {
211
- query = `${ minQuery } and ${ maxQuery }`;
212
- }
213
- else if ( minQuery ) {
307
+ if (minQuery && maxQuery) {
308
+ query = `${minQuery} and ${maxQuery}`;
309
+ } else if (minQuery) {
214
310
  query = minQuery;
215
- }
216
- else if ( maxQuery ) {
311
+ } else if (maxQuery) {
217
312
  query = maxQuery;
218
- }
219
- else {
220
- throw `MEDIA QUERIES EVENTS: Need at least 'min' or 'max' property to create a breakpoint: ${ breakpoint.name }`;
313
+ } else {
314
+ throw `MEDIA QUERIES EVENTS: Need at least 'min' or 'max' property to create a breakpoint: ${breakpoint.name}`;
221
315
  }
222
316
 
223
- return window.matchMedia( query );
224
- }
225
-
317
+ return window.matchMedia(query);
318
+ };
226
319
 
227
320
  // API
228
321
 
229
-
230
322
  /**
231
323
  * Return the value of the property with the name of the current breakpoint of an object
232
324
  *
@@ -236,141 +328,154 @@ export default class MediaQueriesEvents {
236
328
  * mediaQueryEvent.getValue( { "small": "val1", "medium": "val2", ... } );
237
329
  * ```
238
330
  */
239
- getValue<Value>( obj: Record<string, Value> ): Value | undefined {
240
- if ( !this.#currentBreakpoint ) {
331
+ getValue<Value>(obj: Record<string, Value>): Value | undefined {
332
+ if (!this.#currentBreakpoint) {
241
333
  return;
242
334
  }
243
- return obj[ this.#currentBreakpoint.name ];
335
+ return obj[this.#currentBreakpoint.name];
244
336
  }
245
337
 
246
-
247
338
  /**
248
339
  * Bind a function to be called on a specific breakpoint
249
340
  *
250
341
  * @param callback - Callback
251
342
  * @param breakpointName - Name of the breakpoint
252
343
  * @param type - Select when the function will be called: when entering the query, when leaving it, or on both
253
- */
254
- on( callback: FLib.Events.MediaqueriesEvents.Callback, breakpointName: string, type: FLib.Events.MediaqueriesEvents.CallbackType = 'enter' ): this {
255
- if ( !this.#functionHash[ breakpointName ] ) {
344
+ */
345
+ on(
346
+ callback: FLib.Events.MediaqueriesEvents.Callback,
347
+ breakpointName: string,
348
+ type: FLib.Events.MediaqueriesEvents.CallbackType = "enter",
349
+ ): this {
350
+ if (!this.#functionHash[breakpointName]) {
256
351
  return this;
257
352
  }
258
353
 
259
- this.#functionHash[ breakpointName ].push( {
354
+ this.#functionHash[breakpointName].push({
260
355
  callback,
261
- type
262
- } );
356
+ type,
357
+ });
263
358
 
264
359
  return this;
265
360
  }
266
361
 
267
-
268
362
  /**
269
363
  * Unbind a function to be called on a specific breakpoint
270
364
  *
271
365
  * @param callback - Function to remove from the registered function list
272
366
  * @param breakpointName - Name of the breakpoint
273
- */
274
- off( callback: FLib.Events.MediaqueriesEvents.Callback, breakpointName: string ): this {
275
- if ( !this.#functionHash[ breakpointName ] ) {
367
+ */
368
+ off(
369
+ callback: FLib.Events.MediaqueriesEvents.Callback,
370
+ breakpointName: string,
371
+ ): this {
372
+ if (!this.#functionHash[breakpointName]) {
276
373
  return this;
277
374
  }
278
375
 
279
- const obj = this.#functionHash[ breakpointName ].find( o => o.callback === callback );
376
+ const obj = this.#functionHash[breakpointName].find(
377
+ (o) => o.callback === callback,
378
+ );
280
379
 
281
- slice( this.#functionHash[ breakpointName ], obj );
380
+ slice(this.#functionHash[breakpointName], obj);
282
381
 
283
382
  return this;
284
383
  }
285
384
 
286
-
287
385
  /**
288
386
  * Register a function to be called on all media queries change
289
387
  *
290
388
  * @param callback - Function to call on mediaquery change
291
389
  * @param type - Select when the function will be called: when entering the query, when leaving it, or on both
292
- */
293
- register( callback: FLib.Events.MediaqueriesEvents.Callback, type: FLib.Events.MediaqueriesEvents.CallbackType = 'enter' ): this {
294
- this.#functionHash[ this.#globalHashName ].push({
390
+ */
391
+ register(
392
+ callback: FLib.Events.MediaqueriesEvents.Callback,
393
+ type: FLib.Events.MediaqueriesEvents.CallbackType = "enter",
394
+ ): this {
395
+ this.#functionHash[this.#globalHashName].push({
295
396
  callback,
296
- type
397
+ type,
297
398
  });
298
399
 
299
400
  return this;
300
401
  }
301
402
 
302
-
303
403
  /**
304
404
  * Unregister a function
305
405
  *
306
406
  * @param callback - Function to remove from the registered function list
307
- */
308
- remove( callback: FLib.Events.MediaqueriesEvents.Callback ): this {
309
- const obj = this.#functionHash[ this.#globalHashName ].find( o => o.callback === callback );
407
+ */
408
+ remove(callback: FLib.Events.MediaqueriesEvents.Callback): this {
409
+ const obj = this.#functionHash[this.#globalHashName].find(
410
+ (o) => o.callback === callback,
411
+ );
310
412
 
311
- slice( this.#functionHash[ this.#globalHashName ], obj );
413
+ slice(this.#functionHash[this.#globalHashName], obj);
312
414
 
313
415
  return this;
314
416
  }
315
417
 
316
-
317
418
  /**
318
419
  * Force the refresh of all registered function
319
- */
420
+ */
320
421
  refresh(): this {
321
- if ( !this.#currentBreakpoint ) {
422
+ if (!this.#currentBreakpoint) {
322
423
  return this;
323
424
  }
324
425
 
325
- this.#update( this.#currentBreakpoint, this.#currentBreakpoint.query.matches );
426
+ this.#update(
427
+ this.#currentBreakpoint,
428
+ this.#currentBreakpoint.query.matches,
429
+ );
326
430
 
327
431
  return this;
328
432
  }
329
433
 
330
-
331
434
  /**
332
435
  * Call a function with the current breakpoint
333
436
  *
334
437
  * @param callback - Function to call
335
438
  */
336
- get( callback: FLib.Events.MediaqueriesEvents.Callback ): this {
337
- if ( !this.#currentBreakpoint || !callback ) {
439
+ get(callback: FLib.Events.MediaqueriesEvents.Callback): this {
440
+ if (!this.#currentBreakpoint || !callback) {
338
441
  return this;
339
442
  }
340
443
 
341
- callback( this.#currentBreakpoint, this.#currentBreakpoint.query.matches );
444
+ callback(
445
+ this.#currentBreakpoint,
446
+ this.#currentBreakpoint.query.matches,
447
+ );
342
448
 
343
449
  return this;
344
450
  }
345
451
 
346
-
347
452
  /**
348
453
  * Check if we are in a specific range
349
454
  *
350
455
  * @param breakpointName - Name of a breakpoint
351
456
  */
352
- is( breakpointName: string ): boolean {
353
- const breakpoint = this.#getBreakpoint( breakpointName );
354
-
355
- return breakpoint ? breakpoint.query.matches : false;
457
+ is(breakpointName: string): boolean {
458
+ return this.#checkPosition(
459
+ breakpointName,
460
+ "same",
461
+ this.#currentBreakpoint,
462
+ );
356
463
  }
357
464
 
358
-
359
465
  /**
360
466
  * Check if the current breakpoint is in a list
361
467
  *
362
468
  * @param breakpointNameList - Array of breakpoint name
363
469
  */
364
- in( breakpointNameList: string[] ): boolean {
365
- if ( !this.#currentBreakpoint || !breakpointNameList || !breakpointNameList.length ) {
470
+ in(breakpointNameList: string[]): boolean {
471
+ if (!this.#currentBreakpoint || !breakpointNameList?.length) {
366
472
  return false;
367
473
  }
368
- return breakpointNameList.includes( this.#currentBreakpoint.name );
474
+ return breakpointNameList.includes(this.#currentBreakpoint.name);
369
475
  }
370
476
 
371
-
372
477
  /**
373
- * Stop media queries callback
478
+ * Stop media queries callback
374
479
  */
375
480
  suspend(): this {
376
481
  this.#isSuspended = true;
@@ -378,7 +483,6 @@ export default class MediaQueriesEvents {
378
483
  return this;
379
484
  }
380
485
 
381
-
382
486
  /**
383
487
  * Active media queries callback
384
488
  */
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Front Library
2
2
 
3
- @version: 7.1.47
3
+ @version: 7.1.48
4
4
 
5
5
 
6
6
  ## Use