-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
197 lines (183 loc) · 7.89 KB
/
script.js
File metadata and controls
197 lines (183 loc) · 7.89 KB
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
// Register dashboard widgets only if dashboard is defined
if(typeof dashboard !== "undefined"){
// Register - Event Counter Widget
// A simple counter widget for the dashboard to count events based on conditions
// by default it counts all events, but can be filtered by owner or other conditions
dashboard.add('counter-events', class extends dashboard.Widget {
_init(){
this._properties = {
name: "counter-events",
label: "Event Counter",
description: "A simple counter widget to count events based on conditions.",
minSize: 1,
maxSize: 12,
interval: 10000,
autoStart: false,
};
this._options = {
title: this._options.title || this._properties.label,
timeframe: this._options.timeframe || 'day', // day, week, month, year
owner: this._options.owner || 'all', // All, or specific username
icon: this._options.icon || 'activity',
color: this._options.color || 'primary',
};
this._badge = null;
}
conditions(){
const conditions = [];
switch(this._options.timeframe){
case 'day':
conditions.push({key: 'created', operator: '>', value: moment().subtract(1, 'days').format('YYYY-MM-DD')});
break;
case 'week':
conditions.push({key: 'created', operator: '>', value: moment().subtract(7, 'days').format('YYYY-MM-DD')});
break;
case 'month':
conditions.push({key: 'created', operator: '>', value: moment().subtract(30, 'days').format('YYYY-MM-DD')});
break;
case 'year':
conditions.push({key: 'created', operator: '>', value: moment().subtract(365, 'days').format('YYYY-MM-DD')});
break;
}
if(this._options.owner && this._options.owner !== 'all'){
conditions.push({key: 'owner', operator: '=', value: USER_USERNAME});
}
return conditions;
}
title(){
switch(this._options.timeframe){
case 'day':
this._options.title = 'Events today';
break;
case 'week':
this._options.title = 'Events this week';
break;
case 'month':
this._options.title = 'Events this month';
break;
case 'year':
this._options.title = 'Events this year';
break;
}
switch(this._options.owner){
case 'all':
break;
default:
this._options.title = 'My ' + this._options.title.toLowerCase();
break;
}
return this._options.title;
}
_create(){
const self = this;
// Create the Badge
this._builder.Component(
"badge",
this._component.gadget,
{
icon: this._options.icon,
color: this._options.color,
},
function(badge,component){
// Set the badge
self._badge = badge;
// Set Content
component.label = $(document.createElement("h5")).addClass("m-0").text(self._builder.Locale.get(self.title())).appendTo(component.content);
component.count = $(document.createElement("p")).addClass("m-0").text(0).appendTo(component.content);
},
);
}
_load(){
const self = this;
API.endpoint('/event/count').data({conditions: this.conditions()}).execute(function(response){
self.load(response.count);
});
}
_render(){
if(this._badge){
this._badge._component.label.text(this._builder.Locale.get(this.title()));
this._badge._component.count.text(this._data !== null ? this._data : '0');
this._badge._component.iconFrame.attr('class','d-flex justify-content-center align-items-center rounded text-bg-'+this._options.color);
this._badge._component.icon.attr('class','fs-3 bi bi-'+this._options.icon);
}
}
_config(form){
// timeframe
form.add(
'select',
{
name: 'timeframe',
label: this._builder.Locale.get('Timeframe'),
placeholder: this._builder.Locale.get('Select a timeframe'),
options: [
{id: 'day', text: this._builder.Locale.get('Events today')},
{id: 'week', text: this._builder.Locale.get('Events this week')},
{id: 'month', text: this._builder.Locale.get('Events this month')},
{id: 'year', text: this._builder.Locale.get('Events this year')},
],
value: this._options.timeframe,
class: {
component: 'bg-gray-200 p-3 py-2 rounded-0',
},
}
);
// owner
form.add(
'select',
{
name: 'owner',
label: this._builder.Locale.get('Owner'),
placeholder: this._builder.Locale.get('Select an owner'),
options: [
{id: 'all', text: this._builder.Locale.get('All Events')},
{id: USER_USERNAME, text: this._builder.Locale.get('My Events only')},
],
value: this._options.owner,
class: {
component: 'bg-gray-200 p-3 py-2 rounded-0',
},
}
);
// color
form.add(
'select2',
{
name: 'color',
label: this._builder.Locale.get('Color'),
placeholder: this._builder.Locale.get('Select a color'),
options: this.colors(),
value: this._options.color,
class: {
component: 'bg-gray-200 p-3 py-2 rounded-0',
},
callback:{
format: function(option, component){
if (!option.id) { return option.text; }
return $('<div class="px-3 py-2 animate-flicker-hover text-bg-' + option.element.value.toLowerCase() + '" style="margin: -.375rem -.75rem!important;">' + option.text + '</div>');;
},
},
}
);
// icon
form.add(
'select2',
{
name: 'icon',
label: this._builder.Locale.get('Icon'),
placeholder: this._builder.Locale.get('Select an icon'),
options: this.icons(),
value: this._options.icon,
class: {
component: 'bg-gray-200 p-3 py-2 rounded-0',
},
callback:{
format: function(option, component){
if (!option.id) { return option.text; }
return $('<span class=""><i class="me-2 text-bg-light p-1 fs-4 rounded bi bi-' + option.element.value.toLowerCase() + '"></i>' + option.text + '</span>');
},
},
}
);
}
});
}