@fleetbase/ember-core 0.2.18 → 0.2.20
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/addon/services/current-user.js +23 -5
- package/addon/services/fetch.js +12 -1
- package/addon/services/universe.js +132 -16
- package/addon/services/url-search-params.js +189 -35
- package/addon/utils/inject-engine-service.js +7 -2
- package/addon/utils/register-component.js +8 -0
- package/addon/utils/register-helper.js +8 -0
- package/app/utils/register-component.js +1 -0
- package/app/utils/register-helper.js +1 -0
- package/package.json +1 -1
|
@@ -32,6 +32,8 @@ export default class CurrentUserService extends Service.extend(Evented) {
|
|
|
32
32
|
@alias('user.is_admin') isAdmin;
|
|
33
33
|
@alias('user.company_uuid') companyId;
|
|
34
34
|
@alias('user.company_name') companyName;
|
|
35
|
+
@alias('user.role_name') roleName;
|
|
36
|
+
@alias('user.role') role;
|
|
35
37
|
|
|
36
38
|
@computed('id') get optionsPrefix() {
|
|
37
39
|
return `${this.id}:`;
|
|
@@ -94,8 +96,18 @@ export default class CurrentUserService extends Service.extend(Evented) {
|
|
|
94
96
|
// Set environment from user option
|
|
95
97
|
this.theme.setEnvironment();
|
|
96
98
|
|
|
97
|
-
//
|
|
98
|
-
|
|
99
|
+
// Set locale
|
|
100
|
+
if (user.locale) {
|
|
101
|
+
this.setLocale(user.locale);
|
|
102
|
+
} else {
|
|
103
|
+
await this.loadLocale();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Load user whois data
|
|
107
|
+
await this.loadWhois();
|
|
108
|
+
|
|
109
|
+
// Load user organizations
|
|
110
|
+
await this.loadOrganizations();
|
|
99
111
|
|
|
100
112
|
// Optional callback
|
|
101
113
|
if (typeof options?.onUserResolved === 'function') {
|
|
@@ -118,9 +130,7 @@ export default class CurrentUserService extends Service.extend(Evented) {
|
|
|
118
130
|
async loadLocale() {
|
|
119
131
|
try {
|
|
120
132
|
const { locale } = await this.fetch.get('users/locale');
|
|
121
|
-
this.
|
|
122
|
-
this.intl.setLocale(locale);
|
|
123
|
-
this.locale = locale;
|
|
133
|
+
this.setLocale(locale);
|
|
124
134
|
|
|
125
135
|
return locale;
|
|
126
136
|
} catch (error) {
|
|
@@ -207,6 +217,14 @@ export default class CurrentUserService extends Service.extend(Evented) {
|
|
|
207
217
|
return this.getWhoisProperty(key);
|
|
208
218
|
}
|
|
209
219
|
|
|
220
|
+
setLocale(locale) {
|
|
221
|
+
this.setOption('locale', locale);
|
|
222
|
+
this.intl.setLocale(locale);
|
|
223
|
+
this.locale = locale;
|
|
224
|
+
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
|
|
210
228
|
setOption(key, value) {
|
|
211
229
|
key = `${this.optionsPrefix}${dasherize(key)}`;
|
|
212
230
|
|
package/addon/services/fetch.js
CHANGED
|
@@ -360,6 +360,7 @@ export default class FetchService extends Service {
|
|
|
360
360
|
const pathKeyVersion = new Date().toISOString();
|
|
361
361
|
|
|
362
362
|
const request = () => {
|
|
363
|
+
delete options.fromCache;
|
|
363
364
|
return this.get(path, query, options).then((response) => {
|
|
364
365
|
// cache the response
|
|
365
366
|
this.localCache.set(pathKey, response);
|
|
@@ -392,7 +393,7 @@ export default class FetchService extends Service {
|
|
|
392
393
|
// if the version is older than 3 days clear it
|
|
393
394
|
if (!version || shouldExpire || options.clearData === true) {
|
|
394
395
|
this.flushRequestCache(path);
|
|
395
|
-
return request();
|
|
396
|
+
return request().then(resolve);
|
|
396
397
|
}
|
|
397
398
|
|
|
398
399
|
if (options.normalizeToEmberData) {
|
|
@@ -408,6 +409,11 @@ export default class FetchService extends Service {
|
|
|
408
409
|
return request();
|
|
409
410
|
}
|
|
410
411
|
|
|
412
|
+
/**
|
|
413
|
+
* Flushes the local cache for a specific path by setting its value and version to undefined.
|
|
414
|
+
*
|
|
415
|
+
* @param {string} path - The path for which the cache should be flushed.
|
|
416
|
+
*/
|
|
411
417
|
flushRequestCache(path) {
|
|
412
418
|
const pathKey = dasherize(path);
|
|
413
419
|
|
|
@@ -415,6 +421,11 @@ export default class FetchService extends Service {
|
|
|
415
421
|
this.localCache.set(`${pathKey}-version`, undefined);
|
|
416
422
|
}
|
|
417
423
|
|
|
424
|
+
/**
|
|
425
|
+
* Determines whether the cache should be reset by comparing the current version
|
|
426
|
+
* of the console with the cached version. If they differ, the cache is cleared
|
|
427
|
+
* and the new version is saved.
|
|
428
|
+
*/
|
|
418
429
|
shouldResetCache() {
|
|
419
430
|
const consoleVersion = this.localCache.get('console-version');
|
|
420
431
|
|
|
@@ -19,6 +19,7 @@ import config from 'ember-get-config';
|
|
|
19
19
|
export default class UniverseService extends Service.extend(Evented) {
|
|
20
20
|
@service router;
|
|
21
21
|
@service intl;
|
|
22
|
+
@service urlSearchParams;
|
|
22
23
|
@tracked applicationInstance;
|
|
23
24
|
@tracked enginesBooted = false;
|
|
24
25
|
@tracked bootedExtensions = A([]);
|
|
@@ -42,6 +43,7 @@ export default class UniverseService extends Service.extend(Evented) {
|
|
|
42
43
|
widgets: A([]),
|
|
43
44
|
};
|
|
44
45
|
@tracked hooks = {};
|
|
46
|
+
@tracked bootCallbacks = A([]);
|
|
45
47
|
|
|
46
48
|
/**
|
|
47
49
|
* Computed property that returns all administrative menu items.
|
|
@@ -135,6 +137,17 @@ export default class UniverseService extends Service.extend(Evented) {
|
|
|
135
137
|
return this.router.transitionTo(route, ...args);
|
|
136
138
|
}
|
|
137
139
|
|
|
140
|
+
/**
|
|
141
|
+
* Sets the application instance.
|
|
142
|
+
*
|
|
143
|
+
* @param {ApplicationInstance} - The application instance object.
|
|
144
|
+
* @return {void}
|
|
145
|
+
*/
|
|
146
|
+
setApplicationInstance(instance) {
|
|
147
|
+
window.Fleetbase = instance;
|
|
148
|
+
this.applicationInstance = instance;
|
|
149
|
+
}
|
|
150
|
+
|
|
138
151
|
/**
|
|
139
152
|
* Retrieves the application instance.
|
|
140
153
|
*
|
|
@@ -261,6 +274,43 @@ export default class UniverseService extends Service.extend(Evented) {
|
|
|
261
274
|
return this.router.transitionTo(route, slug);
|
|
262
275
|
}
|
|
263
276
|
|
|
277
|
+
/**
|
|
278
|
+
* Redirects to a virtual route if a corresponding menu item exists based on the current URL slug.
|
|
279
|
+
*
|
|
280
|
+
* This asynchronous function checks whether a virtual route exists by extracting the slug from the current
|
|
281
|
+
* window's pathname and looking up a matching menu item in a specified registry. If a matching menu item
|
|
282
|
+
* is found, it initiates a transition to the given route associated with that menu item and returns the
|
|
283
|
+
* transition promise.
|
|
284
|
+
*
|
|
285
|
+
* @async
|
|
286
|
+
*
|
|
287
|
+
* @param {Object} transition - The current transition object from the router.
|
|
288
|
+
* Used to retrieve additional information required for the menu item lookup.
|
|
289
|
+
* @param {string} registryName - The name of the registry to search for the menu item.
|
|
290
|
+
* This registry should contain menu items mapped by their slugs.
|
|
291
|
+
* @param {string} route - The name of the route to transition to if the menu item is found.
|
|
292
|
+
* This is typically the route associated with displaying the menu item's content.
|
|
293
|
+
*
|
|
294
|
+
* @returns {Promise|undefined} - Returns a promise that resolves when the route transition completes
|
|
295
|
+
* if a matching menu item is found. If no matching menu item is found, the function returns undefined.
|
|
296
|
+
*
|
|
297
|
+
*/
|
|
298
|
+
async virtualRouteRedirect(transition, registryName, route, options = {}) {
|
|
299
|
+
const view = this.getViewFromTransition(transition);
|
|
300
|
+
const slug = window.location.pathname.replace('/', '');
|
|
301
|
+
const queryParams = this.urlSearchParams.all();
|
|
302
|
+
const menuItem = await this.lookupMenuItemFromRegistry(registryName, slug, view);
|
|
303
|
+
if (menuItem && transition.from === null) {
|
|
304
|
+
return this.transitionMenuItem(route, menuItem, { queryParams }).then((transition) => {
|
|
305
|
+
if (options && options.restoreQueryParams === true) {
|
|
306
|
+
this.urlSearchParams.setParamsToCurrentUrl(queryParams);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return transition;
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
264
314
|
/**
|
|
265
315
|
* @action
|
|
266
316
|
* Creates a new registry with the given name and options.
|
|
@@ -777,9 +827,11 @@ export default class UniverseService extends Service.extend(Evented) {
|
|
|
777
827
|
*/
|
|
778
828
|
registerMenuPanel(registryName, title, items = [], options = {}) {
|
|
779
829
|
const internalRegistryName = this.createInternalRegistryName(registryName);
|
|
830
|
+
const intl = this._getOption(options, 'intl', null);
|
|
780
831
|
const open = this._getOption(options, 'open', true);
|
|
781
832
|
const slug = this._getOption(options, 'slug', dasherize(title));
|
|
782
833
|
const menuPanel = {
|
|
834
|
+
intl,
|
|
783
835
|
title,
|
|
784
836
|
open,
|
|
785
837
|
items: items.map(({ title, route, ...options }) => {
|
|
@@ -1321,6 +1373,7 @@ export default class UniverseService extends Service.extend(Evented) {
|
|
|
1321
1373
|
* @returns {Object} A new menu item object
|
|
1322
1374
|
*/
|
|
1323
1375
|
_createMenuItem(title, route, options = {}) {
|
|
1376
|
+
const intl = this._getOption(options, 'intl', null);
|
|
1324
1377
|
const priority = this._getOption(options, 'priority', 9);
|
|
1325
1378
|
const icon = this._getOption(options, 'icon', 'circle-dot');
|
|
1326
1379
|
const items = this._getOption(options, 'items');
|
|
@@ -1360,6 +1413,7 @@ export default class UniverseService extends Service.extend(Evented) {
|
|
|
1360
1413
|
// @todo: create menu item class
|
|
1361
1414
|
const menuItem = {
|
|
1362
1415
|
id,
|
|
1416
|
+
intl,
|
|
1363
1417
|
title,
|
|
1364
1418
|
text: title,
|
|
1365
1419
|
route,
|
|
@@ -1391,6 +1445,14 @@ export default class UniverseService extends Service.extend(Evented) {
|
|
|
1391
1445
|
isLoading,
|
|
1392
1446
|
};
|
|
1393
1447
|
|
|
1448
|
+
// make the menu item and universe object a default param of the onClick handler
|
|
1449
|
+
if (typeof onClick === 'function') {
|
|
1450
|
+
const universe = this;
|
|
1451
|
+
menuItem.onClick = function () {
|
|
1452
|
+
return onClick(menuItem, universe);
|
|
1453
|
+
};
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1394
1456
|
return menuItem;
|
|
1395
1457
|
}
|
|
1396
1458
|
|
|
@@ -1713,7 +1775,7 @@ export default class UniverseService extends Service.extend(Evented) {
|
|
|
1713
1775
|
clearInterval(intervalId);
|
|
1714
1776
|
reject(new Error('Timeout: Universe was unable to boot engines'));
|
|
1715
1777
|
},
|
|
1716
|
-
|
|
1778
|
+
1000 * 40
|
|
1717
1779
|
);
|
|
1718
1780
|
});
|
|
1719
1781
|
}
|
|
@@ -1730,7 +1792,7 @@ export default class UniverseService extends Service.extend(Evented) {
|
|
|
1730
1792
|
* @param {ApplicationInstance|null} owner - The Ember ApplicationInstance that owns the engines.
|
|
1731
1793
|
* @return {void}
|
|
1732
1794
|
*/
|
|
1733
|
-
bootEngines(owner = null) {
|
|
1795
|
+
async bootEngines(owner = null) {
|
|
1734
1796
|
const booted = [];
|
|
1735
1797
|
const pending = [];
|
|
1736
1798
|
const additionalCoreExtensions = config.APP.extensions ?? [];
|
|
@@ -1741,10 +1803,10 @@ export default class UniverseService extends Service.extend(Evented) {
|
|
|
1741
1803
|
}
|
|
1742
1804
|
|
|
1743
1805
|
// Set application instance
|
|
1744
|
-
this.
|
|
1806
|
+
this.setApplicationInstance(owner);
|
|
1745
1807
|
|
|
1746
1808
|
const tryBootEngine = (extension) => {
|
|
1747
|
-
this.loadEngine(extension.name).then((engineInstance) => {
|
|
1809
|
+
return this.loadEngine(extension.name).then((engineInstance) => {
|
|
1748
1810
|
if (engineInstance.base && engineInstance.base.setupExtension) {
|
|
1749
1811
|
if (this.bootedExtensions.includes(extension.name)) {
|
|
1750
1812
|
return;
|
|
@@ -1799,12 +1861,15 @@ export default class UniverseService extends Service.extend(Evented) {
|
|
|
1799
1861
|
pending.push(...stillPending);
|
|
1800
1862
|
};
|
|
1801
1863
|
|
|
1802
|
-
return loadInstalledExtensions(additionalCoreExtensions).then((extensions) => {
|
|
1803
|
-
extensions.
|
|
1804
|
-
|
|
1805
|
-
|
|
1864
|
+
return loadInstalledExtensions(additionalCoreExtensions).then(async (extensions) => {
|
|
1865
|
+
for (let i = 0; i < extensions.length; i++) {
|
|
1866
|
+
const extension = extensions[i];
|
|
1867
|
+
await tryBootEngine(extension);
|
|
1868
|
+
}
|
|
1806
1869
|
|
|
1807
|
-
this.
|
|
1870
|
+
this.runBootCallbacks(owner, () => {
|
|
1871
|
+
this.enginesBooted = true;
|
|
1872
|
+
});
|
|
1808
1873
|
});
|
|
1809
1874
|
}
|
|
1810
1875
|
|
|
@@ -1831,10 +1896,10 @@ export default class UniverseService extends Service.extend(Evented) {
|
|
|
1831
1896
|
}
|
|
1832
1897
|
|
|
1833
1898
|
// Set application instance
|
|
1834
|
-
this.
|
|
1899
|
+
this.setApplicationInstance(owner);
|
|
1835
1900
|
|
|
1836
1901
|
const tryBootEngine = (extension) => {
|
|
1837
|
-
this.loadEngine(extension.name).then((engineInstance) => {
|
|
1902
|
+
return this.loadEngine(extension.name).then((engineInstance) => {
|
|
1838
1903
|
if (engineInstance.base && engineInstance.base.setupExtension) {
|
|
1839
1904
|
const engineDependencies = getWithDefault(engineInstance.base, 'engineDependencies', []);
|
|
1840
1905
|
|
|
@@ -1883,12 +1948,15 @@ export default class UniverseService extends Service.extend(Evented) {
|
|
|
1883
1948
|
pending.push(...stillPending);
|
|
1884
1949
|
};
|
|
1885
1950
|
|
|
1886
|
-
return loadExtensions().then((extensions) => {
|
|
1887
|
-
extensions.
|
|
1888
|
-
|
|
1889
|
-
|
|
1951
|
+
return loadExtensions().then(async (extensions) => {
|
|
1952
|
+
for (let i = 0; i < extensions.length; i++) {
|
|
1953
|
+
const extension = extensions[i];
|
|
1954
|
+
await tryBootEngine(extension);
|
|
1955
|
+
}
|
|
1890
1956
|
|
|
1891
|
-
this.
|
|
1957
|
+
this.runBootCallbacks(owner, () => {
|
|
1958
|
+
this.enginesBooted = true;
|
|
1959
|
+
});
|
|
1892
1960
|
});
|
|
1893
1961
|
}
|
|
1894
1962
|
|
|
@@ -1903,6 +1971,54 @@ export default class UniverseService extends Service.extend(Evented) {
|
|
|
1903
1971
|
return this.bootedExtensions.includes(name);
|
|
1904
1972
|
}
|
|
1905
1973
|
|
|
1974
|
+
/**
|
|
1975
|
+
* Registers a callback function to be executed after the engine boot process completes.
|
|
1976
|
+
*
|
|
1977
|
+
* This method ensures that the `bootCallbacks` array is initialized. It then adds the provided
|
|
1978
|
+
* callback to this array. The callbacks registered will be invoked in sequence after the engine
|
|
1979
|
+
* has finished booting, using the `runBootCallbacks` method.
|
|
1980
|
+
*
|
|
1981
|
+
* @param {Function} callback - The function to execute after the engine boots.
|
|
1982
|
+
* The callback should accept two arguments:
|
|
1983
|
+
* - `{Object} universe` - The universe context or environment.
|
|
1984
|
+
* - `{Object} appInstance` - The application instance.
|
|
1985
|
+
*/
|
|
1986
|
+
afterBoot(callback) {
|
|
1987
|
+
if (!isArray(this.bootCallbacks)) {
|
|
1988
|
+
this.bootCallbacks = [];
|
|
1989
|
+
}
|
|
1990
|
+
|
|
1991
|
+
this.bootCallbacks.pushObject(callback);
|
|
1992
|
+
}
|
|
1993
|
+
|
|
1994
|
+
/**
|
|
1995
|
+
* Executes all registered engine boot callbacks in the order they were added.
|
|
1996
|
+
*
|
|
1997
|
+
* This method iterates over the `bootCallbacks` array and calls each callback function,
|
|
1998
|
+
* passing in the `universe` and `appInstance` parameters. After all callbacks have been
|
|
1999
|
+
* executed, it optionally calls a completion function `onComplete`.
|
|
2000
|
+
*
|
|
2001
|
+
* @param {Object} appInstance - The application instance to pass to each callback.
|
|
2002
|
+
* @param {Function} [onComplete] - Optional. A function to call after all boot callbacks have been executed.
|
|
2003
|
+
* It does not receive any arguments.
|
|
2004
|
+
*/
|
|
2005
|
+
runBootCallbacks(appInstance, onComplete = null) {
|
|
2006
|
+
for (let i = 0; i < this.bootCallbacks.length; i++) {
|
|
2007
|
+
const callback = this.bootCallbacks[i];
|
|
2008
|
+
if (typeof callback === 'function') {
|
|
2009
|
+
try {
|
|
2010
|
+
callback(this, appInstance);
|
|
2011
|
+
} catch (error) {
|
|
2012
|
+
debug(`Engine Boot Callback Error: ${error.message}`);
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
|
|
2017
|
+
if (typeof onComplete === 'function') {
|
|
2018
|
+
onComplete();
|
|
2019
|
+
}
|
|
2020
|
+
}
|
|
2021
|
+
|
|
1906
2022
|
/**
|
|
1907
2023
|
* Alias for intl service `t`
|
|
1908
2024
|
*
|
|
@@ -1,35 +1,35 @@
|
|
|
1
1
|
import Service from '@ember/service';
|
|
2
|
-
import {
|
|
2
|
+
import { debounce } from '@ember/runloop';
|
|
3
3
|
import hasJsonStructure from '../utils/has-json-structure';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Service for manipulating URL search parameters.
|
|
7
|
+
*
|
|
8
|
+
* This service provides methods to get, set, remove, and check URL query parameters.
|
|
9
|
+
* It also allows updating the browser's URL without reloading the page.
|
|
10
|
+
*
|
|
11
|
+
* @extends Service
|
|
12
|
+
*/
|
|
5
13
|
export default class UrlSearchParamsService extends Service {
|
|
6
14
|
/**
|
|
7
|
-
*
|
|
15
|
+
* Getter for `urlParams` that ensures it's always up-to-date with the current URL.
|
|
8
16
|
*
|
|
9
|
-
* @
|
|
17
|
+
* @type {URLSearchParams}
|
|
18
|
+
* @private
|
|
10
19
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Update the URL params
|
|
15
|
-
*
|
|
16
|
-
* @void
|
|
17
|
-
*/
|
|
18
|
-
setSearchParams() {
|
|
19
|
-
this.urlParams = new URLSearchParams(window.location.search);
|
|
20
|
-
|
|
21
|
-
return this;
|
|
20
|
+
get urlParams() {
|
|
21
|
+
return new URLSearchParams(window.location.search);
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
25
|
+
* Retrieves the value of a specific query parameter.
|
|
26
26
|
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
27
|
+
* If the parameter value is a JSON string, it will be parsed into an object or array.
|
|
28
|
+
*
|
|
29
|
+
* @param {string} key - The name of the query parameter to retrieve.
|
|
30
|
+
* @returns {*} The value of the query parameter, parsed from JSON if applicable, or null if not found.
|
|
29
31
|
*/
|
|
30
32
|
getParam(key) {
|
|
31
|
-
this.setSearchParams();
|
|
32
|
-
|
|
33
33
|
let value = this.urlParams.get(key);
|
|
34
34
|
|
|
35
35
|
if (hasJsonStructure(value)) {
|
|
@@ -40,47 +40,102 @@ export default class UrlSearchParamsService extends Service {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
43
|
+
* Sets or updates a query parameter in the URL search parameters.
|
|
44
|
+
*
|
|
45
|
+
* If the value is an object or array, it will be stringified to JSON.
|
|
46
|
+
*
|
|
47
|
+
* @param {string} key - The name of the query parameter to set.
|
|
48
|
+
* @param {*} value - The value of the query parameter.
|
|
49
|
+
* @returns {this} Returns the service instance for chaining.
|
|
50
|
+
*/
|
|
51
|
+
setParam(key, value) {
|
|
52
|
+
if (typeof value === 'object') {
|
|
53
|
+
value = JSON.stringify(value);
|
|
54
|
+
} else {
|
|
55
|
+
value = encodeURIComponent(value);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
this.urlParams.set(key, value);
|
|
59
|
+
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Alias for `getParam`.
|
|
44
65
|
*
|
|
45
|
-
* @param {
|
|
46
|
-
* @
|
|
66
|
+
* @param {string} key - The name of the query parameter to retrieve.
|
|
67
|
+
* @returns {*} The value of the query parameter.
|
|
47
68
|
*/
|
|
48
69
|
get(key) {
|
|
49
70
|
return this.getParam(key);
|
|
50
71
|
}
|
|
51
72
|
|
|
52
73
|
/**
|
|
53
|
-
*
|
|
74
|
+
* Sets or updates a query parameter with multiple values.
|
|
54
75
|
*
|
|
55
|
-
* @param {
|
|
56
|
-
* @
|
|
76
|
+
* @param {string} key - The name of the query parameter to set.
|
|
77
|
+
* @param {Array} values - An array of values for the parameter.
|
|
78
|
+
* @returns {this} Returns the service instance for chaining.
|
|
79
|
+
*/
|
|
80
|
+
setParamArray(key, values) {
|
|
81
|
+
this.urlParams.delete(key);
|
|
82
|
+
values.forEach((value) => {
|
|
83
|
+
this.urlParams.append(key, value);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Retrieves all values of a specific query parameter.
|
|
91
|
+
*
|
|
92
|
+
* @param {string} key - The name of the query parameter.
|
|
93
|
+
* @returns {Array} An array of values for the parameter.
|
|
94
|
+
*/
|
|
95
|
+
getParamArray(key) {
|
|
96
|
+
return this.urlParams.getAll(key);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Checks if a specific query parameter exists in the URL.
|
|
101
|
+
*
|
|
102
|
+
* @param {string} key - The name of the query parameter to check.
|
|
103
|
+
* @returns {boolean} True if the parameter exists, false otherwise.
|
|
57
104
|
*/
|
|
58
105
|
exists(key) {
|
|
59
|
-
this.
|
|
106
|
+
return this.urlParams.has(key);
|
|
107
|
+
}
|
|
60
108
|
|
|
109
|
+
/**
|
|
110
|
+
* Checks if a specific query parameter has in the URL.
|
|
111
|
+
*
|
|
112
|
+
* @param {string} key - The name of the query parameter to check.
|
|
113
|
+
* @returns {boolean} True if the parameter exists, false otherwise.
|
|
114
|
+
*/
|
|
115
|
+
has(key) {
|
|
61
116
|
return this.urlParams.has(key);
|
|
62
117
|
}
|
|
63
118
|
|
|
64
119
|
/**
|
|
65
|
-
*
|
|
120
|
+
* Removes a specific query parameter from the URL search parameters.
|
|
66
121
|
*
|
|
67
|
-
* @param {
|
|
68
|
-
* @
|
|
122
|
+
* @param {string} key - The name of the query parameter to remove.
|
|
123
|
+
* @returns {this} Returns the service instance for chaining.
|
|
69
124
|
*/
|
|
70
125
|
remove(key) {
|
|
71
|
-
this.
|
|
126
|
+
this.urlParams.delete(key);
|
|
72
127
|
|
|
73
|
-
return this
|
|
128
|
+
return this;
|
|
74
129
|
}
|
|
75
130
|
|
|
76
131
|
/**
|
|
77
|
-
*
|
|
132
|
+
* Retrieves all query parameters as an object.
|
|
78
133
|
*
|
|
79
|
-
*
|
|
134
|
+
* Each parameter value is processed by `getParam`, which parses JSON values if applicable.
|
|
135
|
+
*
|
|
136
|
+
* @returns {Object} An object containing all query parameters and their values.
|
|
80
137
|
*/
|
|
81
138
|
all() {
|
|
82
|
-
this.setSearchParams();
|
|
83
|
-
|
|
84
139
|
const all = {};
|
|
85
140
|
|
|
86
141
|
for (let key of this.urlParams.keys()) {
|
|
@@ -89,4 +144,103 @@ export default class UrlSearchParamsService extends Service {
|
|
|
89
144
|
|
|
90
145
|
return all;
|
|
91
146
|
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Updates the browser's URL with the current `urlParams` without reloading the page.
|
|
150
|
+
*
|
|
151
|
+
* @returns {void}
|
|
152
|
+
*/
|
|
153
|
+
updateUrl() {
|
|
154
|
+
const url = new URL(window.location.href);
|
|
155
|
+
url.search = this.urlParams.toString();
|
|
156
|
+
window.history.pushState({ path: url.href }, '', url.href);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Updates the browser's URL with the current `urlParams`, debounced to prevent excessive calls.
|
|
161
|
+
*
|
|
162
|
+
* @returns {void}
|
|
163
|
+
*/
|
|
164
|
+
updateUrlDebounced() {
|
|
165
|
+
debounce(this, this.updateUrl, 100);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Clears all query parameters from the URL search parameters.
|
|
170
|
+
*
|
|
171
|
+
* @returns {this} Returns the service instance for chaining.
|
|
172
|
+
*/
|
|
173
|
+
clear() {
|
|
174
|
+
this.urlParams = new URLSearchParams();
|
|
175
|
+
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Returns the full URL as a string with the current `urlParams`.
|
|
181
|
+
*
|
|
182
|
+
* @returns {string} The full URL with updated query parameters.
|
|
183
|
+
*/
|
|
184
|
+
getFullUrl() {
|
|
185
|
+
const url = new URL(window.location.href);
|
|
186
|
+
url.search = this.urlParams.toString();
|
|
187
|
+
|
|
188
|
+
return url.toString();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Returns the current path with the updated query parameters.
|
|
193
|
+
*
|
|
194
|
+
* @returns {string} The path and search portion of the URL.
|
|
195
|
+
*/
|
|
196
|
+
getPathWithParams() {
|
|
197
|
+
return `${window.location.pathname}?${this.urlParams.toString()}`;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Removes a query parameter from the current URL and updates the browser history.
|
|
202
|
+
*
|
|
203
|
+
* This method modifies the browser's URL by removing the specified parameter and uses the History API
|
|
204
|
+
* to update the URL without reloading the page.
|
|
205
|
+
*
|
|
206
|
+
* @param {string} paramToRemove - The name of the query parameter to remove from the URL.
|
|
207
|
+
* @returns {void}
|
|
208
|
+
*/
|
|
209
|
+
removeParamFromCurrentUrl(paramToRemove) {
|
|
210
|
+
const url = new URL(window.location.href);
|
|
211
|
+
url.searchParams.delete(paramToRemove);
|
|
212
|
+
window.history.pushState({ path: url.href }, '', url.href);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Adds or updates a query parameter in the current URL and updates the browser history.
|
|
217
|
+
*
|
|
218
|
+
* This method modifies the browser's URL by adding or updating the specified parameter and uses the History API
|
|
219
|
+
* to update the URL without reloading the page.
|
|
220
|
+
*
|
|
221
|
+
* @param {string} paramName - The name of the query parameter to add or update.
|
|
222
|
+
* @param {string} paramValue - The value of the query parameter.
|
|
223
|
+
* @returns {void}
|
|
224
|
+
*/
|
|
225
|
+
addParamToCurrentUrl(paramName, paramValue) {
|
|
226
|
+
const url = new URL(window.location.href);
|
|
227
|
+
url.searchParams.set(paramName, paramValue);
|
|
228
|
+
window.history.pushState({ path: url.href }, '', url.href);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Adds or updates a query parameters from a provided object into the current URL and updates the browser history.
|
|
233
|
+
*
|
|
234
|
+
* This method modifies the browser's URL by adding or updating the specified parameters and uses the History API
|
|
235
|
+
* to update the URL without reloading the page.
|
|
236
|
+
*
|
|
237
|
+
* @param {Object} params - The query parameters to add or update.
|
|
238
|
+
* @returns {void}
|
|
239
|
+
*/
|
|
240
|
+
setParamsToCurrentUrl(params = {}) {
|
|
241
|
+
for (let param in params) {
|
|
242
|
+
const value = params[param];
|
|
243
|
+
this.addParamToCurrentUrl(param, value);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
92
246
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { getOwner } from '@ember/application';
|
|
2
2
|
import { isArray } from '@ember/array';
|
|
3
|
+
import Service from '@ember/service';
|
|
3
4
|
import isObject from './is-object';
|
|
4
5
|
|
|
5
6
|
function findService(owner, target, serviceName) {
|
|
6
7
|
let service = target[serviceName];
|
|
7
|
-
if (!service) {
|
|
8
|
+
if (!(service instanceof Service)) {
|
|
8
9
|
service = owner.lookup(`service:${serviceName}`);
|
|
9
10
|
}
|
|
10
11
|
|
|
@@ -33,8 +34,12 @@ function automaticServiceResolution(service, target, owner) {
|
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
36
|
|
|
37
|
+
function _getOwner(target) {
|
|
38
|
+
return window.Fleetbase ?? getOwner(target);
|
|
39
|
+
}
|
|
40
|
+
|
|
36
41
|
export default function injectEngineService(target, engineName, serviceName, options = {}) {
|
|
37
|
-
const owner =
|
|
42
|
+
const owner = _getOwner(target);
|
|
38
43
|
const universe = owner.lookup('service:universe');
|
|
39
44
|
const service = universe.getServiceFromEngine(engineName, serviceName);
|
|
40
45
|
const key = options.key || null;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { dasherize } from '@ember/string';
|
|
2
|
+
|
|
3
|
+
export default function registerComponent(owner, componentClass, options = {}) {
|
|
4
|
+
const registrationName = options && options.as ? `component:${options.as}` : `component:${dasherize(componentClass.name).replace('-component', '')}`;
|
|
5
|
+
if (!owner.hasRegistration(registrationName)) {
|
|
6
|
+
owner.register(registrationName, componentClass);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { dasherize } from '@ember/string';
|
|
2
|
+
|
|
3
|
+
export default function registerHelper(owner, name, helperFn) {
|
|
4
|
+
const registrationName = `helper:${dasherize(name)}`;
|
|
5
|
+
if (!owner.hasRegistration(registrationName)) {
|
|
6
|
+
owner.register(registrationName, helperFn);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from '@fleetbase/ember-core/utils/register-component';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from '@fleetbase/ember-core/utils/register-helper';
|
package/package.json
CHANGED