mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
Merge branch 'master' of https://github.com/elasticsearch/kibana4
This commit is contained in:
commit
77a440243c
6 changed files with 142 additions and 68 deletions
|
@ -6,7 +6,7 @@ define(function (require) {
|
||||||
|
|
||||||
var module = require('modules').get('kibana/global_state');
|
var module = require('modules').get('kibana/global_state');
|
||||||
|
|
||||||
module.service('globalState', function (Private, $rootScope) {
|
module.service('globalState', function (Private, $rootScope, $location) {
|
||||||
var State = Private(require('components/state_management/state'));
|
var State = Private(require('components/state_management/state'));
|
||||||
|
|
||||||
_.inherits(GlobalState, State);
|
_.inherits(GlobalState, State);
|
||||||
|
@ -18,6 +18,14 @@ define(function (require) {
|
||||||
return qs.replaceParamInUrl(url, this._urlParam, this.toRISON());
|
return qs.replaceParamInUrl(url, this._urlParam, this.toRISON());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
GlobalState.prototype._readFromURL = function (method) {
|
||||||
|
var search = $location.search();
|
||||||
|
if (method === 'fetch') {
|
||||||
|
return (search[this._urlParam]) ? rison.decode(search[this._urlParam]) : this.toObject();
|
||||||
|
}
|
||||||
|
return rison.decode(search[this._urlParam] || '()');
|
||||||
|
};
|
||||||
|
|
||||||
return new GlobalState();
|
return new GlobalState();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -20,17 +20,22 @@ define(function (require) {
|
||||||
this.fetch();
|
this.fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
State.prototype._readFromURL = function (method) {
|
||||||
|
var search = $location.search();
|
||||||
|
return rison.decode(search[this._urlParam] || '()');
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches the state from the url
|
* Fetches the state from the url
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
State.prototype.fetch = function () {
|
State.prototype.fetch = function () {
|
||||||
var search = $location.search();
|
var stash = this._readFromURL('fetch');
|
||||||
var stash = rison.decode(search[this._urlParam] || '()');
|
|
||||||
_.defaults(stash, this._defaults);
|
_.defaults(stash, this._defaults);
|
||||||
// apply diff to state from stash, this is side effecting so
|
// apply diff to state from stash, will change state in place via side effect
|
||||||
// it will change state in place.
|
|
||||||
var diffResults = applyDiff(this, stash);
|
var diffResults = applyDiff(this, stash);
|
||||||
|
|
||||||
if (diffResults.keys.length) {
|
if (diffResults.keys.length) {
|
||||||
this.emit('fetch_with_changes', diffResults.keys);
|
this.emit('fetch_with_changes', diffResults.keys);
|
||||||
}
|
}
|
||||||
|
@ -41,16 +46,19 @@ define(function (require) {
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
State.prototype.save = function () {
|
State.prototype.save = function () {
|
||||||
var search = $location.search();
|
var stash = this._readFromURL('save');
|
||||||
var stash = rison.decode(search[this._urlParam] || '()');
|
|
||||||
var state = this.toObject();
|
var state = this.toObject();
|
||||||
|
|
||||||
_.defaults(state, this._defaults);
|
_.defaults(state, this._defaults);
|
||||||
// apply diff to stash from state, this is side effecting so
|
// apply diff to state from stash, will change state in place via side effect
|
||||||
// it will change stash in place.
|
|
||||||
var diffResults = applyDiff(stash, state);
|
var diffResults = applyDiff(stash, state);
|
||||||
|
|
||||||
if (diffResults.keys.length) {
|
if (diffResults.keys.length) {
|
||||||
this.emit('save_with_changes', diffResults.keys);
|
this.emit('save_with_changes', diffResults.keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// persist the state in the URL
|
||||||
|
var search = $location.search();
|
||||||
search[this._urlParam] = this.toRISON();
|
search[this._urlParam] = this.toRISON();
|
||||||
$location.search(search);
|
$location.search(search);
|
||||||
};
|
};
|
||||||
|
|
|
@ -81,6 +81,7 @@
|
||||||
'specs/courier/search_source/_get_normalized_sort',
|
'specs/courier/search_source/_get_normalized_sort',
|
||||||
'specs/factories/base_object',
|
'specs/factories/base_object',
|
||||||
'specs/state_management/state',
|
'specs/state_management/state',
|
||||||
|
'specs/state_management/global_state',
|
||||||
'specs/utils/diff_object',
|
'specs/utils/diff_object',
|
||||||
'specs/utils/diff_time_picker_vals',
|
'specs/utils/diff_time_picker_vals',
|
||||||
'specs/factories/events',
|
'specs/factories/events',
|
||||||
|
|
|
@ -7,42 +7,40 @@ define(function (require) {
|
||||||
// Load kibana
|
// Load kibana
|
||||||
require('index');
|
require('index');
|
||||||
|
|
||||||
describe('State Management', function () {
|
describe('Base Object', function () {
|
||||||
describe('BaseObject', function () {
|
var $rootScope;
|
||||||
var $rootScope;
|
var BaseObject;
|
||||||
var BaseObject;
|
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
module('kibana');
|
module('kibana');
|
||||||
|
|
||||||
inject(function (_$rootScope_, Private) {
|
inject(function (_$rootScope_, Private) {
|
||||||
$rootScope = _$rootScope_;
|
$rootScope = _$rootScope_;
|
||||||
BaseObject = Private(require('factories/base_object'));
|
BaseObject = Private(require('factories/base_object'));
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should take an inital set of values', function () {
|
|
||||||
var baseObject = new BaseObject({ message: 'test' });
|
|
||||||
expect(baseObject).to.have.property('message', 'test');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should serialize _attributes to RISON', function () {
|
|
||||||
var baseObject = new BaseObject();
|
|
||||||
baseObject.message = 'Testing... 1234';
|
|
||||||
var rison = baseObject.toRISON();
|
|
||||||
expect(rison).to.equal('(message:\'Testing... 1234\')');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should serialize _attributes for JSON', function () {
|
|
||||||
var baseObject = new BaseObject();
|
|
||||||
baseObject.message = 'Testing... 1234';
|
|
||||||
baseObject._private = 'foo';
|
|
||||||
baseObject.$private = 'stuff';
|
|
||||||
var json = JSON.stringify(baseObject);
|
|
||||||
expect(json).to.equal('{"message":"Testing... 1234"}');
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should take an inital set of values', function () {
|
||||||
|
var baseObject = new BaseObject({ message: 'test' });
|
||||||
|
expect(baseObject).to.have.property('message', 'test');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should serialize _attributes to RISON', function () {
|
||||||
|
var baseObject = new BaseObject();
|
||||||
|
baseObject.message = 'Testing... 1234';
|
||||||
|
var rison = baseObject.toRISON();
|
||||||
|
expect(rison).to.equal('(message:\'Testing... 1234\')');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should serialize _attributes for JSON', function () {
|
||||||
|
var baseObject = new BaseObject();
|
||||||
|
baseObject.message = 'Testing... 1234';
|
||||||
|
baseObject._private = 'foo';
|
||||||
|
baseObject.$private = 'stuff';
|
||||||
|
var json = JSON.stringify(baseObject);
|
||||||
|
expect(json).to.equal('{"message":"Testing... 1234"}');
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
37
test/unit/specs/state_management/global_state.js
Normal file
37
test/unit/specs/state_management/global_state.js
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
define(function (require) {
|
||||||
|
var sinon = require('sinon/sinon');
|
||||||
|
require('components/state_management/global_state');
|
||||||
|
|
||||||
|
// Load kibana
|
||||||
|
require('index');
|
||||||
|
|
||||||
|
describe('State Management', function () {
|
||||||
|
var $rootScope, $location, state;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
module('kibana');
|
||||||
|
|
||||||
|
inject(function (_$location_, globalState) {
|
||||||
|
$location = _$location_;
|
||||||
|
state = globalState;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Global State', function () {
|
||||||
|
it('should use previous state when not in URL', function () {
|
||||||
|
// set satte via URL
|
||||||
|
$location.search({ _g: '(foo:(bar:baz))' });
|
||||||
|
state.fetch();
|
||||||
|
expect(state.toObject()).to.eql({ foo: { bar: 'baz' } });
|
||||||
|
|
||||||
|
$location.search({ _g: '(fizz:buzz)' });
|
||||||
|
state.fetch();
|
||||||
|
expect(state.toObject()).to.eql({ fizz: 'buzz' });
|
||||||
|
|
||||||
|
$location.search({});
|
||||||
|
state.fetch();
|
||||||
|
expect(state.toObject()).to.eql({ fizz: 'buzz' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,5 +1,4 @@
|
||||||
define(function (require) {
|
define(function (require) {
|
||||||
var angular = require('angular');
|
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var sinon = require('sinon/sinon');
|
var sinon = require('sinon/sinon');
|
||||||
require('services/private');
|
require('services/private');
|
||||||
|
@ -8,26 +7,41 @@ define(function (require) {
|
||||||
require('index');
|
require('index');
|
||||||
|
|
||||||
describe('State Management', function () {
|
describe('State Management', function () {
|
||||||
describe('State', function () {
|
var $rootScope, $location, State, Events;
|
||||||
|
|
||||||
var $rootScope, $location, State, Events;
|
beforeEach(function () {
|
||||||
|
module('kibana');
|
||||||
|
|
||||||
beforeEach(function () {
|
inject(function (_$rootScope_, _$location_, Private) {
|
||||||
module('kibana');
|
$location = _$location_;
|
||||||
|
$rootScope = _$rootScope_;
|
||||||
|
State = Private(require('components/state_management/state'));
|
||||||
|
Events = Private(require('factories/events'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
inject(function (_$rootScope_, _$location_, Private) {
|
describe('Provider', function () {
|
||||||
$location = _$location_;
|
it('should reset the state to the defaults', function () {
|
||||||
$rootScope = _$rootScope_;
|
var state = new State('_s', { message: ['test'] });
|
||||||
State = Private(require('components/state_management/state'));
|
state.reset();
|
||||||
Events = Private(require('factories/events'));
|
var search = $location.search();
|
||||||
});
|
expect(search).to.have.property('_s');
|
||||||
|
expect(search._s).to.equal('(message:!(test))');
|
||||||
|
expect(state.message).to.eql(['test']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should apply the defaults upon initialization', function () {
|
||||||
|
var state = new State('_s', { message: 'test' });
|
||||||
|
expect(state).to.have.property('message', 'test');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should inherit from Events', function () {
|
it('should inherit from Events', function () {
|
||||||
var state = new State();
|
var state = new State();
|
||||||
expect(state).to.be.an(Events);
|
expect(state).to.be.an(Events);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Search', function () {
|
||||||
it('should save to $location.search()', function () {
|
it('should save to $location.search()', function () {
|
||||||
var state = new State('_s', { test: 'foo' });
|
var state = new State('_s', { test: 'foo' });
|
||||||
state.save();
|
state.save();
|
||||||
|
@ -47,8 +61,9 @@ define(function (require) {
|
||||||
var search = $location.search();
|
var search = $location.search();
|
||||||
$rootScope.$apply();
|
$rootScope.$apply();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Fetch', function () {
|
||||||
it('should emit an event if changes are fetched', function (done) {
|
it('should emit an event if changes are fetched', function (done) {
|
||||||
var state = new State();
|
var state = new State();
|
||||||
state.on('fetch_with_changes', function (keys) {
|
state.on('fetch_with_changes', function (keys) {
|
||||||
|
@ -91,20 +106,6 @@ define(function (require) {
|
||||||
expect(state).to.have.property('message', 'test');
|
expect(state).to.have.property('message', 'test');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should reset the state to the defaults', function () {
|
|
||||||
var state = new State('_s', { message: ['test'] });
|
|
||||||
state.reset();
|
|
||||||
var search = $location.search();
|
|
||||||
expect(search).to.have.property('_s');
|
|
||||||
expect(search._s).to.equal('(message:!(test))');
|
|
||||||
expect(state.message).to.eql(['test']);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should apply the defaults upon initialization', function () {
|
|
||||||
var state = new State('_s', { message: 'test' });
|
|
||||||
expect(state).to.have.property('message', 'test');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should call fetch when $routeUpdate is fired on $rootScope', function () {
|
it('should call fetch when $routeUpdate is fired on $rootScope', function () {
|
||||||
var state = new State();
|
var state = new State();
|
||||||
var spy = sinon.spy(state, 'fetch');
|
var spy = sinon.spy(state, 'fetch');
|
||||||
|
@ -112,7 +113,28 @@ define(function (require) {
|
||||||
sinon.assert.calledOnce(spy);
|
sinon.assert.calledOnce(spy);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should clear state when missing form URL', function () {
|
||||||
|
var stateObj;
|
||||||
|
var state = new State();
|
||||||
|
|
||||||
|
// set satte via URL
|
||||||
|
$location.search({ _s: '(foo:(bar:baz))' });
|
||||||
|
state.fetch();
|
||||||
|
stateObj = state.toObject();
|
||||||
|
expect(stateObj).to.eql({ foo: { bar: 'baz' } });
|
||||||
|
|
||||||
|
// ensure changing URL changes state
|
||||||
|
$location.search({ _s: '(one:two)' });
|
||||||
|
state.fetch();
|
||||||
|
stateObj = state.toObject();
|
||||||
|
expect(stateObj).to.eql({ one: 'two' });
|
||||||
|
|
||||||
|
// remove search, state should be empty
|
||||||
|
$location.search({});
|
||||||
|
state.fetch();
|
||||||
|
stateObj = state.toObject();
|
||||||
|
expect(stateObj).to.eql({});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue