forked from ChristofSchwarz/db_ext_guidedtour
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_ext_guided_tour_3.js
468 lines (420 loc) · 27 KB
/
db_ext_guided_tour_3.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
define(["qlik", "jquery", "text!./styles.css", "./props", "./tooltips",
"./license", "./picker", "./qlik-css-selectors"], function
(qlik, $, cssContent, props, tooltips, license, picker, qlikCss) {
'use strict';
const hintTxt = 'This is a tour without any tooltips yet. <ul><li>Please click on the small <strong>'
+ '<span style="background:green;color:white;border-radius:10px;"> + </span></strong>'
+ ' icons shown in the top left corner of an object to add it to the tour.</li><li>Then'
+ ' continue to edit them in the right property panel (accordeon) of this extension.</li></ul>';
const noItemsTxt = "\u26A0\uFE0F Please add some objects.";
var guided_tour_global = {
qext: {}, // extension meta-information
hashmap: license.hashmap(location.hostname, 'db_ext_guided_tour'), // hash map for the license check
activeTooltip: {}, // remember all active tours, contains later one entry per extension and the
// an integer shows the active tooltip (0..n) or -2 if tour is inactive, -1 (in hover-mode) if armed
visitedTours: {}, // all extension-ids which will be started, are added to this object
licensedObjs: {}, // list of all extension-ids which have a license
tooltipsCache: {}, // the tour items of each tour will be put here under the key of the objectId when started
noLicenseWarning: {}, // in order not to suppress repeating license warnings , every extension id is added here once the warning was shown
objAliases: {}
}
// const lStorageDefault = '{"openedAt":"18991231000000", "objectsOpened": {}}';
// function noLicenseMsg(mode) {
// return `The ${mode} mode would start now, if you had a license for the guided-tour extension.
// <br/><br/>Get in touch with <a href="mailto:[email protected]">[email protected]</a> '
// or choose a license-free mode of operation.`
// };
$("<style>").html(cssContent).appendTo("head");
$.ajax({
url: '../extensions/db_ext_guided_tour_3/db_ext_guided_tour_3.qext',
dataType: 'json',
async: false, // wait for this call to finish.
success: function (data) { guided_tour_global.qext = data; }
});
function getActiveTour(ownId, currSheet, layout) {
// returns the tour id which is currently active, or false if no tour is active
var activeTour = false;
if ($('.guided-tour-toolip-parent').length == 0) {
// no tour is open on this sheet. Set all tours of this sheet to -2
for (const tourId in guided_tour_global.activeTooltip[currSheet]) {
guided_tour_global.activeTooltip[currSheet][tourId] = -2
}
} else {
for (const sheetId in guided_tour_global.activeTooltip) {
for (const tourId in guided_tour_global.activeTooltip[sheetId]) {
if (guided_tour_global.activeTooltip[sheetId][tourId] > -2) {
if (tourId == ownId) {
// console.log(ownId, `This tour is already active.`);
} else {
if (layout.pConsoleLog) console.log(ownId, `other tour ${tourId} is already active.`);
}
activeTour = tourId;
}
}
}
}
return activeTour;
}
function closeOtherTourObj(ownId, currSheet) {
for (const sheetId in guided_tour_global.activeTooltip) {
for (const tourId in guided_tour_global.activeTooltip[sheetId]) {
if (sheetId != currSheet) {
$(`#${tourId}_tooltip`).remove(); // close tooltips from other sheets found open
guided_tour_global.activeTooltip[sheetId][tourId] = -2;
} else {
}
}
}
}
return {
initialProperties: {
showTitles: false,
disableNavMenu: true,
// pLicenseJSON: { qStringExpression: { qExpr: "='$(vGuidedTourLicense)'" } },
// qHyperCubeDef: {
// qDimensions: []
// }
},
definition: {
type: "items",
component: "accordion",
items: [
// {
// uses: "dimensions",
// min: 0,
// max: 5
// },
// {
// uses: "sorting" // no more needed.
// },
{
uses: "settings"
},
props.tourItems(qlik, guided_tour_global),
props.tourSettings(qlik.currApp(this), guided_tour_global),
props.licensing(qlik.currApp(this), guided_tour_global),
props.about(guided_tour_global)
]
},
snapshot: {
canTakeSnapshot: false
},
resize: function ($element, layout) {
const ownId = layout.qInfo.qId;
const app = qlik.currApp(this);
const enigma = app.model.enigmaModel
const licensed = guided_tour_global.licensedObjs[ownId];
const rootContainer = qlikCss.v(0).pageContainer;
if (qlik.navigation.getMode() != 'edit') picker.pickersOff('*'); // close all pickers if any open
if (layout.pConsoleLog) console.log(ownId, 'resize', layout, guided_tour_global);
// if a tooltip is open, reposition it
if ($(`#${ownId}_tooltip`).length > 0) {
// get the target-selector from a html comment inside the tooltip
const oldSelector = $(`#${ownId}_tooltip`).html().split('-->')[0].split('<!--')[1] || '';
const oldOrient = $(`#${ownId}_tooltip`).attr("orient");
const calcPositions = tooltips.findPositions2(oldSelector, rootContainer, `#${ownId}_tooltip`
, layout, $(`#${ownId}_tooltip`).css('background-color'), oldOrient);
$(`#${ownId}_tooltip`)
.css('left', calcPositions.left).css('right', calcPositions.right) // left or right
.css('top', calcPositions.top).css('bottom', calcPositions.bottom) // top or bottom
.attr('orient', calcPositions.orient);
$('.guided-tour-arrowhead').remove(); // the arrowhead may have changed toother edge; remove the old
if (calcPositions.arrow) $(`#${ownId}_tooltip .lui-tooltip__arrow`).after(calcPositions.arrow); // arrowhead
}
return qlik.Promise.resolve();
},
paint: function ($element, layout) {
var self = this;
const ownId = layout.qInfo.qId;
guided_tour_global.isSingleMode = document.location.href.split('?')[0].split('/').indexOf('single') > -1;
const app = qlik.currApp(this);
const enigma = app.model.enigmaModel;
const currSheet = qlik.navigation.getCurrentSheetId().sheetId;
const mode = qlik.navigation.getMode();
if (layout.pConsoleLog) console.log(ownId, 'paint', layout, guided_tour_global);
if (mode != 'edit') picker.pickersOff(ownId);
const lStorageKey = app.id + '|' + ownId;
const objFieldName = null;
// add sheet to activeTooltip object
if (!Object(guided_tour_global.activeTooltip).hasOwnProperty(currSheet)) {
guided_tour_global.activeTooltip[currSheet] = {};
}
// add this extension id to activeTooltip object
if (!Object(guided_tour_global.activeTooltip[currSheet]).hasOwnProperty(ownId)) {
guided_tour_global.activeTooltip[currSheet][ownId] = -2; // initialize in the global guided_tour_global.activeTooltip array this tour. -2 is: not started
}
if (layout.pConsoleLog) console.log('active tooltip', guided_tour_global.activeTooltip[currSheet][ownId]);
closeOtherTourObj(ownId, currSheet);
// console.log(guided_tour_global.activeTooltip);
// calculate some settings for the HTML, what divs to show, the switch position ...
const switchPosition = ($('#' + ownId + '_hovermode').is(':checked') && $(`#guided-tour-helpicon-${ownId}`).length) ? 'checked' : '';
const showSwitch = layout.pLaunchMode == 'hover' && layout.pTourItems.length > 0;
const showPlayOrRotate = layout.pLaunchMode != 'hover' && layout.pTourItems.length > 0 && layout.pShowIcon;
const showNoItemsHint = layout.pTourItems.length == 0;
$element.html(`
<div id="${ownId}_parent" class="guided-tour-parent"
style="${layout.pObjectStyle}">
<!-- Leonardo Switch -->
<div class="lui-switch"
style="${!showSwitch ? 'display:none;' : ''}">
<label class="lui-switch__label">
<input type="checkbox" class="lui-switch__checkbox" aria-label="Label"
id="${ownId}_hovermode" ${switchPosition} />
<span class="lui-switch__wrap">
<span class="lui-switch__inner"></span>
<span class="lui-switch__switch"></span>
</span>
</label>
</div>
<div style="text-align:center;${!showPlayOrRotate ? 'display:none;' : ''}">
<!-- Rotating icon -->
<span id="${ownId}_rotate" class="guided-tour-rotate lui-icon lui-icon--large lui-icon--reload"
style="display:none;"></span>
<!-- Play icon -->
<span id="${ownId}_play" class="guided-tour-play guided-tour-launch-${ownId} lui-icon lui-icon--large lui-icon--play"></span>
</div>
<div class="guided-tour-no-items-hint" style="${!showNoItemsHint ? 'display:none;' : ''}">
${noItemsTxt}
</div>
<!--preload image: it's not rendered but browser loads is-->
<img style="display:none;" src="../extensions/db_ext_guided_tour_3/pics/how-to-add.gif">
<div class="guided-tour-label guided-tour-launch-${ownId}" style="${showNoItemsHint ? 'display:none;' : `cursor: 'pointer'`}">
${layout.pTextStart}
</div>
</div>
</div>
`);
// if (showNoItemsHint) {
// $(`#${ownId}_parent .guided-tour-no-items-hint`).click(function () {
// if (qlik.navigation.getMode() == 'edit') picker.pickersOn(ownId, enigma, null, layout)
// })
// }
if (showNoItemsHint) {
if (qlik.navigation.getMode() == 'edit') {
picker.pickersOn(ownId, enigma, null, layout, guided_tour_global, currSheet, tooltips);
}
}
$(`.guided-tour-no-items-hint`).on('mouseover', function (o) {
var gtCopy = JSON.parse(JSON.stringify(guided_tour_global));
gtCopy.activeTooltip[currSheet][ownId] = -1;
gtCopy.tooltipsCache[ownId] = [{
selector: `:${ownId}`,
html: `<center><img src="../extensions/db_ext_guided_tour_3/pics/how-to-add.gif" width="300" height="174"></center>
<br/>${hintTxt}`,
tooltipCustomStyles: `width:400px; height:400px; background:white; font-size:medium;`,
buttonCustomStyles: `opacity:0;`,
showCond: 1
}];
tooltips.play3(ownId, layout, 0, false, enigma, gtCopy, currSheet, true);
});
$(`.guided-tour-no-items-hint`).on('mouseout', function (o) {
tooltips.endTour(ownId, guided_tour_global, currSheet, layout, -2);
});
$(`[tid="${ownId}"] ${qlikCss.v(0).innerObject}`).css('background-color', layout.pExtensionBgColor); // set bg-color in Sense Client
guided_tour_global.licensedObjs[ownId] = license.chkLicenseJson(layout.pLicenseJSON, 'db_ext_guided_tour');
const licensed = guided_tour_global.licensedObjs[ownId];
guided_tour_global.tooltipsCache[ownId] = tooltips.getActiveTooltips(layout.pTourItems);
// ---------------------------------------------------
if (layout.pLaunchMode == 'click') {
//---------------------------------------------------
// Standard-Mode ... plays entire tour on click, no auto-launch nor mouse-over
$(`.guided-tour-helpicon-${ownId}`).remove(); // remove help icons, if still rendered.
// $(`#${ownId}_play`).click(function () {1
$(`.guided-tour-launch-${ownId}`).click(function () {
if (!getActiveTour(ownId, currSheet, layout)) {
// tooltips.cacheHypercube(ownId, enigma, objFieldName, layout.pTourField, layout.pTourSelectVal)
// .then(function (hcube) {
guided_tour_global.tooltipsCache[ownId] = tooltips.getActiveTooltips(layout.pTourItems);
tooltips.play3(ownId, layout, 0, false, enigma, guided_tour_global, currSheet);
// })
// .catch(function () { });
} else {
// console.log('already playing', getActiveTour(ownId, currSheet, layout));
tooltips.endTour(ownId, guided_tour_global, currSheet, layout, -2);
}
})
//---------------------------------------------------
} else if (layout.pLaunchMode == 'hover') {
//---------------------------------------------------
$(`#${ownId}_hovermode`).click(function () {
// if (!licensed) {
// $(`#${ownId}_hovermode`).prop('checked', false);
// tooltips.leonardoMsg(ownId, 'Guided-Tour Extension', noLicenseMsg('Mouse-over'), null, 'OK');
// } else {
const hoverModeSwitch = $(`#${ownId}_hovermode`).is(':checked');
if (hoverModeSwitch == true) {
console.log(`switch tour ${ownId} to "on"`);
$('.guided-tour-picker').remove(); // hide pickers, if still open
$(`#${ownId}_tooltip`).remove(); // close open tooltips, if any
guided_tour_global.tooltipsCache[ownId].forEach((tooltipDef, tooltipNo) => {
//layout.pTourItems.forEach((tooltipDef, tooltipNo) => {
const divId = tooltipDef.selector.split(':').slice(-1)[0]; // use the text after : in the selector property;
var newDiv = $(`<div style="${layout.pHoverIconStyles}"
class="guided-tour-helpicon guided-tour-helpicon-${ownId}">
${layout.pHoverIconText}</div>`);
newDiv
.on('click', () => {
// if ($('#' + ownId + '_tooltip').length == 0) {
tooltips.play3(ownId, layout, tooltipNo, false, enigma, guided_tour_global, currSheet, false);
// }
})
.on('mouseover', () => {
// if ($('#' + ownId + '_tooltip').length == 0) {
tooltips.play3(ownId, layout, tooltipNo, false, enigma, guided_tour_global, currSheet, false);
// }
})
.on('mouseout', () => {
// console.log(tooltipNo, 'Closing');
tooltips.endTour(ownId, guided_tour_global, currSheet, layout, -1);
// $('#' + ownId + '_tooltip').remove();
// guided_tour_global.activeTooltip[currSheet][ownId] = -1; // set activeTooltip to armed
// // stop rotating the play icon
// tooltips.playIcon(ownId);
});
$('[tid="' + divId + '"] .guided-tour-helpicon').remove(); // if previous help icon is there, remove it
$('[tid="' + divId + '"]').prepend(newDiv);
// $('[tid="' + divId + '"]')
// .on('mouseover', () => {
// if ($('#' + ownId + '_tooltip').length == 0) { // tooltip is not yet open
// tooltips.play3(ownId, layout, tooltipNo, false, enigma, guided_tour_global, currSheet, false);
// }
// })
// .on('mouseout', () => {
// // console.log(tooltipNo, 'Closing');
// $('#' + ownId + '_tooltip').remove();
// });
});
guided_tour_global.activeTooltip[currSheet][ownId] = -1; // set tour to "armed"
// })
// .catch(function () { });
} else {
// switch to "off", unbind the events;
console.log(`switch tour ${ownId} to "off"`);
$('.guided-tour-helpicon').remove();
$(`#${ownId}_tooltip`).remove();
guided_tour_global.activeTooltip[currSheet][ownId] = -2;
}
// }
})
//---------------------------------------------------
} /*else if (layout.pLaunchMode == 'auto-always') {
//---------------------------------------------------
// Auto-lauch always ... plays entire tour automatically once per session
if (mode == 'analysis' && !guided_tour_global.visitedTours[ownId] && !getActiveTour(ownId, currSheet, layout)) {
tooltips.cacheHypercube(ownId, enigma, objFieldName, layout.pTourField, layout.pTourSelectVal)
.then(function (hcube) {
guided_tour_global.tooltipsCache[ownId] = hcube;
tooltips.play3(ownId, layout, 0, false, enigma, guided_tour_global, currSheet);
guided_tour_global.visitedTours[ownId] = true;
})
.catch(function () { });
}
// on click, tour will be restarted.
$(`#${ownId}_play`).click(function () {
if (!getActiveTour(ownId, currSheet, layout)) {
tooltips.cacheHypercube(ownId, enigma, objFieldName, layout.pTourField, layout.pTourSelectVal)
.then(function (hcube) {
guided_tour_global.tooltipsCache[ownId] = hcube;
tooltips.play3(ownId, layout, 0, false, enigma, guided_tour_global, currSheet);
guided_tour_global.visitedTours[ownId] = true;
})
.catch(function () { });
}
})
//---------------------------------------------------
} else if (layout.pLaunchMode == 'auto-once') {
//---------------------------------------------------
// Auto-lauch once ... plays entire tour automatically and remember per user
// find out if it is the time to auto-start the tour
if (mode == 'analysis' && !getActiveTour(ownId, currSheet, layout)) {
enigma.evaluate("=TimeStamp(Now(),'YYYYMMDDhhmmss')").then(function (serverTime) {
var lStorageValue = JSON.parse(window.localStorage.getItem(lStorageKey) || lStorageDefault);
if (serverTime >= layout.pRelaunchAfter
&& layout.pRelaunchAfter > lStorageValue.openedAt) {
if (licensed) {
tooltips.cacheHypercube(ownId, enigma, objFieldName, layout.pTourField, layout.pTourSelectVal)
.then(function (hcube) {
guided_tour_global.tooltipsCache[ownId] = hcube;
tooltips.play3(ownId, layout, 0, false, enigma, guided_tour_global, currSheet);
lStorageValue.openedAt = serverTime + ''; // save as string
window.localStorage.setItem(lStorageKey, JSON.stringify(lStorageValue));
if (layout.pConsoleLog) console.log(ownId, 'Stored locally: ', JSON.stringify(lStorageValue));
});
} else {
if (layout.pConsoleLog) console.log(ownId, 'auto-once suppressed because no license');
if (!guided_tour_global.noLicenseWarning[ownId]) {
tooltips.leonardoMsg(ownId, 'Guided-Tour Extension', noLicenseMsg('Auto-launch Once'), null, 'OK');
}
guided_tour_global.noLicenseWarning[ownId] = true;
}
//guided_tour_global.visitedTours[ownId] = true;
} else {
if (layout.pConsoleLog) console.log(ownId, 'user already launched this tour.');
}
})
} else {
if (layout.pConsoleLog) console.log(ownId, 'auto-once suppressed because ' + (mode != 'analysis' ? (mode + '-mode') : 'other tour active'));
}
// on click, tour will be restarted.
$(`#${ownId}_play`).click(function () {
if (!getActiveTour(ownId, currSheet, layout)) {
tooltips.cacheHypercube(ownId, enigma, objFieldName, layout.pTourField, layout.pTourSelectVal)
.then(function (hcube) {
guided_tour_global.tooltipsCache[ownId] = hcube;
tooltips.play3(ownId, layout, 0, false, enigma, guided_tour_global, currSheet);
enigma.evaluate("=TimeStamp(Now(),'YYYYMMDDhhmmss')").then(function (serverTime) {
const lStorageValue = JSON.parse(window.localStorage.getItem(lStorageKey) || lStorageDefault);
lStorageValue.openedAt = serverTime + ''; // save as string
window.localStorage.setItem(lStorageKey, JSON.stringify(lStorageValue));
if (layout.pConsoleLog) console.log(ownId, 'Stored locally: ', JSON.stringify(lStorageValue));
})
//guided_tour_global.visitedTours[ownId] = true;
})
.catch(function () { });
}
})
//---------------------------------------------------
} else if (layout.pLaunchMode == 'auto-once-p-obj') {
//---------------------------------------------------
// find out if auto-start of a tooltip is needed
if (mode == 'analysis' && !getActiveTour(ownId, currSheet, layout)) {
if (licensed) {
const lStorageValue = JSON.parse(window.localStorage.getItem(lStorageKey) || lStorageDefault);
// function (ownId, enigma, backendApi, objFieldName, tourFieldName, tourFieldVal, timestampFieldName, lStorageVal)
// console.log(ownId, 'starting in mode auto-once-p-obj', layout.pTimestampFromDim, lStorageValue)
tooltips.cacheHypercube(ownId, enigma, objFieldName, layout.pTourField, layout.pTourSelectVal
, layout.pTimestampFromDim, lStorageValue)
.then(function (hcube) {
guided_tour_global.tooltipsCache[ownId] = hcube;
if (guided_tour_global.tooltipsCache[ownId].length > 0) {
tooltips.play3(ownId, layout, 0, false, enigma, guided_tour_global, currSheet, lStorageKey, lStorageValue);
}
})
.catch(function () { });
} else {
if (layout.pConsoleLog) console.log(ownId, 'auto-once-p-obj suppressed because no license');
if (!guided_tour_global.noLicenseWarning[ownId]) {
tooltips.leonardoMsg(ownId, 'Guided-Tour Extension', noLicenseMsg("Auto-launch Once Per Tooltip"), null, 'OK');
}
guided_tour_global.noLicenseWarning[ownId] = true;
}
} else {
if (layout.pConsoleLog) console.log(ownId, 'auto-once-p-obj suppressed because ' + (mode != 'analysis' ? (mode + '-mode') : 'other tour active'));
}
// on click, tour will be restarted.
$(`#${ownId}_play`).click(function () {
if (!getActiveTour(ownId, currSheet, layout)) {
tooltips.cacheHypercube(ownId, enigma, objFieldName, layout.pTourField, layout.pTourSelectVal)
.then(function (hcube) {
guided_tour_global.tooltipsCache[ownId] = hcube;
tooltips.play3(ownId, layout, 0, false, enigma, guided_tour_global, currSheet);
})
.catch(function () { });
}
})
}
*/
return qlik.Promise.resolve();
}
};
});