summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2015-01-07 14:51:20 +0100
committerMorris Jobke <hey@morrisjobke.de>2015-01-08 13:06:45 +0100
commit8e1904386fe75c77675c882e2d3eca34d5828cec (patch)
tree157973fb67ae3cd5b6846f94db7ec685d6366625 /core
parent14e534e93322d3ce3275f04657de40fcf34d61d9 (diff)
downloadnextcloud-server-8e1904386fe75c77675c882e2d3eca34d5828cec.tar.gz
nextcloud-server-8e1904386fe75c77675c882e2d3eca34d5828cec.zip
Add timeout for notifications
* options for timeout in seconds and if it contains HTML * if timeout is 0 it will show the message permanently * removes the notification after a given (default: 5 seconds) timeframe * based on work by @brantje * provide JS unit tests for notifications
Diffstat (limited to 'core')
-rw-r--r--core/js/js.js37
-rw-r--r--core/js/tests/specs/coreSpec.js97
2 files changed, 134 insertions, 0 deletions
diff --git a/core/js/js.js b/core/js/js.js
index 8bcd546b420..579bf09b2e4 100644
--- a/core/js/js.js
+++ b/core/js/js.js
@@ -669,6 +669,7 @@ OC.msg={
OC.Notification={
queuedNotifications: [],
getDefaultNotificationFunction: null,
+ notificationTimer: 0,
/**
* @param callback
@@ -731,6 +732,42 @@ OC.Notification={
}
},
+
+ /**
+ * Shows a notification that disappears after x seconds, default is
+ * 7 seconds
+ * @param {string} text Message to show
+ * @param {array} [options] options array
+ * @param {int} [options.timeout=7] timeout in seconds, if this is 0 it will show the message permanently
+ * @param {boolean} [options.isHTML=false] an indicator for HTML notifications (true) or text (false)
+ */
+ showTemporary: function(text, options) {
+ var defaults = {
+ isHTML: false,
+ timeout: 7
+ },
+ options = options || {};
+ // merge defaults with passed in options
+ _.defaults(options, defaults);
+
+ // clear previous notifications
+ OC.Notification.hide();
+ if(OC.Notification.notificationTimer) {
+ clearTimeout(OC.Notification.notificationTimer);
+ }
+
+ if(options.isHTML) {
+ OC.Notification.showHtml(text);
+ } else {
+ OC.Notification.show(text);
+ }
+
+ if(options.timeout > 0) {
+ // register timeout to vanish notification
+ OC.Notification.notificationTimer = setTimeout(OC.Notification.hide, (options.timeout * 1000));
+ }
+ },
+
/**
* Returns whether a notification is hidden.
* @return {boolean}
diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js
index 08395f4d4c2..d283839d7e7 100644
--- a/core/js/tests/specs/coreSpec.js
+++ b/core/js/tests/specs/coreSpec.js
@@ -686,5 +686,102 @@ describe('Core base tests', function() {
expect(obj.attached).not.toBeDefined();
});
});
+ describe('Notifications', function() {
+ beforeEach(function(){
+ notificationMock = sinon.mock(OC.Notification);
+ });
+ afterEach(function(){
+ // verify that all expectations are met
+ notificationMock.verify();
+ // restore mocked methods
+ notificationMock.restore();
+ // clean up the global variable
+ delete notificationMock;
+ });
+ it('Should show a plain text notification' , function() {
+ // one is shown ...
+ notificationMock.expects('show').once().withExactArgs('My notification test');
+ // ... but not the HTML one
+ notificationMock.expects('showHtml').never();
+
+ OC.Notification.showTemporary('My notification test');
+
+ // verification is done in afterEach
+ });
+ it('Should show a HTML notification' , function() {
+ // no plain is shown ...
+ notificationMock.expects('show').never();
+ // ... but one HTML notification
+ notificationMock.expects('showHtml').once().withExactArgs('<a>My notification test</a>');
+
+ OC.Notification.showTemporary('<a>My notification test</a>', { isHTML: true });
+
+ // verification is done in afterEach
+ });
+ it('Should hide previous notification and hide itself after 7 seconds' , function() {
+ var clock = sinon.useFakeTimers();
+
+ // previous notifications get hidden
+ notificationMock.expects('hide').once();
+
+ OC.Notification.showTemporary('');
+
+ // verify the first call
+ notificationMock.verify();
+
+ // expect it a second time
+ notificationMock.expects('hide').once();
+
+ // travel in time +7000 milliseconds
+ clock.tick(7000);
+
+ // verification is done in afterEach
+ });
+ it('Should hide itself after a given time' , function() {
+ var clock = sinon.useFakeTimers();
+
+ // previous notifications get hidden
+ notificationMock.expects('hide').once();
+
+ OC.Notification.showTemporary('', { timeout: 10 });
+
+ // verify the first call
+ notificationMock.verify();
+
+ // expect to not be called after 9 seconds
+ notificationMock.expects('hide').never();
+
+ // travel in time +9 seconds
+ clock.tick(9000);
+ // verify this
+ notificationMock.verify();
+
+ // expect the second call one second later
+ notificationMock.expects('hide').once();
+ // travel in time +1 seconds
+ clock.tick(1000);
+
+ // verification is done in afterEach
+ });
+ it('Should not hide itself after a given time if a timeout of 0 is defined' , function() {
+ var clock = sinon.useFakeTimers();
+
+ // previous notifications get hidden
+ notificationMock.expects('hide').once();
+
+ OC.Notification.showTemporary('', { timeout: 0 });
+
+ // verify the first call
+ notificationMock.verify();
+
+ // expect to not be called after 1000 seconds
+ notificationMock.expects('hide').never();
+
+ // travel in time +1000 seconds
+ clock.tick(1000000);
+
+ // verification is done in afterEach
+ });
+ });
});