@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
 
@@ -42,6 +42,8 @@ var loglevel = {
42
42
  var undefinedType = "undefined";
43
43
  var isIE = typeof window !== undefinedType && typeof window.navigator !== undefinedType && /Trident\/|MSIE /.test(window.navigator.userAgent);
44
44
  var logMethods = ["trace", "debug", "info", "warn", "error"];
45
+ var _loggersByName = {};
46
+ var defaultLogger = null;
45
47
 
46
48
  // Cross-browser bind equivalent that works at least back to IE6
47
49
  function bindMethod(obj, methodName) {
@@ -94,23 +96,31 @@ var loglevel = {
94
96
 
95
97
  // These private functions always need `this` to be set properly
96
98
 
97
- function replaceLoggingMethods(level, loggerName) {
99
+ function replaceLoggingMethods() {
98
100
  /*jshint validthis:true */
101
+ var level = this.getLevel();
102
+
103
+ // Replace the actual methods.
99
104
  for (var i = 0; i < logMethods.length; i++) {
100
105
  var methodName = logMethods[i];
101
- this[methodName] = i < level ? noop : this.methodFactory(methodName, level, loggerName);
106
+ this[methodName] = i < level ? noop : this.methodFactory(methodName, level, this.name);
102
107
  }
103
108
 
104
109
  // Define log.log as an alias for log.debug
105
110
  this.log = this.debug;
111
+
112
+ // Return any important warnings.
113
+ if (typeof console === undefinedType && level < this.levels.SILENT) {
114
+ return "No console available for logging";
115
+ }
106
116
  }
107
117
 
108
118
  // In old IE versions, the console isn't present until you first open it.
109
119
  // We build realMethod() replacements here that regenerate logging methods
110
- function enableLoggingWhenConsoleArrives(methodName, level, loggerName) {
120
+ function enableLoggingWhenConsoleArrives(methodName) {
111
121
  return function () {
112
122
  if (typeof console !== undefinedType) {
113
- replaceLoggingMethods.call(this, level, loggerName);
123
+ replaceLoggingMethods.call(this);
114
124
  this[methodName].apply(this, arguments);
115
125
  }
116
126
  };
@@ -118,14 +128,34 @@ var loglevel = {
118
128
 
119
129
  // By default, we use closely bound real methods wherever possible, and
120
130
  // otherwise we wait for a console to appear, and then try again.
121
- function defaultMethodFactory(methodName, level, loggerName) {
131
+ function defaultMethodFactory(methodName, _level, _loggerName) {
122
132
  /*jshint validthis:true */
123
133
  return realMethod(methodName) || enableLoggingWhenConsoleArrives.apply(this, arguments);
124
134
  }
125
- function Logger(name, defaultLevel, factory) {
135
+ function Logger(name, factory) {
136
+ // Private instance variables.
126
137
  var self = this;
127
- var currentLevel;
128
- defaultLevel = defaultLevel == null ? "WARN" : defaultLevel;
138
+ /**
139
+ * The level inherited from a parent logger (or a global default). We
140
+ * cache this here rather than delegating to the parent so that it stays
141
+ * in sync with the actual logging methods that we have installed (the
142
+ * parent could change levels but we might not have rebuilt the loggers
143
+ * in this child yet).
144
+ * @type {number}
145
+ */
146
+ var inheritedLevel;
147
+ /**
148
+ * The default level for this logger, if any. If set, this overrides
149
+ * `inheritedLevel`.
150
+ * @type {number|null}
151
+ */
152
+ var defaultLevel;
153
+ /**
154
+ * A user-specific level for this logger. If set, this overrides
155
+ * `defaultLevel`.
156
+ * @type {number|null}
157
+ */
158
+ var userLevel;
129
159
  var storageKey = "loglevel";
130
160
  if (typeof name === "string") {
131
161
  storageKey += ":" + name;
@@ -158,9 +188,10 @@ var loglevel = {
158
188
  if (typeof storedLevel === undefinedType) {
159
189
  try {
160
190
  var cookie = window.document.cookie;
161
- var location = cookie.indexOf(encodeURIComponent(storageKey) + "=");
191
+ var cookieName = encodeURIComponent(storageKey);
192
+ var location = cookie.indexOf(cookieName + "=");
162
193
  if (location !== -1) {
163
- storedLevel = /^([^;]+)/.exec(cookie.slice(location))[1];
194
+ storedLevel = /^([^;]+)/.exec(cookie.slice(location + cookieName.length + 1))[1];
164
195
  }
165
196
  } catch (ignore) {}
166
197
  }
@@ -177,7 +208,6 @@ var loglevel = {
177
208
  // Use localStorage if available
178
209
  try {
179
210
  window.localStorage.removeItem(storageKey);
180
- return;
181
211
  } catch (ignore) {}
182
212
 
183
213
  // Use session cookie as fallback
@@ -185,6 +215,17 @@ var loglevel = {
185
215
  window.document.cookie = encodeURIComponent(storageKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
186
216
  } catch (ignore) {}
187
217
  }
218
+ function normalizeLevel(input) {
219
+ var level = input;
220
+ if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
221
+ level = self.levels[level.toUpperCase()];
222
+ }
223
+ if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
224
+ return level;
225
+ } else {
226
+ throw new TypeError("log.setLevel() called with invalid level: " + input);
227
+ }
228
+ }
188
229
 
189
230
  /*
190
231
  *
@@ -203,35 +244,34 @@ var loglevel = {
203
244
  };
204
245
  self.methodFactory = factory || defaultMethodFactory;
205
246
  self.getLevel = function () {
206
- return currentLevel;
247
+ if (userLevel != null) {
248
+ return userLevel;
249
+ } else if (defaultLevel != null) {
250
+ return defaultLevel;
251
+ } else {
252
+ return inheritedLevel;
253
+ }
207
254
  };
208
255
  self.setLevel = function (level, persist) {
209
- if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
210
- level = self.levels[level.toUpperCase()];
211
- }
212
- if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
213
- currentLevel = level;
214
- if (persist !== false) {
215
- // defaults to true
216
- persistLevelIfPossible(level);
217
- }
218
- replaceLoggingMethods.call(self, level, name);
219
- if (typeof console === undefinedType && level < self.levels.SILENT) {
220
- return "No console available for logging";
221
- }
222
- } else {
223
- throw "log.setLevel() called with invalid level: " + level;
256
+ userLevel = normalizeLevel(level);
257
+ if (persist !== false) {
258
+ // defaults to true
259
+ persistLevelIfPossible(userLevel);
224
260
  }
261
+
262
+ // NOTE: in v2, this should call rebuild(), which updates children.
263
+ return replaceLoggingMethods.call(self);
225
264
  };
226
265
  self.setDefaultLevel = function (level) {
227
- defaultLevel = level;
266
+ defaultLevel = normalizeLevel(level);
228
267
  if (!getPersistedLevel()) {
229
268
  self.setLevel(level, false);
230
269
  }
231
270
  };
232
271
  self.resetLevel = function () {
233
- self.setLevel(defaultLevel, false);
272
+ userLevel = null;
234
273
  clearPersistedLevel();
274
+ replaceLoggingMethods.call(self);
235
275
  };
236
276
  self.enableAll = function (persist) {
237
277
  self.setLevel(self.levels.TRACE, persist);
@@ -239,13 +279,25 @@ var loglevel = {
239
279
  self.disableAll = function (persist) {
240
280
  self.setLevel(self.levels.SILENT, persist);
241
281
  };
282
+ self.rebuild = function () {
283
+ if (defaultLogger !== self) {
284
+ inheritedLevel = normalizeLevel(defaultLogger.getLevel());
285
+ }
286
+ replaceLoggingMethods.call(self);
287
+ if (defaultLogger === self) {
288
+ for (var childName in _loggersByName) {
289
+ _loggersByName[childName].rebuild();
290
+ }
291
+ }
292
+ };
242
293
 
243
- // Initialize with the right level
294
+ // Initialize all the internal levels.
295
+ inheritedLevel = normalizeLevel(defaultLogger ? defaultLogger.getLevel() : "WARN");
244
296
  var initialLevel = getPersistedLevel();
245
- if (initialLevel == null) {
246
- initialLevel = defaultLevel;
297
+ if (initialLevel != null) {
298
+ userLevel = normalizeLevel(initialLevel);
247
299
  }
248
- self.setLevel(initialLevel, false);
300
+ replaceLoggingMethods.call(self);
249
301
  }
250
302
 
251
303
  /*
@@ -254,15 +306,14 @@ var loglevel = {
254
306
  *
255
307
  */
256
308
 
257
- var defaultLogger = new Logger();
258
- var _loggersByName = {};
309
+ defaultLogger = new Logger();
259
310
  defaultLogger.getLogger = function getLogger(name) {
260
311
  if (typeof name !== "symbol" && typeof name !== "string" || name === "") {
261
312
  throw new TypeError("You must supply a name when creating a logger.");
262
313
  }
263
314
  var logger = _loggersByName[name];
264
315
  if (!logger) {
265
- logger = _loggersByName[name] = new Logger(name, defaultLogger.getLevel(), defaultLogger.methodFactory);
316
+ logger = _loggersByName[name] = new Logger(name, defaultLogger.methodFactory);
266
317
  }
267
318
  return logger;
268
319
  };
@@ -348,7 +399,8 @@ const commands = {
348
399
  nativeShare: 'nativeShareDialog',
349
400
  langInfos: 'getLanguageInfos',
350
401
  branchDefaultLang: 'getBranchDefaultLanguage',
351
- prefContentLang: 'getPreferredContentLocale'
402
+ prefContentLang: 'getPreferredContentLocale',
403
+ userContentLocale: 'getUserContentLocale'
352
404
  };
353
405
 
354
406
  /**
@@ -446,6 +498,12 @@ var locales = {
446
498
  name: 'English',
447
499
  localizedName: 'English'
448
500
  },
501
+ enGB: {
502
+ key: 'enGB',
503
+ locale: 'en_GB',
504
+ name: 'English (United Kingdom)',
505
+ localizedName: 'English (United Kingdom)'
506
+ },
449
507
  es: {
450
508
  key: 'es',
451
509
  locale: 'es_ES',
@@ -937,6 +995,16 @@ const getPreferredContentLocale$2 = content => {
937
995
  }
938
996
  };
939
997
 
998
+ /**
999
+ * Get the current user's content locale, fallback to branch default locale
1000
+ *
1001
+ * @return {String} the user's content locale
1002
+ */
1003
+ const getUserContentLocale$2 = () => {
1004
+ const locale = getBranchDefaultLanguage$2().locale;
1005
+ return locale;
1006
+ };
1007
+
940
1008
  let connection$1 = null;
941
1009
  const fallbackKickIn = 500;
942
1010
 
@@ -947,7 +1015,7 @@ const fallbackKickIn = 500;
947
1015
  * after the time specified in fallbackKickIn runs out.
948
1016
  * @return {Promise<function>} An appropriate send function
949
1017
  */
950
- var fallback = (() => {
1018
+ var fallback = () => {
951
1019
  if (connection$1) {
952
1020
  return connection$1;
953
1021
  }
@@ -957,7 +1025,7 @@ var fallback = (() => {
957
1025
  }, fallbackKickIn);
958
1026
  });
959
1027
  return connection$1;
960
- });
1028
+ };
961
1029
 
962
1030
  /**
963
1031
  * Send a SDK command to the Staffbase App.
@@ -991,6 +1059,8 @@ const sendMessage$2 = async function (cmd) {
991
1059
  return getBranchDefaultLanguage$2();
992
1060
  case commands.prefContentLang:
993
1061
  return getPreferredContentLocale$2.apply(null, payload);
1062
+ case commands.userContentLocale:
1063
+ return getUserContentLocale$2();
994
1064
  case commands.nativeUpload:
995
1065
  case commands.nativeShare:
996
1066
  return unSupported();
@@ -1013,7 +1083,6 @@ var protocol = {
1013
1083
  // send this to call a function in the frontend
1014
1084
  ERROR: 'ERROR' // receive this when something goes wrong
1015
1085
  };
1016
-
1017
1086
  const invocationMapping = {
1018
1087
  [commands.openLink]: 'openLink',
1019
1088
  [commands.nativeUpload]: 'nativeFileUpload',
@@ -1449,6 +1518,15 @@ const getPreferredContentLocale$1 = async content => {
1449
1518
  return sendMessage(commands.prefContentLang, content);
1450
1519
  };
1451
1520
 
1521
+ /**
1522
+ * Get the default content language configured for the branch.
1523
+ *
1524
+ * @return {Promise<Object>}
1525
+ */
1526
+ const getUserContentLocale$1 = async () => {
1527
+ return sendMessage(commands.langInfos).then(res => res.userContentLocale);
1528
+ };
1529
+
1452
1530
  /**
1453
1531
  * Compare [semver](https://semver.org/) version strings to find greater, equal or lesser.
1454
1532
  * 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`.
@@ -1655,6 +1733,13 @@ const getContentLanguages = async () => getContentLanguages$1();
1655
1733
  */
1656
1734
  const getPreferredContentLocale = async content => getPreferredContentLocale$1(content);
1657
1735
 
1736
+ /**
1737
+ * Get the current user's content locale, fallback to branch default locale
1738
+ * @function
1739
+ * @return {Promise<any>}
1740
+ */
1741
+ const getUserContentLocale = async () => getUserContentLocale$1();
1742
+
1658
1743
  /**
1659
1744
  * Open a share dialog on native devices
1660
1745
  *
@@ -1679,6 +1764,7 @@ exports.getBranchDefaultLanguage = getBranchDefaultLanguage;
1679
1764
  exports.getBranchLanguages = getBranchLanguages;
1680
1765
  exports.getContentLanguages = getContentLanguages;
1681
1766
  exports.getPreferredContentLocale = getPreferredContentLocale;
1767
+ exports.getUserContentLocale = getUserContentLocale;
1682
1768
  exports.isAndroidDevice = isAndroidDevice;
1683
1769
  exports.isIosDevice = isIosDevice;
1684
1770
  exports.isMobileApp = isMobileApp;