make all "default index pattern" handling optional
This commit is contained in:
parent
7b246b5714
commit
f94d864cba
|
@ -8,12 +8,12 @@ define(function (require, module, exports) {
|
|||
require('ui/routes')
|
||||
.when('/settings', {
|
||||
redirectTo: '/settings/indices'
|
||||
})
|
||||
.addSetupWork(
|
||||
require('ui/index_patterns/routeSetup/loadDefault')({
|
||||
notRequiredRe: /^\/settings\//
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
require('ui/index_patterns/routeSetup/loadDefault')({
|
||||
notRequiredRe: /^\/settings\//,
|
||||
whenMissingRedirectTo: '/settings/indices'
|
||||
});
|
||||
|
||||
require('ui/modules')
|
||||
.get('apps/settings')
|
||||
|
|
|
@ -1,14 +1,49 @@
|
|||
define(function (require) {
|
||||
let _ = require('lodash');
|
||||
let { NoDefaultIndexPattern, NoDefinedIndexPatterns } = require('ui/errors');
|
||||
let Notifier = require('ui/notify/Notifier');
|
||||
let notify = new Notifier({
|
||||
location: 'Index Patterns'
|
||||
});
|
||||
let _ = require('lodash');
|
||||
let { NoDefaultIndexPattern, NoDefinedIndexPatterns } = require('ui/errors');
|
||||
let Notifier = require('ui/notify/Notifier');
|
||||
let notify = new Notifier({
|
||||
location: 'Index Patterns'
|
||||
});
|
||||
|
||||
module.exports = function (opts) {
|
||||
opts = opts || {};
|
||||
let notRequiredRe = opts.notRequiredRe || null;
|
||||
let whenMissingRedirectTo = opts.whenMissingRedirectTo || null;
|
||||
let defaultRequiredToasts = null;
|
||||
|
||||
require('ui/routes')
|
||||
.addSetupWork(function loadDefaultIndexPattern(Private, Promise, $route, config, indexPatterns) {
|
||||
let getIds = Private(require('../_get_ids'));
|
||||
let rootSearchSource = Private(require('ui/courier/data_source/_root_search_source'));
|
||||
let path = _.get($route, 'current.$$route.originalPath');
|
||||
|
||||
return config.init()
|
||||
.then(function () {
|
||||
return getIds();
|
||||
})
|
||||
.then(function (patterns) {
|
||||
let defaultId = config.get('defaultIndex');
|
||||
let defined = !!defaultId;
|
||||
let exists = _.contains(patterns, defaultId);
|
||||
let required = !notRequiredRe || !path.match(notRequiredRe);
|
||||
|
||||
if (defined && !exists) {
|
||||
config.clear('defaultIndex');
|
||||
defaultId = defined = false;
|
||||
}
|
||||
|
||||
if (!defined && required) {
|
||||
throw new NoDefaultIndexPattern();
|
||||
}
|
||||
|
||||
return notify.event('loading default index pattern', function () {
|
||||
return indexPatterns.get(defaultId).then(function (pattern) {
|
||||
rootSearchSource.getGlobalSource().set('index', pattern);
|
||||
notify.log('index pattern set to', defaultId);
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
.afterWork(
|
||||
// success
|
||||
function () {
|
||||
|
@ -21,49 +56,13 @@ define(function (require) {
|
|||
// failure
|
||||
function (err, kbnUrl) {
|
||||
let hasDefault = !(err instanceof NoDefaultIndexPattern);
|
||||
if (hasDefault) throw err; // rethrow
|
||||
if (hasDefault || !whenMissingRedirectTo) throw err; // rethrow
|
||||
|
||||
kbnUrl.change('/settings/indices');
|
||||
kbnUrl.change(whenMissingRedirectTo);
|
||||
if (!defaultRequiredToasts) defaultRequiredToasts = [];
|
||||
else defaultRequiredToasts.push(notify.error(err));
|
||||
}
|
||||
);
|
||||
|
||||
return function makeIndexPatternLoadDefaultRouteSetup(opts) {
|
||||
opts = opts || {};
|
||||
var notRequiredRe = opts.notRequiredRe || null;
|
||||
|
||||
return function loadDefault(Private, Promise, $route, config, indexPatterns) {
|
||||
let getIds = Private(require('../_get_ids'));
|
||||
var rootSearchSource = Private(require('ui/courier/data_source/_root_search_source'));
|
||||
var path = _.get($route, 'current.$$route.originalPath');
|
||||
|
||||
return config.init()
|
||||
.then(function () {
|
||||
return getIds();
|
||||
})
|
||||
.then(function (patterns) {
|
||||
let defaultId = config.get('defaultIndex');
|
||||
let defined = !!defaultId;
|
||||
let exists = _.contains(patterns, defaultId);
|
||||
let required = !notRequiredRe || !path.match(notRequiredRe);
|
||||
|
||||
if (defined && !exists) {
|
||||
config.clear('defaultIndex');
|
||||
defaultId = defined = false;
|
||||
}
|
||||
|
||||
if (!defined && required) {
|
||||
throw new NoDefaultIndexPattern();
|
||||
}
|
||||
|
||||
return notify.event('loading default index pattern', function () {
|
||||
return indexPatterns.get(defaultId).then(function (pattern) {
|
||||
rootSearchSource.getGlobalSource().set('index', pattern);
|
||||
notify.log('index pattern set to', defaultId);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
};
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue