@qikdev/sdk 0.1.149 → 0.1.151

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qikdev/sdk",
3
- "version": "0.1.149",
3
+ "version": "0.1.151",
4
4
  "type": "module",
5
5
  "description": "Promise based Javascript SDK",
6
6
  "scripts": {
@@ -59,6 +59,7 @@ export default function(qik) {
59
59
 
60
60
  const { data } = await inflightVariablesRequest
61
61
  variables.data = data;
62
+ qik.dispatch('variables', data)
62
63
  }
63
64
 
64
65
  return variables.data;
@@ -30,23 +30,46 @@ export default function(qik) {
30
30
  return true;
31
31
  }
32
32
 
33
+ /**
34
+ *
35
+ * Get the distance between two geo location points
36
+ * @alias geo.distance
37
+ * @param {Object} positionOne The first position
38
+ * @param {Number} positionOne.lat The first positions latitude
39
+ * @param {Number} positionOne.lon The first positions longitude
40
+ * @param {Object} positionTwo The second position
41
+ * @param {Number} positionTwo.lat The second positions latitude
42
+ * @param {Number} positionTwo.lon The second positions longitude
43
+ * @example
44
+ * const meters = await sdk.geo.distance({lat, lon}, {lat, lon})
45
+ * // Returns 634
46
+ */
33
47
  service.distance = function({ lat: lat1, lon: lon1 }, { lat: lat2, lon: lon2 }) {
34
48
  function deg2rad(deg) {
35
49
  return deg * (Math.PI / 180)
36
50
  }
37
51
 
38
- var R = 6371; // Radius of the earth in km
39
- var dLat = deg2rad(lat2 - lat1); // deg2rad below
52
+ var R = 6371000; // Radius of the earth in meters
53
+ var dLat = deg2rad(lat2 - lat1);
40
54
  var dLon = deg2rad(lon2 - lon1);
41
55
  var a =
42
56
  Math.sin(dLat / 2) * Math.sin(dLat / 2) +
43
57
  Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
44
58
  Math.sin(dLon / 2) * Math.sin(dLon / 2);
45
59
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
46
- var d = R * c; // Distance in km
60
+ var d = R * c; // Distance in meters
47
61
  return d;
48
62
  }
49
63
 
64
+ /**
65
+ *
66
+ * Start watching the user's geolocation
67
+ * @alias geo.getPosition
68
+ * @param {Function} callback A function that will be called with updated position data
69
+ * @example
70
+ * const position = await sdk.geo.getPosition()
71
+ * // Returns {coords:{latitude, longitude, altitude, accuracy, ...}}
72
+ */
50
73
  service.getPosition = async function() {
51
74
 
52
75
  if (!isEnabled()) {
@@ -59,6 +82,15 @@ export default function(qik) {
59
82
  })
60
83
  }
61
84
 
85
+ /**
86
+ *
87
+ * Start watching the user's geolocation
88
+ * @alias geo.stopWatchPosition
89
+ * @param {Function} callback A function that will be called with updated position data
90
+ * @example
91
+ * const result = await sdk.geo.watchPosition(callback)
92
+ * // Returns {id:<watcherID>}
93
+ */
62
94
  service.watchPosition = async function(positionDataReceived) {
63
95
 
64
96
  if (!isEnabled()) {
@@ -74,6 +106,15 @@ export default function(qik) {
74
106
  })
75
107
  }
76
108
 
109
+ /**
110
+ *
111
+ * Start watching the user's geolocation
112
+ * @alias geo.stopWatchPosition
113
+ * @param {String} watchID the id of the watcher to stop
114
+ * @example
115
+ * const result = await sdk.geo.stopWatchPosition(watchID)
116
+ * // Returns true after the watcher has been stopped
117
+ */
77
118
  service.stopWatchPosition = async function(watchID) {
78
119
  if (!isEnabled()) {
79
120
  return Promise.reject({ message: 'Geolocation is not enabled' });
@@ -84,6 +125,345 @@ export default function(qik) {
84
125
  return true;
85
126
  }
86
127
 
128
+
129
+ ///////////////////////////////////////////////////
130
+
131
+ /**
132
+ *
133
+ * Retrieves the default timezone for the current user/application
134
+ * will attempt to retrieve an accurate timezone either from the user's organisation or available services within the browser
135
+ * @alias geo.getDefaultTimezone
136
+ * @example
137
+ * const result = sdk.geo.getDefaultTimezone()
138
+ * // Would return something similar to:
139
+ * // Australia/Melbourne
140
+ */
141
+ service.getDefaultTimezone = function() {
142
+
143
+ const currentUser = qik.auth.getCurrentUser();
144
+
145
+ if(currentUser) {
146
+ const defaultUserTimezone = currentUser?.organisation?.timezone;
147
+ if(defaultUserTimezone) {
148
+ return defaultUserTimezone;
149
+ }
150
+ }
151
+
152
+ if (typeof window === 'undefined') {
153
+ return;
154
+ }
155
+
156
+ if (!window.Intl) {
157
+ return;
158
+ }
159
+ return window.Intl.DateTimeFormat().resolvedOptions().timeZone;
160
+ }
161
+
162
+ ///////////////////////////////////////////////////
163
+
164
+ service.countryLookup = {
165
+ "AC": "Ascension Island",
166
+ "AD": "Andorra",
167
+ "AE": "United Arab Emirates",
168
+ "AF": "Afghanistan",
169
+ "AG": "Antigua And Barbuda",
170
+ "AI": "Anguilla",
171
+ "AL": "Albania",
172
+ "AM": "Armenia",
173
+ "AO": "Angola",
174
+ "AQ": "Antarctica",
175
+ "AR": "Argentina",
176
+ "AS": "American Samoa",
177
+ "AT": "Austria",
178
+ "AU": "Australia",
179
+ "AW": "Aruba",
180
+ "AX": "Åland Islands",
181
+ "AZ": "Azerbaijan",
182
+ "BA": "Bosnia & Herzegovina",
183
+ "BB": "Barbados",
184
+ "BD": "Bangladesh",
185
+ "BE": "Belgium",
186
+ "BF": "Burkina Faso",
187
+ "BG": "Bulgaria",
188
+ "BH": "Bahrain",
189
+ "BI": "Burundi",
190
+ "BJ": "Benin",
191
+ "BL": "Saint Barthélemy",
192
+ "BM": "Bermuda",
193
+ "BN": "Brunei Darussalam",
194
+ "BO": "Bolivia, Plurinational State Of",
195
+ "BQ": "Bonaire, Saint Eustatius And Saba",
196
+ "BR": "Brazil",
197
+ "BS": "Bahamas",
198
+ "BT": "Bhutan",
199
+ "BV": "Bouvet Island",
200
+ "BW": "Botswana",
201
+ "BY": "Belarus",
202
+ "BZ": "Belize",
203
+ "CA": "Canada",
204
+ "CC": "Cocos (Keeling) Islands",
205
+ "CD": "Democratic Republic Of Congo",
206
+ "CF": "Central African Republic",
207
+ "CG": "Republic Of Congo",
208
+ "CH": "Switzerland",
209
+ "CI": "Côte d'Ivoire",
210
+ "CK": "Cook Islands",
211
+ "CL": "Chile",
212
+ "CM": "Cameroon",
213
+ "CN": "China",
214
+ "CO": "Colombia",
215
+ "CP": "Clipperton Island",
216
+ "CR": "Costa Rica",
217
+ "CU": "Cuba",
218
+ "CV": "Cabo Verde",
219
+ "CW": "Curacao",
220
+ "CX": "Christmas Island",
221
+ "CY": "Cyprus",
222
+ "CZ": "Czech Republic",
223
+ "DE": "Germany",
224
+ "DG": "Diego Garcia",
225
+ "DJ": "Djibouti",
226
+ "DK": "Denmark",
227
+ "DM": "Dominica",
228
+ "DO": "Dominican Republic",
229
+ "DZ": "Algeria",
230
+ "EA": "Ceuta, Mulilla",
231
+ "EC": "Ecuador",
232
+ "EE": "Estonia",
233
+ "EG": "Egypt",
234
+ "EH": "Western Sahara",
235
+ "ER": "Eritrea",
236
+ "ES": "Spain",
237
+ "ET": "Ethiopia",
238
+ "EU": "European Union",
239
+ "FI": "Finland",
240
+ "FJ": "Fiji",
241
+ "FK": "Falkland Islands",
242
+ "FM": "Micronesia, Federated States Of",
243
+ "FO": "Faroe Islands",
244
+ "TF": "French Southern Territories",
245
+ "FR": "France",
246
+ "FX": "France, Metropolitan",
247
+ "GA": "Gabon",
248
+ "GB": "United Kingdom",
249
+ "GD": "Grenada",
250
+ "GE": "Georgia",
251
+ "GF": "French Guiana",
252
+ "GG": "Guernsey",
253
+ "GH": "Ghana",
254
+ "GI": "Gibraltar",
255
+ "GL": "Greenland",
256
+ "GM": "Gambia",
257
+ "GN": "Guinea",
258
+ "GP": "Guadeloupe",
259
+ "GQ": "Equatorial Guinea",
260
+ "GR": "Greece",
261
+ "GS": "South Georgia And The South Sandwich Islands",
262
+ "GT": "Guatemala",
263
+ "GU": "Guam",
264
+ "GW": "Guinea-bissau",
265
+ "GY": "Guyana",
266
+ "HK": "Hong Kong",
267
+ "HM": "Heard Island And McDonald Islands",
268
+ "HN": "Honduras",
269
+ "HR": "Croatia",
270
+ "HT": "Haiti",
271
+ "HU": "Hungary",
272
+ "IC": "Canary Islands",
273
+ "ID": "Indonesia",
274
+ "IE": "Ireland",
275
+ "IL": "Israel",
276
+ "IM": "Isle Of Man",
277
+ "IN": "India",
278
+ "IO": "British Indian Ocean Territory",
279
+ "IQ": "Iraq",
280
+ "IR": "Iran, Islamic Republic Of",
281
+ "IS": "Iceland",
282
+ "IT": "Italy",
283
+ "JE": "Jersey",
284
+ "JM": "Jamaica",
285
+ "JO": "Jordan",
286
+ "JP": "Japan",
287
+ "KE": "Kenya",
288
+ "KG": "Kyrgyzstan",
289
+ "KH": "Cambodia",
290
+ "KI": "Kiribati",
291
+ "KM": "Comoros",
292
+ "KN": "Saint Kitts And Nevis",
293
+ "KP": "Korea, Democratic People's Republic Of",
294
+ "KR": "Korea, Republic Of",
295
+ "KW": "Kuwait",
296
+ "KY": "Cayman Islands",
297
+ "KZ": "Kazakhstan",
298
+ "LA": "Lao People's Democratic Republic",
299
+ "LB": "Lebanon",
300
+ "LC": "Saint Lucia",
301
+ "LI": "Liechtenstein",
302
+ "LK": "Sri Lanka",
303
+ "LR": "Liberia",
304
+ "LS": "Lesotho",
305
+ "LT": "Lithuania",
306
+ "LU": "Luxembourg",
307
+ "LV": "Latvia",
308
+ "LY": "Libya",
309
+ "MA": "Morocco",
310
+ "MC": "Monaco",
311
+ "MD": "Moldova",
312
+ "ME": "Montenegro",
313
+ "MF": "Saint Martin",
314
+ "MG": "Madagascar",
315
+ "MH": "Marshall Islands",
316
+ "MK": "Macedonia, The Former Yugoslav Republic Of",
317
+ "ML": "Mali",
318
+ "MM": "Myanmar",
319
+ "MN": "Mongolia",
320
+ "MO": "Macao",
321
+ "MP": "Northern Mariana Islands",
322
+ "MQ": "Martinique",
323
+ "MR": "Mauritania",
324
+ "MS": "Montserrat",
325
+ "MT": "Malta",
326
+ "MU": "Mauritius",
327
+ "MV": "Maldives",
328
+ "MW": "Malawi",
329
+ "MX": "Mexico",
330
+ "MY": "Malaysia",
331
+ "MZ": "Mozambique",
332
+ "NA": "Namibia",
333
+ "NC": "New Caledonia",
334
+ "NE": "Niger",
335
+ "NF": "Norfolk Island",
336
+ "NG": "Nigeria",
337
+ "NI": "Nicaragua",
338
+ "NL": "Netherlands",
339
+ "NO": "Norway",
340
+ "NP": "Nepal",
341
+ "NR": "Nauru",
342
+ "NU": "Niue",
343
+ "NZ": "New Zealand",
344
+ "OM": "Oman",
345
+ "PA": "Panama",
346
+ "PE": "Peru",
347
+ "PF": "French Polynesia",
348
+ "PG": "Papua New Guinea",
349
+ "PH": "Philippines",
350
+ "PK": "Pakistan",
351
+ "PL": "Poland",
352
+ "PM": "Saint Pierre And Miquelon",
353
+ "PN": "Pitcairn",
354
+ "PR": "Puerto Rico",
355
+ "PS": "Palestinian Territory, Occupied",
356
+ "PT": "Portugal",
357
+ "PW": "Palau",
358
+ "PY": "Paraguay",
359
+ "QA": "Qatar",
360
+ "RE": "Reunion",
361
+ "RO": "Romania",
362
+ "RS": "Serbia",
363
+ "RU": "Russian Federation",
364
+ "RW": "Rwanda",
365
+ "SA": "Saudi Arabia",
366
+ "SB": "Solomon Islands",
367
+ "SC": "Seychelles",
368
+ "SD": "Sudan",
369
+ "SE": "Sweden",
370
+ "SG": "Singapore",
371
+ "SH": "Saint Helena, Ascension And Tristan Da Cunha",
372
+ "SI": "Slovenia",
373
+ "SJ": "Svalbard And Jan Mayen",
374
+ "SK": "Slovakia",
375
+ "SL": "Sierra Leone",
376
+ "SM": "San Marino",
377
+ "SN": "Senegal",
378
+ "SO": "Somalia",
379
+ "SR": "Suriname",
380
+ "SS": "South Sudan",
381
+ "ST": "Sao Tome and Principe",
382
+ "SU": "USSR",
383
+ "SV": "El Salvador",
384
+ "SX": "Sint Maarten",
385
+ "SY": "Syrian Arab Republic",
386
+ "SZ": "Swaziland",
387
+ "TA": "Tristan de Cunha",
388
+ "TC": "Turks And Caicos Islands",
389
+ "TD": "Chad",
390
+ "TG": "Togo",
391
+ "TH": "Thailand",
392
+ "TJ": "Tajikistan",
393
+ "TK": "Tokelau",
394
+ "TL": "Timor-Leste, Democratic Republic of",
395
+ "TM": "Turkmenistan",
396
+ "TN": "Tunisia",
397
+ "TO": "Tonga",
398
+ "TR": "Turkey",
399
+ "TT": "Trinidad And Tobago",
400
+ "TV": "Tuvalu",
401
+ "TW": "Taiwan",
402
+ "TZ": "Tanzania, United Republic Of",
403
+ "UA": "Ukraine",
404
+ "UG": "Uganda",
405
+ "UK": "United Kingdom",
406
+ "UM": "United States Minor Outlying Islands",
407
+ "US": "United States",
408
+ "UY": "Uruguay",
409
+ "UZ": "Uzbekistan",
410
+ "VA": "Vatican City State",
411
+ "VC": "Saint Vincent And The Grenadines",
412
+ "VE": "Venezuela, Bolivarian Republic Of",
413
+ "VG": "Virgin Islands (British)",
414
+ "VI": "Virgin Islands (US)",
415
+ "VN": "Viet Nam",
416
+ "VU": "Vanuatu",
417
+ "WF": "Wallis And Futuna",
418
+ "WS": "Samoa",
419
+ "XK": "Kosovo",
420
+ "YE": "Yemen",
421
+ "YT": "Mayotte",
422
+ "ZA": "South Africa",
423
+ "ZM": "Zambia",
424
+ "ZW": "Zimbabwe"
425
+ }
426
+
427
+
428
+ /**
429
+ *
430
+ * Retrieves all supported country names
431
+ * @alias geo.countryNames
432
+ * @example
433
+ * const result = sdk.geo.countryNames()
434
+ * // Would return an array of country names:
435
+ * // ['Australia', 'United States', 'Canada', ...]
436
+ */
437
+ service.countryNames = function() {
438
+ return Object.values(service.countryLookup);
439
+ }
440
+
441
+ /**
442
+ *
443
+ * Retrieves the default country for the current user/application
444
+ * will attempt to retrieve the country from the user or application's organisation or from services available within the browser
445
+ * @alias geo.getDefaultCountry
446
+ * @example
447
+ * const result = sdk.geo.getDefaultCountry()
448
+ * // Would return something similar to:
449
+ * // { code:'AU', name:'Australia' }
450
+ */
451
+ service.getDefaultCountry = function() {
452
+ const currentUser = qik.auth.getCurrentUser();
453
+
454
+ if(currentUser) {
455
+ const code = currentUser?.organisation?.countryCode;
456
+ if(code) {
457
+
458
+ const name = service.countryLookup[code];
459
+ return {
460
+ code,
461
+ name
462
+ };
463
+ }
464
+ }
465
+ }
466
+
87
467
  ///////////////////////////////////////////////////
88
468
 
89
469
  return service;
@@ -28,23 +28,26 @@ const loadedExternalScripts = {}
28
28
  * await sdk.utils.loadExternalScript('https://cdn.javascript.com/external/script.js');
29
29
  */
30
30
 
31
+
31
32
  service.loadExternalScript = function(url) {
32
33
 
33
- const promise = new Promise(createNewScript);
34
+ // If we already have a promise
35
+ if (loadedExternalScripts[url]) {
36
+ // return the existing promise
37
+ return loadedExternalScripts[url];
38
+ }
39
+
40
+
41
+ // Create a new promise
42
+ const promise = loadedExternalScripts[url] = new Promise(createNewScript);
34
43
 
35
44
  function createNewScript(resolve, reject) {
36
45
 
37
46
  if (!document) {
47
+ delete loadedExternalScripts[url];
38
48
  return reject('document is undefined')
39
49
  }
40
50
 
41
- if (loadedExternalScripts[url]) {
42
- return resolve(url);
43
- }
44
-
45
- // Avoid duplicate scripts
46
- loadedExternalScripts[url] = true;
47
-
48
51
  //////////////////////////////////////
49
52
 
50
53
  var script = document.createElement('script');
@@ -216,6 +219,11 @@ service.parseURL = function(string) {
216
219
 
217
220
  const withHttp = string => !/^https?:\/\//i.test(string) ? `http://${string}` : string;
218
221
  string = withHttp(string);
222
+ //TODO: test this more.
223
+ string = string.replace(/\s/g, '');
224
+
225
+
226
+
219
227
 
220
228
 
221
229
 
package/src/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '0.1.149'
1
+ export const version = '0.1.151'