From 36db80d45f867f197a8d23e41dd91e0bdbb9512f Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Tue, 18 Feb 2014 10:00:33 +0100 Subject: [PATCH] Add Ember.computed.limit and use it in LimitedArray arrayComputed was added recently in order to make array computations easier. Using arrayComputed we can improve LimitedArray which now uses an isolated multipurpose Ember.computed.limit. --- assets/scripts/lib/ext/ember/computed.js | 26 ++++++++ .../scripts/lib/travis/limited_array.coffee | 63 ++----------------- .../spec/unit/limited_array_spec.coffee | 3 +- assets/scripts/travis.coffee | 1 + 4 files changed, 35 insertions(+), 58 deletions(-) create mode 100644 assets/scripts/lib/ext/ember/computed.js diff --git a/assets/scripts/lib/ext/ember/computed.js b/assets/scripts/lib/ext/ember/computed.js new file mode 100644 index 00000000..089f8fe1 --- /dev/null +++ b/assets/scripts/lib/ext/ember/computed.js @@ -0,0 +1,26 @@ +Ember.computed.limit = function(dependentKey, limitKey) { + var options = { + addedItem: function(array, item, changeMeta, instanceMeta) { + var limit = Ember.get(this, limitKey); + if (changeMeta.index < limit) { + array.insertAt(changeMeta.index, item); + if (Ember.get(array, "length") > limit) { + array.popObject(); + } + } + return array; + }, + removedItem: function(array, item, changeMeta, instanceMeta) { + var limit = Ember.get(this, limitKey); + if (changeMeta.index < limit && changeMeta.index < Ember.get(array, 'length')) { + array.removeAt(changeMeta.index, 1); + var toPush = changeMeta.arrayChanged.objectAt(limit); + if (toPush) { + array.pushObject(toPush); + } + } + return array; + } + }; + return Ember.arrayComputed(dependentKey, limitKey, options); +}; diff --git a/assets/scripts/lib/travis/limited_array.coffee b/assets/scripts/lib/travis/limited_array.coffee index c6baaddc..f5f9941f 100644 --- a/assets/scripts/lib/travis/limited_array.coffee +++ b/assets/scripts/lib/travis/limited_array.coffee @@ -1,17 +1,7 @@ Travis.LimitedArray = Em.ArrayProxy.extend limit: 10 isLoadedBinding: 'content.isLoaded' - - init: -> - @_super.apply this, arguments - - arrangedContent: (-> - content = @get('content') - if @get('disabled') - content - else if content - content.slice(0, @get('limit')) - ).property('content', 'limit', 'disabled') + arrangedContent: Ember.computed.limit('content', 'limit') totalLength: (-> @get('content.length') @@ -21,54 +11,13 @@ Travis.LimitedArray = Em.ArrayProxy.extend totalLength = @get('totalLength') limit = @get('limit') - if @get('disabled') || totalLength <= limit - 0 - else - totalLength - limit - ).property('totalLength', 'limit', 'disabled') + left = totalLength - limit + if left < 0 then 0 else left + ).property('totalLength', 'limit') isMore: (-> - !@get('disabled') && @get('leftLength') > 0 + @get('leftLength') > 0 ).property('leftLength') showAll: -> - @set 'disabled', true - - contentArrayWillChange: (array, index, removedCount, addedCount) -> - @_super.apply this, arguments - - return if @get('disabled') - - if removedCount - arrangedContent = @get 'arrangedContent' - removedObjects = array.slice(index, index + removedCount); - arrangedContent.removeObjects(removedObjects) - - contentArrayDidChange: (array, index, removedCount, addedCount) -> - @_super.apply this, arguments - - return if @get('disabled') - - limit = @get('limit') - - if addedCount - if index < limit - addedObjects = array.slice(index, index + addedCount) - @get('arrangedContent').replace(index, 0, addedObjects) - - @balanceArray() - - balanceArray: -> - limit = @get 'limit' - arrangedContent = @get 'arrangedContent' - length = arrangedContent.get 'length' - content = @get 'content' - - if length > limit - arrangedContent.replace(limit, length - limit) - else if length < limit && content.get('length') > length - count = limit - length - while count > 0 - if next = content.find( (object) -> !arrangedContent.contains(object) ) - arrangedContent.pushObject(next) - count -= 1 + @set 'limit', Infinity diff --git a/assets/scripts/spec/unit/limited_array_spec.coffee b/assets/scripts/spec/unit/limited_array_spec.coffee index 818bbecc..8c482234 100644 --- a/assets/scripts/spec/unit/limited_array_spec.coffee +++ b/assets/scripts/spec/unit/limited_array_spec.coffee @@ -41,7 +41,8 @@ test 'allows to expand array to show all items', -> content = [1, 2, 3] array = Travis.LimitedArray.create content: content, limit: 2 - array.showAll() + Ember.run -> + array.showAll() equal( array.get('length'), 3) deepEqual( array.toArray(), [1, 2, 3]) diff --git a/assets/scripts/travis.coffee b/assets/scripts/travis.coffee index f01a0e0b..dea8798d 100644 --- a/assets/scripts/travis.coffee +++ b/assets/scripts/travis.coffee @@ -1,5 +1,6 @@ require 'ext/jquery' require 'ext/ember/namespace' +require 'ext/ember/computed' require 'app' window.ENV ||= {}