-
Notifications
You must be signed in to change notification settings - Fork 1
/
RandomTips.js
366 lines (317 loc) · 10.2 KB
/
RandomTips.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* Shows random tips to the user, if wanted.
*
* @public
* @module RandomTips
* @requires ../lib/lodash/debounce
* @requires ../AddonSettings
* @requires ../MessageHandler/CustomMessages
*/
// lodash
import debounce from "../lodash/debounce.js";
import * as AddonSettings from "../AddonSettings/AddonSettings.js";
import * as CustomMessages from "../MessageHandler/CustomMessages.js";
/**
* The saved settings of a specific tip.
*
* @public
* @typedef {Object} TipConfigObject
* @property {integer} shownCount how often the tip has already been shown
* @property {integer} dismissedCount how often the tip has already been dismissed
* @property {Object.<string, integer>} [shownContext] how often the tip has
* already been shown in a specific context. The aquivalent to {@link TipObject#shownInContext}
* See {@link module:RandomTips.setContext|setContext}}.
*/
/**
* The configuration stored by this module.
*
* It is stored in the key {@link TIP_SETTING_STORAGE_ID} in the data storage.
*
* @public
* @typedef {Object} ModuleConfig
* @property {Object.<string, module:RandomTips~TipConfigObject>} tips config for each tip
* @property {integer} triggeredOpen how often the whole module/add-on has been
* triggered, i.e. how often the {@link module:RandomTips.init|init} method has been called.
*/
const TIP_MESSAGE_BOX_ID = "messageTips";
const TIP_SETTING_STORAGE_ID = "randomTips";
const GLOBAL_RANDOMIZE = 0.2; // (%)
const DEBOUNCE_SAVING = 1000; // ms
const MESSAGE_TIP_ID = "messageTip";
// default values/settings for tip/tipconfig
/**
* @private
* @type {TipObject}
*/
const DEFAULT_TIP_SPEC = Object.freeze({
requiredTriggers: 10,
randomizeDisplay: false,
allowDismiss: true
});
/**
* @private
* @type {TipConfigObject}
*/
const DEFAULT_TIP_CONFIG = Object.freeze({
shownCount: 0,
dismissedCount: 0,
shownContext: {}
});
/**
* @private
* @type {TipObject[]}
* @see {@link tips}
*/
let tips;
/**
* @private
* @type {ModuleConfig}
*/
let moduleConfig = {
tips: {}
};
/**
* @private
* @type {TipConfigObject}
*/
let tipShowing = null;
/**
* @private
* @type {string}
*/
let context = null;
/**
* Save the current config.
*
* @function
* @name saveConfig
* @private
* @returns {void}
*/
let saveConfig = null; // will be assigned in init()
/**
* Hook for the dismiss event.
*
* @private
* @param {Object} param
* @returns {void}
*/
function messageDismissed(param) {
const elMessage = param.elMessage;
const id = elMessage.dataset.tipId;
if (tipShowing.id !== id) {
throw new Error("cached tip and dismissed tip differ");
}
// update config
moduleConfig.tips[id].dismissedCount = (moduleConfig.tips[id].dismissedCount || 0) + 1;
saveConfig();
// cleanup values
tipShowing = null;
delete elMessage.dataset.tipId;
console.info(`Tip ${id} has been dismissed.`);
}
/**
* Returns true or false at random. The passed propability indicates how
* much of the calls should return "true" on average.
*
* @private
* @param {number} propability
* @returns {bool}
*/
function randomizePassed(propability) {
return (Math.random() < propability);
}
/**
* Shows this tip.
*
* @private
* @param {TipObject} tipSpec
* @param {TipConfigObject} thisTipConfig the settings of the tip
* @returns {void}
*/
function showTip(tipSpec, thisTipConfig) {
const elMessage = CustomMessages.getHtmlElement(MESSAGE_TIP_ID);
elMessage.dataset.tipId = tipSpec.id;
CustomMessages.showMessage(MESSAGE_TIP_ID, tipSpec.text, tipSpec.allowDismiss, tipSpec.actionButton);
// update config
thisTipConfig.shownCount = thisTipConfig.shownCount + 1;
thisTipConfig.shownContext[context] = (thisTipConfig.shownContext[context] || 0) + 1;
saveConfig();
tipShowing = tipSpec;
}
/**
* Returns true or false at random. The passed procentage indicates how
* much of the calls should return "true" on average.
*
* @private
* @param {TipObject} tipSpecOrig
* @returns {Array.<TipObject, Object>}
*/
function applyTipSpecAndConfigDefaults(tipSpecOrig) {
// shallow-clone object (no deep object are being modified)
const tipSpec = Object.assign({}, DEFAULT_TIP_SPEC, tipSpecOrig);
// also convert to default value if just set to "true"
if (tipSpec.randomizeDisplay === true) {
// default value for randomizeDisplay = 0.5
tipSpec.randomizeDisplay = 0.5;
}
// create option if needed
let thisTipConfig = moduleConfig.tips[tipSpec.id];
if (thisTipConfig === undefined) {
// deep-clone default object
thisTipConfig = JSON.parse(JSON.stringify(DEFAULT_TIP_CONFIG));
// save it actually in config
moduleConfig.tips[tipSpec.id] = thisTipConfig;
saveConfig();
}
console.log("Applied default values for tip spec and tip config:", tipSpec, thisTipConfig);
return [tipSpec, thisTipConfig];
}
/**
* Returns whether the tip has already be shown enough times or may not
* be shown, because of some other requirement.
*
* @private
* @param {TipObject} tipSpec
* @param {TipConfigObject} thisTipConfig
* @param {Object} tipSpecOrig
* @returns {Promise} bool
*/
async function shouldBeShown(tipSpec, thisTipConfig, tipSpecOrig) {
if (tipSpec.showTip) {
const returnValue = await tipSpec.showTip(tipSpec, thisTipConfig, tipSpecOrig, moduleConfig);
saveConfig();
switch (returnValue) {
// pass-through booleans
case true:
case false:
return returnValue;
case null:
// continue checking when null is returned
break;
default:
throw new Error(`tipSpec.showTip was expected to return a boolean
or null, but returned "${returnValue}".`);
}
}
// require some global triggers, if needed
if (moduleConfig.triggeredOpen < tipSpec.requiredTriggers) {
return false;
}
// only show result in x% of cases (uses float points < 1 for percentage)
if (tipSpec.randomizeDisplay && !randomizePassed(tipSpec.randomizeDisplay)) {
return false;
}
// do not show if it has been dismissed enough times
if (tipSpec.maximumDismiss && thisTipConfig.dismissedCount >= tipSpec.maximumDismiss) {
return false;
}
// block when it is shown too much times in a given context
if (tipSpec.maximumInContest) {
if (context in tipSpec.maximumInContest) {
const tipShownInCurrentContext = moduleConfig.tips[tipSpec.id].shownContext[context] || 0;
if (tipShownInCurrentContext >= tipSpec.maximumInContest[context]) {
return false;
}
}
}
// NOTE: do not return true above this line (for obvious reasons)
// or has it been shown enough times already?
// dismiss is shown enough times?
let requiredDismissCount;
if (Number.isFinite(tipSpec.requireDismiss)) {
requiredDismissCount = tipSpec.requireDismiss;
} else if (tipSpec.requireDismiss === true) { // bool
requiredDismissCount = tipSpec.requiredShowCount;
} else {
// ignore dismiss count
requiredDismissCount = null;
}
// check context check if needed
if (tipSpec.showInContext) {
if (context in tipSpec.showInContext) {
const tipShownInCurrentContext = moduleConfig.tips[tipSpec.id].shownContext[context] || 0;
if (tipShownInCurrentContext < tipSpec.showInContext[context]) {
return true;
}
}
}
return (tipSpec.requiredShowCount === null || thisTipConfig.shownCount < tipSpec.requiredShowCount) // not already shown enough times already?
|| (requiredDismissCount !== null && thisTipConfig.dismissedCount < requiredDismissCount); // not dismissed enough times?
}
/**
* Sets the context for the current session.
*
* @public
* @param {string} newContext
* @returns {void}
*/
export function setContext(newContext) {
context = newContext;
}
/**
* Selects and shows a random tip.
*
* @public
* @returns {Promise}
*/
export async function showRandomTip() {
// only try to select tip, if one is even available
if (tips.length === 0) {
console.info("no tips to show available anymore");
return Promise.reject(new Error("no tips to show available anymore"));
}
// randomly select element
const randomNumber = Math.floor(Math.random() * tips.length);
const tipSpecOrig = tips[randomNumber];
// prepare tip spec
const [tipSpec, thisTipConfig] = applyTipSpecAndConfigDefaults(tipSpecOrig);
if (!(await shouldBeShown(tipSpec, thisTipConfig, tipSpecOrig))) {
// remove tip
tips.splice(randomNumber, 1);
// retry random selection
return showRandomTip();
}
console.info("selected tip to be shown:", randomNumber, tipSpec);
return showTip(tipSpec, thisTipConfig);
}
/**
* Shows the random tip only randomly so the user is not annoyed.
*
* @public
* @returns {Promise}
*/
export function showRandomTipIfWanted() {
saveConfig();
// randomize tip showing in general
if (!randomizePassed(GLOBAL_RANDOMIZE)) {
console.info("show no random tip, because randomize did not pass");
return Promise.reject(new Error("show no random tip, because randomize did not pass"));
}
return showRandomTip();
}
/**
* Initialises the module.
*
* @public
* @param {TipObject[]} tipsToShow the tips object to init
* @returns {Promise.<void>}
*/
export function init(tipsToShow) {
// use local shallow copy, so we can modify it
// inner objects won't be modified, so we do not need to deep-clone it.
tips = tipsToShow.slice();
// load function
// We need to assign it here to make it testable.
saveConfig = debounce(() => {
AddonSettings.set(TIP_SETTING_STORAGE_ID, moduleConfig);
}, DEBOUNCE_SAVING);
// register HTMLElement
CustomMessages.registerMessageType(MESSAGE_TIP_ID, document.getElementById(TIP_MESSAGE_BOX_ID));
CustomMessages.setHook(MESSAGE_TIP_ID, "dismissStart", messageDismissed);
return AddonSettings.get(TIP_SETTING_STORAGE_ID).then((randomTips) => {
moduleConfig = randomTips;
// count one open trigger
moduleConfig.triggeredOpen = (moduleConfig.triggeredOpen || 0) + 1;
});
}