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)
@ -66,7 +62,7 @@ class Index extends App.Controller
# ) # )
load: (data) => load: (data) =>
# get meta data # get meta data
@overview = data.overview @overview = data.overview
App.Overview.refresh( @overview, options: { clear: true } ) App.Overview.refresh( @overview, options: { clear: true } )
@ -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)
@ -99,10 +89,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: ->
@ -110,7 +96,7 @@ class Index extends App.Controller
# set page title # set page title
@title @overview.meta.name @title @overview.meta.name
# get total pages # get total pages
pages_total = parseInt( ( @tickets_count / @overview.view[@view_mode].per_page ) + 0.99999 ) || 1 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('li').removeClass('active')
# html.find("[data-id=\"#{@start_page}\"]").parents('li').addClass('active') # html.find("[data-id=\"#{@start_page}\"]").parents('li').addClass('active')
@html html @html html
# create table/overview # create table/overview
table = '' table = ''
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)
@ -162,13 +148,13 @@ class Index extends App.Controller
objects: @ticket_list_show, objects: @ticket_list_show,
checkbox: true, checkbox: true,
) )
# append content table # append content table
@el.find('.table-overview').append(table) @el.find('.table-overview').append(table)
# start user popups # start user popups
@userPopups() @userPopups()
# start bulk action observ # start bulk action observ
@el.find('.bulk-action').append( @bulk_form() ) @el.find('.bulk-action').append( @bulk_form() )
@ -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,8 +90,13 @@ class _Singleton extends Spine.Controller
# fire event # fire event
if item['event'] if item['event']
console.log( "ws:onmessage event:" + item['event'] ) if typeof item['event'] is 'object'
Spine.trigger( item['event'], item['data'] ) 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 # bind to send messages
Spine.bind 'ws:send', (data) => Spine.bind 'ws:send', (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