allow apps to push variables from the server to the client

This commit is contained in:
spalger 2015-08-10 14:15:31 -07:00
parent 153e1957c1
commit ea4f0cd474
8 changed files with 36 additions and 15 deletions

View file

@ -34,9 +34,12 @@ module.exports = function (kibana) {
'leaflet'
),
constants: function (server, options) {
injectVars: function (server, options) {
let config = server.config();
return {
defaultAppId: options.defaultAppId
kbnIndex: config.get('kibana.index'),
esShardTimeout: config.get('elasticsearch.shardTimeout')
};
}
}

View file

@ -49,3 +49,14 @@ require('ui/chrome')
courier.start();
});
});
var vars = require('ui/chrome').getInjectedVars();
require('ui/modules').get('kibana')
.constant('kbnIndex', vars.kbnIndex)
.constant('esShardTimeout', vars.esShardTimeout)
.constant('esUrl', (function () {
var a = document.createElement('a');
a.href = '/elasticsearch';
return a.href;
}()));

View file

@ -47,6 +47,12 @@ module.exports = class Plugin {
let register = (server, options, next) => {
this.server = server;
// bind the server and options to all
// apps created by this plugin
for (let app of this.apps) {
app.getInjectedVars = _.partial(app.getInjectedVars, server, options);
}
server.log(['plugins', 'debug'], {
tmpl: 'Initializing plugin <%= plugin.id %>',
plugin: this

View file

@ -19,7 +19,12 @@ class UiApp {
this.hidden = this.spec.hidden;
this.autoloadOverrides = this.spec.autoload;
this.templateName = this.spec.templateName || 'uiApp';
// once this resolves, no reason to run it again
this.getModules = _.once(this.getModules);
// variables that are injected into the browser, must serialize to JSON
this.getInjectedVars = this.spec.injectVars || _.noop;
}
getModules() {

View file

@ -13,6 +13,8 @@ class UiExports {
}
consumePlugin(plugin) {
plugin.apps = new UiAppCollection(this);
var types = _.keys(plugin.uiExportsSpecs);
if (!types) return false;
@ -46,7 +48,8 @@ class UiExports {
case 'apps':
return (plugin, specs) => {
for (let spec of [].concat(specs || [])) {
this.apps.new(_.defaults({}, spec, { id: plugin.id }));
let app = this.apps.new(_.defaults({}, spec, { id: plugin.id }));
plugin.apps.add(app);
}
};

View file

@ -84,8 +84,7 @@ module.exports = async (kbnServer, server, config) => {
buildSha: _.get(kbnServer, 'build.sha', '@@buildSha'),
buildNumber: _.get(kbnServer, 'build.number', '@@buildNum'),
cacheBust: _.get(kbnServer, 'build.number', ''),
kbnIndex: config.get('kibana.index'),
esShardTimeout: config.get('elasticsearch.shardTimeout'),
vars: app.getInjectedVars()
};
return this.view(app.templateName, {

View file

@ -6,21 +6,11 @@ module.exports = function (chrome, internals) {
chrome.setupAngular = function () {
var kibana = modules.get('kibana');
var esUrl = (function () {
var a = document.createElement('a');
a.href = '/elasticsearch';
return a.href;
}());
kibana
.constant('kbnVersion', internals.version)
.constant('buildNum', internals.buildNumber)
.constant('kbnIndex', internals.kbnIndex)
.constant('esShardTimeout', internals.esShardTimeout)
.constant('esUrl', esUrl)
.constant('commitSha', internals.buildSha)
.constant('cacheBust', internals.cacheBust)
.constant('minimumElasticsearchVersion', '2.0.0')
.constant('sessionId', Date.now())
.directive('kbnChrome', function ($rootScope) {
return {

View file

@ -31,4 +31,8 @@ module.exports = function (chrome, internals) {
return internals.app.id;
};
chrome.getInjectedVars = function () {
return internals.vars;
};
};