From ea4f0cd47418e83be080f6dd5a254b6a16e24fb5 Mon Sep 17 00:00:00 2001 From: spalger Date: Mon, 10 Aug 2015 14:15:31 -0700 Subject: [PATCH] allow apps to push variables from the server to the client --- src/plugins/kibana/index.js | 7 +++++-- src/plugins/kibana/public/kibana.js | 11 +++++++++++ src/server/plugins/Plugin.js | 6 ++++++ src/ui/UiApp.js | 5 +++++ src/ui/UiExports.js | 5 ++++- src/ui/index.js | 3 +-- src/ui/public/chrome/api/angular.js | 10 ---------- src/ui/public/chrome/api/apps.js | 4 ++++ 8 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/plugins/kibana/index.js b/src/plugins/kibana/index.js index 34f9a8246152..eab524a31695 100644 --- a/src/plugins/kibana/index.js +++ b/src/plugins/kibana/index.js @@ -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') }; } } diff --git a/src/plugins/kibana/public/kibana.js b/src/plugins/kibana/public/kibana.js index 687edb367895..d62fb4007688 100644 --- a/src/plugins/kibana/public/kibana.js +++ b/src/plugins/kibana/public/kibana.js @@ -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; +}())); + diff --git a/src/server/plugins/Plugin.js b/src/server/plugins/Plugin.js index 702e4275a1ca..9f8eced5271e 100644 --- a/src/server/plugins/Plugin.js +++ b/src/server/plugins/Plugin.js @@ -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 diff --git a/src/ui/UiApp.js b/src/ui/UiApp.js index 686c8cb314b4..dd5e45bbb1f2 100644 --- a/src/ui/UiApp.js +++ b/src/ui/UiApp.js @@ -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() { diff --git a/src/ui/UiExports.js b/src/ui/UiExports.js index 4c1ea9812961..c314a67378f2 100644 --- a/src/ui/UiExports.js +++ b/src/ui/UiExports.js @@ -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); } }; diff --git a/src/ui/index.js b/src/ui/index.js index 5d03cd13091b..ef8e1a236d5f 100644 --- a/src/ui/index.js +++ b/src/ui/index.js @@ -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, { diff --git a/src/ui/public/chrome/api/angular.js b/src/ui/public/chrome/api/angular.js index ac30527a3b1d..544b9472dfda 100644 --- a/src/ui/public/chrome/api/angular.js +++ b/src/ui/public/chrome/api/angular.js @@ -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 { diff --git a/src/ui/public/chrome/api/apps.js b/src/ui/public/chrome/api/apps.js index 44280701b87d..8094d9d83113 100644 --- a/src/ui/public/chrome/api/apps.js +++ b/src/ui/public/chrome/api/apps.js @@ -31,4 +31,8 @@ module.exports = function (chrome, internals) { return internals.app.id; }; + chrome.getInjectedVars = function () { + return internals.vars; + }; + };