Added init version of auto save.

This commit is contained in:
Martin Edenhofer 2013-05-30 00:25:04 +02:00
parent 5ee16d758b
commit b8a1ccd9e1
4 changed files with 106 additions and 26 deletions

View file

@ -49,6 +49,9 @@ class App.TicketCreate extends App.Controller
@log 'AgentTicketPhone', 'error', defaults @log 'AgentTicketPhone', 'error', defaults
@render(defaults) @render(defaults)
# start auto save
@autosave()
meta: => meta: =>
text = App.i18n.translateInline( @article_attributes['title'] ) text = App.i18n.translateInline( @article_attributes['title'] )
subject = @el.find('[name=subject]').val() subject = @el.find('[name=subject]').val()
@ -76,6 +79,18 @@ class App.TicketCreate extends App.Controller
release: => release: =>
# @clearInterval( @key, 'ticket_zoom' ) # @clearInterval( @key, 'ticket_zoom' )
@el.remove() @el.remove()
@clearInterval( @id, @auto_save_key )
autosave: =>
@auto_save_key = 'create' + @type + @id
update = =>
data = @formParam( @el.find('.ticket-create') )
diff = difference( @autosaveLast, data )
if !@autosaveLast || ( diff && !_.isEmpty( diff ) )
@autosaveLast = data
console.log('form hash changed', diff, data)
App.TaskManager.update( @task_key, { 'state': data })
@interval( update, 10000, @id, @auto_save_key )
# get data / in case also ticket data for split # get data / in case also ticket data for split
fetch: (params) -> fetch: (params) ->
@ -135,7 +150,7 @@ class App.TicketCreate extends App.Controller
render: (template = {}) -> render: (template = {}) ->
# set defaults # set defaults
defaults = template['options'] || {} defaults = template['options'] || @form_state || {}
if !( 'ticket_state_id' of defaults ) if !( 'ticket_state_id' of defaults )
defaults['ticket_state_id'] = App.Collection.findByAttribute( 'TicketState', 'name', 'open' ).id defaults['ticket_state_id'] = App.Collection.findByAttribute( 'TicketState', 'name', 'open' ).id
if !( 'ticket_priority_id' of defaults ) if !( 'ticket_priority_id' of defaults )

View file

@ -34,6 +34,9 @@ class App.TicketZoom extends App.Controller
@fetch( @ticket_id, false) @fetch( @ticket_id, false)
@interval( update, 30000, @key, 'ticket_zoom' ) @interval( update, 30000, @key, 'ticket_zoom' )
# start auto save
@autosave()
meta: => meta: =>
return if !@ticket return if !@ticket
meta = meta =
@ -62,6 +65,17 @@ class App.TicketZoom extends App.Controller
@clearInterval( @key, 'ticket_zoom' ) @clearInterval( @key, 'ticket_zoom' )
@el.remove() @el.remove()
autosave: =>
@auto_save_key = 'zoom' + @id
update = =>
data = @formParam( @el.find('.ticket-update') )
diff = difference( @autosaveLast, data )
if !@autosaveLast || ( diff && !_.isEmpty( diff ) )
@autosaveLast = data
console.log('form hash changed', diff, data)
App.TaskManager.update( @task_key, { 'state': data })
@interval( update, 10000, @id, @auto_save_key )
fetch: (ticket_id, force) -> fetch: (ticket_id, force) ->
return if !@Session.all() return if !@Session.all()
@ -141,34 +155,36 @@ class App.TicketZoom extends App.Controller
for article in @articles for article in @articles
new Article( article: article ) new Article( article: article )
defaults = @form_state || {}
@configure_attributes_ticket = [ @configure_attributes_ticket = [
{ name: 'ticket_state_id', display: 'State', tag: 'select', multiple: false, null: true, relation: 'TicketState', filter: @edit_form, translate: true, class: 'span2', item_class: 'pull-left' }, { name: 'ticket_state_id', display: 'State', tag: 'select', multiple: false, null: true, relation: 'TicketState', filter: @edit_form, translate: true, class: 'span2', item_class: 'pull-left', default: defaults['ticket_state_id'] },
{ name: 'ticket_priority_id', display: 'Priority', tag: 'select', multiple: false, null: true, relation: 'TicketPriority', filter: @edit_form, translate: true, class: 'span2', item_class: 'pull-left' }, { name: 'ticket_priority_id', display: 'Priority', tag: 'select', multiple: false, null: true, relation: 'TicketPriority', filter: @edit_form, translate: true, class: 'span2', item_class: 'pull-left', default: defaults['ticket_priority_id'] },
{ name: 'group_id', display: 'Group', tag: 'select', multiple: false, null: true, relation: 'Group', filter: @edit_form, class: 'span2', item_class: 'pull-left' }, { name: 'group_id', display: 'Group', tag: 'select', multiple: false, null: true, relation: 'Group', filter: @edit_form, class: 'span2', item_class: 'pull-left', default: defaults['group_id'] },
{ name: 'owner_id', display: 'Owner', tag: 'select', multiple: false, null: true, relation: 'User', filter: @edit_form, nulloption: true, class: 'span2', item_class: 'pull-left' }, { name: 'owner_id', display: 'Owner', tag: 'select', multiple: false, null: true, relation: 'User', filter: @edit_form, nulloption: true, class: 'span2', item_class: 'pull-left', default: defaults['owner_id'] },
] ]
if @isRole('Customer') if @isRole('Customer')
@configure_attributes_ticket = [ @configure_attributes_ticket = [
{ name: 'ticket_state_id', display: 'State', tag: 'select', multiple: false, null: true, relation: 'TicketState', filter: @edit_form, translate: true, class: 'span2', item_class: 'pull-left' }, { name: 'ticket_state_id', display: 'State', tag: 'select', multiple: false, null: true, relation: 'TicketState', filter: @edit_form, translate: true, class: 'span2', item_class: 'pull-left', default: defaults['ticket_state_id'] },
{ name: 'ticket_priority_id', display: 'Priority', tag: 'select', multiple: false, null: true, relation: 'TicketPriority', filter: @edit_form, translate: true, class: 'span2', item_class: 'pull-left' }, { name: 'ticket_priority_id', display: 'Priority', tag: 'select', multiple: false, null: true, relation: 'TicketPriority', filter: @edit_form, translate: true, class: 'span2', item_class: 'pull-left', default: defaults['ticket_priority_id'] },
] ]
@configure_attributes_article = [ @configure_attributes_article = [
{ name: 'ticket_article_type_id', display: 'Type', tag: 'select', multiple: false, null: true, relation: 'TicketArticleType', filter: @edit_form, default: '9', translate: true, class: 'medium', item_class: '' }, { name: 'ticket_article_type_id', display: 'Type', tag: 'select', multiple: false, null: true, relation: 'TicketArticleType', filter: @edit_form, default: '9', translate: true, class: 'medium', item_class: '', default: defaults['ticket_article_type_id'] },
{ name: 'to', display: 'To', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide' }, { name: 'to', display: 'To', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide', default: defaults['to'] },
{ name: 'cc', display: 'Cc', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide' }, { name: 'cc', display: 'Cc', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide', default: defaults['cc'] },
{ name: 'subject', display: 'Subject', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide' }, { name: 'subject', display: 'Subject', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide', default: defaults['subject'] },
{ name: 'in_reply_to', display: 'In Reply to', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide' }, { name: 'in_reply_to', display: 'In Reply to', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide', default: defaults['in_reply_to'] },
{ name: 'body', display: 'Text', tag: 'textarea', rows: 6, limit: 100, null: true, class: 'span7', item_class: '', upload: true }, { name: 'body', display: 'Text', tag: 'textarea', rows: 6, limit: 100, null: true, class: 'span7', item_class: '', upload: true, default: defaults['body'] },
{ name: 'internal', display: 'Visability', tag: 'select', default: false, null: true, options: { true: 'internal', false: 'public' }, class: 'medium', item_class: '' }, { name: 'internal', display: 'Visability', tag: 'select', default: false, null: true, options: { true: 'internal', false: 'public' }, class: 'medium', item_class: '', default: defaults['internal'] },
] ]
if @isRole('Customer') if @isRole('Customer')
@configure_attributes_article = [ @configure_attributes_article = [
{ name: 'to', display: 'To', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide' }, { name: 'to', display: 'To', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide', default: defaults['to'] },
{ name: 'cc', display: 'Cc', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide' }, { name: 'cc', display: 'Cc', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide', default: defaults['cc'] },
{ name: 'subject', display: 'Subject', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide' }, { name: 'subject', display: 'Subject', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide', default: defaults['subject'] },
{ name: 'in_reply_to', display: 'In Reply to', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide' }, { name: 'in_reply_to', display: 'In Reply to', tag: 'input', type: 'text', limit: 100, null: true, class: 'span7', item_class: 'hide', default: defaults['in_reply_to'] },
{ name: 'body', display: 'Text', tag: 'textarea', rows: 6, limit: 100, null: true, class: 'span7', item_class: '', upload: true }, { name: 'body', display: 'Text', tag: 'textarea', rows: 6, limit: 100, null: true, class: 'span7', item_class: '', upload: true, default: defaults['body'] },
] ]
@html App.view('agent_ticket_zoom')( @html App.view('agent_ticket_zoom')(
@ -623,6 +639,7 @@ class TicketZoomRouter extends App.ControllerPermanent
constructor: (params) -> constructor: (params) ->
super super
@log 'zoom router', params @log 'zoom router', params
# cleanup params # cleanup params
clean_params = clean_params =
ticket_id: params.ticket_id ticket_id: params.ticket_id

View file

@ -15,8 +15,6 @@ class App.TaskWidget extends App.Controller
App.TaskManager.reset() App.TaskManager.reset()
@el.html('') @el.html('')
App.TaskManager.syncInitial()
sync = => sync = =>
App.TaskManager.sync() App.TaskManager.sync()
@delay( sync, 3000, 'task-widget' ) @delay( sync, 3000, 'task-widget' )

View file

@ -9,16 +9,21 @@ class App.TaskManager
_instance ?= new _Singleton _instance ?= new _Singleton
_instance.all() _instance.all()
@add: ( type, type_id, callback, params, to_not_show ) -> @add: ( type, type_id, callback, params, to_not_show, state ) ->
if _instance == undefined if _instance == undefined
_instance ?= new _Singleton _instance ?= new _Singleton
_instance.add( type, type_id, callback, params, to_not_show ) _instance.add( type, type_id, callback, params, to_not_show, state )
@get: ( key ) -> @get: ( key ) ->
if _instance == undefined if _instance == undefined
_instance ?= new _Singleton _instance ?= new _Singleton
_instance.get( key ) _instance.get( key )
@update: ( key, params ) ->
if _instance == undefined
_instance ?= new _Singleton
_instance.update( key, params )
@remove: ( key ) -> @remove: ( key ) ->
if _instance == undefined if _instance == undefined
_instance ?= new _Singleton _instance ?= new _Singleton
@ -39,6 +44,11 @@ class App.TaskManager
_instance ?= new _Singleton _instance ?= new _Singleton
_instance.syncTasksInitial() _instance.syncTasksInitial()
@syncSave: ->
if _instance == undefined
_instance ?= new _Singleton
_instance.syncSave()
@sync: -> @sync: ->
if _instance == undefined if _instance == undefined
_instance ?= new _Singleton _instance ?= new _Singleton
@ -50,11 +60,12 @@ class _Singleton extends App.Controller
constructor: -> constructor: ->
@tasks = {} @tasks = {}
@task_count = 0 @task_count = 0
@syncTasksInitial()
all: -> all: ->
@tasks @tasks
add: ( type, type_id, callback, params, to_not_show = false ) -> add: ( type, type_id, callback, params, to_not_show = false, state ) ->
for key, task of @tasks for key, task of @tasks
if task.type is type && task.type_id is type_id if task.type is type && task.type_id is type_id
return key if to_not_show return key if to_not_show
@ -99,6 +110,13 @@ class _Singleton extends App.Controller
params_app = _.clone(params) params_app = _.clone(params)
params_app['el'] = $('#content_permanent_' + @task_count ) params_app['el'] = $('#content_permanent_' + @task_count )
params_app['task_key'] = @task_count params_app['task_key'] = @task_count
# check if we have old state there
if !state
oldTask = @get_by_type( type, type_id )
if oldTask
state = oldTask.state
params_app['form_state'] = state
if to_not_show if to_not_show
params_app['doNotLog'] = 1 params_app['doNotLog'] = 1
a = new App[callback]( params_app ) a = new App[callback]( params_app )
@ -128,6 +146,12 @@ class _Singleton extends App.Controller
get: ( key ) => get: ( key ) =>
return @tasks[key] return @tasks[key]
update: ( key, params ) =>
return false if !@tasks[key]
for item, value of params
@tasks[key][item] = value
@syncSave()
remove: ( key, to_not_show = false ) => remove: ( key, to_not_show = false ) =>
if @tasks[key] if @tasks[key]
@tasks[key].worker.release() @tasks[key].worker.release()
@ -143,6 +167,11 @@ class _Singleton extends App.Controller
@tasks = {} @tasks = {}
App.Event.trigger 'ui:rerender' App.Event.trigger 'ui:rerender'
get_by_type: (type, type_id) =>
store = @syncLoad() || []
for item in store
return item if item.type is type && item.type_id is type_id
syncAdd: (task) => syncAdd: (task) =>
store = @syncLoad() || [] store = @syncLoad() || []
for item in store for item in store
@ -163,6 +192,27 @@ class _Singleton extends App.Controller
storeNew.push item storeNew.push item
App.Store.write( 'tasks', storeNew ) App.Store.write( 'tasks', storeNew )
syncSave: =>
store = @syncLoad() || []
storeNew = []
for item in store
for key, task of @tasks
if task.type is item.type && task.type_id is item.type_id
console.log('MATCH', item)
if @tasks[key]['state']
item['state'] = @tasks[key]['state']
storeNew.push item
# if @tasks[key]
# @tasks[key].worker.release()
#
# storeNew.push item
# item =
# type: task.type
# type_id: task.type_id
# params: task.params
# callback: task.callback
App.Store.write( 'tasks', storeNew )
syncLoad: => syncLoad: =>
App.Store.get( 'tasks' ) App.Store.get( 'tasks' )
@ -176,7 +226,7 @@ class _Singleton extends App.Controller
@delay( @delay(
=> =>
task = store.shift() task = store.shift()
@add(task.type, task.type_id, task.callback, task.params, true) @add(task.type, task.type_id, task.callback, task.params, true, task.state)
task_count * 500 task_count * 500
) )