Improved online notification. Widget should stay open on updates and close after click to an object.

This commit is contained in:
Martin Edenhofer 2015-01-21 00:30:19 +01:00
parent 55fb234e5b
commit 8ee9002c33
6 changed files with 102 additions and 67 deletions

View file

@ -8,7 +8,7 @@ class App.OnlineNotificationWidget extends App.Controller
@bind 'OnlineNotification::changed', => @bind 'OnlineNotification::changed', =>
@delay( @delay(
=> @fetch() => @fetch()
1000 1200
'online-notification-changed' 'online-notification-changed'
) )
@ -20,14 +20,14 @@ class App.OnlineNotificationWidget extends App.Controller
if !@access() if !@access()
@el.find('activity-counter').html('') @el.find('activity-counter').html('')
return return
@start() @createContainer()
if @access() if @access()
@start() @createContainer()
@subscribeId = App.OnlineNotification.subscribe( @start ) @subscribeId = App.OnlineNotification.subscribe( @updateContent )
release: => release: =>
@stop() @removeContainer()
App.OnlineNotification.unsubscribe( @subscribeId ) App.OnlineNotification.unsubscribe( @subscribeId )
access: -> access: ->
@ -46,17 +46,13 @@ class App.OnlineNotificationWidget extends App.Controller
else else
@toggle.append('<div class="activity-counter">' + count.toString() + '</div>') @toggle.append('<div class="activity-counter">' + count.toString() + '</div>')
markAllAsSeen: () => markAllAsRead: =>
@ajax( @ajax(
id: 'markAllAsSeen' id: 'markAllAsRead'
type: 'POST' type: 'POST'
url: @apiPath + '/online_notifications/markAllAsSeen' url: @apiPath + '/online_notifications/mark_all_as_read'
data: JSON.stringify( '' ) data: JSON.stringify( '' )
processData: true processData: true
success: (data, status, xhr) =>
if data.result is 'ok'
else
fail: =>
) )
removeClickCatcher: () => removeClickCatcher: () =>
@ -65,34 +61,44 @@ class App.OnlineNotificationWidget extends App.Controller
@clickCatcher = null @clickCatcher = null
onShow: => onShow: =>
# show frontend times @updateContent()
$('#markAllAsSeen').bind('click', (e) =>
e.preventDefault() # set heigth of notification popover
@markAllAsSeen() height = $('#app').height()
$('.js-notificationsContainer').css('height', "#{height-12}px")
$('.js-notificationsContainer .arrow').css('top', '20px')
# close notification list on click
$('.js-notificationsContainer').on('click', (e) =>
#console.log('CL')
@hidePopover()
) )
@frontendTimeUpdate()
# mark all notifications as read
$('.js-markAllAsRead').on('click', (e) =>
e.preventDefault()
@markAllAsRead()
)
# add clickCatcher # add clickCatcher
@clickCatcher = new App.clickCatcher @clickCatcher = new App.clickCatcher
holder: @el.offsetParent() holder: @el.offsetParent()
callback: @hidePopover callback: @hidePopover
zIndexScale: 4 zIndexScale: 4
onHide: => onHide: =>
$('#markAllAsSeen').unbind('click')
@removeClickCatcher() @removeClickCatcher()
hidePopover: => hidePopover: =>
@toggle.popover('hide') @toggle.popover('hide')
stop: => fetch: =>
@counterUpdate(0) load = (items) =>
@toggle.popover('destroy') App.OnlineNotification.refresh( items, { clear: true } )
@updateContent()
App.OnlineNotification.fetchFull(load)
start: => updateContent: =>
@stop()
# show popover
items = App.OnlineNotification.search( sortBy: 'created_at', order: 'DESC' ) items = App.OnlineNotification.search( sortBy: 'created_at', order: 'DESC' )
counter = 0 counter = 0
for item in items for item in items
@ -100,26 +106,46 @@ class App.OnlineNotificationWidget extends App.Controller
counter = counter + 1 counter = counter + 1
@counterUpdate(counter) @counterUpdate(counter)
# update title
$('.js-notificationsContainer .popover-title').html(
App.i18n.translateInline( 'Notifications' ) + " <span class='popover-notificationsCounter'>#{counter}</span>"
)
# update content
items = @prepareForObjectList(items) items = @prepareForObjectList(items)
$('.js-notificationsContainer .popover-content').html(
$( App.view('widget/online_notification_content')(items: items) )
)
@toggle.popover # show frontend times
trigger: 'click' @frontendTimeUpdate()
container: 'body'
html: true
placement: 'right'
viewport: { "selector": "#app", "padding": 10 }
template: App.view('widget/online_notification')()
title: ->
App.i18n.translateInline( 'Notifications' ) + " <span class='popover-notificationsCounter'>#{counter}</span>"
content: =>
# insert data
$( App.view('widget/online_notification_content')(items: items) )
.on
'shown.bs.popover': @onShow
'hide.bs.popover': @onHide
fetch: => createContainer: =>
load = (items) => @removeContainer()
App.OnlineNotification.refresh( items, { clear: true } )
@start() # show popover
App.OnlineNotification.fetchFull(load) waitUntilOldPopoverIsRemoved = =>
@toggle.popover
trigger: 'click'
container: 'body'
html: true
placement: 'right'
viewport: { "selector": "#app", "padding": 10 }
template: App.view('widget/online_notification')()
title: ' '
content: ' '
.on
'shown.bs.popover': @onShow
'hide.bs.popover': @onHide
@updateContent()
@delay(
=> waitUntilOldPopoverIsRemoved()
600
'popover'
)
removeContainer: =>
@counterUpdate(0)
@toggle.popover('destroy')

View file

@ -1,8 +1,8 @@
<div class="popover popover--notifications" role="tooltip"> <div class="popover popover--notifications js-notificationsContainer" role="tooltip">
<div class="arrow"></div> <div class="arrow"></div>
<div class="popover-notificationsHeader"> <div class="popover-notificationsHeader">
<span class="popover-title"></span> <span class="popover-title"></span>
<a class="popover-notificationsMarkRead" id="markAllAsSeen"><%- @T( 'Mark all as read' ) %></a> <a class="popover-notificationsMarkRead js-markAllAsRead"><%- @T( 'Mark all as read' ) %></a>
</div> </div>
<div class="popover-content"></div> <div class="popover-content"></div>
</div> </div>

View file

@ -1,5 +0,0 @@
<span class="popover-notificationsTitle">
<%- @T( 'Notifications' ) %>
<span class="popover-notificationsCounter"><%= @counter %></span>
<a class="popover-notificationsMarkRead" id="markAllAsSeen"><%- @T( 'Mark all as read' ) %></a>
</span>

View file

@ -2605,7 +2605,7 @@ footer {
margin-left: auto; margin-left: auto;
} }
a.list-group-item.active > .badge, a.list-group-item.active > .badge,
.nav-pills > .active > a > .badge { .nav-pills > .active > a > .badge {
color: #96969b; color: #96969b;
background: none; background: none;
@ -2634,10 +2634,6 @@ footer {
z-index: 1110; z-index: 1110;
box-shadow: 0 1px 14px rgba(0,8,14,.25); box-shadow: 0 1px 14px rgba(0,8,14,.25);
// max-height: calc(100vh - 20px); // max-height: calc(100vh - 20px);
&.is-visible {
display: block;
}
} }
.popover-title { .popover-title {

View file

@ -88,12 +88,30 @@ curl http://localhost/api/v1/online_notifications -v -u #{login}:#{password} -H
model_update_render(OnlineNotification, params) model_update_render(OnlineNotification, params)
end end
def markAllAsSeen =begin
Resource:
PUT /api/v1/online_notifications/mark_all_as_read
Payload:
{}
Response:
{}
Test:
curl http://localhost/api/v1/online_notifications/mark_all_as_read -v -u #{login}:#{password} -X POST -d '{}'
=end
def mark_all_as_read
notifications = OnlineNotification.list(current_user,100) notifications = OnlineNotification.list(current_user,100)
notifications.each do |notification| notifications.each do |notification|
OnlineNotification.seen({:id => notification['id']}) if !notification['seen']
OnlineNotification.seen( :id => notification['id'] )
end
end end
render :json => {data:'ok'} render :json => {}, :status => :ok
end end
end end

View file

@ -2,8 +2,8 @@ Zammad::Application.routes.draw do
api_path = Rails.configuration.api_path api_path = Rails.configuration.api_path
# groups # groups
match api_path + '/online_notifications', :to => 'online_notifications#index', :via => :get match api_path + '/online_notifications', :to => 'online_notifications#index', :via => :get
match api_path + '/online_notifications/:id', :to => 'online_notifications#update', :via => :put match api_path + '/online_notifications/:id', :to => 'online_notifications#update', :via => :put
match api_path + '/online_notifications/markAllAsSeen', :to => 'online_notifications#markAllAsSeen', :via => :post match api_path + '/online_notifications/mark_all_as_read', :to => 'online_notifications#mark_all_as_read', :via => :post
end end