From 3d594e11b31c33bfe12665f7aa6479647589c70a Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 5 Apr 2016 00:04:48 +0200 Subject: [PATCH] Improved App.CollectionController controller. --- .../_application_controller_generic.coffee | 39 ++++++++++++++----- .../controllers/agent_ticket_create.coffee | 9 ++--- .../app/controllers/taskbar_widget.coffee | 22 +++++++---- .../app/controllers/ticket_overview.coffee | 2 +- .../widget/online_notification.coffee | 3 ++ .../app/lib/app_post/task_manager.coffee | 3 +- .../javascripts/app/lib/app_post/utils.coffee | 11 +++++- .../app/views/widget/task_item.jst.eco | 2 +- public/assets/tests/html_utils.js | 12 ++++++ 9 files changed, 74 insertions(+), 29 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller_generic.coffee b/app/assets/javascripts/app/controllers/_application_controller_generic.coffee index a2551a2b1..8214e5595 100644 --- a/app/assets/javascripts/app/controllers/_application_controller_generic.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller_generic.coffee @@ -764,6 +764,7 @@ class App.CollectionController extends App.Controller model: '_need_to_be_defined_' sortBy: 'name' order: 'ASC', + insertPosition: 'after' constructor: -> @events = @constructor.events unless @events @@ -819,8 +820,14 @@ class App.CollectionController extends App.Controller @domChange(param[1]) else if param[0] is 'domRemove' @domRemove(param[1]) + else if param[0] is 'change' + @collectionSync(param[1]) + else if param[0] is 'remove' + @collectionSync(param[1], 'destroy') else if param[0] is 'renderAll' @renderAll() + else + @log 'error', "Unknown type #{param[0]}", param[1] if !@queue[0] @onRenderEnd() @queueRunning = false @@ -845,7 +852,7 @@ class App.CollectionController extends App.Controller if type is 'destroy' ids = [] for item in items - ids.push item.id + ids.push item[@uniqKey] @queue.push ['domRemove', ids] @uIRunner() return @@ -875,7 +882,9 @@ class App.CollectionController extends App.Controller lastOrderNew.push id # try to find positions of new items + @log 'debug', 'collectionSync lastOrderNew', lastOrderNew applyOrder = App.Utils.diffPositionAdd(lastOrderNew, newOrder) + @log 'debug', 'collectionSync applyOrder', applyOrder if !applyOrder @queue.push ['renderAll'] @uIRunner() @@ -888,7 +897,7 @@ class App.CollectionController extends App.Controller newItems = [] for apply in applyOrder - item = App[@model].find(apply.id) + item = @itemGet(apply.id) item.meta_position = apply.position newItems.push item @queue.push ['domChange', newItems] @@ -943,7 +952,7 @@ class App.CollectionController extends App.Controller for field of @observe @log 'debug', 'domChange|change|compare', field, currentItem[field], attributes[field] diff = !_.isEqual(currentItem[field], attributes[field]) - @log 'debug', 'domChange|diff', diff, item + @log 'debug', 'domChange|diff', diff if diff changedItems.push item @itemAttributesSet(item[@uniqKey], attributes) @@ -963,8 +972,10 @@ class App.CollectionController extends App.Controller @collectionOrderSet() @onRenderEnd() + itemDestroy: (id) => + App[@model].destroy(id) + itemsAll: => - #App[@model].all() App[@model].search(sortBy: @sortBy, order: @order) itemAttributesDiff: (item) => @@ -993,6 +1004,9 @@ class App.CollectionController extends App.Controller attributes[field] = item[field] attributes + itemGet: (id) => + App[@model].find(id) + renderParts: (items) => @log 'debug', 'renderParts', items for item in items @@ -1009,14 +1023,20 @@ class App.CollectionController extends App.Controller html = $(App.view(@template)( item: item )) + itemCount = Object.keys(@renderList).length @renderList[item[@uniqKey]] = html if el is false return html else if !el - position = item.meta_position + 1 - console.log('!el', item, position, item.meta_position) - #if item.meta_position - @el.find(".js-item:nth-child(#{position})").before(html) + position = item.meta_position + if itemCount > position + position += 1 + console.log('!el', item, position, item.meta_position, @el.find(".js-item:nth-child(#{position})")) + element = @el.find(".js-item:nth-child(#{position})") + if @insertPosition is 'before' + element.before(html) + else + element.after(html) else el.replaceWith(html) @@ -1029,7 +1049,6 @@ class App.CollectionController extends App.Controller click: (e) => row = $(e.target).closest('.js-item') id = row.data('id') - console.log('click id', id) @onClick(id, e) onClick: (id, e) -> @@ -1041,7 +1060,7 @@ class App.CollectionController extends App.Controller row = $(e.target).closest('.js-item') id = row.data('id') @onRemove(id,e) - App[@model].destroy(id) + @itemDestroy(id) onRemove: (id, e) -> # nothing diff --git a/app/assets/javascripts/app/controllers/agent_ticket_create.coffee b/app/assets/javascripts/app/controllers/agent_ticket_create.coffee index 817c4d57f..d2ccb4814 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_create.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_create.coffee @@ -19,12 +19,9 @@ class App.TicketCreate extends App.Controller @default_type = 'phone-in' # remember split info if exists - split = '' + @split = '' if @ticket_id && @article_id - split = "/#{@ticket_id}/#{@article_id}" - - # update navbar highlighting - @navupdate "#ticket/create/id/#{@id}#{split}" + @split = "/#{@ticket_id}/#{@article_id}" # lisen if view need to be rerendered @bind 'ticket_create_rerender', (defaults) => @@ -119,7 +116,7 @@ class App.TicketCreate extends App.Controller "#ticket/create/id/#{@id}" show: => - @navupdate(url: '#', type: 'menu') + @navupdate "#ticket/create/id/#{@id}#{@split}", type: 'menu' changed: => formCurrent = @formParam( @$('.ticket-create') ) diff --git a/app/assets/javascripts/app/controllers/taskbar_widget.coffee b/app/assets/javascripts/app/controllers/taskbar_widget.coffee index c891f9e65..2191c9f80 100644 --- a/app/assets/javascripts/app/controllers/taskbar_widget.coffee +++ b/app/assets/javascripts/app/controllers/taskbar_widget.coffee @@ -34,20 +34,26 @@ class App.TaskbarWidget extends App.CollectionController # bind to changes @bind('taskInit', => - console.log('renderAll aaa') - @renderAll() - console.log('renderAll bbb') - #@queue.push ['renderAll'] - #@uIRunner() + @queue.push ['renderAll'] + @uIRunner() ) @bind('taskUpdate', (tasks) => - @queue.push ['domChange', tasks] + @queue.push ['change', tasks] @uIRunner() ) - @bind('taskRemove', (task_ids) => - @queue.push ['domRemove', task_ids] + @bind('taskRemove', (tasks) => + @queue.push ['remove', tasks] @uIRunner() ) + @bind('taskCollectionOrderSet', (task_keys) => + @collectionOrderSet(task_keys) + ) + + itemGet: (key) -> + App.TaskManager.get(key) + + itemDestroy: (key) -> + App.TaskManager.remove(key) itemsAll: -> App.TaskManager.allWithMeta() diff --git a/app/assets/javascripts/app/controllers/ticket_overview.coffee b/app/assets/javascripts/app/controllers/ticket_overview.coffee index 4dc556a12..a493c67c8 100644 --- a/app/assets/javascripts/app/controllers/ticket_overview.coffee +++ b/app/assets/javascripts/app/controllers/ticket_overview.coffee @@ -386,7 +386,7 @@ class Table extends App.Controller 'click': callbackCheckbox ) - @setSelected( @selected ) + @setSelected(@selected) # start user popups @userPopups() diff --git a/app/assets/javascripts/app/controllers/widget/online_notification.coffee b/app/assets/javascripts/app/controllers/widget/online_notification.coffee index 1cacc0ff7..4ae7eed4e 100644 --- a/app/assets/javascripts/app/controllers/widget/online_notification.coffee +++ b/app/assets/javascripts/app/controllers/widget/online_notification.coffee @@ -136,6 +136,8 @@ class App.OnlineNotificationWidget extends App.Controller url: "#{@apiPath}/online_notifications/mark_all_as_read" data: JSON.stringify('') processData: true + success: (data, status, xhr) => + @fetch() ) updateHeight: -> @@ -219,6 +221,7 @@ class App.OnlineNotificationContentWidget extends App.CollectionController sortBy: 'created_at' order: 'DESC' alreadyShown: {} + insertPosition: 'before' onRenderEnd: -> @container.counterGen() diff --git a/app/assets/javascripts/app/lib/app_post/task_manager.coffee b/app/assets/javascripts/app/lib/app_post/task_manager.coffee index e841e4a10..e09c547e7 100644 --- a/app/assets/javascripts/app/lib/app_post/task_manager.coffee +++ b/app/assets/javascripts/app/lib/app_post/task_manager.coffee @@ -332,7 +332,7 @@ class _taskManagerSingleton extends App.Controller delete @allTasksByKey[key] # rerender taskbar - App.Event.trigger('taskRemove', [task.key]) + App.Event.trigger('taskRemove', [task]) # destroy in backend storage @taskDestroy(task) @@ -369,6 +369,7 @@ class _taskManagerSingleton extends App.Controller if task.prio isnt prio task.prio = prio @taskUpdate(task, true) + App.Event.trigger('taskCollectionOrderSet', order) # release one task release: (key) => diff --git a/app/assets/javascripts/app/lib/app_post/utils.coffee b/app/assets/javascripts/app/lib/app_post/utils.coffee index c8f78ab6e..87cf4624f 100644 --- a/app/assets/javascripts/app/lib/app_post/utils.coffee +++ b/app/assets/javascripts/app/lib/app_post/utils.coffee @@ -632,15 +632,22 @@ class App.Utils newOrderMethod = (a, b, applyOrder) -> for position of b if a[position] isnt b[position] + positionInt = parseInt(position) # changes to complex, whole rerender if _.contains(a, b[position]) + console.log('aaa too complex', a, b[position]) return false # insert new item and try next - a.splice(position, 0, b[position]) + a.splice(positionInt, 0, b[position]) + positionNew = 0 + for positionA of a + if b[positionA] is b[position] + positionNew = parseInt(positionA) + break apply = - position: parseInt(position) + position: positionNew id: b[position] applyOrder.push apply newOrderMethod(a, b, applyOrder) diff --git a/app/assets/javascripts/app/views/widget/task_item.jst.eco b/app/assets/javascripts/app/views/widget/task_item.jst.eco index 896001246..6852cc567 100644 --- a/app/assets/javascripts/app/views/widget/task_item.jst.eco +++ b/app/assets/javascripts/app/views/widget/task_item.jst.eco @@ -1,4 +1,4 @@ - +