Skip to content

Commit

Permalink
Extract subUrl hooks from KbnChrome directive
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Apr 1, 2019
1 parent 39eb3af commit ad54a16
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import ngMock from 'ng_mock';
import expect from '@kbn/expect';

import { SubUrlRouteFilterProvider } from '../sub_url_route_filter';
import { SubUrlRouteFilterProvider } from '../sub_url_hooks';

describe('kbn-chrome subUrlRouteFilter()', () => {
describe('no ngRoute', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/legacy/ui/public/chrome/api/angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { toastNotifications } from '../../notify';
import { UrlOverflowServiceProvider } from '../../error_url_overflow';

import { directivesProvider } from '../directives';
import { registerSubUrlHooks } from './sub_url_hooks';

const URL_LIMIT_WARN_WITHIN = 1000;

Expand Down Expand Up @@ -137,6 +138,7 @@ export function initAngularApi(chrome, internals) {
});

directivesProvider(chrome, internals);
registerSubUrlHooks(kibana, internals);

uiModules.link(kibana);
};
Expand Down
80 changes: 80 additions & 0 deletions src/legacy/ui/public/chrome/api/sub_url_hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import url from 'url';

import {
getUnhashableStatesProvider,
unhashUrl,
} from '../../state_management/state_hashing';

export function registerSubUrlHooks(angularModule, internals) {
angularModule.run(($rootScope, Private, $location) => {
const getUnhashableStates = Private(getUnhashableStatesProvider);
const subUrlRouteFilter = Private(SubUrlRouteFilterProvider);

function updateSubUrls() {
const urlWithHashes = window.location.href;
const urlWithStates = unhashUrl(urlWithHashes, getUnhashableStates());
internals.trackPossibleSubUrl(urlWithStates);
}

function onRouteChange($event) {
if (subUrlRouteFilter($event)) {
updateSubUrls();
}
}

$rootScope.$on('$locationChangeStart', (e, newUrl) => {
// This handler fixes issue #31238 where browser back navigation
// fails due to angular 1.6 parsing url encoded params wrong.
const absUrlHash = url.parse($location.absUrl()).hash.slice(1);
const decodedAbsUrlHash = decodeURIComponent(absUrlHash);
const hash = url.parse(newUrl).hash.slice(1);
const decodedHash = decodeURIComponent(hash);
if (absUrlHash !== hash && decodedHash === decodedAbsUrlHash) {
// replace the urlencoded hash with the version that angular sees.
$location.url(absUrlHash).replace();
}
});

$rootScope.$on('$routeChangeSuccess', onRouteChange);
$rootScope.$on('$routeUpdate', onRouteChange);
updateSubUrls(); // initialize sub urls
});
}

/**
* Creates a function that will be called on each route change
* to determine if the event should be used to update the last
* subUrl of chrome links/tabs
* @injected
*/
export function SubUrlRouteFilterProvider($injector) {
if (!$injector.has('$route')) {
return function alwaysUpdate() {
return true;
};
}

const $route = $injector.get('$route');
return function ignoreRedirectToRoutes() {
return Boolean($route.current && !$route.current.redirectTo);
};
}
42 changes: 3 additions & 39 deletions src/legacy/ui/public/chrome/directives/kbn_chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@
import React from 'react';
import ReactDOM from 'react-dom';
import $ from 'jquery';
import url from 'url';

import { uiModules } from '../../modules';
import {
getUnhashableStatesProvider,
unhashUrl,
} from '../../state_management/state_hashing';

import {
notify,
GlobalBannerList,
banners,
} from '../../notify';
import { SubUrlRouteFilterProvider } from './sub_url_route_filter';

import { I18nContext } from '../../i18n';

export function kbnChromeProvider(chrome, internals) {
Expand All @@ -58,39 +54,7 @@ export function kbnChromeProvider(chrome, internals) {
},

controllerAs: 'chrome',
controller($scope, $rootScope, Private, $location) {
const getUnhashableStates = Private(getUnhashableStatesProvider);
const subUrlRouteFilter = Private(SubUrlRouteFilterProvider);

function updateSubUrls() {
const urlWithHashes = window.location.href;
const urlWithStates = unhashUrl(urlWithHashes, getUnhashableStates());
internals.trackPossibleSubUrl(urlWithStates);
}

function onRouteChange($event) {
if (subUrlRouteFilter($event)) {
updateSubUrls();
}
}

$rootScope.$on('$locationChangeStart', (e, newUrl) => {
// This handler fixes issue #31238 where browser back navigation
// fails due to angular 1.6 parsing url encoded params wrong.
const absUrlHash = url.parse($location.absUrl()).hash.slice(1);
const decodedAbsUrlHash = decodeURIComponent(absUrlHash);
const hash = url.parse(newUrl).hash.slice(1);
const decodedHash = decodeURIComponent(hash);
if (absUrlHash !== hash && decodedHash === decodedAbsUrlHash) {
// replace the urlencoded hash with the version that angular sees.
$location.url(absUrlHash).replace();
}
});

$rootScope.$on('$routeChangeSuccess', onRouteChange);
$rootScope.$on('$routeUpdate', onRouteChange);
updateSubUrls(); // initialize sub urls

controller($scope) {
// Notifications
$scope.notifList = notify._notifs;

Expand Down
37 changes: 0 additions & 37 deletions src/legacy/ui/public/chrome/directives/sub_url_route_filter.js

This file was deleted.

0 comments on commit ad54a16

Please sign in to comment.