| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- define([
- 'angular',
- 'lodash',
- 'kbn',
- 'moment',
- 'jquery',
- ],
- function (angular, _, kbn, moment, $) {
- "use strict";
- var module = angular.module('grafana.routes');
- module.controller('DashFromDBCtrl', function($scope, $routeParams, backendSrv) {
- function dashboardLoadFailed(title) {
- $scope.initDashboard({meta: {}, model: {title: title}}, $scope);
- }
- if (!$routeParams.slug) {
- backendSrv.get('/api/dashboards/home').then(function(result) {
- var meta = result.meta;
- meta.canSave = meta.canShare = meta.canEdit = meta.canStar = false;
- $scope.initDashboard(result, $scope);
- },function() {
- dashboardLoadFailed('Not found');
- });
- return;
- }
- return backendSrv.getDashboard($routeParams.slug).then(function(result) {
- $scope.initDashboard(result, $scope);
- }, function() {
- dashboardLoadFailed('Not found');
- });
- });
- module.controller('DashFromSnapshotCtrl', function($scope, $routeParams, backendSrv) {
- backendSrv.get('/api/snapshots/' + $routeParams.key).then(function(result) {
- $scope.initDashboard(result, $scope);
- }, function() {
- $scope.initDashboard({
- meta: {
- isSnapshot: true,
- canSave: false,
- canEdit: false,
- },
- model: {
- title: 'Snapshot not found'
- }
- }, $scope);
- });
- });
- module.controller('DashFromImportCtrl', function($scope, $location, alertSrv) {
- if (!window.grafanaImportDashboard) {
- alertSrv.set('Not found', 'Cannot reload page with unsaved imported dashboard', 'warning', 7000);
- $location.path('');
- return;
- }
- $scope.initDashboard({meta: {}, model: window.grafanaImportDashboard }, $scope);
- });
- module.controller('NewDashboardCtrl', function($scope) {
- $scope.initDashboard({
- meta: {},
- model: {
- title: "New dashboard",
- rows: [{ height: '250px', panels:[] }]
- },
- }, $scope);
- });
- module.controller('DashFromFileCtrl', function($scope, $rootScope, $http, $routeParams) {
- var file_load = function(file) {
- return $http({
- url: "public/dashboards/"+file.replace(/\.(?!json)/,"/")+'?' + new Date().getTime(),
- method: "GET",
- transformResponse: function(response) {
- return angular.fromJson(response);
- }
- }).then(function(result) {
- if(!result) {
- return false;
- }
- return result.data;
- },function() {
- $scope.appEvent('alert-error', ["Dashboard load failed", "Could not load <i>dashboards/"+file+"</i>. Please make sure it exists"]);
- return false;
- });
- };
- file_load($routeParams.jsonFile).then(function(result) {
- $scope.initDashboard({meta: {fromFile: true}, model: result}, $scope);
- });
- });
- module.controller('DashFromScriptCtrl', function($scope, $rootScope, $http, $routeParams, $q, dashboardSrv, datasourceSrv, $timeout) {
- var execute_script = function(result) {
- var services = {
- dashboardSrv: dashboardSrv,
- datasourceSrv: datasourceSrv,
- $q: $q,
- };
- /*jshint -W054 */
- var script_func = new Function('ARGS','kbn','_','moment','window','document','$','jQuery', 'services', result.data);
- var script_result = script_func($routeParams, kbn, _ , moment, window, document, $, $, services);
- // Handle async dashboard scripts
- if (_.isFunction(script_result)) {
- var deferred = $q.defer();
- script_result(function(dashboard) {
- $timeout(function() {
- deferred.resolve({ data: dashboard });
- });
- });
- return deferred.promise;
- }
- return { data: script_result };
- };
- var script_load = function(file) {
- var url = 'public/dashboards/'+file.replace(/\.(?!js)/,"/") + '?' + new Date().getTime();
- return $http({ url: url, method: "GET" })
- .then(execute_script)
- .then(null,function(err) {
- console.log('Script dashboard error '+ err);
- $scope.appEvent('alert-error', ["Script Error", "Please make sure it exists and returns a valid dashboard"]);
- return false;
- });
- };
- script_load($routeParams.jsFile).then(function(result) {
- $scope.initDashboard({meta: {fromScript: true, canDelete: false}, model: result.data}, $scope);
- });
- });
- });
|