[PATCH V2] [Wok 0/3] Implement Asynchronous Notifications frontend

Changes in V2: - replaced wok.notification by wok.message.notify Lucio Correia (3): Implement notifications JS API Implement wok.message.notify Implement notifications loop ui/js/src/wok.api.js | 18 ++++++++++++++++++ ui/js/src/wok.main.js | 5 +++++ ui/js/src/wok.message.js | 8 ++++++-- ui/js/src/wok.utils.js | 19 +++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) -- 1.9.1

Signed-off-by: Lucio Correia <luciojhc@linux.vnet.ibm.com> --- ui/js/src/wok.api.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ui/js/src/wok.api.js b/ui/js/src/wok.api.js index 24ea495..56e90ed 100644 --- a/ui/js/src/wok.api.js +++ b/ui/js/src/wok.api.js @@ -59,6 +59,24 @@ var wok = { }); }, + getNotifications: function (suc, err) { + wok.requestJSON({ + url: 'notifications', + type : 'GET', + dataType : 'json', + success : suc, + error: err + }); + }, + + removeNotification: function (id) { + wok.requestJSON({ + url: 'notifications/' + id, + type : 'DELETE', + dataType : 'json', + }); + }, + login : function(settings, suc, err) { $.ajax({ url : "login", -- 1.9.1

* add onclick parameter to wok.message for handling click on close button * create wok.message.notify to show notifications Signed-off-by: Lucio Correia <luciojhc@linux.vnet.ibm.com> --- ui/js/src/wok.message.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ui/js/src/wok.message.js b/ui/js/src/wok.message.js index b74d188..e4c2ebd 100644 --- a/ui/js/src/wok.message.js +++ b/ui/js/src/wok.message.js @@ -18,7 +18,7 @@ * limitations under the License. */ -wok.message = function(msg, level, node, closeable) { +wok.message = function(msg, level, node, closeable, onclick) { "use strict"; var container = node || $('#alert-fields'); if ($(container).size() < 1) { @@ -26,7 +26,7 @@ wok.message = function(msg, level, node, closeable) { } var message = '<div role="alert" class="alert ' + (level || '') + ' alert-dismissible fade in" style="display: none;">'; if(!node || closeable) { - message += '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true"><i class="fa fa-times-circle"></i></span></button>'; + message += '<button type="button" class="close" data-dismiss="alert" aria-label="Close" onclick="' + (onclick || '') + '"><span aria-hidden="true"><i class="fa fa-times-circle"></i></span></button>'; } message += msg; message += '</div>'; @@ -66,3 +66,7 @@ wok.message.success = function(msg, node, closeable) { "use strict"; wok.message(msg, 'alert-success', node, closeable); }; +wok.message.notify = function(notification, node) { + "use strict"; + wok.message(notification.message, 'alert-warning', node, true, "wok.removeNotification('" + notification.code + "')"); +}; -- 1.9.1

Check for new notifications each 5 seconds. Duplicated notifications are controlled by postedNotifications var. Signed-off-by: Lucio Correia <luciojhc@linux.vnet.ibm.com> --- ui/js/src/wok.main.js | 5 +++++ ui/js/src/wok.utils.js | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/ui/js/src/wok.main.js b/ui/js/src/wok.main.js index 804c12c..2146b3d 100644 --- a/ui/js/src/wok.main.js +++ b/ui/js/src/wok.main.js @@ -18,6 +18,8 @@ * limitations under the License. */ +wok.NOTIFICATION_INTERVAL = 5000 +wok.postedNotifications = [] wok.tabMode = {}; wok.config = undefined; @@ -296,6 +298,7 @@ wok.main = function() { * 5) About button click event * 6) Help button click event * 7) Peers button click event + * 8) Start notifications loop */ var searchingPeers = false; var initListeners = function() { @@ -466,6 +469,8 @@ wok.main = function() { wok.message.error(data.responseJSON.reason); } ); + + setTimeout(wok.notificationsLoop, wok.NOTIFICATION_INTERVAL); }; diff --git a/ui/js/src/wok.utils.js b/ui/js/src/wok.utils.js index e5701e6..214749a 100644 --- a/ui/js/src/wok.utils.js +++ b/ui/js/src/wok.utils.js @@ -193,3 +193,22 @@ wok.urlSafeB64Decode = function(str) { wok.urlSafeB64Encode = function(str) { return $.base64.btoa(str, true).replace(/\+/g, '-').replace(/\//g, '_'); } + +wok.notificationsLoop = function notificationsLoop() { + wok.getNotifications( + function(notifications){ + if(notifications && notifications.length > 0) { + $.each(notifications, function(i, notif) { + if (wok.postedNotifications.indexOf(notif.message) == -1) { + wok.message.notify(notif, '#message-container-area'); + wok.postedNotifications.push(notif.message); + } + }) + }; + setTimeout(notificationsLoop, wok.NOTIFICATION_INTERVAL); + }, + function(data){ + setTimeout(notificationsLoop, wok.NOTIFICATION_INTERVAL); + } + ); +} -- 1.9.1
participants (2)
-
Aline Manera
-
Lucio Correia