From b7a675874c5c50d1e77c82ad95547653e6d0fce3 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 2 Jun 2013 17:56:07 +0200 Subject: [PATCH] Init version of session disable to only have one active browser window with tasks. --- .../_application_controller.js.coffee | 42 ++++++++++++++++++- .../app/controllers/task_widget.js.coffee | 28 +++++++++++++ .../app/lib/app_post/delay.js.coffee | 11 +++++ .../app/lib/app_post/interval.js.coffee | 11 +++++ .../app/lib/app_post/task_manager.js.coffee | 29 ++++++++++++- .../views/{error.jst.eco => modal.jst.eco} | 10 +++-- script/websocket-server.rb | 3 ++ 7 files changed, 126 insertions(+), 8 deletions(-) rename app/assets/javascripts/app/views/{error.jst.eco => modal.jst.eco} (69%) diff --git a/app/assets/javascripts/app/controllers/_application_controller.js.coffee b/app/assets/javascripts/app/controllers/_application_controller.js.coffee index f9a1d77ef..e9a6912c5 100644 --- a/app/assets/javascripts/app/controllers/_application_controller.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller.js.coffee @@ -391,7 +391,7 @@ class App.ControllerModal extends App.Controller # do not use @el, because it's inserted by js if options delete options.el - + # callbacks # @callback = {} # if options.success @@ -439,7 +439,8 @@ class App.ErrorModal extends App.ControllerModal @render() render: -> - @html App.view('error')( + @html App.view('modal')( + title: 'Error', message: @message detail: @detail close: @close @@ -448,3 +449,40 @@ class App.ErrorModal extends App.ControllerModal backdrop: false, keyboard: false, ) + +class App.SessionReloadModal extends App.ControllerModal + constructor: -> + super + @render() + + render: -> + @html App.view('modal')( + title: @title || '?' + message: @message || '?' + detail: @detail + close: @close + button: @button + ) + @modalShow( + backdrop: @backdrop, + keyboard: @keyboard, + ) + + modalHide: (e) -> + @reload(e) + + submit: (e) -> + @reload(e) + + reload: (e) -> + if e + e.preventDefault() + if window.location.reload + window.location.reload() + return true + if window.location.href + window.location.href = window.location.href + return true + + throw "Cant reload page!" + diff --git a/app/assets/javascripts/app/controllers/task_widget.js.coffee b/app/assets/javascripts/app/controllers/task_widget.js.coffee index 24b9cb600..cf3edafd9 100644 --- a/app/assets/javascripts/app/controllers/task_widget.js.coffee +++ b/app/assets/javascripts/app/controllers/task_widget.js.coffee @@ -15,6 +15,34 @@ class App.TaskWidget extends App.Controller App.TaskManager.reset() @el.html('') + # only do take over check after spool messages are finised + App.Event.bind 'spool:sent', (data) => + @spoolSent = true + + # session take over message + App.Event.bind 'session:takeover', (data) => + + # only if spool messages are already sent + return if !@spoolSent + + # check if error message is already shown + if !@error + + # only if new client id isnt own client id + if data.client_id isnt App.TaskManager.clientId() + @error = new App.SessionReloadModal( + title: 'Session' + message: 'Session taken over... please reload page or work with other browser window.' + keyboard: false + backdrop: true + close: true + button: 'Reload application' + ) + + # disable all delay's and interval's + App.Delay.reset() + App.Interval.reset() + render: -> return if _.isEmpty( @Session.all() ) diff --git a/app/assets/javascripts/app/lib/app_post/delay.js.coffee b/app/assets/javascripts/app/lib/app_post/delay.js.coffee index b9e297d7d..2670ecab9 100644 --- a/app/assets/javascripts/app/lib/app_post/delay.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/delay.js.coffee @@ -16,6 +16,11 @@ class App.Delay _instance ?= new _Singleton _instance.clearLevel( level ) + @reset: ( level ) -> + if _instance == undefined + _instance ?= new _Singleton + _instance.reset( level ) + @_all: -> if _instance == undefined _instance ?= new _Singleton @@ -78,6 +83,12 @@ class _Singleton extends Spine.Module @clear( key, level ) @levelStack[level] = {} + reset: -> + for level, items of @levelStack + for key, data of items + @clear( key, level ) + @levelStack[level] = {} + _all: -> return @levelStack diff --git a/app/assets/javascripts/app/lib/app_post/interval.js.coffee b/app/assets/javascripts/app/lib/app_post/interval.js.coffee index 6ce3ea1cb..53ef1c4ef 100644 --- a/app/assets/javascripts/app/lib/app_post/interval.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/interval.js.coffee @@ -16,6 +16,11 @@ class App.Interval _instance ?= new _Singleton _instance.clearLevel( level ) + @reset: ( level ) -> + if _instance == undefined + _instance ?= new _Singleton + _instance.reset( level ) + @_all: -> if _instance == undefined _instance ?= new _Singleton @@ -76,5 +81,11 @@ class _Singleton extends Spine.Module @clear( key, level ) @levelStack[level] = {} + reset: -> + for level, items of @levelStack + for key, data of items + @clear( key, level ) + @levelStack[level] = {} + _all: -> return @levelStack diff --git a/app/assets/javascripts/app/lib/app_post/task_manager.js.coffee b/app/assets/javascripts/app/lib/app_post/task_manager.js.coffee index f284598f0..0d72490fa 100644 --- a/app/assets/javascripts/app/lib/app_post/task_manager.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/task_manager.js.coffee @@ -54,6 +54,11 @@ class App.TaskManager _instance ?= new _Singleton _instance.workerAll() + @clientId: -> + if _instance == undefined + _instance ?= new _Singleton + _instance.clientId() + class _Singleton extends App.Controller @include App.Log @@ -230,18 +235,38 @@ class _Singleton extends App.Controller if !task throw "No such task with '#{key}' of order" prio++ - task.prio = prio - task.save() + if task.prio isnt prio + task.prio = prio + task.save() reset: => App.Taskbar.deleteAll() App.Event.trigger 'ui:rerender' + clientId: => + if !@clientIdInt + @clientIdInt = Math.floor( Math.random() * 99999999 ) + @clientIdInt + tasksInitial: => # reopen tasks # App.Taskbar.fetch() tasks = @all() return if !tasks + + # check if we have different + + # broadcast to other browser instance + App.WebSocket.send( + action: 'broadcast' + event: 'session:takeover' + spool: true + data: + recipient: + user_id: [ App.Session.get( 'id' ) ] + client_id: @clientId() + ) + task_count = 0 for task in tasks task_count += 1 diff --git a/app/assets/javascripts/app/views/error.jst.eco b/app/assets/javascripts/app/views/modal.jst.eco similarity index 69% rename from app/assets/javascripts/app/views/error.jst.eco rename to app/assets/javascripts/app/views/modal.jst.eco index 147b6ddcd..aea5f2ddf 100644 --- a/app/assets/javascripts/app/views/error.jst.eco +++ b/app/assets/javascripts/app/views/modal.jst.eco @@ -2,7 +2,7 @@ <% if @close: %> × <% end %> -

<%- @T( 'Error' ) %>

+

<%- @T( @title ) %>

diff --git a/script/websocket-server.rb b/script/websocket-server.rb index 449ae9f1f..6c97d755e 100755 --- a/script/websocket-server.rb +++ b/script/websocket-server.rb @@ -172,6 +172,9 @@ EventMachine.run { end } + + # send spool:sent event to client + @clients[client_id][:websocket].send( '[{"event":"spool:sent"}]' ) end # get session