|
|
|
@ -28,28 +28,57 @@ interface State { |
|
|
|
|
loadedDataSourceValue: string | null | undefined; |
|
|
|
|
datasource: DataSourceApi | null; |
|
|
|
|
isCollapsed: boolean; |
|
|
|
|
angularScope: AngularQueryComponentScope | null; |
|
|
|
|
hasTextEditMode: boolean; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export class QueryEditorRow extends PureComponent<Props, State> { |
|
|
|
|
element: HTMLElement | null = null; |
|
|
|
|
angularScope: AngularQueryComponentScope | null; |
|
|
|
|
angularQueryEditor: AngularComponent | null = null; |
|
|
|
|
|
|
|
|
|
state: State = { |
|
|
|
|
datasource: null, |
|
|
|
|
isCollapsed: false, |
|
|
|
|
angularScope: null, |
|
|
|
|
loadedDataSourceValue: undefined, |
|
|
|
|
hasTextEditMode: false, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
componentDidMount() { |
|
|
|
|
this.loadDatasource(); |
|
|
|
|
this.props.panel.events.on('refresh', this.onPanelRefresh); |
|
|
|
|
this.props.panel.events.on('data-error', this.onPanelDataError); |
|
|
|
|
this.props.panel.events.on('data-received', this.onPanelDataReceived); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
componentWillUnmount() { |
|
|
|
|
this.props.panel.events.off('refresh', this.onPanelRefresh); |
|
|
|
|
this.props.panel.events.off('data-error', this.onPanelDataError); |
|
|
|
|
this.props.panel.events.off('data-received', this.onPanelDataReceived); |
|
|
|
|
|
|
|
|
|
if (this.angularQueryEditor) { |
|
|
|
|
this.angularQueryEditor.destroy(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
onPanelDataError = () => { |
|
|
|
|
// Some query controllers listen to data error events and need a digest
|
|
|
|
|
if (this.angularQueryEditor) { |
|
|
|
|
// for some reason this needs to be done in next tick
|
|
|
|
|
setTimeout(this.angularQueryEditor.digest); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
onPanelDataReceived = () => { |
|
|
|
|
// Some query controllers listen to data error events and need a digest
|
|
|
|
|
if (this.angularQueryEditor) { |
|
|
|
|
// for some reason this needs to be done in next tick
|
|
|
|
|
setTimeout(this.angularQueryEditor.digest); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
onPanelRefresh = () => { |
|
|
|
|
if (this.state.angularScope) { |
|
|
|
|
this.state.angularScope.range = getTimeSrv().timeRange(); |
|
|
|
|
if (this.angularScope) { |
|
|
|
|
this.angularScope.range = getTimeSrv().timeRange(); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -73,7 +102,11 @@ export class QueryEditorRow extends PureComponent<Props, State> { |
|
|
|
|
const dataSourceSrv = getDatasourceSrv(); |
|
|
|
|
const datasource = await dataSourceSrv.get(query.datasource || panel.datasource); |
|
|
|
|
|
|
|
|
|
this.setState({ datasource, loadedDataSourceValue: this.props.dataSourceValue }); |
|
|
|
|
this.setState({ |
|
|
|
|
datasource, |
|
|
|
|
loadedDataSourceValue: this.props.dataSourceValue, |
|
|
|
|
hasTextEditMode: false, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
componentDidUpdate() { |
|
|
|
@ -98,21 +131,14 @@ export class QueryEditorRow extends PureComponent<Props, State> { |
|
|
|
|
const scopeProps = { ctrl: this.getAngularQueryComponentScope() }; |
|
|
|
|
|
|
|
|
|
this.angularQueryEditor = loader.load(this.element, scopeProps, template); |
|
|
|
|
this.angularScope = scopeProps.ctrl; |
|
|
|
|
|
|
|
|
|
// give angular time to compile
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
this.setState({ angularScope: scopeProps.ctrl }); |
|
|
|
|
this.setState({ hasTextEditMode: !!this.angularScope.toggleEditorMode }); |
|
|
|
|
}, 10); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
componentWillUnmount() { |
|
|
|
|
this.props.panel.events.off('refresh', this.onPanelRefresh); |
|
|
|
|
|
|
|
|
|
if (this.angularQueryEditor) { |
|
|
|
|
this.angularQueryEditor.destroy(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
onToggleCollapse = () => { |
|
|
|
|
this.setState({ isCollapsed: !this.state.isCollapsed }); |
|
|
|
|
}; |
|
|
|
@ -138,10 +164,8 @@ export class QueryEditorRow extends PureComponent<Props, State> { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
onToggleEditMode = () => { |
|
|
|
|
const { angularScope } = this.state; |
|
|
|
|
|
|
|
|
|
if (angularScope && angularScope.toggleEditorMode) { |
|
|
|
|
angularScope.toggleEditorMode(); |
|
|
|
|
if (this.angularScope && this.angularScope.toggleEditorMode) { |
|
|
|
|
this.angularScope.toggleEditorMode(); |
|
|
|
|
this.angularQueryEditor.digest(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -150,11 +174,6 @@ export class QueryEditorRow extends PureComponent<Props, State> { |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
get hasTextEditMode() { |
|
|
|
|
const { angularScope } = this.state; |
|
|
|
|
return angularScope && angularScope.toggleEditorMode; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
onRemoveQuery = () => { |
|
|
|
|
this.props.onRemoveQuery(this.props.query); |
|
|
|
|
}; |
|
|
|
@ -171,10 +190,8 @@ export class QueryEditorRow extends PureComponent<Props, State> { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
renderCollapsedText(): string | null { |
|
|
|
|
const { angularScope } = this.state; |
|
|
|
|
|
|
|
|
|
if (angularScope && angularScope.getCollapsedText) { |
|
|
|
|
return angularScope.getCollapsedText(); |
|
|
|
|
if (this.angularScope && this.angularScope.getCollapsedText) { |
|
|
|
|
return this.angularScope.getCollapsedText(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
@ -182,7 +199,7 @@ export class QueryEditorRow extends PureComponent<Props, State> { |
|
|
|
|
|
|
|
|
|
render() { |
|
|
|
|
const { query, inMixedMode } = this.props; |
|
|
|
|
const { datasource, isCollapsed } = this.state; |
|
|
|
|
const { datasource, isCollapsed, hasTextEditMode } = this.state; |
|
|
|
|
const isDisabled = query.hide; |
|
|
|
|
|
|
|
|
|
const bodyClasses = classNames('query-editor-row__body gf-form-query', { |
|
|
|
@ -212,7 +229,7 @@ export class QueryEditorRow extends PureComponent<Props, State> { |
|
|
|
|
{isCollapsed && <div>{this.renderCollapsedText()}</div>} |
|
|
|
|
</div> |
|
|
|
|
<div className="query-editor-row__actions"> |
|
|
|
|
{this.hasTextEditMode && ( |
|
|
|
|
{hasTextEditMode && ( |
|
|
|
|
<button |
|
|
|
|
className="query-editor-row__action" |
|
|
|
|
onClick={this.onToggleEditMode} |
|
|
|
|