Commit 574a37e4 by Ryan McKinley Committed by GitHub

@grafana/runtime: expose location update (#17428)

parent 10a4a899
export interface LocationUpdate {
path?: string;
query?: UrlQueryMap;
/**
* Add the query argument to the existing URL
*/
partial?: boolean;
/**
* Do not change this unless you are the angular router
*/
routeParams?: UrlQueryMap;
/*
* If true this will replace url state (ie cause no new browser history)
*/
replace?: boolean;
}
export type UrlQueryValue = string | number | boolean | string[] | number[] | boolean[];
export type UrlQueryMap = { [s: string]: UrlQueryValue };
export interface LocationSrv {
update(options: LocationUpdate): void;
}
let singletonInstance: LocationSrv;
export function setLocationSrv(instance: LocationSrv) {
singletonInstance = instance;
}
export function getLocationSrv(): LocationSrv {
return singletonInstance;
}
export * from './backendSrv'; export * from './backendSrv';
export * from './AngularLoader'; export * from './AngularLoader';
export * from './dataSourceSrv'; export * from './dataSourceSrv';
export * from './LocationSrv';
import { LocationUpdate } from 'app/types'; import { LocationUpdate } from '@grafana/runtime';
import { actionCreatorFactory } from 'app/core/redux'; import { actionCreatorFactory } from 'app/core/redux';
export const updateLocation = actionCreatorFactory<LocationUpdate>('UPDATE_LOCATION').create(); export const updateLocation = actionCreatorFactory<LocationUpdate>('UPDATE_LOCATION').create();
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @preserve jquery-param (c) 2015 KNOWLEDGECODE | MIT * @preserve jquery-param (c) 2015 KNOWLEDGECODE | MIT
*/ */
import { UrlQueryMap } from 'app/types'; import { UrlQueryMap } from '@grafana/runtime';
export function renderUrl(path: string, query: UrlQueryMap | undefined): string { export function renderUrl(path: string, query: UrlQueryMap | undefined): string {
if (query && Object.keys(query).length > 0) { if (query && Object.keys(query).length > 0) {
...@@ -11,7 +11,7 @@ export function renderUrl(path: string, query: UrlQueryMap | undefined): string ...@@ -11,7 +11,7 @@ export function renderUrl(path: string, query: UrlQueryMap | undefined): string
return path; return path;
} }
export function encodeURIComponentAsAngularJS(val, pctEncodeSpaces) { export function encodeURIComponentAsAngularJS(val: string, pctEncodeSpaces?: boolean) {
return encodeURIComponent(val) return encodeURIComponent(val)
.replace(/%40/gi, '@') .replace(/%40/gi, '@')
.replace(/%3A/gi, ':') .replace(/%3A/gi, ':')
...@@ -21,15 +21,15 @@ export function encodeURIComponentAsAngularJS(val, pctEncodeSpaces) { ...@@ -21,15 +21,15 @@ export function encodeURIComponentAsAngularJS(val, pctEncodeSpaces) {
.replace(/%20/g, pctEncodeSpaces ? '%20' : '+'); .replace(/%20/g, pctEncodeSpaces ? '%20' : '+');
} }
export function toUrlParams(a) { export function toUrlParams(a: any) {
const s = []; const s = [];
const rbracket = /\[\]$/; const rbracket = /\[\]$/;
const isArray = obj => { const isArray = (obj: any) => {
return Object.prototype.toString.call(obj) === '[object Array]'; return Object.prototype.toString.call(obj) === '[object Array]';
}; };
const add = (k, v) => { const add = (k: string, v: any) => {
v = typeof v === 'function' ? v() : v === null ? '' : v === undefined ? '' : v; v = typeof v === 'function' ? v() : v === null ? '' : v === undefined ? '' : v;
if (typeof v !== 'boolean') { if (typeof v !== 'boolean') {
s[s.length] = encodeURIComponentAsAngularJS(k, true) + '=' + encodeURIComponentAsAngularJS(v, true); s[s.length] = encodeURIComponentAsAngularJS(k, true) + '=' + encodeURIComponentAsAngularJS(v, true);
...@@ -38,7 +38,7 @@ export function toUrlParams(a) { ...@@ -38,7 +38,7 @@ export function toUrlParams(a) {
} }
}; };
const buildParams = (prefix, obj) => { const buildParams = (prefix: string, obj: any) => {
let i, len, key; let i, len, key;
if (prefix) { if (prefix) {
......
...@@ -14,7 +14,7 @@ import { updateLocation } from 'app/core/actions'; ...@@ -14,7 +14,7 @@ import { updateLocation } from 'app/core/actions';
import { PanelModel } from '../../state'; import { PanelModel } from '../../state';
import { DashboardModel } from '../../state'; import { DashboardModel } from '../../state';
import { LS_PANEL_COPY_KEY } from 'app/core/constants'; import { LS_PANEL_COPY_KEY } from 'app/core/constants';
import { LocationUpdate } from 'app/types'; import { LocationUpdate } from '@grafana/runtime';
export interface Props { export interface Props {
panel: PanelModel; panel: PanelModel;
......
...@@ -13,8 +13,9 @@ import sortByKeys from 'app/core/utils/sort_by_keys'; ...@@ -13,8 +13,9 @@ import sortByKeys from 'app/core/utils/sort_by_keys';
// Types // Types
import { PanelModel, GridPos } from './PanelModel'; import { PanelModel, GridPos } from './PanelModel';
import { DashboardMigrator } from './DashboardMigrator'; import { DashboardMigrator } from './DashboardMigrator';
import { TimeRange } from '@grafana/ui/src'; import { TimeRange } from '@grafana/ui';
import { UrlQueryValue, KIOSK_MODE_TV, DashboardMeta } from 'app/types'; import { UrlQueryValue } from '@grafana/runtime';
import { KIOSK_MODE_TV, DashboardMeta } from 'app/types';
import { toUtc, DateTimeInput, dateTime, isDateTime } from '@grafana/ui/src/utils/moment_wrapper'; import { toUtc, DateTimeInput, dateTime, isDateTime } from '@grafana/ui/src/utils/moment_wrapper';
export interface CloneOptions { export interface CloneOptions {
......
...@@ -22,7 +22,8 @@ import { getNavModel } from 'app/core/selectors/navModel'; ...@@ -22,7 +22,8 @@ import { getNavModel } from 'app/core/selectors/navModel';
import { getRouteParamsId } from 'app/core/selectors/location'; import { getRouteParamsId } from 'app/core/selectors/location';
// Types // Types
import { StoreState, UrlQueryMap } from 'app/types/'; import { StoreState } from 'app/types/';
import { UrlQueryMap } from '@grafana/runtime';
import { NavModel, DataSourceSettings, DataSourcePluginMeta } from '@grafana/ui'; import { NavModel, DataSourceSettings, DataSourcePluginMeta } from '@grafana/ui';
import { getDataSourceLoadingNav } from '../state/navModel'; import { getDataSourceLoadingNav } from '../state/navModel';
import PluginStateinfo from 'app/features/plugins/PluginStateInfo'; import PluginStateinfo from 'app/features/plugins/PluginStateInfo';
......
...@@ -6,7 +6,8 @@ import { LayoutMode } from 'app/core/components/LayoutSelector/LayoutSelector'; ...@@ -6,7 +6,8 @@ import { LayoutMode } from 'app/core/components/LayoutSelector/LayoutSelector';
import { updateLocation, updateNavIndex, UpdateNavIndexAction } from 'app/core/actions'; import { updateLocation, updateNavIndex, UpdateNavIndexAction } from 'app/core/actions';
import { buildNavModel } from './navModel'; import { buildNavModel } from './navModel';
import { DataSourceSettings, DataSourcePluginMeta } from '@grafana/ui'; import { DataSourceSettings, DataSourcePluginMeta } from '@grafana/ui';
import { StoreState, LocationUpdate } from 'app/types'; import { StoreState } from 'app/types';
import { LocationUpdate } from '@grafana/runtime';
import { actionCreatorFactory } from 'app/core/redux'; import { actionCreatorFactory } from 'app/core/redux';
import { ActionOf, noPayloadActionCreatorFactory } from 'app/core/redux/actionCreatorFactory'; import { ActionOf, noPayloadActionCreatorFactory } from 'app/core/redux/actionCreatorFactory';
import { getPluginSettings } from 'app/features/plugins/PluginSettingsCache'; import { getPluginSettings } from 'app/features/plugins/PluginSettingsCache';
......
...@@ -55,7 +55,7 @@ import { ...@@ -55,7 +55,7 @@ import {
toggleLogLevelAction, toggleLogLevelAction,
} from './actionTypes'; } from './actionTypes';
import { updateLocation } from 'app/core/actions/location'; import { updateLocation } from 'app/core/actions/location';
import { LocationUpdate } from 'app/types'; import { LocationUpdate } from '@grafana/runtime';
import TableModel from 'app/core/table_model'; import TableModel from 'app/core/table_model';
import { isLive } from '@grafana/ui/src/components/RefreshPicker/RefreshPicker'; import { isLive } from '@grafana/ui/src/components/RefreshPicker/RefreshPicker';
......
...@@ -4,7 +4,8 @@ import { hot } from 'react-hot-loader'; ...@@ -4,7 +4,8 @@ import { hot } from 'react-hot-loader';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
// Types // Types
import { StoreState, UrlQueryMap } from 'app/types'; import { StoreState } from 'app/types';
import { UrlQueryMap } from '@grafana/runtime';
import Page from 'app/core/components/Page/Page'; import Page from 'app/core/components/Page/Page';
import { getPluginSettings } from './PluginSettingsCache'; import { getPluginSettings } from './PluginSettingsCache';
......
...@@ -5,7 +5,8 @@ import { connect } from 'react-redux'; ...@@ -5,7 +5,8 @@ import { connect } from 'react-redux';
import find from 'lodash/find'; import find from 'lodash/find';
// Types // Types
import { StoreState, UrlQueryMap } from 'app/types'; import { UrlQueryMap } from '@grafana/runtime';
import { StoreState } from 'app/types';
import { import {
NavModel, NavModel,
NavModelItem, NavModelItem,
......
...@@ -16,6 +16,9 @@ import { KeybindingSrv, setKeybindingSrv } from 'app/core/services/keybindingSrv ...@@ -16,6 +16,9 @@ import { KeybindingSrv, setKeybindingSrv } from 'app/core/services/keybindingSrv
import { AngularLoader, setAngularLoader } from 'app/core/services/AngularLoader'; import { AngularLoader, setAngularLoader } from 'app/core/services/AngularLoader';
import { configureStore } from 'app/store/configureStore'; import { configureStore } from 'app/store/configureStore';
import { LocationUpdate, setLocationSrv } from '@grafana/runtime';
import { updateLocation } from 'app/core/actions';
// Types // Types
import { KioskUrlValue } from 'app/types'; import { KioskUrlValue } from 'app/types';
...@@ -40,7 +43,12 @@ export class GrafanaCtrl { ...@@ -40,7 +43,12 @@ export class GrafanaCtrl {
setDataSourceSrv(datasourceSrv); setDataSourceSrv(datasourceSrv);
setTimeSrv(timeSrv); setTimeSrv(timeSrv);
setKeybindingSrv(keybindingSrv); setKeybindingSrv(keybindingSrv);
configureStore(); const store = configureStore();
setLocationSrv({
update: (opt: LocationUpdate) => {
store.dispatch(updateLocation(opt));
},
});
$scope.init = () => { $scope.init = () => {
$scope.contextSrv = contextSrv; $scope.contextSrv = contextSrv;
......
...@@ -89,6 +89,8 @@ export function configureStore() { ...@@ -89,6 +89,8 @@ export function configureStore() {
? applyMiddleware(toggleLogActionsMiddleware, thunk, epicMiddleware, logger) ? applyMiddleware(toggleLogActionsMiddleware, thunk, epicMiddleware, logger)
: applyMiddleware(thunk, epicMiddleware); : applyMiddleware(thunk, epicMiddleware);
setStore(createStore(rootReducer, {}, composeEnhancers(storeEnhancers))); const store = createStore(rootReducer, {}, composeEnhancers(storeEnhancers));
setStore(store);
epicMiddleware.run(rootEpic); epicMiddleware.run(rootEpic);
return store;
} }
export interface LocationUpdate { import { UrlQueryMap } from '@grafana/runtime';
path?: string;
query?: UrlQueryMap;
routeParams?: UrlQueryMap;
partial?: boolean;
/*
* If true this will replace url state (ie cause no new browser history)
*/
replace?: boolean;
}
export interface LocationState { export interface LocationState {
url: string; url: string;
...@@ -17,6 +8,3 @@ export interface LocationState { ...@@ -17,6 +8,3 @@ export interface LocationState {
replace: boolean; replace: boolean;
lastUpdated: number; lastUpdated: number;
} }
export type UrlQueryValue = string | number | boolean | string[] | number[] | boolean[];
export type UrlQueryMap = { [s: string]: UrlQueryValue };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment