Files
redash/client/app/components/dashboards/widget.js
2019-03-25 22:14:51 +02:00

161 lines
3.9 KiB
JavaScript

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 notification from '@/services/notification';
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() {
'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(() => {
notification.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;