Improved websocket support.

This commit is contained in:
Martin Edenhofer 2012-07-30 19:59:04 +02:00
parent 15f4c57356
commit ca73a740b6
6 changed files with 121 additions and 161 deletions

View file

@ -8,8 +8,6 @@ class App.DashboardTicket extends App.Controller
constructor: -> constructor: ->
super super
@tickets = []
@tickets_count = 0
@start_page = 1 @start_page = 1
@navupdate '#' @navupdate '#'
@ -61,13 +59,7 @@ class App.DashboardTicket extends App.Controller
@log 'refetch...', record @log 'refetch...', record
@fetch() @fetch()
# load user collection # App.Store.write( @key, data )
@loadCollection( type: 'User', data: data.users )
# load ticket collection
@loadCollection( type: 'Ticket', data: data.tickets )
App.Store.write( @key, data )
@render( data ) @render( data )
@ -75,7 +67,6 @@ class App.DashboardTicket extends App.Controller
@overview = data.overview @overview = data.overview
@tickets_count = data.tickets_count @tickets_count = data.tickets_count
@tickets = data.tickets
@ticket_list = data.ticket_list @ticket_list = data.ticket_list
pages_total = parseInt( ( @tickets_count / @overview.view.d.per_page ) + 0.99999 ) || 1 pages_total = parseInt( ( @tickets_count / @overview.view.d.per_page ) + 0.99999 ) || 1

View file

@ -20,9 +20,6 @@ class Index extends App.Controller
@title '' @title ''
@navupdate '#ticket/view/' + @view @navupdate '#ticket/view/' + @view
@tickets = []
@tickets_count = 0
@start_page = 1
@meta = {} @meta = {}
@bulk = {} @bulk = {}
@ -47,7 +44,6 @@ class Index extends App.Controller
if cache if cache
@overview = cache.overview @overview = cache.overview
@tickets_count = cache.tickets_count @tickets_count = cache.tickets_count
@tickets = cache.tickets
@ticket_list = cache.ticket_list @ticket_list = cache.ticket_list
@load(cache) @load(cache)
@ -81,12 +77,6 @@ class Index extends App.Controller
@log 'refetch...', record @log 'refetch...', record
@fetch() @fetch()
# load user collection
@loadCollection( type: 'User', data: data.users )
# load ticket collection
@loadCollection( type: 'Ticket', data: data.tickets )
@ticket_list_show = [] @ticket_list_show = []
for ticket_id in @ticket_list for ticket_id in @ticket_list
@ticket_list_show.push App.Ticket.find(ticket_id) @ticket_list_show.push App.Ticket.find(ticket_id)
@ -100,10 +90,6 @@ class Index extends App.Controller
# render page # render page
@render() @render()
# refresh/load default collections
# for key, value of data.default_collections
# App[key].refresh( value, options: { clear: true } )
render: -> render: ->
return if Config['ActiveController'] isnt '#ticket_overview_' + @view return if Config['ActiveController'] isnt '#ticket_overview_' + @view
@ -144,7 +130,7 @@ class Index extends App.Controller
if @view_mode is 'm' if @view_mode is 'm'
table = App.view('agent_ticket_view/detail')( table = App.view('agent_ticket_view/detail')(
overview: @overview, overview: @overview,
objects: @tickets, objects: @ticket_list_show,
checkbox: true checkbox: true
) )
table = $(table) table = $(table)
@ -184,15 +170,6 @@ class Index extends App.Controller
@el.find('.bulk-action').removeClass('hide') @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) => page: (e) =>
e.preventDefault() e.preventDefault()
id = $(e.target).data('id') id = $(e.target).data('id')
@ -269,8 +246,6 @@ class Index extends App.Controller
# refresh view after all tickets are proceeded # refresh view after all tickets are proceeded
if @bulk_count_index == @bulk_count if @bulk_count_index == @bulk_count
@tickets = []
# rebuild navbar with updated ticket count of overviews # rebuild navbar with updated ticket count of overviews
App.WebSocket.send( event: 'navupdate_ticket_overview' ) App.WebSocket.send( event: 'navupdate_ticket_overview' )
@ -448,7 +423,7 @@ class Router extends App.Controller
cache = App.Store.get( @key ) cache = App.Store.get( @key )
if cache if cache
@tickets_count = cache.tickets_count @tickets_count = cache.tickets_count
@tickets = cache.tickets @ticket_list = cache.ticket_list
@redirect() @redirect()
else else
App.Com.ajax( App.Com.ajax(
@ -463,9 +438,9 @@ class Router extends App.Controller
) )
load: (data) => load: (data) =>
@tickets = data.tickets @ticket_list = data.ticket_list
@tickets_count = data.tickets_count @tickets_count = data.tickets_count
App.Store.write( data ) # App.Store.write( data )
@redirect() @redirect()
redirect: => redirect: =>
@ -475,17 +450,17 @@ class Router extends App.Controller
# redirect # redirect
if @direction == 'next' if @direction == 'next'
if @tickets[ @position ] && @tickets[ @position ] if @ticket_list[ @position ] && @ticket_list[ @position ]
Config['LastOverviewPosition']++ Config['LastOverviewPosition']++
@navigate 'ticket/zoom/' + @tickets[ @position ].id + '/nav/true' @navigate 'ticket/zoom/' + @ticket_list[ @position ] + '/nav/true'
else else
@navigate 'ticket/zoom/' + @tickets[ @position - 1 ].id + '/nav/true' @navigate 'ticket/zoom/' + @ticket_list[ @position - 1 ] + '/nav/true'
else else
if @tickets[ @position - 2 ] && @tickets[ @position - 2 ].id + '/nav/true' if @ticket_list[ @position - 2 ] && @ticket_list[ @position - 2 ] + '/nav/true'
Config['LastOverviewPosition']-- Config['LastOverviewPosition']--
@navigate 'ticket/zoom/' + @tickets[ @position - 2 ].id + '/nav/true' @navigate 'ticket/zoom/' + @ticket_list[ @position - 2 ] + '/nav/true'
else 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/:position/:direction'] = Router
Config.Routes['ticket/view/:view'] = Index Config.Routes['ticket/view/:view'] = Index

View file

@ -31,113 +31,10 @@
#= require_tree ./models #= require_tree ./models
#= require_tree ./controllers #= require_tree ./controllers
#= require_tree ./views #= require_tree ./views
#= require ./lib/interface_handle.js.coffee
class App extends Spine.Controller class App extends Spine.Controller
@view: (name) -> @view: (name) ->
JST["app/views/#{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 window.App = App

View file

@ -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()

View file

@ -16,7 +16,7 @@ class App.WebSocket
_instance.auth(args) _instance.auth(args)
# The actual Singleton class # The actual Singleton class
class _Singleton extends Spine.Controller class _Singleton
queue: [] queue: []
constructor: (@args) -> constructor: (@args) ->
@ -90,6 +90,11 @@ class _Singleton extends Spine.Controller
# fire event # fire event
if item['event'] if item['event']
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'] ) console.log( "ws:onmessage event:" + item['event'] )
Spine.trigger( item['event'], item['data'] ) Spine.trigger( item['event'], item['data'] )

View file

@ -111,11 +111,13 @@ puts 'push overview ' + overview.meta[:url].to_s
:data => { :data => {
:overview => overview_data[:overview], :overview => overview_data[:overview],
:ticket_list => ticket_list, :ticket_list => ticket_list,
:tickets => tickets,
:tickets_count => overview_data[:tickets_count], :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, :collection => 'ticket_overview_' + overview.meta[:url].to_s,
}) })
end end