Angular alert service - add auto dismiss

This patch auto dismisses 'info' and 'success' messages for
the Angular toast service.  It uses settings defined in
conf.js in the framework directory, which were copied over
from settings.py. We've copied over those settings
because we don't want to mix client-side settings with
other settings.  This is a first step in that goal.

To test:
1. Insert 'horizon.framework.widgets.toast.service'
as dependency (for example, into launch-instance-model.service.js)
3. Put several toastService.add(<type>, <message>);
and see that they are dismissed.
4. Also, change the auto_fade_alerts setting in conf.js
to see it adjust accordingly.

Partially Implements: blueprint launch-instance-redesign
Change-Id: I848c8e495f1a360aecc3a7353011b69aaead79e8
This commit is contained in:
Cindy Lu 2015-08-31 14:16:54 -07:00
parent 38b4be52d4
commit 4e12da4ab6
6 changed files with 38 additions and 4 deletions

View File

@ -32,5 +32,9 @@
speed: 0.8,
trail: 50
}
})
.value('horizon.framework.conf.toastOptions', {
'delay': 3000,
'dimissible': ['alert-success', 'alert-info']
});
})();

View File

@ -35,7 +35,10 @@
.module('horizon.framework.widgets.toast')
.factory('horizon.framework.widgets.toast.service', toastService);
function toastService() {
toastService.$inject = ['$timeout',
'horizon.framework.conf.toastOptions'];
function toastService($timeout, toastOptions) {
var toasts = [];
var service = {
@ -75,6 +78,18 @@
}
}
function autoDismiss(toast) {
$timeout(function dismiss() {
var index = toasts.indexOf(toast);
var dismissible = toastOptions.dimissible.indexOf('alert-' + toast.type);
// check if the toast exists and if it is dismissible (by checking
// the toastOptions config), then we remove it after a delay
if (index > -1 && dismissible > -1) {
toasts.splice(index, 1);
}
}, toastOptions.delay);
}
/**
* Remove a single toast.
*/
@ -92,6 +107,7 @@
msg: msg,
cancel: cancel
};
autoDismiss(toast);
toasts.push(toast);
}

View File

@ -24,9 +24,7 @@
describe('toast factory', function() {
var $compile,
$scope,
service;
var $compile, $scope, $timeout, service;
var successMsg = "I am success.";
var dangerMsg = "I am danger.";
@ -38,6 +36,7 @@
service = $injector.get('horizon.framework.widgets.toast.service');
$scope = $injector.get('$rootScope').$new();
$compile = $injector.get('$compile');
$timeout = $injector.get('$timeout');
}));
it('should create different toasts', function() {
@ -52,6 +51,18 @@
expect(service.get()[2].msg).toBe(infoMsg);
});
it('should dismiss specific toasts after a delay', function() {
service.add('danger', dangerMsg);
service.add('success', successMsg);
service.add('info', infoMsg);
expect(service.get().length).toBe(3);
$timeout.flush();
expect(service.get().length).toBe(1);
expect(service.get()[0].type).toBe('danger');
});
it('should provide a function to clear all toasts', function() {
service.add('success', successMsg);
service.add('success', successMsg);

View File

@ -37,6 +37,7 @@
beforeEach(module('horizon.framework.util.http'));
beforeEach(module('horizon.framework.util.i18n'));
beforeEach(module('horizon.framework.conf'));
beforeEach(module('horizon.framework.widgets.toast'));
beforeEach(module('horizon.app.core.openstack-service-api'));

View File

@ -33,6 +33,7 @@
///////////////////////
beforeEach(module('horizon.framework.conf'));
beforeEach(module('horizon.framework.util.http'));
beforeEach(module('horizon.framework.widgets.toast'));
beforeEach(module('horizon.app.core.openstack-service-api'));

View File

@ -80,6 +80,7 @@
////////////////
beforeEach(module('horizon.framework.conf'));
beforeEach(module('horizon.framework.util.http'));
beforeEach(module('horizon.framework.widgets.toast'));
beforeEach(module('horizon.app.core.openstack-service-api'));