From 23a58866bd106e1f93ffa117568652c45257dbc1 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Thu, 13 Dec 2012 03:10:37 +0100 Subject: [PATCH] Fix LimitedArray to work properly with SortableMixin --- assets/scripts/app/controllers/repos.coffee | 6 ++- assets/scripts/app/views/sidebar.coffee | 1 - .../scripts/lib/travis/limited_array.coffee | 29 +++++------- .../spec/unit/limited_array_spec.coffee | 46 +++++++++++++++++++ 4 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 assets/scripts/spec/unit/limited_array_spec.coffee diff --git a/assets/scripts/app/controllers/repos.coffee b/assets/scripts/app/controllers/repos.coffee index a5b70701..fc9fe39c 100644 --- a/assets/scripts/app/controllers/repos.coffee +++ b/assets/scripts/app/controllers/repos.coffee @@ -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()) diff --git a/assets/scripts/app/views/sidebar.coffee b/assets/scripts/app/views/sidebar.coffee index 781d370d..3b34fdf0 100644 --- a/assets/scripts/app/views/sidebar.coffee +++ b/assets/scripts/app/views/sidebar.coffee @@ -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 diff --git a/assets/scripts/lib/travis/limited_array.coffee b/assets/scripts/lib/travis/limited_array.coffee index d58dd352..b9f1a6d8 100644 --- a/assets/scripts/lib/travis/limited_array.coffee +++ b/assets/scripts/lib/travis/limited_array.coffee @@ -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 diff --git a/assets/scripts/spec/unit/limited_array_spec.coffee b/assets/scripts/spec/unit/limited_array_spec.coffee new file mode 100644 index 00000000..f4db7ef4 --- /dev/null +++ b/assets/scripts/spec/unit/limited_array_spec.coffee @@ -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]