@fleetbase/ember-core 0.2.17 → 0.2.19

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,35 +1,35 @@
1
1
  import Service from '@ember/service';
2
- import { tracked } from '@glimmer/tracking';
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
- * The active URL params
15
+ * Getter for `urlParams` that ensures it's always up-to-date with the current URL.
8
16
  *
9
- * @var {Array}
17
+ * @type {URLSearchParams}
18
+ * @private
10
19
  */
11
- @tracked urlParams;
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
- * Get a param
25
+ * Retrieves the value of a specific query parameter.
26
26
  *
27
- * @param {String} key the url param
28
- * @return mixed
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
- * Get a param
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 {String} key the url param
46
- * @return mixed
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
- * Determines if a queryParam exists
74
+ * Sets or updates a query parameter with multiple values.
54
75
  *
55
- * @param {String} key the url param
56
- * @var {Boolean}
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.setSearchParams();
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
- * Remove a queryparam
120
+ * Removes a specific query parameter from the URL search parameters.
66
121
  *
67
- * @param {String} key the url param
68
- * @void
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.setSearchParams();
126
+ this.urlParams.delete(key);
72
127
 
73
- return this.urlParams.delete(key);
128
+ return this;
74
129
  }
75
130
 
76
131
  /**
77
- * Returns object of all params
132
+ * Retrieves all query parameters as an object.
78
133
  *
79
- * @return {Array}
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 = getOwner(target);
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;
@@ -1,3 +1,9 @@
1
+ import { isBlank } from '@ember/utils';
2
+
1
3
  export default function isEmptyObject(obj) {
2
- return Object.keys(obj).length === 0 && obj.constructor === Object;
4
+ if (isBlank(obj)) {
5
+ return true;
6
+ }
7
+
8
+ return obj.constructor === Object && Object.keys(obj).length === 0;
3
9
  }
@@ -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
+ }
@@ -8,17 +8,12 @@ export default function serializeNormalizeRelationsWithinHash(hash, primaryKey =
8
8
 
9
9
  for (let attr in hash) {
10
10
  if (typeof attr === 'string' && attr.includes('_uuid')) {
11
- // console.log(attr, hash[attr]);
12
11
  if (typeof hash[attr] === 'object' && !isBlank(hash[attr])) {
13
12
  // the relation has loaded back into `_uuid` - change this to the proper `uuid` string value and set the relationship
14
13
  const relation = hash[attr];
15
14
  const id = get(relation, primaryKey);
16
15
  const relationAttr = attr.replace('_uuid', '');
17
16
 
18
- // console.log(`normalizing relation ${relationAttr} from ${attr}`);
19
- // console.log(relationAttr, relation);
20
- // console.log(attr, id);
21
-
22
17
  setProperties(hash, {
23
18
  [relationAttr]: relation,
24
19
  [attr]: id,
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fleetbase/ember-core",
3
- "version": "0.2.17",
3
+ "version": "0.2.19",
4
4
  "description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.",
5
5
  "keywords": [
6
6
  "fleetbase-core",