-
Notifications
You must be signed in to change notification settings - Fork 1
/
qs-emergo-buttons.js
317 lines (275 loc) · 8 KB
/
qs-emergo-buttons.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
/**
* E-mergo Buttons Extension
*
* @since 20180609
* @author Laurens Offereins <https://github.com/lmoffereins>
*
* @param {Object} qlik Qlik's core API
* @param {Object} qUtil Qlik utility library
* @param {Object} _ Underscore
* @param {Object} props Property panel definition
* @param {Object} initProps Initial properties
* @param {Object} emergoActions E-mergo Actions API
* @param {Object} buttonLayout Button layout API
* @param {Object} util E-mergo utility functions
* @param {String} css Extension stylesheet
* @param {String} tmpl Extension template file
* @return {Object} Extension structure
*/
define([
"qlik",
"util",
"underscore",
"./properties",
"./initial-properties",
"./util/qs-emergo-actions",
"./util/button",
"./util/util",
"text!./style.css",
"text!./template.ng.html"
], function( qlik, qUtil, _, props, initProps, emergoActions, buttonLayout, util, css, tmpl ) {
// Add global styles to the page
util.registerStyle("qs-emergo-buttons", css);
/**
* Holds the reference to the current app's API
*
* @type {Object}
*/
var app = qlik.currApp(),
/**
* Holds the app's current theme data
*
* @type {Object}
*/
currTheme,
/**
* Return the color picker's corresponding palette color
*
* @param {Object} picker Color picker value
* @return {String} Color value
*/
getPaletteColor = function( picker ) {
if (picker && picker.color) {
if (-1 !== picker.index) {
var paletteColor = _.get(currTheme || {}, "properties.palettes.ui.0.colors".split("."), []);
return picker.index - 1 < paletteColor.length ? paletteColor[picker.index - 1] : picker.color;
} else {
return picker.color;
}
} else {
return "#000000";
}
},
/**
* Extension controller function
*
* @param {Object} $scope Extension scope
* @return {Void}
*/
controller = ["$scope", function( $scope ) {
/**
* Cache functions for this controller
*
* @type {Object} Cache functions
*/
var cache = util.createCache("qs-emergo-buttons/".concat($scope.$id));
/**
* Button select handler
*
* @param {Object} button Button data
* @return {Void}
*/
$scope.do = function( button ) {
var cb = getBtn.bind(this, button.cId);
// Execute button actions when not editing the sheet and interaction is allowed
if (! $scope.object.inEditState() && ! ($scope.options && $scope.options.noInteraction)) {
emergoActions.doMany(cb, $scope).then( function( done ) {
// Evaluate navigation settings
return (false !== done) && emergoActions.doNavigation(cb, $scope);
}).catch(console.error);
}
};
/**
* Return the set of list styles
*
* @return {Object} List styles
*/
$scope.listStyle = function() {
var style = buttonLayout.style(true).apply(this);
// Spacing size
style["--qs-emergo-buttons-spacing-size"] = "".concat(this.layout.props.buttonLayout.spacingSize || 10, "px");
return style;
};
/**
* Return the set of list classes
*
* @return {Array} List classes
*/
$scope.listClass = function() {
var classes = [], aligns = {
1: "align-left",
2: "align-center",
0: "align-right"
};
// Indicate when interaction is not allowed
if ($scope.options && $scope.options.noInteraction) {
classes.push('no-interaction');
}
classes.push(this.layout.props.buttonLayout.orientation ? "vertical" : "horizontal");
classes.push("100%" === this.layout.props.buttonLayout.width ? "full-width" : "auto-width");
classes.push(this.layout.props.buttonLayout.noSpacing ? "spacing" : "nospacing");
classes.push(aligns[this.layout.props.buttonLayout.position % 3]);
return classes;
};
/**
* Return the list of buttons to paint
*
* @return {Array} Buttons
*/
$scope.btns = function() {
return this.layout.props.buttonSet.dynamic ? btnsDynamic() : this.layout.props.buttons;
};
/**
* Return the list of dynamic buttons
*
* @return {Array} Buttons
*/
function btnsDynamic() {
var buttons = [],
dynamic = $scope.layout.props.buttonSet,
def = dynamic.definition[0],
size = dynamic.limit ? props.BUTTON_LIMIT : undefined;
// Add rule to the set definition for inclusion in the cache key
def.rule = dynamic.rule;
def.size = size;
// Bail early when buttons are cached
if (cache.exists(def)) {
return cache.get(def);
}
// Generate buttons
dynamic.rule.split("|").slice(0, size).filter(Boolean).forEach( function( a ) {
var b = util.parseDynamicParams(util.copy(def), a.split("~"));
// Assign unique cId for each button
b.cId = qUtil.generateId();
// Default to the outline style
if (! b.colorExpression.length) {
b.styleType = "style";
}
buttons.push(b);
});
// Cache button data
cache.set(def, buttons);
return buttons;
}
/**
* Return the specified active button
*
* @param {String} cId Button identifier
* @return {Object} Button
*/
function getBtn( cId ) {
return _.find($scope.btns(), { "cId": cId });
}
/**
* Return the set of button classes
*
* @param {Object} button Button data
* @return {Array} Button classes
*/
$scope.btnClass = function( button ) {
var classes = [];
// Style class
if (-1 !== ["color", "colorExpression"].indexOf(button.styleType)) {
var color = "color" === button.styleType ? getPaletteColor(button.color) : button[button.styleType];
if (color) {
classes.push(util.isDarkColor(color) ? "lui-button--custom-inverse" : "lui-button--custom");
}
} else if (button.style) {
classes.push(button.style);
}
return classes;
};
/**
* Return the set of button styles
*
* @param {Object} button Button data
* @return {Object} Button styles
*/
$scope.btnStyle = function( button ) {
var style = {};
// Color
if ("color" === button.styleType) {
style["background-color"] = getPaletteColor(button.color);
} else if ("colorExpression" === button.styleType && !! button.colorExpression) {
style["background-color"] = button.colorExpression;
}
return style;
};
/**
* Return whether the button is visible
*
* @param {Object} button Button data
* @return {Boolean} Is the button visible?
*/
$scope.btnVisible = function( button ) {
return "" === button.visible || isNaN(parseInt(button.visible)) || !! parseInt(button.visible);
};
/**
* Return whether the button is disabled
*
* @param {Object} button Button data
* @return {Boolean} Is the button disabled?
*/
$scope.btnDisabled = function( button ) {
return ! ("" === button.enabled || isNaN(parseInt(button.enabled)) || !! parseInt(button.enabled));
};
/**
* Return button label
*
* Parses the label for icons.
*
* @param {Object} button Button data
* @return {String} Button label
*/
$scope.btnLabel = function( button ) {
return buttonLayout.parseLabel(button.label);
};
/**
* Clean up when the controller is destroyed
*
* @return {Void}
*/
$scope.$on("$destroy", function() {
// Clear the controller's cache
cache.clear();
});
}];
// Find the appprops object and subscribe to layout changes
// This listener remains running in memory without end, but it is only
// created once for all instances of this extension.
app.getObject("AppPropsList").then( function( obj ) {
obj.layoutSubscribe( function() {
// Set the current theme
app.theme.getApplied().then( function( theme ) {
currTheme = theme;
});
});
});
// Apply property panel patches for button layout
buttonLayout.applyDefinition(props);
// Apply initial properties patches for button layout
buttonLayout.applyInitialProperties(initProps);
return {
definition: props,
initialProperties: initProps,
template: tmpl,
controller: controller,
mounted: emergoActions.mount,
beforeDestroy: emergoActions.destroy,
support: {
snapshot: false,
export: false,
exportData: false
}
};
});