@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>
@@ -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
  /**
@@ -937,6 +989,16 @@ const getPreferredContentLocale$2 = content => {
937
989
  }
938
990
  };
939
991
 
992
+ /**
993
+ * Get the current user's content locale, fallback to branch default locale
994
+ *
995
+ * @return {String} the user's content locale
996
+ */
997
+ const getUserContentLocale$2 = () => {
998
+ const locale = getBranchDefaultLanguage$2().locale;
999
+ return locale;
1000
+ };
1001
+
940
1002
  let connection$1 = null;
941
1003
  const fallbackKickIn = 500;
942
1004
 
@@ -947,7 +1009,7 @@ const fallbackKickIn = 500;
947
1009
  * after the time specified in fallbackKickIn runs out.
948
1010
  * @return {Promise<function>} An appropriate send function
949
1011
  */
950
- var fallback = (() => {
1012
+ var fallback = () => {
951
1013
  if (connection$1) {
952
1014
  return connection$1;
953
1015
  }
@@ -957,7 +1019,7 @@ var fallback = (() => {
957
1019
  }, fallbackKickIn);
958
1020
  });
959
1021
  return connection$1;
960
- });
1022
+ };
961
1023
 
962
1024
  /**
963
1025
  * Send a SDK command to the Staffbase App.
@@ -991,6 +1053,8 @@ const sendMessage$2 = async function (cmd) {
991
1053
  return getBranchDefaultLanguage$2();
992
1054
  case commands.prefContentLang:
993
1055
  return getPreferredContentLocale$2.apply(null, payload);
1056
+ case commands.userContentLocale:
1057
+ return getUserContentLocale$2();
994
1058
  case commands.nativeUpload:
995
1059
  case commands.nativeShare:
996
1060
  return unSupported();
@@ -1013,7 +1077,6 @@ var protocol = {
1013
1077
  // send this to call a function in the frontend
1014
1078
  ERROR: 'ERROR' // receive this when something goes wrong
1015
1079
  };
1016
-
1017
1080
  const invocationMapping = {
1018
1081
  [commands.openLink]: 'openLink',
1019
1082
  [commands.nativeUpload]: 'nativeFileUpload',
@@ -1449,6 +1512,15 @@ const getPreferredContentLocale$1 = async content => {
1449
1512
  return sendMessage(commands.prefContentLang, content);
1450
1513
  };
1451
1514
 
1515
+ /**
1516
+ * Get the default content language configured for the branch.
1517
+ *
1518
+ * @return {Promise<Object>}
1519
+ */
1520
+ const getUserContentLocale$1 = async () => {
1521
+ return sendMessage(commands.langInfos).then(res => res.userContentLocale);
1522
+ };
1523
+
1452
1524
  /**
1453
1525
  * Compare [semver](https://semver.org/) version strings to find greater, equal or lesser.
1454
1526
  * 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 +1727,13 @@ const getContentLanguages = async () => getContentLanguages$1();
1655
1727
  */
1656
1728
  const getPreferredContentLocale = async content => getPreferredContentLocale$1(content);
1657
1729
 
1730
+ /**
1731
+ * Get the current user's content locale, fallback to branch default locale
1732
+ * @function
1733
+ * @return {Promise<any>}
1734
+ */
1735
+ const getUserContentLocale = async () => getUserContentLocale$1();
1736
+
1658
1737
  /**
1659
1738
  * Open a share dialog on native devices
1660
1739
  *
@@ -1679,6 +1758,7 @@ exports.getBranchDefaultLanguage = getBranchDefaultLanguage;
1679
1758
  exports.getBranchLanguages = getBranchLanguages;
1680
1759
  exports.getContentLanguages = getContentLanguages;
1681
1760
  exports.getPreferredContentLocale = getPreferredContentLocale;
1761
+ exports.getUserContentLocale = getUserContentLocale;
1682
1762
  exports.isAndroidDevice = isAndroidDevice;
1683
1763
  exports.isIosDevice = isIosDevice;
1684
1764
  exports.isMobileApp = isMobileApp;