@staffbase/plugins-client-sdk 2.0.2 → 3.0.0

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,7 +2,7 @@
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>
@@ -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
  /**
@@ -941,6 +993,16 @@
941
993
  }
942
994
  };
943
995
 
996
+ /**
997
+ * Get the current user's content locale, fallback to branch default locale
998
+ *
999
+ * @return {String} the user's content locale
1000
+ */
1001
+ const getUserContentLocale$2 = () => {
1002
+ const locale = getBranchDefaultLanguage$2().locale;
1003
+ return locale;
1004
+ };
1005
+
944
1006
  let connection$1 = null;
945
1007
  const fallbackKickIn = 500;
946
1008
 
@@ -951,7 +1013,7 @@
951
1013
  * after the time specified in fallbackKickIn runs out.
952
1014
  * @return {Promise<function>} An appropriate send function
953
1015
  */
954
- var fallback = (() => {
1016
+ var fallback = () => {
955
1017
  if (connection$1) {
956
1018
  return connection$1;
957
1019
  }
@@ -961,7 +1023,7 @@
961
1023
  }, fallbackKickIn);
962
1024
  });
963
1025
  return connection$1;
964
- });
1026
+ };
965
1027
 
966
1028
  /**
967
1029
  * Send a SDK command to the Staffbase App.
@@ -995,6 +1057,8 @@
995
1057
  return getBranchDefaultLanguage$2();
996
1058
  case commands.prefContentLang:
997
1059
  return getPreferredContentLocale$2.apply(null, payload);
1060
+ case commands.userContentLocale:
1061
+ return getUserContentLocale$2();
998
1062
  case commands.nativeUpload:
999
1063
  case commands.nativeShare:
1000
1064
  return unSupported();
@@ -1017,7 +1081,6 @@
1017
1081
  // send this to call a function in the frontend
1018
1082
  ERROR: 'ERROR' // receive this when something goes wrong
1019
1083
  };
1020
-
1021
1084
  const invocationMapping = {
1022
1085
  [commands.openLink]: 'openLink',
1023
1086
  [commands.nativeUpload]: 'nativeFileUpload',
@@ -1453,6 +1516,15 @@
1453
1516
  return sendMessage(commands.prefContentLang, content);
1454
1517
  };
1455
1518
 
1519
+ /**
1520
+ * Get the default content language configured for the branch.
1521
+ *
1522
+ * @return {Promise<Object>}
1523
+ */
1524
+ const getUserContentLocale$1 = async () => {
1525
+ return sendMessage(commands.langInfos).then(res => res.userContentLocale);
1526
+ };
1527
+
1456
1528
  /**
1457
1529
  * Compare [semver](https://semver.org/) version strings to find greater, equal or lesser.
1458
1530
  * 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 +1731,13 @@
1659
1731
  */
1660
1732
  const getPreferredContentLocale = async content => getPreferredContentLocale$1(content);
1661
1733
 
1734
+ /**
1735
+ * Get the current user's content locale, fallback to branch default locale
1736
+ * @function
1737
+ * @return {Promise<any>}
1738
+ */
1739
+ const getUserContentLocale = async () => getUserContentLocale$1();
1740
+
1662
1741
  /**
1663
1742
  * Open a share dialog on native devices
1664
1743
  *
@@ -1683,6 +1762,7 @@
1683
1762
  exports.getBranchLanguages = getBranchLanguages;
1684
1763
  exports.getContentLanguages = getContentLanguages;
1685
1764
  exports.getPreferredContentLocale = getPreferredContentLocale;
1765
+ exports.getUserContentLocale = getUserContentLocale;
1686
1766
  exports.isAndroidDevice = isAndroidDevice;
1687
1767
  exports.isIosDevice = isIosDevice;
1688
1768
  exports.isMobileApp = isMobileApp;