Commit 12f487e2 by Torkel Ödegaard

feat(plugin-editors): more work on plugin editor loading

parent 30a8a434
......@@ -16,6 +16,8 @@ import "./directives/password_strenght";
import "./directives/spectrum_picker";
import "./directives/tags";
import "./directives/value_select_dropdown";
import "./directives/plugin_directive_loader";
import "./directives/rebuild_on_change";
import "./directives/give_focus";
import './jquery_extended';
import './partials';
......
///<reference path="../../headers/common.d.ts" />
import angular from 'angular';
import _ from 'lodash';
import coreModule from '../core_module';
function pluginDirectiveLoader($compile, datasourceSrv) {
function getPluginComponentDirective(options) {
return function() {
return {
templateUrl: options.Component.templateUrl,
restrict: 'E',
controller: options.Component,
controllerAs: 'ctrl',
bindToController: true,
scope: options.bindings,
link: (scope, elem, attrs, ctrl) => {
if (ctrl.link) {
ctrl.link(scope, elem, attrs, ctrl);
}
}
};
};
}
function getModule(scope, attrs) {
switch (attrs.type) {
case "metrics-query-editor":
let datasource = scope.target.datasource || scope.ctrl.panel.datasource;
return datasourceSrv.get(datasource).then(ds => {
if (!scope.target.refId) {
scope.target.refId = 'A';
}
return System.import(ds.meta.module).then(dsModule => {
return {
name: 'metrics-query-editor-' + ds.meta.id,
bindings: {target: "=", panelCtrl: "="},
attrs: {"target": "target", "panel-ctrl": "ctrl"},
Component: dsModule.MetricsQueryEditor
};
});
});
case 'datasource-config-view':
return System.import(scope.datasourceMeta.module).then(function(dsModule) {
return {
name: 'ds-config-' + scope.datasourceMeta.id,
bindings: {meta: "=", current: "="},
attrs: {meta: "datasourceMeta", current: "current"},
Component: dsModule.ConfigView,
};
});
}
}
function appendAndCompile(scope, elem, componentInfo) {
console.log('compile', elem, componentInfo);
var child = angular.element(document.createElement(componentInfo.name));
_.each(componentInfo.attrs, (value, key) => {
child.attr(key, value);
});
$compile(child)(scope);
elem.empty();
elem.append(child);
}
function registerPluginComponent(scope, elem, attrs, componentInfo) {
if (!componentInfo.Component.registered) {
var directiveName = attrs.$normalize(componentInfo.name);
var directiveFn = getPluginComponentDirective(componentInfo);
coreModule.directive(directiveName, directiveFn);
componentInfo.Component.registered = true;
}
appendAndCompile(scope, elem, componentInfo);
}
return {
restrict: 'E',
link: function(scope, elem, attrs) {
getModule(scope, attrs).then(function (componentInfo) {
registerPluginComponent(scope, elem, attrs, componentInfo);
});
}
};
}
coreModule.directive('pluginDirectiveLoader', pluginDirectiveLoader);
///<reference path="../../headers/common.d.ts" />
import angular from 'angular';
import _ from 'lodash';
import $ from 'jquery';
import coreModule from '../core_module';
function getBlockNodes(nodes) {
var node = nodes[0];
var endNode = nodes[nodes.length - 1];
var blockNodes;
for (var i = 1; node !== endNode && (node = node.nextSibling); i++) {
if (blockNodes || nodes[i] !== node) {
if (!blockNodes) {
blockNodes = $([].slice.call(nodes, 0, i));
}
blockNodes.push(node);
}
}
return blockNodes || nodes;
}
function rebuildOnChange($compile) {
return {
transclude: true,
priority: 600,
restrict: 'A',
link: function(scope, elem, attrs, ctrl, transclude) {
var childScope, previousElements;
var uncompiledHtml;
scope.$watch(attrs.rebuildOnChange, function rebuildOnChangeAction(value) {
if (childScope) {
childScope.$destroy();
childScope = null;
elem.empty();
}
if (value) {
if (!childScope) {
transclude(function(clone, newScope) {
childScope = newScope;
elem.append($compile(clone)(childScope));
});
}
}
});
}
};
}
coreModule.directive('rebuildOnChange', rebuildOnChange);
......@@ -41,7 +41,12 @@
<div class="clearfix"></div>
</div>
<ds-config-view ng-if="datasourceMeta.id" ds-meta="datasourceMeta" current="current"></ds-config-view>
<div rebuild-on-change="datasourceMeta.id">
<plugin-directive-loader type="datasource-config-view">
</plugin-directive-loader>
</div>
<!-- <ds&#45;config&#45;view ds&#45;meta="datasourceMeta" current="current"></ds&#45;config&#45;view> -->
<div ng-if="testing" style="margin-top: 25px">
<h5 ng-show="!testing.done">Testing.... <i class="fa fa-spiner fa-spin"></i></h5>
......
......@@ -50,7 +50,7 @@ var module = angular.module('grafana.directives');
module.directive('grafanaPanel', function() {
return {
restrict: 'E',
templateUrl: 'app/features/panel/partials/panel.html',
templateUrl: 'public/app/features/panel/partials/panel.html',
transclude: true,
scope: { ctrl: "=" },
link: function(scope, elem) {
......
......@@ -5,105 +5,6 @@ import _ from 'lodash';
var directivesModule = angular.module('grafana.directives');
function pluginDirectiveLoader($compile, datasourceSrv) {
function getPluginComponentDirective(options) {
return function() {
return {
templateUrl: options.Component.templateUrl,
restrict: 'E',
controller: options.Component,
controllerAs: 'ctrl',
bindToController: true,
scope: options.bindings,
link: (scope, elem, attrs, ctrl) => {
if (ctrl.link) {
ctrl.link(scope, elem, attrs, ctrl);
}
}
};
};
}
function getModule(scope, attrs) {
switch (attrs.type) {
case "metrics-query-editor": {
let datasource = scope.target.datasource || scope.ctrl.panel.datasource;
return datasourceSrv.get(datasource).then(ds => {
if (!scope.target.refId) {
scope.target.refId = 'A';
}
return System.import(ds.meta.module).then(dsModule => {
return {
name: 'metrics-query-editor-' + ds.meta.id,
bindings: {target: "=", panelCtrl: "="},
attrs: {"target": "target", "panel-ctrl": "ctrl"},
Component: dsModule.MetricsQueryEditor
};
});
});
}
}
}
function appendAndCompile(scope, elem, componentInfo) {
var child = angular.element(document.createElement(componentInfo.name));
_.each(componentInfo.attrs, (value, key) => {
child.attr(key, value);
});
$compile(child)(scope);
elem.empty();
elem.append(child);
}
function registerPluginComponent(scope, elem, attrs, componentInfo) {
if (!componentInfo.Component.registered) {
var directiveName = attrs.$normalize(componentInfo.name);
var directiveFn = getPluginComponentDirective(componentInfo);
directivesModule.directive(directiveName, directiveFn);
componentInfo.Component.registered = true;
}
appendAndCompile(scope, elem, componentInfo);
}
return {
restrict: 'E',
link: function(scope, elem, attrs) {
getModule(scope, attrs).then(function (componentInfo) {
registerPluginComponent(scope, elem, attrs, componentInfo);
});
}
};
}
/** @ngInject */
function metricsQueryEditor(dynamicDirectiveSrv, datasourceSrv) {
return dynamicDirectiveSrv.create({
watchPath: "ctrl.panel.datasource",
directive: scope => {
let datasource = scope.target.datasource || scope.ctrl.panel.datasource;
return datasourceSrv.get(datasource).then(ds => {
scope.datasource = ds;
if (!scope.target.refId) {
scope.target.refId = 'A';
}
return System.import(ds.meta.module).then(dsModule => {
return {
name: 'metrics-query-editor-' + ds.meta.id,
fn: dsModule.metricsQueryEditor,
};
});
});
}
});
}
/** @ngInject */
function metricsQueryOptions(dynamicDirectiveSrv, datasourceSrv) {
return dynamicDirectiveSrv.create({
......@@ -121,6 +22,4 @@ function metricsQueryOptions(dynamicDirectiveSrv, datasourceSrv) {
});
}
directivesModule.directive('pluginDirectiveLoader', pluginDirectiveLoader);
directivesModule.directive('metricsQueryEditor', metricsQueryEditor);
directivesModule.directive('metricsQueryOptions', metricsQueryOptions);
......@@ -23,11 +23,16 @@ function (GraphiteDatasource) {
return {templateUrl: 'public/app/plugins/datasource/graphite/partials/config.html'};
}
function ConfigView() {
}
ConfigView.templateUrl = 'public/app/plugins/datasource/graphite/partials/config.html';
return {
Datasource: GraphiteDatasource,
configView: configView,
annotationsQueryEditor: annotationsQueryEditor,
metricsQueryEditor: metricsQueryEditor,
metricsQueryOptions: metricsQueryOptions,
ConfigView: ConfigView
};
});
declare var Datasource: any;
export default Datasource;
define([
'./datasource',
],
function (PromDatasource) {
'use strict';
function metricsQueryEditor() {
return {controller: 'PrometheusQueryCtrl', templateUrl: 'public/app/plugins/datasource/prometheus/partials/query.editor.html'};
}
function configView() {
return {templateUrl: 'public/app/plugins/datasource/prometheus/partials/config.html'};
}
return {
Datasource: PromDatasource,
metricsQueryEditor: metricsQueryEditor,
configView: configView,
};
});
import {PrometheusDatasource} from './datasource';
import {PrometheusQueryCtrl} from './query_ctrl';
// function metricsQueryEditor() {
// return {controller: 'PrometheusQueryCtrl', templateUrl: 'public/app/plugins/datasource/prometheus/partials/query.editor.html'};
// }
//
// function configView() {
// return {templateUrl: ''};
// }
class PrometheusConfigViewCtrl {
static templateUrl = 'public/app/plugins/datasource/prometheus/partials/config.html';
}
export {
PrometheusDatasource as Datasource,
PrometheusQueryCtrl as MetricsQueryEditor,
PrometheusConfigViewCtrl as ConfigView
};
define([
'angular',
'lodash',
],
function (angular, _) {
'use strict';
///<reference path="../../../headers/common.d.ts" />
var module = angular.module('grafana.controllers');
import angular from 'angular';
import _ from 'lodash';
import moment from 'moment';
module.controller('PrometheusQueryCtrl', function($scope, templateSrv) {
$scope.panelCtrl = $scope.ctrl;
$scope.panel = $scope.panelCtrl.panel;
import * as dateMath from 'app/core/utils/datemath';
$scope.init = function() {
var target = $scope.target;
function PrometheusQueryCtrl($scope, templateSrv) {
$scope.panelCtrl = $scope.ctrl;
$scope.panel = $scope.panelCtrl.panel;
target.expr = target.expr || '';
target.intervalFactor = target.intervalFactor || 2;
$scope.init = function() {
var target = $scope.target;
$scope.metric = '';
$scope.resolutions = _.map([1,2,3,4,5,10], function(f) {
return {factor: f, label: '1/' + f};
});
target.expr = target.expr || '';
target.intervalFactor = target.intervalFactor || 2;
$scope.$on('typeahead-updated', function() {
$scope.$apply($scope.inputMetric);
$scope.refreshMetricData();
});
};
$scope.metric = '';
$scope.resolutions = _.map([1,2,3,4,5,10], function(f) {
return {factor: f, label: '1/' + f};
});
$scope.refreshMetricData = function() {
if (!_.isEqual($scope.oldTarget, $scope.target)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.paneCtrl.refresh();
}
};
$scope.$on('typeahead-updated', function() {
$scope.$apply($scope.inputMetric);
$scope.refreshMetricData();
});
};
$scope.inputMetric = function() {
$scope.target.expr += $scope.target.metric;
$scope.metric = '';
};
$scope.refreshMetricData = function() {
if (!_.isEqual($scope.oldTarget, $scope.target)) {
$scope.oldTarget = angular.copy($scope.target);
$scope.paneCtrl.refresh();
}
};
$scope.suggestMetrics = function(query, callback) {
$scope.datasource
.performSuggestQuery(query)
.then(callback);
};
$scope.inputMetric = function() {
$scope.target.expr += $scope.target.metric;
$scope.metric = '';
};
$scope.suggestMetrics = function(query, callback) {
$scope.datasource
.performSuggestQuery(query)
.then(callback);
};
$scope.linkToPrometheus = function() {
var range = Math.ceil(($scope.range.to.valueOf() - $scope.range.from.valueOf()) / 1000);
var endTime = $scope.range.to.utc().format('YYYY-MM-DD HH:mm');
var expr = {
expr: templateSrv.replace($scope.target.expr, $scope.panel.scopedVars),
range_input: range + 's',
end_input: endTime,
step_input: '',
stacked: $scope.panel.stack,
tab: 0
};
var hash = encodeURIComponent(JSON.stringify([expr]));
return $scope.datasource.directUrl + '/graph#' + hash;
$scope.linkToPrometheus = function() {
var range = Math.ceil(($scope.range.to.valueOf() - $scope.range.from.valueOf()) / 1000);
var endTime = $scope.range.to.utc().format('YYYY-MM-DD HH:mm');
var expr = {
expr: templateSrv.replace($scope.target.expr, $scope.panel.scopedVars),
range_input: range + 's',
end_input: endTime,
step_input: '',
stacked: $scope.panel.stack,
tab: 0
};
var hash = encodeURIComponent(JSON.stringify([expr]));
return $scope.datasource.directUrl + '/graph#' + hash;
};
$scope.init();
});
$scope.init();
}
});
export {PrometheusQueryCtrl};
......@@ -59,7 +59,6 @@
<script src="[[.AppSubUrl]]/public/vendor/npm/es5-shim/es5-shim.js"></script>
<script src="[[.AppSubUrl]]/public/vendor/npm/es6-shim/es6-shim.js"></script>
<script src="[[.AppSubUrl]]/public/vendor/npm/es6-promise/dist/es6-promise.js"></script>
<script src="[[.AppSubUrl]]/public/vendor/npm/systemjs/dist/system-polyfills.js"></script>
<script src="[[.AppSubUrl]]/public/vendor/npm/systemjs/dist/system.src.js"></script>
<script src="[[.AppSubUrl]]/public/app/system.conf.js"></script>
<script src="[[.AppSubUrl]]/public/app/boot.js"></script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment