Merge branch 'sortable-fix'

This commit is contained in:
Piotr Sarnacki 2012-12-14 05:12:06 +01:00
commit 4bdf94484f
4 changed files with 61 additions and 21 deletions

View File

@ -2,7 +2,6 @@ require 'travis/limited_array'
Travis.ReposController = Ember.ArrayController.extend
defaultTab: 'recent'
sortProperties: ['sortOrder']
isLoadedBinding: 'content.isLoaded'
init: ->
@ -21,7 +20,10 @@ Travis.ReposController = Ember.ArrayController.extend
viewRecent: ->
content = Travis.LimitedArray.create
content: Travis.Repo.find()
content: Em.ArrayProxy.extend(Em.SortableMixin).create(
sortProperties: ['sortOrder']
content: Travis.Repo.find()
)
limit: 30
@set('content', content)
# @set('content', Travis.Repo.find())

View File

@ -45,7 +45,6 @@
didInsertElement: ->
queues = for queue in Travis.QUEUES
Travis.LimitedArray.create
insertAtTheBeginning: false
content: Travis.Job.queued(queue.name), limit: 20
id: "queue_#{queue.name}"
name: queue.display

View File

@ -1,7 +1,6 @@
Travis.LimitedArray = Em.ArrayProxy.extend
limit: 10
isLoadedBinding: 'content.isLoaded'
insertAtTheBeginning: true
init: ->
@_super.apply this, arguments
@ -21,21 +20,20 @@ Travis.LimitedArray = Em.ArrayProxy.extend
leftLength: (->
totalLength = @get('totalLength')
limit = @get('limit')
if totalLength > limit
totalLength - limit
else
if @get('disabled') || totalLength <= limit
0
).property('totalLength', 'limit')
else
totalLength - limit
).property('totalLength', 'limit', 'disabled')
isMore: (->
!@get('disabled') && @get('leftLength') > 0
).property('leftLength')
showAll: ->
@set 'limit', 1000000000
@set 'disabled', true
contentArrayWillChange: (array, index, removedCount, addedCount) ->
@_super.apply this, arguments
@ -51,14 +49,12 @@ Travis.LimitedArray = Em.ArrayProxy.extend
return if @get('disabled')
limit = @get('limit')
if addedCount
arrangedContent = @get('arrangedContent')
addedObjects = array.slice(index, index + addedCount)
for object in addedObjects
if @get 'insertAtTheBeginning'
arrangedContent.unshiftObject(object)
else
arrangedContent.pushObject(object)
if index < limit
addedObjects = array.slice(index, index + addedCount)
@replaceContent(index, 0, addedObjects)
@balanceArray()
@ -74,8 +70,5 @@ Travis.LimitedArray = Em.ArrayProxy.extend
count = limit - length
while count > 0
if next = content.find( (object) -> !arrangedContent.contains(object) )
if @get('insertAtTheBeginning')
arrangedContent.unshiftObject(next)
else
arrangedContent.pushObject(next)
arrangedContent.pushObject(next)
count -= 1

View 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]