Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split state and record for a list item, fixed #129 #130

Merged
merged 8 commits into from
Apr 26, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions addon/components/frost-list-item-expansion.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import Ember from 'ember'
const {ViewUtils} = Ember
const {isSimpleClick} = ViewUtils
import computed, {readOnly} from 'ember-computed-decorators'
import {Component} from 'ember-frost-core'
import {PropTypes} from 'ember-prop-types'

Expand All @@ -30,6 +29,7 @@ export default Component.extend({
PropTypes.EmberObject,
PropTypes.object
]),
isExpanded: PropTypes.bool,

onExpand: PropTypes.func.isRequired
},
Expand All @@ -41,12 +41,6 @@ export default Component.extend({

// == Computed Properties ===================================================

@readOnly
@computed('model.isExpanded')
isExpanded (isExpanded) {
return isExpanded
},

// == Functions =============================================================

// == DOM Events ============================================================
Expand Down
8 changes: 1 addition & 7 deletions addon/components/frost-list-item-selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import Ember from 'ember'
const {ViewUtils} = Ember
const {isSimpleClick} = ViewUtils
import computed, {readOnly} from 'ember-computed-decorators'
import {Component} from 'ember-frost-core'
import {PropTypes} from 'ember-prop-types'

Expand All @@ -28,6 +27,7 @@ export default Component.extend({
PropTypes.EmberObject,
PropTypes.object
]),
isSelected: PropTypes.bool,

onSelect: PropTypes.func.isRequired
},
Expand All @@ -39,12 +39,6 @@ export default Component.extend({

// == Computed Properties ===================================================

@readOnly
@computed('model.isSelected')
isSelected (isSelected) {
return isSelected
},

// == Functions =============================================================

// == DOM Events ============================================================
Expand Down
2 changes: 2 additions & 0 deletions addon/components/frost-list-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export default Component.extend({
PropTypes.EmberObject,
PropTypes.object
]).isRequired,
isExpanded: PropTypes.bool,
isSelected: PropTypes.bool,

onExpand: PropTypes.func,
onSelect: PropTypes.func
Expand Down
31 changes: 10 additions & 21 deletions addon/components/frost-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import Ember from 'ember'
const {$, A, get, isEmpty, isNone, run, set} = Ember
const {$, A, get, isEmpty, isNone, run} = Ember
import computed, {readOnly} from 'ember-computed-decorators'
import {Component} from 'ember-frost-core'
import {selection} from 'ember-frost-list'
Expand Down Expand Up @@ -102,15 +102,15 @@ export default Component.extend({
}

return items.map(item => {
run.next(() => {
set(item, 'isExpanded', isEmpty(expandedItems) ? false : expandedItems.some(
selectedItem => _itemComparator(selectedItem, item))
)
set(item, 'isSelected', isEmpty(selectedItems) ? false : selectedItems.some(
selectedItem => _itemComparator(selectedItem, item))
)
})
return item
return {
record: item,
states: {
isExpanded: isEmpty(expandedItems) ? false : expandedItems.some(
selectedItem => _itemComparator(selectedItem, item)),
isSelected: isEmpty(selectedItems) ? false : selectedItems.some(
selectedItem => _itemComparator(selectedItem, item))
}
}
})
},

Expand All @@ -129,17 +129,6 @@ export default Component.extend({

// == Lifecycle Hooks =======================================================

didUpdateAttrs ({newAttrs}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry - can you put a quick explanation on the removal of this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need the frost scroll with the vertical-collection. The vertical collection has the scrolling capability.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted this 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you push the changeset? It still appears as removed in the PR

if (newAttrs.scrollTop) {
// TODO Push this down into frost-scroll
const scrollbar = this.$('.frost-scroll')[0]
if (scrollbar) {
scrollbar.scrollTop = newAttrs.scrollTop
window.Ps.update(scrollbar)
}
}
},

init () {
this._super(...arguments)
const itemKey = this.get('itemKey')
Expand Down
69 changes: 28 additions & 41 deletions addon/templates/components/frost-list-content-container.hbs
Original file line number Diff line number Diff line change
@@ -1,44 +1,31 @@
{{! Template for the frost-list-content-container component }}
{{#vertical-collection
alwaysRemeasure=true
alwaysUseDefaultHeight=alwaysUseDefaultHeight
bufferSize=bufferSize
content=items
defaultHeight=defaultHeight
firstReached=onLoadPrevious
lastReached=onLoadNext
scrollPosition=scrollTop
useContentProxy=false

{{#if pagination}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to remove the "basic" {{#each}} loop in the pagination case? I'd prefer to not introduce {{#vertical-collection}} for those people using the pagination scenario in a minor release

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove it. I thought you asked me to replace the each by a vertical-collection.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'd like to keep the existing pagination so that we don't risk breaking

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping me when that's done and I'll merge

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I reverted this :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, still appears as removed in the PR

{{#frost-scroll
hook=(concat hookPrefix '-scroll')
}}
{{#each items as |model index|}}
{{yield model index}}
{{else}}
{{yield to="inverse"}}
{{/each}}
{{/frost-scroll}}
{{else}}
{{#vertical-collection
alwaysRemeasure=true
alwaysUseDefaultHeight=alwaysUseDefaultHeight
bufferSize=bufferSize
content=items
defaultHeight=defaultHeight
firstReached=onLoadPrevious
lastReached=onLoadNext
scrollPosition=scrollTop
useContentProxy=false

key='@identity'
resizeDebounce=64
firstVisibleChanged=null
lastVisibleChanged=null
didMountCollection=null
idForFirstItem=null
renderFromLast=false
renderAllInitially=false
shouldRender=true
minimumMovement=15
containerSelector=null
containerHeight=null
key='@identity'
resizeDebounce=64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

64?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what this is. I simply used the same vertical collection as below... I feel like we have a lot of properties that might not be necessary in the vertical collection.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that's fine - it's like 15 FPSish - just an odd default, but no problem

firstVisibleChanged=null
lastVisibleChanged=null
didMountCollection=null
idForFirstItem=null
renderFromLast=false
renderAllInitially=false
shouldRender=true
minimumMovement=15
containerSelector=null
containerHeight=null

as |model index|
}}
{{yield model index}}
{{else}}
{{yield to="inverse"}}
{{/vertical-collection}}
{{/if}}
as |model index|
}}
{{yield model index}}
{{else}}
{{yield to="inverse"}}
{{/vertical-collection}}
18 changes: 12 additions & 6 deletions addon/templates/components/frost-list.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
'frost-list-item-container'
(if (eq index 0) ' first')
(if (eq index (sub _items.length 1)) ' last')
(if model.isSelected ' is-selected')
(if model.states.isSelected ' is-selected')
}}
data-test={{hook (concat hook '-item-container') index=index }}
>
Expand All @@ -54,7 +54,8 @@
{{frost-list-item-expansion
hook=(concat hookPrefix '-expansion')
hookQualifiers=(hash index=index)
model=model
model=model.record
isExpanded=model.states.isExpanded
onExpand=(action '_expand')
}}
{{/if}}
Expand All @@ -63,25 +64,30 @@
{{frost-list-item-selection
hook=(concat hookPrefix '-selection')
hookQualifiers=(hash index=index)
model=model
model=model.record
isSelected=model.states.isSelected
onSelect=(action '_select')
}}
{{/if}}

{{component item
hook=(concat hookPrefix '-item')
hookQualifiers=(hash index=index)
model=model
model=model.record
isExpanded=model.states.isExpanded
isSelected=model.states.isSelected
onSelect=(action '_select')
}}
</div>

{{#if model.isExpanded}}
{{#if model.states.isExpanded}}
<div class='frost-list-item-container-expansion'>
{{component itemExpansion
hook=(concat hookPrefix '-item-expansion')
hookQualifiers=(hash index=index)
model=model
model=model.record
isExpanded=model.states.isExpanded
isSelected=model.states.isSelected
onSelect=(action '_select')
}}
</div>
Expand Down
35 changes: 30 additions & 5 deletions addon/utils/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default {
*/
basic (selectedItems, item, rangeState, itemComparator) {
// If a previous set of selections is present
const index = selectedItems.findIndex(selectedItem => itemComparator(selectedItem, item))
const index = this._findIndex(selectedItems, item, itemComparator)
if (selectedItems.get('length') > 1 || index === -1) {
// Clear the other selections and select the item
selectedItems.setObjects([item])
Expand Down Expand Up @@ -63,7 +63,7 @@ export default {
range (items, selectedItems, item, rangeState, itemComparator, itemKey) {
// If an anchor isn't set or in the current list of items, then set the anchor and exit
const rangeAnchor = rangeState['anchor']
const anchor = rangeAnchor ? items.findIndex(currentItem => itemComparator(currentItem, rangeAnchor)) : -1
const anchor = this._findIndex(items, rangeAnchor, itemComparator)
if (anchor === -1) {
// Range select is always a positive selection (no deselect)
rangeState['anchor'] = item
Expand All @@ -77,7 +77,7 @@ export default {
return
}
// Find the indices of the endpoint
const endpoint = items.findIndex(currentItem => itemComparator(currentItem, item))
const endpoint = this._findIndex(items, item, itemComparator)

// Select all of the items between the anchor and the item (inclusive)
if (anchor < endpoint) {
Expand All @@ -88,7 +88,7 @@ export default {

// If an endpoint was already selected remove selected items that were
// in the previous range but aren't in the new range
const previousEndpoint = items.findIndex(currentItem => itemComparator(currentItem, rangeState['endpoint']))
const previousEndpoint = this._findIndex(items, rangeState['endpoint'], itemComparator)
if (previousEndpoint >= 0) {
// If both endpoints are above the anchor
if (anchor < endpoint && anchor < previousEndpoint) {
Expand Down Expand Up @@ -131,7 +131,7 @@ export default {
* @param {Function} itemComparator - comparator for items
*/
specific (selectedItems, item, rangeState, itemComparator) {
const index = selectedItems.findIndex(selectedItem => itemComparator(selectedItem, item))
const index = this._findIndex(selectedItems, item, itemComparator)
const isCurrentlySelected = (index >= 0)
const isSelected = !isCurrentlySelected

Expand All @@ -146,5 +146,30 @@ export default {
} else {
selectedItems.removeAt(index)
}
},

/**
* Reproducing the `findIndex` behavior to avoid use cases where it's not defined.
* @param {Array} array the array of elements
* @param {Object} rhs right hand side value to compare to the elements in the array
* @param {Function} compareFct the compare function
* @returns {Number} the index of the rhs value if it's in the array otherwise -1
*/
_findIndex (array, rhs, compareFct) {
if (array.findIndex) {
return array.findIndex(currentItem => compareFct(currentItem, rhs))
} else {
let findIndex = -1
if (array && rhs && compareFct) {
for (let index = 0; index !== array.length; index++) {
const lhs = array.objectAt(index)
if (compareFct(lhs, rhs)) {
findIndex = index
break
}
}
}
return findIndex
}
}
}
32 changes: 24 additions & 8 deletions blueprints/ember-frost-list/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
const blueprintHelper = require('ember-frost-core/blueprint-helper')

module.exports = {
afterInstall: function () {
return this.addAddonsToProject({
packages: [
{name: 'ember-frost-core', target: '^1.5.1'},
{name: 'ember-frost-sort', target: '^7.0.0'},
{name: 'ember-math-helpers', target: '^2.0.0'},
{name: 'smoke-and-mirrors', target: '~0.6.2'}
]
afterInstall: function (options) {
const addonsToAdd = [
{name: 'ember-frost-core', target: '^1.14.3'},
{name: 'ember-frost-sort', target: '^7.0.0'},
{name: 'ember-math-helpers', target: '^2.0.5'},
{name: 'smoke-and-mirrors', target: 'github:ciena-frost/smoke-and-mirrors'}
]

// Get the packages installed in the consumer app/addon. Packages that are already installed in the consumer within
// the required semver range will not be re-installed or have blueprints re-run.
const consumerPackages = blueprintHelper.consumer.getPackages(options)

// Get the packages to install (not already installed) from a list of potential packages
return blueprintHelper.packageHandler.getPkgsToInstall(addonsToAdd, consumerPackages).then((pkgsToInstall) => {
if (pkgsToInstall.length !== 0) {
// Call the blueprint hook
return this.addAddonsToProject({
packages: pkgsToInstall
})
}
})
},

normalizeEntityName: function () {
// this prevents an error when the entityName is
// not specified (since that doesn't actually matter to us)
}
}
Loading