@staffbase/plugins-client-sdk 2.0.2 → 3.0.0-beta.4

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>
@@ -40,6 +40,8 @@ var loglevel = {
40
40
  var undefinedType = "undefined";
41
41
  var isIE = typeof window !== undefinedType && typeof window.navigator !== undefinedType && /Trident\/|MSIE /.test(window.navigator.userAgent);
42
42
  var logMethods = ["trace", "debug", "info", "warn", "error"];
43
+ var _loggersByName = {};
44
+ var defaultLogger = null;
43
45
 
44
46
  // Cross-browser bind equivalent that works at least back to IE6
45
47
  function bindMethod(obj, methodName) {
@@ -92,23 +94,31 @@ var loglevel = {
92
94
 
93
95
  // These private functions always need `this` to be set properly
94
96
 
95
- function replaceLoggingMethods(level, loggerName) {
97
+ function replaceLoggingMethods() {
96
98
  /*jshint validthis:true */
99
+ var level = this.getLevel();
100
+
101
+ // Replace the actual methods.
97
102
  for (var i = 0; i < logMethods.length; i++) {
98
103
  var methodName = logMethods[i];
99
- this[methodName] = i < level ? noop : this.methodFactory(methodName, level, loggerName);
104
+ this[methodName] = i < level ? noop : this.methodFactory(methodName, level, this.name);
100
105
  }
101
106
 
102
107
  // Define log.log as an alias for log.debug
103
108
  this.log = this.debug;
109
+
110
+ // Return any important warnings.
111
+ if (typeof console === undefinedType && level < this.levels.SILENT) {
112
+ return "No console available for logging";
113
+ }
104
114
  }
105
115
 
106
116
  // In old IE versions, the console isn't present until you first open it.
107
117
  // We build realMethod() replacements here that regenerate logging methods
108
- function enableLoggingWhenConsoleArrives(methodName, level, loggerName) {
118
+ function enableLoggingWhenConsoleArrives(methodName) {
109
119
  return function () {
110
120
  if (typeof console !== undefinedType) {
111
- replaceLoggingMethods.call(this, level, loggerName);
121
+ replaceLoggingMethods.call(this);
112
122
  this[methodName].apply(this, arguments);
113
123
  }
114
124
  };
@@ -116,14 +126,34 @@ var loglevel = {
116
126
 
117
127
  // By default, we use closely bound real methods wherever possible, and
118
128
  // otherwise we wait for a console to appear, and then try again.
119
- function defaultMethodFactory(methodName, level, loggerName) {
129
+ function defaultMethodFactory(methodName, _level, _loggerName) {
120
130
  /*jshint validthis:true */
121
131
  return realMethod(methodName) || enableLoggingWhenConsoleArrives.apply(this, arguments);
122
132
  }
123
- function Logger(name, defaultLevel, factory) {
133
+ function Logger(name, factory) {
134
+ // Private instance variables.
124
135
  var self = this;
125
- var currentLevel;
126
- defaultLevel = defaultLevel == null ? "WARN" : defaultLevel;
136
+ /**
137
+ * The level inherited from a parent logger (or a global default). We
138
+ * cache this here rather than delegating to the parent so that it stays
139
+ * in sync with the actual logging methods that we have installed (the
140
+ * parent could change levels but we might not have rebuilt the loggers
141
+ * in this child yet).
142
+ * @type {number}
143
+ */
144
+ var inheritedLevel;
145
+ /**
146
+ * The default level for this logger, if any. If set, this overrides
147
+ * `inheritedLevel`.
148
+ * @type {number|null}
149
+ */
150
+ var defaultLevel;
151
+ /**
152
+ * A user-specific level for this logger. If set, this overrides
153
+ * `defaultLevel`.
154
+ * @type {number|null}
155
+ */
156
+ var userLevel;
127
157
  var storageKey = "loglevel";
128
158
  if (typeof name === "string") {
129
159
  storageKey += ":" + name;
@@ -156,9 +186,10 @@ var loglevel = {
156
186
  if (typeof storedLevel === undefinedType) {
157
187
  try {
158
188
  var cookie = window.document.cookie;
159
- var location = cookie.indexOf(encodeURIComponent(storageKey) + "=");
189
+ var cookieName = encodeURIComponent(storageKey);
190
+ var location = cookie.indexOf(cookieName + "=");
160
191
  if (location !== -1) {
161
- storedLevel = /^([^;]+)/.exec(cookie.slice(location))[1];
192
+ storedLevel = /^([^;]+)/.exec(cookie.slice(location + cookieName.length + 1))[1];
162
193
  }
163
194
  } catch (ignore) {}
164
195
  }
@@ -175,7 +206,6 @@ var loglevel = {
175
206
  // Use localStorage if available
176
207
  try {
177
208
  window.localStorage.removeItem(storageKey);
178
- return;
179
209
  } catch (ignore) {}
180
210
 
181
211
  // Use session cookie as fallback
@@ -183,6 +213,17 @@ var loglevel = {
183
213
  window.document.cookie = encodeURIComponent(storageKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
184
214
  } catch (ignore) {}
185
215
  }
216
+ function normalizeLevel(input) {
217
+ var level = input;
218
+ if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
219
+ level = self.levels[level.toUpperCase()];
220
+ }
221
+ if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
222
+ return level;
223
+ } else {
224
+ throw new TypeError("log.setLevel() called with invalid level: " + input);
225
+ }
226
+ }
186
227
 
187
228
  /*
188
229
  *
@@ -201,35 +242,34 @@ var loglevel = {
201
242
  };
202
243
  self.methodFactory = factory || defaultMethodFactory;
203
244
  self.getLevel = function () {
204
- return currentLevel;
245
+ if (userLevel != null) {
246
+ return userLevel;
247
+ } else if (defaultLevel != null) {
248
+ return defaultLevel;
249
+ } else {
250
+ return inheritedLevel;
251
+ }
205
252
  };
206
253
  self.setLevel = function (level, persist) {
207
- if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
208
- level = self.levels[level.toUpperCase()];
209
- }
210
- if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
211
- currentLevel = level;
212
- if (persist !== false) {
213
- // defaults to true
214
- persistLevelIfPossible(level);
215
- }
216
- replaceLoggingMethods.call(self, level, name);
217
- if (typeof console === undefinedType && level < self.levels.SILENT) {
218
- return "No console available for logging";
219
- }
220
- } else {
221
- throw "log.setLevel() called with invalid level: " + level;
254
+ userLevel = normalizeLevel(level);
255
+ if (persist !== false) {
256
+ // defaults to true
257
+ persistLevelIfPossible(userLevel);
222
258
  }
259
+
260
+ // NOTE: in v2, this should call rebuild(), which updates children.
261
+ return replaceLoggingMethods.call(self);
223
262
  };
224
263
  self.setDefaultLevel = function (level) {
225
- defaultLevel = level;
264
+ defaultLevel = normalizeLevel(level);
226
265
  if (!getPersistedLevel()) {
227
266
  self.setLevel(level, false);
228
267
  }
229
268
  };
230
269
  self.resetLevel = function () {
231
- self.setLevel(defaultLevel, false);
270
+ userLevel = null;
232
271
  clearPersistedLevel();
272
+ replaceLoggingMethods.call(self);
233
273
  };
234
274
  self.enableAll = function (persist) {
235
275
  self.setLevel(self.levels.TRACE, persist);
@@ -237,13 +277,25 @@ var loglevel = {
237
277
  self.disableAll = function (persist) {
238
278
  self.setLevel(self.levels.SILENT, persist);
239
279
  };
280
+ self.rebuild = function () {
281
+ if (defaultLogger !== self) {
282
+ inheritedLevel = normalizeLevel(defaultLogger.getLevel());
283
+ }
284
+ replaceLoggingMethods.call(self);
285
+ if (defaultLogger === self) {
286
+ for (var childName in _loggersByName) {
287
+ _loggersByName[childName].rebuild();
288
+ }
289
+ }
290
+ };
240
291
 
241
- // Initialize with the right level
292
+ // Initialize all the internal levels.
293
+ inheritedLevel = normalizeLevel(defaultLogger ? defaultLogger.getLevel() : "WARN");
242
294
  var initialLevel = getPersistedLevel();
243
- if (initialLevel == null) {
244
- initialLevel = defaultLevel;
295
+ if (initialLevel != null) {
296
+ userLevel = normalizeLevel(initialLevel);
245
297
  }
246
- self.setLevel(initialLevel, false);
298
+ replaceLoggingMethods.call(self);
247
299
  }
248
300
 
249
301
  /*
@@ -252,15 +304,14 @@ var loglevel = {
252
304
  *
253
305
  */
254
306
 
255
- var defaultLogger = new Logger();
256
- var _loggersByName = {};
307
+ defaultLogger = new Logger();
257
308
  defaultLogger.getLogger = function getLogger(name) {
258
309
  if (typeof name !== "symbol" && typeof name !== "string" || name === "") {
259
310
  throw new TypeError("You must supply a name when creating a logger.");
260
311
  }
261
312
  var logger = _loggersByName[name];
262
313
  if (!logger) {
263
- logger = _loggersByName[name] = new Logger(name, defaultLogger.getLevel(), defaultLogger.methodFactory);
314
+ logger = _loggersByName[name] = new Logger(name, defaultLogger.methodFactory);
264
315
  }
265
316
  return logger;
266
317
  };
@@ -346,7 +397,8 @@ const commands = {
346
397
  nativeShare: 'nativeShareDialog',
347
398
  langInfos: 'getLanguageInfos',
348
399
  branchDefaultLang: 'getBranchDefaultLanguage',
349
- prefContentLang: 'getPreferredContentLocale'
400
+ prefContentLang: 'getPreferredContentLocale',
401
+ userContentLocale: 'getUserContentLocale'
350
402
  };
351
403
 
352
404
  /**
@@ -935,6 +987,16 @@ const getPreferredContentLocale$2 = content => {
935
987
  }
936
988
  };
937
989
 
990
+ /**
991
+ * Get the current user's content locale, fallback to branch default locale
992
+ *
993
+ * @return {String} the user's content locale
994
+ */
995
+ const getUserContentLocale$2 = () => {
996
+ const locale = getBranchDefaultLanguage$2().locale;
997
+ return locale;
998
+ };
999
+
938
1000
  let connection$1 = null;
939
1001
  const fallbackKickIn = 500;
940
1002
 
@@ -989,6 +1051,8 @@ const sendMessage$2 = async function (cmd) {
989
1051
  return getBranchDefaultLanguage$2();
990
1052
  case commands.prefContentLang:
991
1053
  return getPreferredContentLocale$2.apply(null, payload);
1054
+ case commands.userContentLocale:
1055
+ return getUserContentLocale$2();
992
1056
  case commands.nativeUpload:
993
1057
  case commands.nativeShare:
994
1058
  return unSupported();
@@ -1011,7 +1075,6 @@ var protocol = {
1011
1075
  // send this to call a function in the frontend
1012
1076
  ERROR: 'ERROR' // receive this when something goes wrong
1013
1077
  };
1014
-
1015
1078
  const invocationMapping = {
1016
1079
  [commands.openLink]: 'openLink',
1017
1080
  [commands.nativeUpload]: 'nativeFileUpload',
@@ -1447,6 +1510,15 @@ const getPreferredContentLocale$1 = async content => {
1447
1510
  return sendMessage(commands.prefContentLang, content);
1448
1511
  };
1449
1512
 
1513
+ /**
1514
+ * Get the default content language configured for the branch.
1515
+ *
1516
+ * @return {Promise<Object>}
1517
+ */
1518
+ const getUserContentLocale$1 = async () => {
1519
+ return sendMessage(commands.langInfos).then(res => res.userContentLocale);
1520
+ };
1521
+
1450
1522
  /**
1451
1523
  * Compare [semver](https://semver.org/) version strings to find greater, equal or lesser.
1452
1524
  * 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`.
@@ -1653,6 +1725,13 @@ const getContentLanguages = async () => getContentLanguages$1();
1653
1725
  */
1654
1726
  const getPreferredContentLocale = async content => getPreferredContentLocale$1(content);
1655
1727
 
1728
+ /**
1729
+ * Get the current user's content locale, fallback to branch default locale
1730
+ * @function
1731
+ * @return {Promise<any>}
1732
+ */
1733
+ const getUserContentLocale = async () => getUserContentLocale$1();
1734
+
1656
1735
  /**
1657
1736
  * Open a share dialog on native devices
1658
1737
  *
@@ -1671,5 +1750,5 @@ const getPreferredContentLocale = async content => getPreferredContentLocale$1(c
1671
1750
  const openNativeShareDialog = async content => openNativeShareDialog$1(content);
1672
1751
  /* experimental */
1673
1752
 
1674
- export { deviceCanDownload, getAppVersion, getBranchDefaultLanguage, getBranchLanguages, getContentLanguages, getPreferredContentLocale, isAndroidDevice, isIosDevice, isMobileApp, isNativeApp, openLink, openLinkExternal, openLinkInternal, openNativeFileDialog, openNativeShareDialog };
1753
+ export { deviceCanDownload, getAppVersion, getBranchDefaultLanguage, getBranchLanguages, getContentLanguages, getPreferredContentLocale, getUserContentLocale, isAndroidDevice, isIosDevice, isMobileApp, isNativeApp, openLink, openLinkExternal, openLinkInternal, openNativeFileDialog, openNativeShareDialog };
1675
1754
  //# sourceMappingURL=plugins-client-sdk.esm.js.map