2020import angular from 'angular' ;
2121import _ from 'lodash' ;
2222import { Subscription } from 'rxjs' ;
23+ import { map } from 'rxjs/operators' ;
2324import { i18n } from '@kbn/i18n' ;
2425
2526import React from 'react' ;
@@ -29,13 +30,17 @@ import { VisualizeConstants } from '../visualize_constants';
2930import { getEditBreadcrumbs } from '../breadcrumbs' ;
3031
3132import { addHelpMenuToAppChrome } from '../help_menu/help_menu_util' ;
32- import { FilterStateManager } from '../../../../../data/public' ;
3333import { unhashUrl } from '../../../../../../../plugins/kibana_utils/public' ;
3434import { kbnBaseUrl } from '../../../../../../../plugins/kibana_legacy/public' ;
3535import {
3636 SavedObjectSaveModal ,
3737 showSaveModal ,
3838} from '../../../../../../../plugins/saved_objects/public' ;
39+ import {
40+ esFilters ,
41+ connectToQueryState ,
42+ syncQueryStateWithUrl ,
43+ } from '../../../../../../../plugins/data/public' ;
3944
4045import { initVisEditorDirective } from './visualization_editor' ;
4146import { initVisualizationDirective } from './visualization' ;
@@ -65,28 +70,21 @@ export function initEditorDirective(app, deps) {
6570
6671function VisualizeAppController (
6772 $scope ,
68- $element ,
6973 $route ,
7074 $window ,
7175 $injector ,
7276 $timeout ,
7377 kbnUrl ,
7478 redirectWhenMissing ,
75- Promise ,
76- globalState ,
77- config
79+ kbnUrlStateStorage ,
80+ history
7881) {
7982 const {
8083 indexPatterns,
8184 localStorage,
8285 visualizeCapabilities,
8386 share,
84- data : {
85- query : {
86- filterManager,
87- timefilter : { timefilter } ,
88- } ,
89- } ,
87+ data : { query : queryService } ,
9088 toastNotifications,
9189 chrome,
9290 getBasePath,
@@ -97,6 +95,17 @@ function VisualizeAppController(
9795 setActiveUrl,
9896 } = getServices ( ) ;
9997
98+ const {
99+ filterManager,
100+ timefilter : { timefilter } ,
101+ } = queryService ;
102+
103+ // starts syncing `_g` portion of url with query services
104+ const { stop : stopSyncingQueryServiceStateWithUrl } = syncQueryStateWithUrl (
105+ queryService ,
106+ kbnUrlStateStorage
107+ ) ;
108+
100109 // Retrieve the resolved SavedVis instance.
101110 const savedVis = $route . current . locals . savedVis ;
102111 const _applyVis = ( ) => {
@@ -284,26 +293,24 @@ function VisualizeAppController(
284293 linked : ! ! savedVis . savedSearchId ,
285294 } ;
286295
287- const useHash = config . get ( 'state:storeInSessionStorage' ) ;
288296 const { stateContainer, stopStateSync } = useVisualizeAppState ( {
289- useHash,
290297 stateDefaults,
298+ kbnUrlStateStorage,
291299 } ) ;
292300
293- const filterStateManager = new FilterStateManager (
294- globalState ,
295- ( ) => {
296- // Temporary AppState replacement
297- return {
298- set filters ( _filters ) {
299- stateContainer . transitions . set ( 'filters' , _filters ) ;
300- } ,
301- get filters ( ) {
302- return stateContainer . getState ( ) . filters ;
303- } ,
304- } ;
301+ // sync initial app filters from state to filterManager
302+ filterManager . setAppFilters ( _ . cloneDeep ( stateContainer . getState ( ) . filters ) ) ;
303+ // setup syncing of app filters between appState and filterManager
304+ const stopSyncingAppFilters = connectToQueryState (
305+ queryService ,
306+ {
307+ set : ( { filters } ) => stateContainer . transitions . set ( 'filters' , filters ) ,
308+ get : ( ) => ( { filters : stateContainer . getState ( ) . filters } ) ,
309+ state$ : stateContainer . state$ . pipe ( map ( state => ( { filters : state . filters } ) ) ) ,
305310 } ,
306- filterManager
311+ {
312+ filters : esFilters . FilterStateStore . APP_STATE ,
313+ }
307314 ) ;
308315
309316 // The savedVis is pulled from elasticsearch, but the appState is pulled from the url, with the
@@ -335,6 +342,24 @@ function VisualizeAppController(
335342 }
336343 ) ;
337344
345+ const updateSavedQueryFromUrl = savedQueryId => {
346+ if ( ! savedQueryId ) {
347+ delete $scope . savedQuery ;
348+
349+ return ;
350+ }
351+
352+ if ( $scope . savedQuery && $scope . savedQuery . id === savedQueryId ) {
353+ return ;
354+ }
355+
356+ savedQueryService . getSavedQuery ( savedQueryId ) . then ( savedQuery => {
357+ $scope . $evalAsync ( ( ) => {
358+ $scope . updateSavedQuery ( savedQuery ) ;
359+ } ) ;
360+ } ) ;
361+ } ;
362+
338363 function init ( ) {
339364 if ( vis . indexPattern ) {
340365 $scope . indexPattern = vis . indexPattern ;
@@ -388,14 +413,14 @@ function VisualizeAppController(
388413 } ;
389414
390415 $scope . timeRange = timefilter . getTime ( ) ;
391- $scope . opts = _ . pick ( $scope , 'savedVis' , 'isAddToDashMode' ) ;
392416
393417 const unsubscribeStateUpdates = stateContainer . subscribe ( state => {
394418 const newQuery = migrateLegacyQuery ( state . query ) ;
395419 if ( ! _ . isEqual ( state . query , newQuery ) ) {
396420 stateContainer . transitions . set ( 'query' , newQuery ) ;
397421 }
398422 persistOnChange ( state ) ;
423+ updateSavedQueryFromUrl ( state . savedQuery ) ;
399424
400425 // if the browser history was changed manually we need to reflect changes in the editor
401426 if ( ! _ . isEqual ( vis . getState ( ) , state . vis ) ) {
@@ -413,6 +438,9 @@ function VisualizeAppController(
413438 $scope . $broadcast ( 'render' ) ;
414439 } ;
415440
441+ // update the query if savedQuery is stored
442+ updateSavedQueryFromUrl ( initialState . savedQuery ) ;
443+
416444 const subscriptions = new Subscription ( ) ;
417445
418446 subscriptions . add (
@@ -438,7 +466,7 @@ function VisualizeAppController(
438466
439467 // update the searchSource when query updates
440468 $scope . fetch = function ( ) {
441- const { query, filters , linked } = stateContainer . getState ( ) ;
469+ const { query, linked , filters } = stateContainer . getState ( ) ;
442470 $scope . query = query ;
443471 $scope . linked = linked ;
444472 savedVis . searchSource . setField ( 'query' , query ) ;
@@ -451,7 +479,6 @@ function VisualizeAppController(
451479 subscribeWithScope ( $scope , filterManager . getUpdates$ ( ) , {
452480 next : ( ) => {
453481 $scope . filters = filterManager . getFilters ( ) ;
454- $scope . globalFilters = filterManager . getGlobalFilters ( ) ;
455482 } ,
456483 } )
457484 ) ;
@@ -466,13 +493,14 @@ function VisualizeAppController(
466493 $scope . _handler . destroy ( ) ;
467494 }
468495 savedVis . destroy ( ) ;
469- filterStateManager . destroy ( ) ;
470496 subscriptions . unsubscribe ( ) ;
471497 $scope . vis . off ( 'apply' , _applyVis ) ;
472498
473499 unsubscribePersisted ( ) ;
474500 unsubscribeStateUpdates ( ) ;
475501 stopStateSync ( ) ;
502+ stopSyncingQueryServiceStateWithUrl ( ) ;
503+ stopSyncingAppFilters ( ) ;
476504 } ) ;
477505
478506 $timeout ( ( ) => {
@@ -501,23 +529,14 @@ function VisualizeAppController(
501529 } ) ;
502530 } ;
503531
504- $scope . onQuerySaved = savedQuery => {
505- $scope . savedQuery = savedQuery ;
506- } ;
507-
508- $scope . onSavedQueryUpdated = savedQuery => {
509- $scope . savedQuery = { ...savedQuery } ;
510- } ;
511-
512532 $scope . onClearSavedQuery = ( ) => {
513533 delete $scope . savedQuery ;
514534 stateContainer . transitions . removeSavedQuery ( defaultQuery ) ;
515535 filterManager . setFilters ( filterManager . getGlobalFilters ( ) ) ;
516- $scope . fetch ( ) ;
517536 } ;
518537
519538 const updateStateFromSavedQuery = savedQuery => {
520- stateContainer . transitions . set ( 'query' , savedQuery . attributes . query ) ;
539+ stateContainer . transitions . updateFromSavedQuery ( savedQuery ) ;
521540
522541 const savedQueryFilters = savedQuery . attributes . filters || [ ] ;
523542 const globalFilters = filterManager . getGlobalFilters ( ) ;
@@ -532,25 +551,12 @@ function VisualizeAppController(
532551 timefilter . setRefreshInterval ( savedQuery . attributes . timefilter . refreshInterval ) ;
533552 }
534553 }
535-
536- $scope . fetch ( ) ;
537554 } ;
538555
539- // update the query if savedQuery is stored
540- if ( stateContainer . getState ( ) . savedQuery ) {
541- savedQueryService . getSavedQuery ( stateContainer . getState ( ) . savedQuery ) . then ( savedQuery => {
542- $scope . $evalAsync ( ( ) => {
543- $scope . savedQuery = savedQuery ;
544- } ) ;
545- } ) ;
546- }
547-
548- $scope . $watch ( 'savedQuery' , newSavedQuery => {
549- if ( ! newSavedQuery ) return ;
550- stateContainer . transitions . set ( 'savedQuery' , newSavedQuery . id ) ;
551-
552- updateStateFromSavedQuery ( newSavedQuery ) ;
553- } ) ;
556+ $scope . updateSavedQuery = savedQuery => {
557+ $scope . savedQuery = savedQuery ;
558+ updateStateFromSavedQuery ( savedQuery ) ;
559+ } ;
554560
555561 $scope . $watch ( 'linked' , linked => {
556562 if ( linked && ! savedVis . savedSearchId ) {
@@ -626,7 +632,10 @@ function VisualizeAppController(
626632 savedVis . vis . title = savedVis . title ;
627633 savedVis . vis . description = savedVis . description ;
628634 } else {
629- kbnUrl . change ( `${ VisualizeConstants . EDIT_PATH } /{{id}}` , { id : savedVis . id } ) ;
635+ history . replace ( {
636+ ...history . location ,
637+ pathname : `${ VisualizeConstants . EDIT_PATH } /${ savedVis . id } ` ,
638+ } ) ;
630639 }
631640 }
632641 } ) ;
0 commit comments