@staffbase/plugins-client-sdk 2.0.2 → 3.0.0-alpha.2

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.
@@ -2,12 +2,12 @@
2
2
  * Bundle of @staffbase/plugins-client-sdk
3
3
  * @file Staffbase plugins client SDK for JavaScript
4
4
  * @see https://github.com/Staffbase/plugins-client-sdk#readme
5
- * @version 2.0.1
5
+ * @version 3.0.0-alpha.2
6
6
  *
7
7
  * @author Stefan Staude <stefan.staude@staffbase.com>
8
8
  * @author Daniel Große <daniel.grosse@staffbase.com>
9
9
  *
10
- * @copyright 2024
10
+ * @copyright 2026
11
11
  * @license Apache-2.0
12
12
  */
13
13
 
@@ -46,6 +46,8 @@
46
46
  var undefinedType = "undefined";
47
47
  var isIE = typeof window !== undefinedType && typeof window.navigator !== undefinedType && /Trident\/|MSIE /.test(window.navigator.userAgent);
48
48
  var logMethods = ["trace", "debug", "info", "warn", "error"];
49
+ var _loggersByName = {};
50
+ var defaultLogger = null;
49
51
 
50
52
  // Cross-browser bind equivalent that works at least back to IE6
51
53
  function bindMethod(obj, methodName) {
@@ -98,23 +100,31 @@
98
100
 
99
101
  // These private functions always need `this` to be set properly
100
102
 
101
- function replaceLoggingMethods(level, loggerName) {
103
+ function replaceLoggingMethods() {
102
104
  /*jshint validthis:true */
105
+ var level = this.getLevel();
106
+
107
+ // Replace the actual methods.
103
108
  for (var i = 0; i < logMethods.length; i++) {
104
109
  var methodName = logMethods[i];
105
- this[methodName] = i < level ? noop : this.methodFactory(methodName, level, loggerName);
110
+ this[methodName] = i < level ? noop : this.methodFactory(methodName, level, this.name);
106
111
  }
107
112
 
108
113
  // Define log.log as an alias for log.debug
109
114
  this.log = this.debug;
115
+
116
+ // Return any important warnings.
117
+ if (typeof console === undefinedType && level < this.levels.SILENT) {
118
+ return "No console available for logging";
119
+ }
110
120
  }
111
121
 
112
122
  // In old IE versions, the console isn't present until you first open it.
113
123
  // We build realMethod() replacements here that regenerate logging methods
114
- function enableLoggingWhenConsoleArrives(methodName, level, loggerName) {
124
+ function enableLoggingWhenConsoleArrives(methodName) {
115
125
  return function () {
116
126
  if (typeof console !== undefinedType) {
117
- replaceLoggingMethods.call(this, level, loggerName);
127
+ replaceLoggingMethods.call(this);
118
128
  this[methodName].apply(this, arguments);
119
129
  }
120
130
  };
@@ -122,14 +132,34 @@
122
132
 
123
133
  // By default, we use closely bound real methods wherever possible, and
124
134
  // otherwise we wait for a console to appear, and then try again.
125
- function defaultMethodFactory(methodName, level, loggerName) {
135
+ function defaultMethodFactory(methodName, _level, _loggerName) {
126
136
  /*jshint validthis:true */
127
137
  return realMethod(methodName) || enableLoggingWhenConsoleArrives.apply(this, arguments);
128
138
  }
129
- function Logger(name, defaultLevel, factory) {
139
+ function Logger(name, factory) {
140
+ // Private instance variables.
130
141
  var self = this;
131
- var currentLevel;
132
- defaultLevel = defaultLevel == null ? "WARN" : defaultLevel;
142
+ /**
143
+ * The level inherited from a parent logger (or a global default). We
144
+ * cache this here rather than delegating to the parent so that it stays
145
+ * in sync with the actual logging methods that we have installed (the
146
+ * parent could change levels but we might not have rebuilt the loggers
147
+ * in this child yet).
148
+ * @type {number}
149
+ */
150
+ var inheritedLevel;
151
+ /**
152
+ * The default level for this logger, if any. If set, this overrides
153
+ * `inheritedLevel`.
154
+ * @type {number|null}
155
+ */
156
+ var defaultLevel;
157
+ /**
158
+ * A user-specific level for this logger. If set, this overrides
159
+ * `defaultLevel`.
160
+ * @type {number|null}
161
+ */
162
+ var userLevel;
133
163
  var storageKey = "loglevel";
134
164
  if (typeof name === "string") {
135
165
  storageKey += ":" + name;
@@ -162,9 +192,10 @@
162
192
  if (typeof storedLevel === undefinedType) {
163
193
  try {
164
194
  var cookie = window.document.cookie;
165
- var location = cookie.indexOf(encodeURIComponent(storageKey) + "=");
195
+ var cookieName = encodeURIComponent(storageKey);
196
+ var location = cookie.indexOf(cookieName + "=");
166
197
  if (location !== -1) {
167
- storedLevel = /^([^;]+)/.exec(cookie.slice(location))[1];
198
+ storedLevel = /^([^;]+)/.exec(cookie.slice(location + cookieName.length + 1))[1];
168
199
  }
169
200
  } catch (ignore) {}
170
201
  }
@@ -181,7 +212,6 @@
181
212
  // Use localStorage if available
182
213
  try {
183
214
  window.localStorage.removeItem(storageKey);
184
- return;
185
215
  } catch (ignore) {}
186
216
 
187
217
  // Use session cookie as fallback
@@ -189,6 +219,17 @@
189
219
  window.document.cookie = encodeURIComponent(storageKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
190
220
  } catch (ignore) {}
191
221
  }
222
+ function normalizeLevel(input) {
223
+ var level = input;
224
+ if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
225
+ level = self.levels[level.toUpperCase()];
226
+ }
227
+ if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
228
+ return level;
229
+ } else {
230
+ throw new TypeError("log.setLevel() called with invalid level: " + input);
231
+ }
232
+ }
192
233
 
193
234
  /*
194
235
  *
@@ -207,35 +248,34 @@
207
248
  };
208
249
  self.methodFactory = factory || defaultMethodFactory;
209
250
  self.getLevel = function () {
210
- return currentLevel;
251
+ if (userLevel != null) {
252
+ return userLevel;
253
+ } else if (defaultLevel != null) {
254
+ return defaultLevel;
255
+ } else {
256
+ return inheritedLevel;
257
+ }
211
258
  };
212
259
  self.setLevel = function (level, persist) {
213
- if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
214
- level = self.levels[level.toUpperCase()];
215
- }
216
- if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
217
- currentLevel = level;
218
- if (persist !== false) {
219
- // defaults to true
220
- persistLevelIfPossible(level);
221
- }
222
- replaceLoggingMethods.call(self, level, name);
223
- if (typeof console === undefinedType && level < self.levels.SILENT) {
224
- return "No console available for logging";
225
- }
226
- } else {
227
- throw "log.setLevel() called with invalid level: " + level;
260
+ userLevel = normalizeLevel(level);
261
+ if (persist !== false) {
262
+ // defaults to true
263
+ persistLevelIfPossible(userLevel);
228
264
  }
265
+
266
+ // NOTE: in v2, this should call rebuild(), which updates children.
267
+ return replaceLoggingMethods.call(self);
229
268
  };
230
269
  self.setDefaultLevel = function (level) {
231
- defaultLevel = level;
270
+ defaultLevel = normalizeLevel(level);
232
271
  if (!getPersistedLevel()) {
233
272
  self.setLevel(level, false);
234
273
  }
235
274
  };
236
275
  self.resetLevel = function () {
237
- self.setLevel(defaultLevel, false);
276
+ userLevel = null;
238
277
  clearPersistedLevel();
278
+ replaceLoggingMethods.call(self);
239
279
  };
240
280
  self.enableAll = function (persist) {
241
281
  self.setLevel(self.levels.TRACE, persist);
@@ -243,13 +283,25 @@
243
283
  self.disableAll = function (persist) {
244
284
  self.setLevel(self.levels.SILENT, persist);
245
285
  };
286
+ self.rebuild = function () {
287
+ if (defaultLogger !== self) {
288
+ inheritedLevel = normalizeLevel(defaultLogger.getLevel());
289
+ }
290
+ replaceLoggingMethods.call(self);
291
+ if (defaultLogger === self) {
292
+ for (var childName in _loggersByName) {
293
+ _loggersByName[childName].rebuild();
294
+ }
295
+ }
296
+ };
246
297
 
247
- // Initialize with the right level
298
+ // Initialize all the internal levels.
299
+ inheritedLevel = normalizeLevel(defaultLogger ? defaultLogger.getLevel() : "WARN");
248
300
  var initialLevel = getPersistedLevel();
249
- if (initialLevel == null) {
250
- initialLevel = defaultLevel;
301
+ if (initialLevel != null) {
302
+ userLevel = normalizeLevel(initialLevel);
251
303
  }
252
- self.setLevel(initialLevel, false);
304
+ replaceLoggingMethods.call(self);
253
305
  }
254
306
 
255
307
  /*
@@ -258,15 +310,14 @@
258
310
  *
259
311
  */
260
312
 
261
- var defaultLogger = new Logger();
262
- var _loggersByName = {};
313
+ defaultLogger = new Logger();
263
314
  defaultLogger.getLogger = function getLogger(name) {
264
315
  if (typeof name !== "symbol" && typeof name !== "string" || name === "") {
265
316
  throw new TypeError("You must supply a name when creating a logger.");
266
317
  }
267
318
  var logger = _loggersByName[name];
268
319
  if (!logger) {
269
- logger = _loggersByName[name] = new Logger(name, defaultLogger.getLevel(), defaultLogger.methodFactory);
320
+ logger = _loggersByName[name] = new Logger(name, defaultLogger.methodFactory);
270
321
  }
271
322
  return logger;
272
323
  };
@@ -352,7 +403,8 @@
352
403
  nativeShare: 'nativeShareDialog',
353
404
  langInfos: 'getLanguageInfos',
354
405
  branchDefaultLang: 'getBranchDefaultLanguage',
355
- prefContentLang: 'getPreferredContentLocale'
406
+ prefContentLang: 'getPreferredContentLocale',
407
+ userContentLocale: 'getUserContentLocale'
356
408
  };
357
409
 
358
410
  /**
@@ -450,6 +502,12 @@
450
502
  name: 'English',
451
503
  localizedName: 'English'
452
504
  },
505
+ enGB: {
506
+ key: 'enGB',
507
+ locale: 'en_GB',
508
+ name: 'English (United Kingdom)',
509
+ localizedName: 'English (United Kingdom)'
510
+ },
453
511
  es: {
454
512
  key: 'es',
455
513
  locale: 'es_ES',
@@ -941,6 +999,16 @@
941
999
  }
942
1000
  };
943
1001
 
1002
+ /**
1003
+ * Get the current user's content locale, fallback to branch default locale
1004
+ *
1005
+ * @return {String} the user's content locale
1006
+ */
1007
+ const getUserContentLocale$2 = () => {
1008
+ const locale = getBranchDefaultLanguage$2().locale;
1009
+ return locale;
1010
+ };
1011
+
944
1012
  let connection$1 = null;
945
1013
  const fallbackKickIn = 500;
946
1014
 
@@ -951,7 +1019,7 @@
951
1019
  * after the time specified in fallbackKickIn runs out.
952
1020
  * @return {Promise<function>} An appropriate send function
953
1021
  */
954
- var fallback = (() => {
1022
+ var fallback = () => {
955
1023
  if (connection$1) {
956
1024
  return connection$1;
957
1025
  }
@@ -961,7 +1029,7 @@
961
1029
  }, fallbackKickIn);
962
1030
  });
963
1031
  return connection$1;
964
- });
1032
+ };
965
1033
 
966
1034
  /**
967
1035
  * Send a SDK command to the Staffbase App.
@@ -995,6 +1063,8 @@
995
1063
  return getBranchDefaultLanguage$2();
996
1064
  case commands.prefContentLang:
997
1065
  return getPreferredContentLocale$2.apply(null, payload);
1066
+ case commands.userContentLocale:
1067
+ return getUserContentLocale$2();
998
1068
  case commands.nativeUpload:
999
1069
  case commands.nativeShare:
1000
1070
  return unSupported();
@@ -1017,7 +1087,6 @@
1017
1087
  // send this to call a function in the frontend
1018
1088
  ERROR: 'ERROR' // receive this when something goes wrong
1019
1089
  };
1020
-
1021
1090
  const invocationMapping = {
1022
1091
  [commands.openLink]: 'openLink',
1023
1092
  [commands.nativeUpload]: 'nativeFileUpload',
@@ -1453,6 +1522,15 @@
1453
1522
  return sendMessage(commands.prefContentLang, content);
1454
1523
  };
1455
1524
 
1525
+ /**
1526
+ * Get the default content language configured for the branch.
1527
+ *
1528
+ * @return {Promise<Object>}
1529
+ */
1530
+ const getUserContentLocale$1 = async () => {
1531
+ return sendMessage(commands.langInfos).then(res => res.userContentLocale);
1532
+ };
1533
+
1456
1534
  /**
1457
1535
  * Compare [semver](https://semver.org/) version strings to find greater, equal or lesser.
1458
1536
  * This library supports the full semver specification, including comparing versions with different number of digits like `1.0.0`, `1.0`, `1`, and pre-release versions like `1.0.0-alpha`.
@@ -1659,6 +1737,13 @@
1659
1737
  */
1660
1738
  const getPreferredContentLocale = async content => getPreferredContentLocale$1(content);
1661
1739
 
1740
+ /**
1741
+ * Get the current user's content locale, fallback to branch default locale
1742
+ * @function
1743
+ * @return {Promise<any>}
1744
+ */
1745
+ const getUserContentLocale = async () => getUserContentLocale$1();
1746
+
1662
1747
  /**
1663
1748
  * Open a share dialog on native devices
1664
1749
  *
@@ -1683,6 +1768,7 @@
1683
1768
  exports.getBranchLanguages = getBranchLanguages;
1684
1769
  exports.getContentLanguages = getContentLanguages;
1685
1770
  exports.getPreferredContentLocale = getPreferredContentLocale;
1771
+ exports.getUserContentLocale = getUserContentLocale;
1686
1772
  exports.isAndroidDevice = isAndroidDevice;
1687
1773
  exports.isIosDevice = isIosDevice;
1688
1774
  exports.isMobileApp = isMobileApp;