-
Notifications
You must be signed in to change notification settings - Fork 0
/
appSelectors.js
543 lines (462 loc) · 15.4 KB
/
appSelectors.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
var config = {
host: window.location.hostname,
prefix: "/",
port: window.location.port,
isSecure: window.location.protocol === "https:"
};
require.config({
baseUrl: ( config.isSecure ? "https://" : "http://" ) + config.host + (config.port ? ":" + config.port: "") + config.prefix + "resources",
paths: {
"mashupModules": ( config.isSecure ? "https://" : "http://" ) + config.host + (config.port ? ":" + config.port: "") + config.prefix + "extensions" + config.prefix + "GestureMashupDemo"
}
});
define(["mashupModules/publisher"], function ( publisher ) {
var selectionInProgress = false;
var selectionInfo = {};
var topThreeSelInfo = {};
var outlierSelInfo = {};
var myLeapUI, myMyoUI;
//////////////////////////// Clear all selections /////////////////////////
function clearAll(app){
app.clearAll();
}
//////////////////////////// Range Selection Logic /////////////////////////
function startRangeSelection(leapUI){
if(!selectionInProgress){
selectionInProgress = true;
setTimeout(function(){selectionInProgress = false;}, 1000);
myLeapUI = leapUI;
selectionInfo = {};
getCursorPos()
.then(replaceTargetRange)
.then(identifyObj)
.then(getID)
.then(getObjMinMax)
.then(getCanvasDims)
.then(findFingers)
.then(trackFingers,
function(error){
console.log("ERROR: ", error);
completeSelection();
}
);
}
}
// Called from myoUI (via main) when user has finished with selection
function makeRangeSelection(myoUI){
myMyoUI = myoUI;
getObjectProperties(selectionInfo.extQVID)
.then(filterValues)
.then(makeSelection,
function(error){
console.log("ERROR: ", error);
completeSelection(myMyoUI);
}
);
}
////////// Range Selection Utilities
// Get Cursor position
function getCursorPos(){
return new Promise(function(resolve, reject){
var cursorX = document.getElementById('cursor').offsetLeft;
var cursorY = document.getElementById('cursor').offsetTop;
// console.log("Cursor",cursorX, cursorY);
selectionInfo.cursorX = cursorX;
selectionInfo.cursorY = cursorY;
if(selectionInfo.cursorX)
return resolve(selectionInfo);
else
return reject(Error("Unable to find cursor"));
})
}
// Replace target with range overlay
function replaceTargetRange(result){
return new Promise(function(resolve, reject) {
$("#cursor").children().remove();
var cursorLVert = $("<div id='cursorLY'>");
var cursorRVert = $("<div id='cursorRY'>");
$("#cursor").append(cursorLVert);
$("#cursor").append(cursorRVert);
var cursorLVertTxt = $("<div id='cursorLtxt'>");
var cursorRVertTxt = $("<div id='cursorRtxt'>");
$("#cursorLY").append(cursorLVertTxt);
$("#cursorRY").append(cursorRVertTxt);
if(selectionInfo.cursorX)
return resolve(selectionInfo);
else
return reject(Error("Unable to build new target"));
});
}
// Identify object below cursor
function identifyObj(result){
return new Promise(function(resolve, reject) {
var el;
var exts = document.getElementsByClassName('qvobject');
for(var i=0; i<exts.length; i++){
var e = exts[i];
var eRect = e.getBoundingClientRect();
if((eRect.top<selectionInfo.cursorY) && (eRect.bottom>selectionInfo.cursorY) && (eRect.left<selectionInfo.cursorX) && (eRect.right>selectionInfo.cursorX)){
el = e;
}
};
selectionInfo.el = el;
// console.log("el",el);
if(selectionInfo.el)
return resolve(selectionInfo);
else
return reject(Error("Unable to identify object"));
});
}
// Get ID
function getID(result){
return new Promise(function(resolve, reject) {
selectionInfo.extID = $(selectionInfo.el).attr("id");
// console.log("extID",selectionInfo.extID);
selectionInfo.extQVID = $(selectionInfo.el).attr("data-qvid");
// console.log("extQVID",selectionInfo.extQVID);
if(selectionInfo.extQVID)
return resolve(selectionInfo);
else
return reject(Error("Unable to get object ID"));
});
}
// Get object min & max
function getObjMinMax(result){
return new Promise(function(resolve, reject) {
app.getObjectProperties(selectionInfo.extQVID).then(function(model){
// Get Field name
// var dim = model.layout.qHyperCube.qDimensionInfo[0].qFallbackTitle;
var dim = model.layout.qHyperCube.qDimensionInfo[0].qGroupFieldDefs[0]; // Must use this because dim is a master item
selectionInfo.dim = dim;
var xMeasure = model.layout.qHyperCube.qMeasureInfo[0];
selectionInfo.extMin = xMeasure.qMin,
selectionInfo.extMax = xMeasure.qMax
// console.log(selectionInfo.extQVID +
// ": min: "+ selectionInfo.extMin +
// " max: "+selectionInfo.extMax);
if(selectionInfo.extMax)
return resolve(selectionInfo);
else
return reject(Error("Unable to get object min/max"));
})
})
}
// Get canvas left position & width
function getCanvasDims(result){
return new Promise(function(resolve, reject) {
var extCanvii = $(selectionInfo.el).find("canvas");
var extCanvas = extCanvii[extCanvii.length-1];
selectionInfo.extLeft = $(extCanvas).offset().left;
selectionInfo.extTop = $(extCanvas).offset().top;
selectionInfo.extWidth = $(extCanvas).width();
selectionInfo.extHeight = $(extCanvas).height();
selectionInfo.extCenterX = selectionInfo.extLeft + selectionInfo.extWidth/2;
selectionInfo.extCenterY = $(extCanvas).offset().top + $(extCanvas).height()/2;
// console.log("ext pos",selectionInfo.extLeft,selectionInfo.extWidth);
if(selectionInfo.extCenterY)
return resolve(selectionInfo);
else
return reject(Error("Unable to get object dimensions"));
})
}
// Look for both fingers
function findFingers(result){
return new Promise(function(resolve, reject) {
var confirm = myLeapUI.setSelectionInfo(selectionInfo);
if(confirm)
return resolve(confirm);
else
return reject(Error("Unable to set info for leap"));
})
}
// Track both fingers
function trackFingers(result){
return new Promise(function(resolve, reject) {
var confirm = myLeapUI.setRangeTracking(true);
// $("#cursor").css('left',selectionInfo.extCenterX);
$("#cursor").css('top',selectionInfo.extTop);
$("#cursor").children("#cursorLY").css('height',selectionInfo.extHeight);
$("#cursor").children("#cursorRY").css('height',selectionInfo.extHeight);
if(confirm)
return resolve(confirm);
else
return reject(Error("Unable to set range tracking for leap"));
})
}
// Put all extension measure values into xValues array
function getObjectProperties(obj){
return new Promise(function(resolve, reject) {
app.getObjectProperties(selectionInfo.extQVID).then(function(model){
var xValues = [];
hyperCube = model.layout.qHyperCube.qDataPages[0].qMatrix;
$.each(hyperCube,function(value){
// console.log(hyperCube[value]);
var xVal = {
"title": hyperCube[value][0].qText,
"X": hyperCube[value][1].qNum
}
xValues.push(xVal);
});
if(xValues.length){
// console.log("All Objs", xValues);
return resolve(xValues);
}else{
console.log("No values found in Measure!");
completeSelection();
return reject("No values found in Measure!");
}
});
});
}
// Find values between min & max and return selected items
function filterValues(vals){
console.log("Calling filterValues...");
return new Promise(function(resolve, reject) {
var selectedItems = [];
vals.forEach(function(val){
if((val.X>selectionInfo.lValue) && (val.X<selectionInfo.rValue)){
selectedItems.push({qText: val.title});
}
});
// return selection;
if(selectedItems.length){
// console.log("Selected Items", selectedItems);
return resolve(selectedItems);
}else{
completeSelection();
return reject(Error("No values selected in Measure!"));
}
});
}
// Send selected items to Qlik Sense Engine to make selection and force redraw
function makeSelection(selArray){
// console.log("Making Selection",selArray);
app.field(selectionInfo.dim).selectValues(selArray, true, true);
// selectMatch() is another API call that could be used, but it doesn't work on master items.
// app.field(selectionInfo.dim).selectMatch('>3000000<8000000', true);
completeSelection();
}
// Clean up after range selection
function completeSelection(){
console.log("Completing selection");
$("#cursor").children().remove();
var cursorY = $("<div id='cursorY'>");
var cursorX = $("<div id='cursorX'>");
$("#cursor").append(cursorY);
$("#cursor").append(cursorX);
myLeapUI.setRangeTracking(false);
myLeapUI.resetInitPos();
}
//////////////////////////// Top Three Selection Logic /////////////////////////
function makeTopThreeSelection(leapUI){
myLeapUI = leapUI;
if(!selectionInProgress){
console.log("Performing Top Three Selection");
selectionInProgress = true;
setTimeout(function(){selectionInProgress = false;}, 1000);
topThreeSelInfo = {};
getCursorPosTop()
.then(replaceTargetTop)
.then(identifyObjTop)
.then(getIdTop)
.then(getObjTopThree)
.then(selectObjTopThree)
.then(completeSelection,
function(error){
console.log("ERROR: ", error);
completeSelection();
}
);
}
}
////////// Top Three Selection Utilities
// Get Cursor position
function getCursorPosTop(){
return new Promise(function(resolve, reject) {
var cursorX = document.getElementById('cursor').offsetLeft;
var cursorY = document.getElementById('cursor').offsetTop;
// console.log("Cursor",cursorX, cursorY);
topThreeSelInfo.cursorX = cursorX;
topThreeSelInfo.cursorY = cursorY;
if(topThreeSelInfo.cursorY){
return resolve(topThreeSelInfo);
}else{
return reject("Unable to find cursor");
}
});
}
// Replace target with circle overlay
function replaceTargetTop(result){
return new Promise(function(resolve, reject) {
$("#cursor").children().remove();
var cursorBall = $("<div id='cursorBall'>");
$("#cursor").append(cursorBall);
if($("#cursor").children()){
return resolve(topThreeSelInfo);
}else{
return reject("Unable to make new target");
}
});
}
// Identify object below cursor
function identifyObjTop(result){
return new Promise(function(resolve, reject) {
var el;
var exts = document.getElementsByClassName('qvobject');
for(var i=0; i<exts.length; i++){
var e = exts[i];
var eRect = e.getBoundingClientRect();
if((eRect.top<topThreeSelInfo.cursorY) && (eRect.bottom>topThreeSelInfo.cursorY) && (eRect.left<topThreeSelInfo.cursorX) && (eRect.right>topThreeSelInfo.cursorX)){
el = e;
}
};
topThreeSelInfo.el = el;
if(el){
return resolve(topThreeSelInfo);
}else{
return reject("Unable to find object");
}
});
}
// Get ID
function getIdTop(result){
return new Promise(function(resolve, reject) {
topThreeSelInfo.extID = $(topThreeSelInfo.el).attr("id");
// console.log("extID",topThreeSelInfo.extID);
topThreeSelInfo.extQVID = $(topThreeSelInfo.el).attr("data-qvid");
// console.log("extQVID",topThreeSelInfo.extQVID);
if(topThreeSelInfo.extQVID){
return resolve(topThreeSelInfo);
}else{
return reject("Unable to get object ID");
}
});
}
// Get the object's top three items
function getObjTopThree(result){
return new Promise(function(resolve, reject) {
// console.log("Calling getObjectProperties for " + topThreeSelInfo.extQVID);
app.getObjectProperties(topThreeSelInfo.extQVID).then(function(model){
// Get Field name
var dim = model.layout.qHyperCube.qDimensionInfo[0].qGroupFieldDefs[0]; // Must use this because dim is a master item
topThreeSelInfo.dim = dim;
var topThree = [];
var qMtrx = model.layout.qHyperCube.qDataPages[0].qMatrix;
topThree.push({qText: qMtrx[0][0].qText});
topThree.push({qText: qMtrx[1][0].qText});
topThree.push({qText: qMtrx[2][0].qText});
// console.log("topThree", topThree);
topThreeSelInfo.topThree = topThree;
return resolve(topThreeSelInfo);
}).catch(function(error){
return reject(error);
});
});
}
// Send selected items to Qlik Sense Engine to make selection and force redraw
function selectObjTopThree(result){
// console.log("Making Selection on "+ topThreeSelInfo.dimim+ " for ",topThreeSelInfo.topThree);
return new Promise(function(resolve, reject) {
app.field(topThreeSelInfo.dim).selectValues(topThreeSelInfo.topThree, true, true).then(function(response){
return resolve(response);
}).catch(function(error){
return reject(error);
});
});
}
//////////////////////////// Outlier Selection Logic /////////////////////////
function makeOutlierSelection(leapUI){
console.log("Calling makeOutlierSelection...");
if(!selectionInProgress){
selectionInProgress = true;
setTimeout(function(){selectionInProgress = false;}, 1000);
selectionInfo = {};
myLeapUI = leapUI;
getCursorPos()
.then(replaceTargetOutlier)
.then(identifyObj)
.then(getID)
.then(getObjectOutlierProperties)
.then(filterOutlierValues)
.then(sendOutlierSelection,
function(error){
console.log("ERROR: ", error);
completeSelection();
}
);
}
}
////////// Outlier Selection Utilities
// Replace target with circle overlay
function replaceTargetOutlier(result){
// console.log("Calling replaceTargetOutlier...");
return new Promise(function(resolve, reject) {
$("#cursor").children().remove();
var cursorBall = $("<div id='cursorBallGreen'>");
$("#cursor").append(cursorBall);
if($("#cursor").children()){
return resolve(selectionInfo);
}else{
return reject("Unable to make new target");
}
});
}
// Put all extension measure values into xValues array
function getObjectOutlierProperties(obj){
// console.log("Calling replaceTargetOutlier...");
return new Promise(function(resolve, reject) {
app.getObjectProperties(selectionInfo.extQVID).then(function(model){
var dim = model.layout.qHyperCube.qDimensionInfo[0].qGroupFieldDefs[0]; // Must use this because dim is a master item
selectionInfo.dim = dim;
var xValues = [];
hyperCube = model.layout.qHyperCube.qDataPages[0].qMatrix;
$.each(hyperCube,function(value){
var xVal = {
"title": hyperCube[value][0].qText,
"X": hyperCube[value][1].qNum
}
xValues.push(xVal);
});
xValues.shift();
if(xValues.length){
return resolve(xValues);
}else{
completeSelection();
return reject("No values found in Measure!");
}
});
});
}
// Find outlier values and return selected items
function filterOutlierValues(vals){
// console.log("Calling filterOutlierValues...");
return new Promise(function(resolve, reject) {
var selectedItems = [];
for(var i=1; i < vals.length-1; i++){
if((vals[i].X > vals[i-1].X*2) && (vals[i].X > vals[i+1].X*2)){
selectedItems.push({qText: vals[i].title});
}
}
if(selectedItems.length){
// console.log("Selected Items", selectedItems);
return resolve(selectedItems);
}else{
completeSelection();
return reject(Error("No values selected in Measure!"));
}
});
}
// Send selected items to Qlik Sense Engine to make selection and force redraw
function sendOutlierSelection(selArray){
// console.log("Making Selection",selArray);
app.field(selectionInfo.dim).selectValues(selArray, true, true);
completeSelection();
}
return {
startRangeSelection: startRangeSelection,
makeRangeSelection: makeRangeSelection,
makeTopThreeSelection: makeTopThreeSelection,
makeOutlierSelection: makeOutlierSelection,
clearAll: clearAll
};
});