Fix LimitedArray to work properly with SortableMixin
This commit is contained in:
parent
9f7796a023
commit
23a58866bd
|
@ -2,7 +2,6 @@ require 'travis/limited_array'
|
||||||
|
|
||||||
Travis.ReposController = Ember.ArrayController.extend
|
Travis.ReposController = Ember.ArrayController.extend
|
||||||
defaultTab: 'recent'
|
defaultTab: 'recent'
|
||||||
sortProperties: ['sortOrder']
|
|
||||||
isLoadedBinding: 'content.isLoaded'
|
isLoadedBinding: 'content.isLoaded'
|
||||||
|
|
||||||
init: ->
|
init: ->
|
||||||
|
@ -21,7 +20,10 @@ Travis.ReposController = Ember.ArrayController.extend
|
||||||
|
|
||||||
viewRecent: ->
|
viewRecent: ->
|
||||||
content = Travis.LimitedArray.create
|
content = Travis.LimitedArray.create
|
||||||
content: Travis.Repo.find()
|
content: Em.ArrayProxy.extend(Em.SortableMixin).create(
|
||||||
|
sortProperties: ['sortOrder']
|
||||||
|
content: Travis.Repo.find()
|
||||||
|
)
|
||||||
limit: 30
|
limit: 30
|
||||||
@set('content', content)
|
@set('content', content)
|
||||||
# @set('content', Travis.Repo.find())
|
# @set('content', Travis.Repo.find())
|
||||||
|
|
|
@ -45,7 +45,6 @@
|
||||||
didInsertElement: ->
|
didInsertElement: ->
|
||||||
queues = for queue in Travis.QUEUES
|
queues = for queue in Travis.QUEUES
|
||||||
Travis.LimitedArray.create
|
Travis.LimitedArray.create
|
||||||
insertAtTheBeginning: false
|
|
||||||
content: Travis.Job.queued(queue.name), limit: 20
|
content: Travis.Job.queued(queue.name), limit: 20
|
||||||
id: "queue_#{queue.name}"
|
id: "queue_#{queue.name}"
|
||||||
name: queue.display
|
name: queue.display
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
Travis.LimitedArray = Em.ArrayProxy.extend
|
Travis.LimitedArray = Em.ArrayProxy.extend
|
||||||
limit: 10
|
limit: 10
|
||||||
isLoadedBinding: 'content.isLoaded'
|
isLoadedBinding: 'content.isLoaded'
|
||||||
insertAtTheBeginning: true
|
|
||||||
|
|
||||||
init: ->
|
init: ->
|
||||||
@_super.apply this, arguments
|
@_super.apply this, arguments
|
||||||
|
@ -21,21 +20,20 @@ Travis.LimitedArray = Em.ArrayProxy.extend
|
||||||
leftLength: (->
|
leftLength: (->
|
||||||
totalLength = @get('totalLength')
|
totalLength = @get('totalLength')
|
||||||
limit = @get('limit')
|
limit = @get('limit')
|
||||||
if totalLength > limit
|
|
||||||
totalLength - limit
|
if @get('disabled') || totalLength <= limit
|
||||||
else
|
|
||||||
0
|
0
|
||||||
).property('totalLength', 'limit')
|
else
|
||||||
|
totalLength - limit
|
||||||
|
).property('totalLength', 'limit', 'disabled')
|
||||||
|
|
||||||
isMore: (->
|
isMore: (->
|
||||||
!@get('disabled') && @get('leftLength') > 0
|
!@get('disabled') && @get('leftLength') > 0
|
||||||
).property('leftLength')
|
).property('leftLength')
|
||||||
|
|
||||||
showAll: ->
|
showAll: ->
|
||||||
@set 'limit', 1000000000
|
|
||||||
@set 'disabled', true
|
@set 'disabled', true
|
||||||
|
|
||||||
|
|
||||||
contentArrayWillChange: (array, index, removedCount, addedCount) ->
|
contentArrayWillChange: (array, index, removedCount, addedCount) ->
|
||||||
@_super.apply this, arguments
|
@_super.apply this, arguments
|
||||||
|
|
||||||
|
@ -51,14 +49,12 @@ Travis.LimitedArray = Em.ArrayProxy.extend
|
||||||
|
|
||||||
return if @get('disabled')
|
return if @get('disabled')
|
||||||
|
|
||||||
|
limit = @get('limit')
|
||||||
|
|
||||||
if addedCount
|
if addedCount
|
||||||
arrangedContent = @get('arrangedContent')
|
if index < limit
|
||||||
addedObjects = array.slice(index, index + addedCount)
|
addedObjects = array.slice(index, index + addedCount)
|
||||||
for object in addedObjects
|
@replaceContent(index, 0, addedObjects)
|
||||||
if @get 'insertAtTheBeginning'
|
|
||||||
arrangedContent.unshiftObject(object)
|
|
||||||
else
|
|
||||||
arrangedContent.pushObject(object)
|
|
||||||
|
|
||||||
@balanceArray()
|
@balanceArray()
|
||||||
|
|
||||||
|
@ -74,8 +70,5 @@ Travis.LimitedArray = Em.ArrayProxy.extend
|
||||||
count = limit - length
|
count = limit - length
|
||||||
while count > 0
|
while count > 0
|
||||||
if next = content.find( (object) -> !arrangedContent.contains(object) )
|
if next = content.find( (object) -> !arrangedContent.contains(object) )
|
||||||
if @get('insertAtTheBeginning')
|
arrangedContent.pushObject(next)
|
||||||
arrangedContent.unshiftObject(next)
|
|
||||||
else
|
|
||||||
arrangedContent.pushObject(next)
|
|
||||||
count -= 1
|
count -= 1
|
||||||
|
|
46
assets/scripts/spec/unit/limited_array_spec.coffee
Normal file
46
assets/scripts/spec/unit/limited_array_spec.coffee
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
describe 'Travis.LimitedArray', ->
|
||||||
|
it 'limits given content', ->
|
||||||
|
content = [1, 2, 3]
|
||||||
|
array = Travis.LimitedArray.create content: content, limit: 2
|
||||||
|
expect( array.get('length') ).toEqual 2
|
||||||
|
expect( array.toArray() ).toEqual [1, 2]
|
||||||
|
|
||||||
|
it 'inserts content at the right place when unshifting', ->
|
||||||
|
content = [1, 2, 3]
|
||||||
|
array = Travis.LimitedArray.create content: content, limit: 2
|
||||||
|
content.unshiftObject 0
|
||||||
|
expect( array.get('length') ).toEqual 2
|
||||||
|
expect( array.toArray() ).toEqual [0, 1]
|
||||||
|
|
||||||
|
it 'does not insert content when it\'s inserted not in the limited range', ->
|
||||||
|
content = [1, 2, 3]
|
||||||
|
array = Travis.LimitedArray.create content: content, limit: 2
|
||||||
|
content.pushObject 0
|
||||||
|
expect( array.get('length') ).toEqual 2
|
||||||
|
expect( array.toArray() ).toEqual [1, 2]
|
||||||
|
|
||||||
|
it 'properly removes items', ->
|
||||||
|
content = [1, 2, 3]
|
||||||
|
array = Travis.LimitedArray.create content: content, limit: 2
|
||||||
|
content.shiftObject()
|
||||||
|
|
||||||
|
expect( array.get('length') ).toEqual 2
|
||||||
|
expect( array.toArray() ).toEqual [2, 3]
|
||||||
|
|
||||||
|
content.shiftObject()
|
||||||
|
|
||||||
|
expect( array.get('length') ).toEqual 1
|
||||||
|
expect( array.toArray() ).toEqual [3]
|
||||||
|
|
||||||
|
content.shiftObject()
|
||||||
|
|
||||||
|
expect( array.get('length') ).toEqual 0
|
||||||
|
|
||||||
|
it 'allows to expand array to show all items', ->
|
||||||
|
content = [1, 2, 3]
|
||||||
|
array = Travis.LimitedArray.create content: content, limit: 2
|
||||||
|
|
||||||
|
array.showAll()
|
||||||
|
|
||||||
|
expect( array.get('length') ).toEqual 3
|
||||||
|
expect( array.toArray() ).toEqual [1, 2, 3]
|
Loading…
Reference in New Issue
Block a user