-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
229 lines (204 loc) · 9.51 KB
/
index.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
(async function myFunc () {
// const element = document.getElementById("myTable");
// const refApi = await element.getRefApi();
// const obj = await refApi.getObject();
// let objLayout = await obj.getLayout();
// Build qlik/api config
const config = {
authType: "oauth2",
host: "cvh.eu.qlikcloud.com",
clientId: "bf494408325438d08169cb6677f8ec29",
redirectUri: "https://localhost:8080/oauth_callback.html",
accessTokenStorage: "session"
};
const customRedirectUri = 'https://localhost:8080/oauth_callback_custom.html'
//Trying to get OAuth token using redirect
// GENERATING CODE VERIFIER
function dec2hex(dec) {
return ("0" + dec.toString(16)).substr(-2);
}
function generateCodeVerifier() {
var array = new Uint32Array(56 / 2);
window.crypto.getRandomValues(array);
return Array.from(array, dec2hex).join("");
}
function sha256(plain) {
// returns promise ArrayBuffer
const encoder = new TextEncoder();
const data = encoder.encode(plain);
return window.crypto.subtle.digest("SHA-256", data);
}
function base64urlencode(a) {
var str = "";
var bytes = new Uint8Array(a);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
str += String.fromCharCode(bytes[i]);
}
return btoa(str)
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/, "");
}
async function generateCodeChallengeFromVerifier(v) {
var hashed = await sha256(v);
var base64encoded = base64urlencode(hashed);
return base64encoded;
}
const codeVerifier = generateCodeVerifier();
localStorage.setItem("codeVerifier", codeVerifier);
const codeChallenge = await generateCodeChallengeFromVerifier(codeVerifier)
const state = generateCodeVerifier()
const headTag = document.getElementsByTagName('HEAD')[0];
const metaTag = document.createElement('meta');
metaTag.setAttribute("http-equiv", "refresh");
metaTag.setAttribute("content", `3;url=https://${config.host}/oauth/authorize?client_id=${config.clientId}&code_challenge_method=S256&redirect_uri=${customRedirectUri}&response_type=code&scope=user_default&state=${state}&code_challenge=${codeChallenge}`);
headTag.appendChild(metaTag);
return
window.qlikApi.auth.setDefaultHostConfig(config);
const { data: mySpaces } = await window.qlikApi.spaces.getSpaces();
console.log("mySpaces: ", mySpaces);
// open new app session
const appSession = window.qlikApi.qix.openAppSession("633bcf3c-c309-4bc1-9fc3-969bc41aa869");
// get the "qix document (qlik app)"
const app = await appSession.getDoc();
// get a variable
const myVar = await app.getVariableByName('vShowArch'); // change with your variable name
// show current value
const currentValue = await myVar.getLayout();
console.log("current var value: ", currentValue.qNum);
// set variable value (number)
await myVar.setNumValue(1); // change with your variable value. If it is a string, change method to `setStringValue`
// show new value
const newValue = await myVar.getLayout();
console.log("new var value: ", newValue.qNum);
//get collections
const collections = await window.qlikApi.collections.getCollections();
console.log("collections: ", collections);
//1.Get data from existing filterpane
const filterPane = await app.getObject("CvJ");
//Get layout
const filterPaneLayout = await filterPane.getLayout();
//Get listbox within filter pane. It is an array, loop over it if you have more than once
const listbox = await app.getObject(filterPaneLayout.qChildList.qItems[0].qInfo.qId);
//2.Get list box data
//Get listbox layout
const listboxLayout = await listbox.getLayout();
//Read listbox height. This allow me to get all data at once (if they are few) or make more calls for retrieving all data.
const listboxHeight = listboxLayout.qListObject.qSize.qcy
//We recommend to get more data only when user perform a scroll into your dropdown/select component and you reach end of your current data.
//E.g. I will get first 200 rows, then when user scroll down and he is close to end of my 200 rows, perform another getListObjectData, and get
//200 rows for populating my dropdown/select component, and so on...
//listboxHeight is useful to understand where is my end of data.
const listboxDataDef = {
"qPath": "/qListObjectDef",
"qPages": [
{
"qLeft": 0,
"qTop": 0, //Use this to move down along data. 0 is first record.
"qWidth": 1, // This is number of column. List box should always have 1 column. Max is 1,000 cells (width * height)
"qHeight": 200 //This is the number of rows retrieved from engine. Max is 1,000 cells (width * height)
}
]
};
const listboxData = await listbox.getListObjectData(listboxDataDef);
console.log("listboxData: ", listboxData[0].qMatrix);
//Get current selections
const currentSelectionsDef = {
"qInfo": {
"qType": "CurrentSelection"
},
"qSelectionObjectDef": {}
}
const currentSelections = await app.createSessionObject(currentSelectionsDef);
const currentSelectionsLayout = await currentSelections.getLayout();
//Show current selections. I'm showing an array which will contain current selections information.
//Here we are looking only for which field is selected, not values.
//With current selections object we can see max 6 selected values, then we will se 7 of 20, 11 of 50, and so on
//For reading selected values we need to create listbox for each selected fields and then get data
console.log("current selections: ", currentSelectionsLayout.qSelectionObject.qSelections);
console.log("current selection field: ", currentSelectionsLayout.qSelectionObject.qSelections[0]?.qField);
//Get first field as example and create listbox for getting data.
//In normal scenario, you should loop over all selected fields and create listbox for each field
if(currentSelectionsLayout.qSelectionObject.qSelections[0]?.qField) {
const sessionListboxDef = {
"qInfo": {
"qType": "ListObject"
},
"qListObjectDef": {
"qStateName": "$",
"qLibraryId": "",
"qDef": {
"qFieldDefs": [
currentSelectionsLayout.qSelectionObject.qSelections[0].qField
],
"qSortCriterias": [
{
"qSortByState": 1
}
]
}
}
}
const sessionListbox = await app.createSessionObject(sessionListboxDef);
const sessionListboxLayout = await sessionListbox.getLayout();
//Read listbox height
const sessionListboxHeight = sessionListboxLayout.qListObject.qSize.qcy
//Same as mentioned for filterpane, we need height for understanding how many values we have. Use this for pagination when need to get all data
const sessionListboxDataDef = {
"qPath": "/qListObjectDef",
"qPages": [
{
"qLeft": 0,
"qTop": 0,
"qWidth": 1,
"qHeight": 20
}
]
}
//Method for reading data from field
const sessionListboxData = await sessionListbox.getListObjectData(sessionListboxDataDef);
//Showing values.
//Here you will find, for each value, the current selection state. For selected value you will find qState: "S".
//Use these selected values for creating your url with selections.
console.log(sessionListboxData[0].qMatrix)
//When you have finished with listbox data, destroy it!!!
await app.destroySessionObject(sessionListbox.id);
}
//Test on vis with viewState
// const element = document.getElementById("qlikTable");
// const refApi = await element.getRefApi();
// const chartRefApi = await refApi.getChartRefApi()
// const viewState = await chartRefApi.getViewState();
// if (viewState && typeof viewState.getViewState === 'function') {
// viewstate = viewState.getViewState();
// console.log("viewstate: ",viewstate);
// }
}());
// console.log("start");
async function readTable() {
const element = document.getElementById("myTable");
const refApi = await element.getRefApi();
console.log("refApi", refApi);
const obj = await refApi.getObject();
console.log("obj",obj);
}
async function changeViz() {
//Change viz removing and adding qlik embed tag
// const element = document.getElementById("qlikChart");
// //Delete qlik embed viz
// element.remove()
// //Append a new one
// const container = document.getElementById("container");
// const qlikEmbedViz = `<qlik-embed
// ui="analytics/sheet"
// app-id="c0b1c44e-1735-4eb1-89a5-36dde6247405"
// object-id="jEtEvB"
// id="qlikChart"
// >
// </qlik-embed>`
// container.insertAdjacentHTML('beforeend', qlikEmbedViz)
//Change viz changin object prop
const element = document.getElementById("qlikChart");
element.setAttribute("object-id", "jEtEvB")
}