mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 09:19:04 -04:00
Merge branch 'master' into vislib/refactor
This commit is contained in:
commit
346d3940c9
6 changed files with 142 additions and 68 deletions
|
@ -6,7 +6,7 @@ define(function (require) {
|
|||
|
||||
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'));
|
||||
|
||||
_.inherits(GlobalState, State);
|
||||
|
@ -18,6 +18,14 @@ define(function (require) {
|
|||
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();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -20,17 +20,22 @@ define(function (require) {
|
|||
this.fetch();
|
||||
}
|
||||
|
||||
State.prototype._readFromURL = function (method) {
|
||||
var search = $location.search();
|
||||
return rison.decode(search[this._urlParam] || '()');
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches the state from the url
|
||||
* @returns {void}
|
||||
*/
|
||||
State.prototype.fetch = function () {
|
||||
var search = $location.search();
|
||||
var stash = rison.decode(search[this._urlParam] || '()');
|
||||
var stash = this._readFromURL('fetch');
|
||||
|
||||
_.defaults(stash, this._defaults);
|
||||
// apply diff to state from stash, this is side effecting so
|
||||
// it will change state in place.
|
||||
// apply diff to state from stash, will change state in place via side effect
|
||||
var diffResults = applyDiff(this, stash);
|
||||
|
||||
if (diffResults.keys.length) {
|
||||
this.emit('fetch_with_changes', diffResults.keys);
|
||||
}
|
||||
|
@ -41,16 +46,19 @@ define(function (require) {
|
|||
* @returns {void}
|
||||
*/
|
||||
State.prototype.save = function () {
|
||||
var search = $location.search();
|
||||
var stash = rison.decode(search[this._urlParam] || '()');
|
||||
var stash = this._readFromURL('save');
|
||||
var state = this.toObject();
|
||||
|
||||
_.defaults(state, this._defaults);
|
||||
// apply diff to stash from state, this is side effecting so
|
||||
// it will change stash in place.
|
||||
// apply diff to state from stash, will change state in place via side effect
|
||||
var diffResults = applyDiff(stash, state);
|
||||
|
||||
if (diffResults.keys.length) {
|
||||
this.emit('save_with_changes', diffResults.keys);
|
||||
}
|
||||
|
||||
// persist the state in the URL
|
||||
var search = $location.search();
|
||||
search[this._urlParam] = this.toRISON();
|
||||
$location.search(search);
|
||||
};
|
||||
|
|
|
@ -81,6 +81,7 @@
|
|||
'specs/courier/search_source/_get_normalized_sort',
|
||||
'specs/factories/base_object',
|
||||
'specs/state_management/state',
|
||||
'specs/state_management/global_state',
|
||||
'specs/utils/diff_object',
|
||||
'specs/factories/events',
|
||||
'specs/vislib/color',
|
||||
|
|
|
@ -7,42 +7,40 @@ define(function (require) {
|
|||
// Load kibana
|
||||
require('index');
|
||||
|
||||
describe('State Management', function () {
|
||||
describe('BaseObject', function () {
|
||||
var $rootScope;
|
||||
var BaseObject;
|
||||
describe('Base Object', function () {
|
||||
var $rootScope;
|
||||
var BaseObject;
|
||||
|
||||
beforeEach(function () {
|
||||
module('kibana');
|
||||
beforeEach(function () {
|
||||
module('kibana');
|
||||
|
||||
inject(function (_$rootScope_, Private) {
|
||||
$rootScope = _$rootScope_;
|
||||
BaseObject = Private(require('factories/base_object'));
|
||||
});
|
||||
inject(function (_$rootScope_, Private) {
|
||||
$rootScope = _$rootScope_;
|
||||
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) {
|
||||
var angular = require('angular');
|
||||
var _ = require('lodash');
|
||||
var sinon = require('sinon/sinon');
|
||||
require('services/private');
|
||||
|
@ -8,26 +7,41 @@ define(function (require) {
|
|||
require('index');
|
||||
|
||||
describe('State Management', function () {
|
||||
describe('State', function () {
|
||||
var $rootScope, $location, State, Events;
|
||||
|
||||
var $rootScope, $location, State, Events;
|
||||
beforeEach(function () {
|
||||
module('kibana');
|
||||
|
||||
beforeEach(function () {
|
||||
module('kibana');
|
||||
inject(function (_$rootScope_, _$location_, Private) {
|
||||
$location = _$location_;
|
||||
$rootScope = _$rootScope_;
|
||||
State = Private(require('components/state_management/state'));
|
||||
Events = Private(require('factories/events'));
|
||||
});
|
||||
});
|
||||
|
||||
inject(function (_$rootScope_, _$location_, Private) {
|
||||
$location = _$location_;
|
||||
$rootScope = _$rootScope_;
|
||||
State = Private(require('components/state_management/state'));
|
||||
Events = Private(require('factories/events'));
|
||||
});
|
||||
describe('Provider', function () {
|
||||
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 inherit from Events', function () {
|
||||
var state = new State();
|
||||
expect(state).to.be.an(Events);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Search', function () {
|
||||
it('should save to $location.search()', function () {
|
||||
var state = new State('_s', { test: 'foo' });
|
||||
state.save();
|
||||
|
@ -47,8 +61,9 @@ define(function (require) {
|
|||
var search = $location.search();
|
||||
$rootScope.$apply();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('Fetch', function () {
|
||||
it('should emit an event if changes are fetched', function (done) {
|
||||
var state = new State();
|
||||
state.on('fetch_with_changes', function (keys) {
|
||||
|
@ -91,20 +106,6 @@ define(function (require) {
|
|||
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 () {
|
||||
var state = new State();
|
||||
var spy = sinon.spy(state, 'fetch');
|
||||
|
@ -112,7 +113,28 @@ define(function (require) {
|
|||
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