From 8bd9ae34eb60c5bc2feb546d1473156779f566e9 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Thu, 30 Jan 2014 21:53:18 +0400 Subject: [PATCH] Added mozilla notifications support, added search clear, fixed minor bugs --- app/css/app.css | 9 ++++++- app/index.html | 6 ++--- app/js/app.js | 2 +- app/js/directives.js | 2 +- app/js/services.js | 57 ++++++++++++++++++++++++++------------- app/partials/im.html | 3 +++ app/partials/message.html | 4 +-- 7 files changed, 57 insertions(+), 26 deletions(-) diff --git a/app/css/app.css b/app/css/app.css index 7c39e8aa..340ecb8d 100644 --- a/app/css/app.css +++ b/app/css/app.css @@ -400,6 +400,14 @@ fieldset[disabled] .btn-tg.active { background-color: #FFF; } +.im_dialogs_search_clear { + position: absolute; + margin-left: 260px; + margin-top: -25px; + color: #999; + font-size: 15px; +} + .im_dialogs_wrap { @@ -890,7 +898,6 @@ div.im_message_video_thumb { } div.im_message_author, div.im_message_body { - border-radius: 2px; display: block; overflow: hidden; } diff --git a/app/index.html b/app/index.html index 83b32f6e..688f5517 100644 --- a/app/index.html +++ b/app/index.html @@ -7,7 +7,7 @@ - + @@ -52,10 +52,10 @@ - + - + diff --git a/app/js/app.js b/app/js/app.js index d734b1bd..480af2a2 100644 --- a/app/js/app.js +++ b/app/js/app.js @@ -55,7 +55,7 @@ config(['$locationProvider', '$routeProvider', '$compileProvider', function($loc // $locationProvider.html5Mode(true); $routeProvider.when('/', {templateUrl: 'partials/welcome.html?2', controller: 'AppWelcomeController'}); $routeProvider.when('/login', {templateUrl: 'partials/login.html?2', controller: 'AppLoginController'}); - $routeProvider.when('/im', {templateUrl: 'partials/im.html?5', controller: 'AppIMController', reloadOnSearch: false}); + $routeProvider.when('/im', {templateUrl: 'partials/im.html?6', controller: 'AppIMController', reloadOnSearch: false}); $routeProvider.otherwise({redirectTo: '/'}); }]); diff --git a/app/js/directives.js b/app/js/directives.js index 49bb55e6..3fd4b8c2 100644 --- a/app/js/directives.js +++ b/app/js/directives.js @@ -25,7 +25,7 @@ angular.module('myApp.directives', ['myApp.filters']) restrict: 'AE', scope: true, translude: false, - templateUrl: 'partials/message.html?5' + templateUrl: 'partials/message.html?6' }; }) diff --git a/app/js/services.js b/app/js/services.js index b4e50d3e..82248291 100644 --- a/app/js/services.js +++ b/app/js/services.js @@ -1013,6 +1013,7 @@ angular.module('myApp.services', []) notification.message = notificationMessage; notification.image = notificationPhoto.placeholder; + notification.key = 'msg' + message.id; if (notificationPhoto.location) { MtpApiFileManager.downloadSmallFile(notificationPhoto.location, notificationPhoto.size).then(function (url) { @@ -1117,6 +1118,8 @@ angular.module('myApp.services', []) if (foundDialog) { dialogsUpdated[peerID] = --foundDialog[0].unread_count; } + + NotificationsManager.cancel('msg' + messageID); } } } @@ -1917,8 +1920,10 @@ angular.module('myApp.services', []) .service('NotificationsManager', function ($rootScope, $window, $timeout, $interval, MtpApiManager, AppPeersManager, IdleManager) { - var notificationsUiSupport = window.webkitNotifications !== undefined; - var notificationsShown = []; + var notificationsUiSupport = 'Notification' in window; + var notificationsShown = {}; + // var lastClosed = []; + var notificationIndex = 0; var notificationsCount = 0; var peerSettings = {}; var faviconEl = $('link[rel="icon"]'); @@ -1956,6 +1961,7 @@ angular.module('myApp.services', []) return { start: start, notify: notify, + cancel: notificationCancel, getPeerSettings: getPeerSettings }; @@ -1981,25 +1987,23 @@ angular.module('myApp.services', []) return false; } - var havePermission = window.webkitNotifications.checkPermission(); - // console.log('perm', havePermission); - - if (havePermission != 0) { // 0 is PERMISSION_ALLOWED + if (Notification.permission !== 'granted' && Notification.permission !== 'denied') { $($window).on('click', requestPermission); } + try { $($window).on('beforeunload', notificationsClear); } catch (e) {} } function requestPermission() { - window.webkitNotifications.requestPermission(); + Notification.requestPermission(); $($window).off('click', requestPermission); } function notify (data) { - // console.log('notify', $rootScope.idle.isIDLE); + // console.log('notify', $rootScope.idle.isIDLE, notificationsUiSupport); if (!$rootScope.idle.isIDLE) { return false; } @@ -2007,15 +2011,17 @@ angular.module('myApp.services', []) notificationsCount++; if (!notificationsUiSupport || - window.webkitNotifications.checkPermission() != 0) { + Notification.permission !== 'granted') { return false; } - var notification = window.webkitNotifications.createNotification( - data.image || '', - data.title || '', - data.message || '' - ); + var idx = ++notificationIndex, + key = data.key || 'k' + idx; + + var notification = new Notification(data.title, { + icon: data.image || '', + body: data.message || '' + }); notification.onclick = function () { notification.close(); @@ -2026,24 +2032,39 @@ angular.module('myApp.services', []) } }; + notification.onclose = function () { + delete notificationsShown[key]; + // lastClosed.push(+new Date()); + notificationsClear(); + }; + // console.log('notify', notification); notification.show(); - notificationsShown.push(notification); + notificationsShown[key] = notification; }; + function notificationCancel (key) { + var notification = notificationsShown[key]; + if (notification) { + try { + if (notification.close) { + notification.close(); + } + } catch (e) {} + } + } + function notificationsClear() { angular.forEach(notificationsShown, function (notification) { try { if (notification.close) { notification.close() - } else if (notification.cancel) { - notification.cancel(); } } catch (e) {} }); - notificationsShown = []; + notificationsShown = {}; } }) diff --git a/app/partials/im.html b/app/partials/im.html index e3637cc0..99d6dcbe 100644 --- a/app/partials/im.html +++ b/app/partials/im.html @@ -7,6 +7,9 @@
diff --git a/app/partials/message.html b/app/partials/message.html index 871ae101..d72f71f9 100644 --- a/app/partials/message.html +++ b/app/partials/message.html @@ -80,7 +80,7 @@