-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathwidget.js
158 lines (139 loc) · 3.87 KB
/
widget.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
import { filter } from 'lodash';
import template from './widget.html';
import editTextBoxTemplate from './edit-text-box.html';
import widgetDialogTemplate from './widget-dialog.html';
import EditParameterMappingsDialog from '@/components/dashboards/EditParameterMappingsDialog';
import './widget.less';
import './widget-dialog.less';
const WidgetDialog = {
template: widgetDialogTemplate,
bindings: {
resolve: '<',
close: '&',
dismiss: '&',
},
controller() {
this.widget = this.resolve.widget;
},
};
const EditTextBoxComponent = {
template: editTextBoxTemplate,
bindings: {
resolve: '<',
close: '&',
dismiss: '&',
},
controller(toastr) {
'ngInject';
this.saveInProgress = false;
this.widget = this.resolve.widget;
this.saveWidget = () => {
this.saveInProgress = true;
if (this.widget.new_text !== this.widget.existing_text) {
this.widget.text = this.widget.new_text;
this.widget
.save()
.then(() => {
this.close();
})
.catch(() => {
toastr.error('Widget can not be updated');
})
.finally(() => {
this.saveInProgress = false;
});
} else {
this.close();
}
};
},
};
function DashboardWidgetCtrl($scope, $location, $uibModal, $window, $rootScope, $timeout, Events, currentUser) {
this.canViewQuery = currentUser.hasPermission('view_query');
this.editTextBox = () => {
this.widget.existing_text = this.widget.text;
this.widget.new_text = this.widget.text;
$uibModal.open({
component: 'editTextBox',
resolve: {
widget: this.widget,
},
});
};
this.expandVisualization = () => {
$uibModal.open({
component: 'widgetDialog',
resolve: {
widget: this.widget,
},
size: 'lg',
});
};
this.hasParameters = () => this.widget.query.getParametersDefs().length > 0;
this.editParameterMappings = () => {
EditParameterMappingsDialog.showModal({
dashboard: this.dashboard,
widget: this.widget,
}).result.then((valuesChanged) => {
this.localParameters = null;
// refresh widget if any parameter value has been updated
if (valuesChanged) {
$timeout(() => this.refresh());
}
$scope.$applyAsync();
$rootScope.$broadcast('dashboard.update-parameters');
});
};
this.localParametersDefs = () => {
if (!this.localParameters) {
this.localParameters = filter(
this.widget.getParametersDefs(),
param => !this.widget.isStaticParam(param),
);
}
return this.localParameters;
};
this.deleteWidget = () => {
if (!$window.confirm(`Are you sure you want to remove "${this.widget.getName()}" from the dashboard?`)) {
return;
}
this.widget.delete().then(() => {
if (this.deleted) {
this.deleted({});
}
});
};
Events.record('view', 'widget', this.widget.id);
this.load = (refresh = false) => {
const maxAge = $location.search().maxAge;
this.widget.load(refresh, maxAge);
};
this.refresh = () => {
this.load(true);
};
if (this.widget.visualization) {
Events.record('view', 'query', this.widget.visualization.query.id, { dashboard: true });
Events.record('view', 'visualization', this.widget.visualization.id, { dashboard: true });
this.type = 'visualization';
this.load();
} else if (this.widget.restricted) {
this.type = 'restricted';
} else {
this.type = 'textbox';
}
}
export default function init(ngModule) {
ngModule.component('editTextBox', EditTextBoxComponent);
ngModule.component('widgetDialog', WidgetDialog);
ngModule.component('dashboardWidget', {
template,
controller: DashboardWidgetCtrl,
bindings: {
widget: '<',
public: '<',
dashboard: '<',
deleted: '&onDelete',
},
});
}
init.init = true;