mirror of https://github.com/grafana/grafana
parent
35c853e906
commit
32a1d1445c
@ -0,0 +1,6 @@ |
||||
///<reference path="../headers/common.d.ts" />
|
||||
|
||||
import {Emitter} from './utils/emitter'; |
||||
|
||||
var appEvents = new Emitter(); |
||||
export default appEvents; |
@ -1,91 +0,0 @@ |
||||
define([ |
||||
'angular', |
||||
'lodash', |
||||
'../core_module', |
||||
], |
||||
function (angular, _, coreModule) { |
||||
'use strict'; |
||||
|
||||
coreModule.default.service('alertSrv', function($timeout, $sce, $rootScope, $modal, $q) { |
||||
var self = this; |
||||
|
||||
this.init = function() { |
||||
$rootScope.onAppEvent('alert-error', function(e, alert) { |
||||
self.set(alert[0], alert[1], 'error'); |
||||
}, $rootScope); |
||||
$rootScope.onAppEvent('alert-warning', function(e, alert) { |
||||
self.set(alert[0], alert[1], 'warning', 5000); |
||||
}, $rootScope); |
||||
$rootScope.onAppEvent('alert-success', function(e, alert) { |
||||
self.set(alert[0], alert[1], 'success', 3000); |
||||
}, $rootScope); |
||||
$rootScope.onAppEvent('confirm-modal', this.showConfirmModal, $rootScope); |
||||
}; |
||||
|
||||
// List of all alert objects
|
||||
this.list = []; |
||||
|
||||
this.set = function(title,text,severity,timeout) { |
||||
var newAlert = { |
||||
title: title || '', |
||||
text: text || '', |
||||
severity: severity || 'info', |
||||
}; |
||||
|
||||
var newAlertJson = angular.toJson(newAlert); |
||||
|
||||
// remove same alert if it already exists
|
||||
_.remove(self.list, function(value) { |
||||
return angular.toJson(value) === newAlertJson; |
||||
}); |
||||
|
||||
self.list.push(newAlert); |
||||
if (timeout > 0) { |
||||
$timeout(function() { |
||||
self.list = _.without(self.list,newAlert); |
||||
}, timeout); |
||||
} |
||||
|
||||
if (!$rootScope.$$phase) { |
||||
$rootScope.$digest(); |
||||
} |
||||
|
||||
return(newAlert); |
||||
}; |
||||
|
||||
this.clear = function(alert) { |
||||
self.list = _.without(self.list,alert); |
||||
}; |
||||
|
||||
this.clearAll = function() { |
||||
self.list = []; |
||||
}; |
||||
|
||||
this.showConfirmModal = function(e, payload) { |
||||
var scope = $rootScope.$new(); |
||||
|
||||
scope.title = payload.title; |
||||
scope.text = payload.text; |
||||
scope.text2 = payload.text2; |
||||
scope.onConfirm = payload.onConfirm; |
||||
scope.icon = payload.icon || "fa-check"; |
||||
scope.yesText = payload.yesText || "Yes"; |
||||
scope.noText = payload.noText || "Cancel"; |
||||
|
||||
var confirmModal = $modal({ |
||||
template: 'public/app/partials/confirm_modal.html', |
||||
persist: false, |
||||
modalClass: 'confirm-modal', |
||||
show: false, |
||||
scope: scope, |
||||
keyboard: false |
||||
}); |
||||
|
||||
$q.when(confirmModal).then(function(modalEl) { |
||||
modalEl.modal('show'); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
}); |
||||
}); |
@ -0,0 +1,99 @@ |
||||
///<reference path="../../headers/common.d.ts" />
|
||||
|
||||
import angular from 'angular'; |
||||
import _ from 'lodash'; |
||||
import $ from 'jquery'; |
||||
import coreModule from 'app/core/core_module'; |
||||
import appEvents from 'app/core/app_events'; |
||||
|
||||
export class AlertSrv { |
||||
list: any[]; |
||||
|
||||
/** @ngInject */ |
||||
constructor(private $timeout, private $sce, private $rootScope, private $modal) { |
||||
this.list = []; |
||||
} |
||||
|
||||
init() { |
||||
this.$rootScope.onAppEvent('alert-error', (e, alert) => { |
||||
this.set(alert[0], alert[1], 'error', 0); |
||||
}, this.$rootScope); |
||||
|
||||
this.$rootScope.onAppEvent('alert-warning', (e, alert) => { |
||||
this.set(alert[0], alert[1], 'warning', 5000); |
||||
}, this.$rootScope); |
||||
|
||||
this.$rootScope.onAppEvent('alert-success', (e, alert) => { |
||||
this.set(alert[0], alert[1], 'success', 3000); |
||||
}, this.$rootScope); |
||||
|
||||
appEvents.on('confirm-modal', this.showConfirmModal.bind(this)); |
||||
|
||||
this.$rootScope.onAppEvent('confirm-modal', (e, data) => { |
||||
this.showConfirmModal(data); |
||||
}, this.$rootScope); |
||||
} |
||||
|
||||
set(title, text, severity, timeout) { |
||||
var newAlert = { |
||||
title: title || '', |
||||
text: text || '', |
||||
severity: severity || 'info', |
||||
}; |
||||
|
||||
var newAlertJson = angular.toJson(newAlert); |
||||
|
||||
// remove same alert if it already exists
|
||||
_.remove(this.list, function(value) { |
||||
return angular.toJson(value) === newAlertJson; |
||||
}); |
||||
|
||||
this.list.push(newAlert); |
||||
if (timeout > 0) { |
||||
this.$timeout(() => { |
||||
this.list = _.without(this.list, newAlert); |
||||
}, timeout); |
||||
} |
||||
|
||||
if (!this.$rootScope.$$phase) { |
||||
this.$rootScope.$digest(); |
||||
} |
||||
|
||||
return(newAlert); |
||||
} |
||||
|
||||
clear(alert) { |
||||
this.list = _.without(this.list, alert); |
||||
} |
||||
|
||||
clearAll() { |
||||
this.list = []; |
||||
} |
||||
|
||||
showConfirmModal(payload) { |
||||
var scope = this.$rootScope.$new(); |
||||
|
||||
scope.title = payload.title; |
||||
scope.text = payload.text; |
||||
scope.text2 = payload.text2; |
||||
scope.onConfirm = payload.onConfirm; |
||||
scope.icon = payload.icon || "fa-check"; |
||||
scope.yesText = payload.yesText || "Yes"; |
||||
scope.noText = payload.noText || "Cancel"; |
||||
|
||||
var confirmModal = this.$modal({ |
||||
template: 'public/app/partials/confirm_modal.html', |
||||
persist: false, |
||||
modalClass: 'confirm-modal', |
||||
show: false, |
||||
scope: scope, |
||||
keyboard: false |
||||
}); |
||||
|
||||
confirmModal.then(function(modalEl) { |
||||
modalEl.modal('show'); |
||||
}); |
||||
} |
||||
} |
||||
|
||||
coreModule.service('alertSrv', AlertSrv); |
@ -0,0 +1,14 @@ |
||||
import {describe, beforeEach, it, sinon, expect} from 'test/lib/common'; |
||||
|
||||
import {GrafanaApp} from 'app/app'; |
||||
|
||||
describe('GrafanaApp', () => { |
||||
|
||||
var app = new GrafanaApp(); |
||||
|
||||
it('can call inits', () => { |
||||
expect(app).to.not.be(null); |
||||
}); |
||||
}); |
||||
|
||||
|
Loading…
Reference in new issue