williaster commented on a change in pull request #3581: Dashboard refactory
URL:
https://github.com/apache/incubator-superset/pull/3581#discussion_r147904958
##########
File path: superset/assets/javascripts/dashboard/components/Dashboard.jsx
##########
@@ -0,0 +1,334 @@
+import React from 'react';
+import PropTypes from 'prop-types';
+
+import AlertsWrapper from '../../components/AlertsWrapper';
+import GridLayout from './GridLayout';
+import Header from './Header';
+import DashboardAlert from './DashboardAlert';
+import { getExploreUrl } from '../../explore/exploreUtils';
+import { areObjectsEqual } from '../../reduxUtils';
+import { t } from '../../locales';
+
+import '../../../stylesheets/dashboard.css';
+
+const propTypes = {
+ actions: PropTypes.object,
+ initMessages: PropTypes.array,
+ dashboard: PropTypes.object.isRequired,
+ slices: PropTypes.object,
+ datasources: PropTypes.object,
+ filters: PropTypes.object,
+ refresh: PropTypes.bool,
+ timeout: PropTypes.number,
+ userId: PropTypes.string,
+ isStarred: PropTypes.bool,
+ isFiltersChanged: PropTypes.bool,
+ updateDashboardTitle: PropTypes.func,
+ readFilters: PropTypes.func,
+ fetchFaveStar: PropTypes.func,
+ saveFaveStar: PropTypes.func,
+ renderSlices: PropTypes.func,
+ startPeriodicRender: PropTypes.func,
+ getFormDataExtra: PropTypes.func,
+ fetchSlice: PropTypes.func,
+ saveSlice: PropTypes.func,
+ removeSlice: PropTypes.func,
+ removeChart: PropTypes.func,
+ toggleExpandSlice: PropTypes.func,
+ updateDashboardLayout: PropTypes.func,
+ addSlicesToDashboard: PropTypes.func,
+ addFilter: PropTypes.func,
+ clearFilter: PropTypes.func,
+ removeFilter: PropTypes.func,
+};
+
+const defaultProps = {
+ initMessages: [],
+ dashboard: {},
+ slices: {},
+ datasources: {},
+ filters: {},
+ timeout: 60,
+ userId: '',
+ isStarred: false,
+ isFiltersChanged: false,
+};
+
+class Dashboard extends React.PureComponent {
+ constructor(props) {
+ super(props);
+ this.refreshTimer = null;
+ this.firstLoad = true;
+
+ // alert for unsaved changes
+ this.state = {
+ alert: null,
+ };
+ }
+
+ componentDidMount() {
+ this.loadPreSelectFilters();
+ this.firstLoad = false;
+ this.bindResizeToWindowResize();
+ }
+
+ componentWillReceiveProps(nextProps) {
+ // check filters is changed
+ if (!areObjectsEqual(nextProps.filters, this.props.filters)) {
+ this.renderUnsavedChangeAlert();
+ }
+ }
+
+ componentDidUpdate(prevProps) {
+ if (!areObjectsEqual(prevProps.filters, this.props.filters) &&
this.props.refresh) {
+ Object.keys(this.props.filters).forEach(sliceId =>
(this.refreshExcept(sliceId)));
+ }
+ }
+
+ onBeforeUnload(hasChanged) {
+ if (hasChanged) {
+ window.addEventListener('beforeunload', this.unload);
+ } else {
+ window.removeEventListener('beforeunload', this.unload);
+ }
+ }
+
+ onChange() {
+ this.onBeforeUnload(true);
+ this.renderUnsavedChangeAlert();
+ }
+
+ onSave() {
+ this.onBeforeUnload(false);
+ this.setState({
+ alert: '',
+ });
+ }
+
+ // return charts in array
+ getAllSlices() {
+ return Object.keys(this.props.slices).map(key => (this.props.slices[key]));
+ }
+
+ getFormDataExtra(slice) {
+ const formDataExtra = Object.assign({}, slice.formData);
+ const extraFilters = this.effectiveExtraFilters(slice.slice_id);
+ formDataExtra.filters = formDataExtra.filters.concat(extraFilters);
+ return formDataExtra;
+ }
+
+ unload() {
+ const message = t('You have unsaved changes.');
+ window.event.returnValue = message; // Gecko + IE
+ return message; // Gecko + Webkit, Safari, Chrome etc.
+ }
+
+ fetchSlice(slice, force = false) {
+ return this.props.actions.runQuery(
+ this.getFormDataExtra(slice), force, this.props.timeout, slice.chartKey);
+ }
+
+ effectiveExtraFilters(sliceId) {
+ const metadata = this.props.dashboard.metadata;
+ const filters = this.props.filters;
+ const f = [];
+ const immuneSlices = metadata.filter_immune_slices || [];
+ if (sliceId && immuneSlices.includes(sliceId)) {
+ // The slice is immune to dashboard filters
+ return f;
+ }
+
+ // Building a list of fields the slice is immune to filters on
+ let immuneToFields = [];
+ if (
+ sliceId &&
+ metadata.filter_immune_slice_fields &&
+ metadata.filter_immune_slice_fields[sliceId]) {
+ immuneToFields = metadata.filter_immune_slice_fields[sliceId];
+ }
+ for (const filteringSliceId in filters) {
+ if (filteringSliceId === sliceId.toString()) {
+ // Filters applied by the slice don't apply to itself
+ continue;
+ }
+ for (const field in filters[filteringSliceId]) {
+ if (!immuneToFields.includes(field)) {
+ f.push({
+ col: field,
+ op: 'in',
+ val: filters[filteringSliceId][field],
+ });
+ }
+ }
+ }
+ return f;
+ }
+
+ jsonEndpoint(data, force = false) {
+ let endpoint = getExploreUrl(data, 'json', force);
+ if (endpoint.charAt(0) !== '/') {
+ // Known issue for IE <= 11:
+ //
https://connect.microsoft.com/IE/feedbackdetail/view/1002846/pathname-incorrect-for-out-of-document-elements
+ endpoint = '/' + endpoint;
+ }
+ return endpoint;
+ }
+
+ loadPreSelectFilters() {
+ for (const key in this.props.filters) {
+ for (const col in this.props.filters[key]) {
+ const sliceId = parseInt(key, 10);
+ this.props.actions.addFilter(sliceId, col,
+ this.props.filters[key][col], false, false);
+ }
+ }
+ }
+
+ refreshExcept(sliceId) {
+ const immune = this.props.dashboard.metadata.filter_immune_slices || [];
+ const slices = this.getAllSlices()
+ .filter(slice => slice.slice_id !== sliceId &&
immune.indexOf(slice.slice_id) === -1);
+ this.renderSlices(slices);
+ }
+
+ stopPeriodicRender() {
+ if (this.refreshTimer) {
+ clearTimeout(this.refreshTimer);
+ this.refreshTimer = null;
+ }
+ }
+
+ startPeriodicRender(interval) {
+ this.stopPeriodicRender();
+ const dash = this;
+ const immune = this.props.dashboard.metadata.timed_refresh_immune_slices
|| [];
+ const refreshAll = () => {
+ const affectedSlices = this.getAllSlices()
+ .filter(slice => immune.indexOf(slice.slice_id) === -1);
+ dash.renderSlices(affectedSlices, true, interval * 0.2);
+ };
+ const fetchAndRender = function () {
+ refreshAll();
+ if (interval > 0) {
+ dash.refreshTimer = setTimeout(fetchAndRender, interval);
+ }
+ };
+
+ fetchAndRender();
+ }
+
+ readFilters() {
+ // Returns a list of human readable active filters
+ return JSON.stringify(this.props.filters, null, ' ');
+ }
+
+ bindResizeToWindowResize() {
+ let resizeTimer;
+ const dash = this;
+ const allSlices = this.getAllSlices();
+ $(window).on('resize', () => {
Review comment:
why use `$` here and not straight `JS`?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services