From ca73a740b63afa033949cdf628e5216700ec5c3e Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 30 Jul 2012 19:59:04 +0200 Subject: [PATCH] Improved websocket support. --- .../controllers/_dashboard/ticket.js.coffee | 11 +- .../controllers/agent_ticket_view.js.coffee | 57 +++------- app/assets/javascripts/app/index.js.coffee | 105 +----------------- .../app/lib/interface_handle.js.coffee | 90 +++++++++++++++ .../javascripts/app/lib/websocket.js.coffee | 11 +- lib/web_socket.rb | 8 +- 6 files changed, 121 insertions(+), 161 deletions(-) create mode 100644 app/assets/javascripts/app/lib/interface_handle.js.coffee diff --git a/app/assets/javascripts/app/controllers/_dashboard/ticket.js.coffee b/app/assets/javascripts/app/controllers/_dashboard/ticket.js.coffee index 5e4759bc4..eb2807ae4 100644 --- a/app/assets/javascripts/app/controllers/_dashboard/ticket.js.coffee +++ b/app/assets/javascripts/app/controllers/_dashboard/ticket.js.coffee @@ -8,8 +8,6 @@ class App.DashboardTicket extends App.Controller constructor: -> super - @tickets = [] - @tickets_count = 0 @start_page = 1 @navupdate '#' @@ -61,13 +59,7 @@ class App.DashboardTicket extends App.Controller @log 'refetch...', record @fetch() - # load user collection - @loadCollection( type: 'User', data: data.users ) - - # load ticket collection - @loadCollection( type: 'Ticket', data: data.tickets ) - - App.Store.write( @key, data ) +# App.Store.write( @key, data ) @render( data ) @@ -75,7 +67,6 @@ class App.DashboardTicket extends App.Controller @overview = data.overview @tickets_count = data.tickets_count - @tickets = data.tickets @ticket_list = data.ticket_list pages_total = parseInt( ( @tickets_count / @overview.view.d.per_page ) + 0.99999 ) || 1 diff --git a/app/assets/javascripts/app/controllers/agent_ticket_view.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_view.js.coffee index 6359c9f9a..c51fdd6f3 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_view.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_view.js.coffee @@ -20,9 +20,6 @@ class Index extends App.Controller @title '' @navupdate '#ticket/view/' + @view - @tickets = [] - @tickets_count = 0 - @start_page = 1 @meta = {} @bulk = {} @@ -47,7 +44,6 @@ class Index extends App.Controller if cache @overview = cache.overview @tickets_count = cache.tickets_count - @tickets = cache.tickets @ticket_list = cache.ticket_list @load(cache) @@ -66,7 +62,7 @@ class Index extends App.Controller # ) load: (data) => - + # get meta data @overview = data.overview App.Overview.refresh( @overview, options: { clear: true } ) @@ -81,12 +77,6 @@ class Index extends App.Controller @log 'refetch...', record @fetch() - # load user collection - @loadCollection( type: 'User', data: data.users ) - - # load ticket collection - @loadCollection( type: 'Ticket', data: data.tickets ) - @ticket_list_show = [] for ticket_id in @ticket_list @ticket_list_show.push App.Ticket.find(ticket_id) @@ -99,10 +89,6 @@ class Index extends App.Controller # render page @render() - - # refresh/load default collections -# for key, value of data.default_collections -# App[key].refresh( value, options: { clear: true } ) render: -> @@ -110,7 +96,7 @@ class Index extends App.Controller # set page title @title @overview.meta.name - + # get total pages pages_total = parseInt( ( @tickets_count / @overview.view[@view_mode].per_page ) + 0.99999 ) || 1 @@ -138,13 +124,13 @@ class Index extends App.Controller # html.find('li').removeClass('active') # html.find("[data-id=\"#{@start_page}\"]").parents('li').addClass('active') @html html - + # create table/overview table = '' if @view_mode is 'm' table = App.view('agent_ticket_view/detail')( overview: @overview, - objects: @tickets, + objects: @ticket_list_show, checkbox: true ) table = $(table) @@ -162,13 +148,13 @@ class Index extends App.Controller objects: @ticket_list_show, checkbox: true, ) - + # append content table @el.find('.table-overview').append(table) - + # start user popups @userPopups() - + # start bulk action observ @el.find('.bulk-action').append( @bulk_form() ) @@ -184,15 +170,6 @@ class Index extends App.Controller @el.find('.bulk-action').removeClass('hide') ) - # set waypoint if not already at the end - if @start_page < pages_total - a = => -# alert('You have scrolled to an entry.') - @start_page = @start_page + 1 - @fetch() - -# $('footer').waypoint( a, { offset: '150%', triggerOnce: true } ) - page: (e) => e.preventDefault() id = $(e.target).data('id') @@ -269,8 +246,6 @@ class Index extends App.Controller # refresh view after all tickets are proceeded if @bulk_count_index == @bulk_count - @tickets = [] - # rebuild navbar with updated ticket count of overviews App.WebSocket.send( event: 'navupdate_ticket_overview' ) @@ -448,7 +423,7 @@ class Router extends App.Controller cache = App.Store.get( @key ) if cache @tickets_count = cache.tickets_count - @tickets = cache.tickets + @ticket_list = cache.ticket_list @redirect() else App.Com.ajax( @@ -463,9 +438,9 @@ class Router extends App.Controller ) load: (data) => - @tickets = data.tickets + @ticket_list = data.ticket_list @tickets_count = data.tickets_count - App.Store.write( data ) +# App.Store.write( data ) @redirect() redirect: => @@ -475,17 +450,17 @@ class Router extends App.Controller # redirect if @direction == 'next' - if @tickets[ @position ] && @tickets[ @position ] + if @ticket_list[ @position ] && @ticket_list[ @position ] Config['LastOverviewPosition']++ - @navigate 'ticket/zoom/' + @tickets[ @position ].id + '/nav/true' + @navigate 'ticket/zoom/' + @ticket_list[ @position ] + '/nav/true' else - @navigate 'ticket/zoom/' + @tickets[ @position - 1 ].id + '/nav/true' + @navigate 'ticket/zoom/' + @ticket_list[ @position - 1 ] + '/nav/true' else - if @tickets[ @position - 2 ] && @tickets[ @position - 2 ].id + '/nav/true' + if @ticket_list[ @position - 2 ] && @ticket_list[ @position - 2 ] + '/nav/true' Config['LastOverviewPosition']-- - @navigate 'ticket/zoom/' + @tickets[ @position - 2 ].id + '/nav/true' + @navigate 'ticket/zoom/' + @ticket_list[ @position - 2 ] + '/nav/true' else - @navigate 'ticket/zoom/' + @tickets[ @position - 1 ].id + '/nav/true' + @navigate 'ticket/zoom/' + @ticket_list[ @position - 1 ] + '/nav/true' Config.Routes['ticket/view/:view/:position/:direction'] = Router Config.Routes['ticket/view/:view'] = Index diff --git a/app/assets/javascripts/app/index.js.coffee b/app/assets/javascripts/app/index.js.coffee index 946d8cf21..bd2e8f347 100644 --- a/app/assets/javascripts/app/index.js.coffee +++ b/app/assets/javascripts/app/index.js.coffee @@ -31,113 +31,10 @@ #= require_tree ./models #= require_tree ./controllers #= require_tree ./views +#= require ./lib/interface_handle.js.coffee class App extends Spine.Controller @view: (name) -> JST["app/views/#{name}"] -### -class App.Config extends Spine.Module - constructor: -> - super - @config = {} - - set: (key, value) => - @config[key] = value - - get: (key) => - @config[key] - - append: (key, value) => - if !@config[key] - @config[key] = [] - - @config[key].push = value - - -Config2 = new App.Config -Config2.set( 'a', 123) -console.log '1112222', Config2.get( 'a') -### - -class App.Run extends Spine.Controller - constructor: -> - super - @log 'RUN app' - @el = $('#app') - - # create web socket connection - App.WebSocket.connect() - - # init of i18n - new App.i18n - - # start navigation controller - new App.Navigation( el: @el.find('#navigation') ); - - # check if session already exists/try to get session data from server - App.Auth.loginCheck() - - # start notify controller - new App.Notify( el: @el.find('#notify') ); - - # start content - new App.Content( el: @el.find('#content') ); - - # bind to fill selected text into - $(@el).bind('mouseup', => - window.Session['UISelection'] = @getSelected() + '' - ) - - getSelected: -> - text = ''; - if window.getSelection - text = window.getSelection() - else if document.getSelection - text = document.getSelection() - else if document.selection - text = document.selection.createRange().text - text - -class App.Content extends Spine.Controller - className: 'container' - - constructor: -> - super - @log 'RUN content'#, @ - - for route, callback of Config.Routes - do (route, callback) => - @route(route, (params) -> - - # remember current controller - Config['ActiveController'] = route - - # send current controller - params_only = {} - for i of params - if typeof params[i] isnt 'object' - params_only[i] = params[i] - App.WebSocket.send( - action: 'active_controller', - controller: route, - params: params_only, - ) - - # unbind in controller area - @el.unbind() - @el.undelegate() - - # remove waypoints - $('footer').waypoint('remove') - - params.el = @el - new callback( params ) - - # scroll to top -# window.scrollTo(0,0) - ) - - Spine.Route.setup() - window.App = App \ No newline at end of file diff --git a/app/assets/javascripts/app/lib/interface_handle.js.coffee b/app/assets/javascripts/app/lib/interface_handle.js.coffee new file mode 100644 index 000000000..184239801 --- /dev/null +++ b/app/assets/javascripts/app/lib/interface_handle.js.coffee @@ -0,0 +1,90 @@ + +class App.Run extends App.Controller + constructor: -> + super + @log 'RUN app' + @el = $('#app') + + # create web socket connection + App.WebSocket.connect() + + # init of i18n + new App.i18n + + # start navigation controller + new App.Navigation( el: @el.find('#navigation') ); + + # check if session already exists/try to get session data from server + App.Auth.loginCheck() + + # bind new events + Spine.bind 'loadCollection', (data) => + + # load collections + if data.collections + for key of data.collections + + @log 'loadCollection', key, data.collections[key] + @loadCollection( type: key, data: data.collections[key] ) + + # start notify controller + new App.Notify( el: @el.find('#notify') ); + + # start content + new App.Content( el: @el.find('#content') ); + + # bind to fill selected text into + $(@el).bind('mouseup', => + window.Session['UISelection'] = @getSelected() + '' + ) + + getSelected: -> + text = ''; + if window.getSelection + text = window.getSelection() + else if document.getSelection + text = document.getSelection() + else if document.selection + text = document.selection.createRange().text + text + +class App.Content extends Spine.Controller + className: 'container' + + constructor: -> + super + @log 'RUN content'#, @ + + for route, callback of Config.Routes + do (route, callback) => + @route(route, (params) -> + + # remember current controller + Config['ActiveController'] = route + + # send current controller + params_only = {} + for i of params + if typeof params[i] isnt 'object' + params_only[i] = params[i] + App.WebSocket.send( + action: 'active_controller', + controller: route, + params: params_only, + ) + + # unbind in controller area + @el.unbind() + @el.undelegate() + + # remove waypoints + $('footer').waypoint('remove') + + params.el = @el + new callback( params ) + + # scroll to top +# window.scrollTo(0,0) + ) + + Spine.Route.setup() \ No newline at end of file diff --git a/app/assets/javascripts/app/lib/websocket.js.coffee b/app/assets/javascripts/app/lib/websocket.js.coffee index 82227882f..f538f2486 100644 --- a/app/assets/javascripts/app/lib/websocket.js.coffee +++ b/app/assets/javascripts/app/lib/websocket.js.coffee @@ -16,7 +16,7 @@ class App.WebSocket _instance.auth(args) # The actual Singleton class -class _Singleton extends Spine.Controller +class _Singleton queue: [] constructor: (@args) -> @@ -90,8 +90,13 @@ class _Singleton extends Spine.Controller # fire event if item['event'] - console.log( "ws:onmessage event:" + item['event'] ) - Spine.trigger( item['event'], item['data'] ) + if typeof item['event'] is 'object' + for event in item['event'] + console.log( "ws:onmessage event:" + event ) + Spine.trigger( event, item['data'] ) + else + console.log( "ws:onmessage event:" + item['event'] ) + Spine.trigger( item['event'], item['data'] ) # bind to send messages Spine.bind 'ws:send', (data) => diff --git a/lib/web_socket.rb b/lib/web_socket.rb index 68178ac86..ab4b4f305 100644 --- a/lib/web_socket.rb +++ b/lib/web_socket.rb @@ -111,11 +111,13 @@ puts 'push overview ' + overview.meta[:url].to_s :data => { :overview => overview_data[:overview], :ticket_list => ticket_list, - :tickets => tickets, :tickets_count => overview_data[:tickets_count], - :users => users, + :collections => { + :User => users, + :Ticket => tickets, + } }, - :event => 'ticket_overview_rebuild', + :event => [ 'loadCollection', 'ticket_overview_rebuild' ], :collection => 'ticket_overview_' + overview.meta[:url].to_s, }) end