Improved App.CollectionController controller.
This commit is contained in:
parent
15cfe935ea
commit
3d594e11b3
9 changed files with 74 additions and 29 deletions
|
@ -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
|
||||
|
|
|
@ -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') )
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -386,7 +386,7 @@ class Table extends App.Controller
|
|||
'click': callbackCheckbox
|
||||
)
|
||||
|
||||
@setSelected( @selected )
|
||||
@setSelected(@selected)
|
||||
|
||||
# start user popups
|
||||
@userPopups()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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) =>
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<a href="<%- @item.meta.url %>" title="<%= @item.meta.title %>" class="nav-tab task js-locationVerify <%= @item.meta.class %><% if @item.active: %> is-active<% end %><% if @item.notify: %> is-modified<% end %>" data-key="<%- @item.key %>">
|
||||
<a href="<%- @item.meta.url %>" title="<%= @item.meta.title %>" class="nav-tab task js-item js-locationVerify <%= @item.meta.class %><% if @item.active: %> is-active<% end %><% if @item.notify: %> is-modified<% end %>" data-key="<%- @item.key %>">
|
||||
<div class="nav-tab-icon" title="<%- @Ti(@item.meta.iconTitle) %>">
|
||||
<% if @item.meta.type is 'task': %>
|
||||
<% if @item.notify: %>
|
||||
|
|
|
@ -1468,6 +1468,18 @@ test("check diffPosition format", function() {
|
|||
result = false
|
||||
verify = App.Utils.diffPositionAdd(a, b)
|
||||
deepEqual(verify, result)
|
||||
|
||||
a = ['Ticket-347', 'TicketCreateScreen-2217']
|
||||
b = ['Ticket-347', 'TicketCreateScreen-2217', 'TicketCreateScreen-71517']
|
||||
result = [
|
||||
{
|
||||
position: 2,
|
||||
id: 'TicketCreateScreen-71517',
|
||||
},
|
||||
]
|
||||
verify = App.Utils.diffPositionAdd(a, b)
|
||||
deepEqual(verify, result)
|
||||
|
||||
});
|
||||
|
||||
}
|
Loading…
Reference in a new issue