Skip to content

Commit

Permalink
Switch back to old API for selection
Browse files Browse the repository at this point in the history
A previous commit (88498d4) changed the
`selection` API so that it was always an array, even if `selectionMode` is
`single` or `none`. This commit changes back to the previous state.
* When `selectionMode` is `none`, `selection` is `null`
* When it is `single`, it is the selected item
* When it is `multiple`, it is an array of selected items
  • Loading branch information
Phil Nachum committed Feb 23, 2015
1 parent d75c4ad commit 074ad3d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 71 deletions.
9 changes: 6 additions & 3 deletions app/templates/ember_table/documentation.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,12 @@
<td>undefined</td>
<td>
<p>
An array of the rows currently selected. If
<code>selectionMode</code> is set to <code>'single'</code>, the array
will contain either one or zero elements.
The currently selected elements. If <code>selectionMode</code> is set
to <code>'none'</code>, <code>selection</code> is null. If
<code>selectionMode</code> is set to <code>'single'</code>,
<code>selection</code> is the selected element. If
<code>selectionMode</code> is set to <code>'multiple'</code>,
<code>selection</code> is an array of the selected elements.
</p>
</td>
</tr>
Expand Down
87 changes: 55 additions & 32 deletions dist/ember-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -1147,13 +1147,29 @@ Ember.Table.EmberTableComponent = Ember.Component.extend(Ember.AddeparMixins.Sty
columnMode: 'standard',
selectionMode: 'single',
selection: Ember.computed(function(key, val) {
var selection, selectionMode;
selectionMode = this.get('selectionMode');
if (arguments.length > 1 && val) {
this.get('persistedSelection').clear();
this.get('persistedSelection').addObjects(val);
this.get('rangeSelection').clear();
switch (selectionMode) {
case 'single':
this.get('persistedSelection').addObject(val);
break;
case 'multiple':
this.get('persistedSelection').addObjects(val);
}
}
selection = this.get('persistedSelection').copy().addObjects(this.get('rangeSelection'));
switch (selectionMode) {
case 'none':
return null;
case 'single':
return selection[0] || null;
case 'multiple':
return selection;
}
return this.get('persistedSelection').copy().addObjects(this.get('rangeSelection'));
}).property('persistedSelection.[]', 'rangeSelection.[]'),
}).property('persistedSelection.[]', 'rangeSelection.[]', 'selectionMode'),
isEmberTable: true,
columnsFillTable: true,
init: function() {
Expand Down Expand Up @@ -1458,7 +1474,14 @@ Ember.Table.EmberTableComponent = Ember.Component.extend(Ember.AddeparMixins.Sty
},
lastSelected: null,
isSelected: function(row) {
return this.get('selection').contains(row.get('content'));
switch (this.get('selectionMode')) {
case 'none':
return false;
case 'single':
return this.get('selection') === row.get('content');
case 'multiple':
return this.get('selection').contains(row.get('content'));
}
},
setSelected: function(row, val) {
var item;
Expand All @@ -1483,37 +1506,37 @@ Ember.Table.EmberTableComponent = Ember.Component.extend(Ember.AddeparMixins.Sty
if (!item) {
return;
}
if (this.get('selectionMode') === 'none') {
return;
}
if (this.get('selectionMode') === 'single') {
this.get('persistedSelection').clear();
return this.get('persistedSelection').addObject(item);
} else {
if (event.shiftKey) {
this.get('rangeSelection').clear();
lastIndex = this.rowIndex(this.get('lastSelected'));
if (lastIndex === -1) {
lastIndex = 0;
}
curIndex = this.rowIndex(this.getRowForEvent(event));
minIndex = Math.min(lastIndex, curIndex);
maxIndex = Math.max(lastIndex, curIndex);
return this.get('rangeSelection').addObjects(this.get('bodyContent').slice(minIndex, maxIndex + 1).mapBy('content'));
} else {
if (!event.ctrlKey && !event.metaKey) {
this.get('persistedSelection').clear();
switch (this.get('selectionMode')) {
case 'none':
break;
case 'single':
this.get('persistedSelection').clear();
return this.get('persistedSelection').addObject(item);
case 'multiple':
if (event.shiftKey) {
this.get('rangeSelection').clear();
lastIndex = this.rowIndex(this.get('lastSelected'));
if (lastIndex === -1) {
lastIndex = 0;
}
curIndex = this.rowIndex(this.getRowForEvent(event));
minIndex = Math.min(lastIndex, curIndex);
maxIndex = Math.max(lastIndex, curIndex);
return this.get('rangeSelection').addObjects(this.get('bodyContent').slice(minIndex, maxIndex + 1).mapBy('content'));
} else {
this.persistSelection();
if (!event.ctrlKey && !event.metaKey) {
this.get('persistedSelection').clear();
this.get('rangeSelection').clear();
} else {
this.persistSelection();
}
if (this.get('persistedSelection').contains(item)) {
this.get('persistedSelection').removeObject(item);
} else {
this.get('persistedSelection').addObject(item);
}
return this.set('lastSelected', row);
}
if (this.get('persistedSelection').contains(item)) {
this.get('persistedSelection').removeObject(item);
} else {
this.get('persistedSelection').addObject(item);
}
return this.set('lastSelected', row);
}
}
},
findRow: function(content) {
Expand Down
4 changes: 2 additions & 2 deletions dist/ember-table.min.js

Large diffs are not rendered by default.

82 changes: 48 additions & 34 deletions src/component.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,21 @@ Ember.AddeparMixins.ResizeHandlerMixin,
# An array of the rows currently selected. If `selectionMode` is set to
# 'single', the array will contain either one or zero elements.
selection: Ember.computed (key, val) ->
selectionMode = @get 'selectionMode'
if arguments.length > 1 and val
@get('persistedSelection').clear()
@get('persistedSelection').addObjects val
@get('rangeSelection').clear()
@get('persistedSelection').copy().addObjects(@get('rangeSelection'))
.property 'persistedSelection.[]', 'rangeSelection.[]'
switch selectionMode
when 'single'
@get('persistedSelection').addObject val
when 'multiple'
@get('persistedSelection').addObjects val
selection = @get('persistedSelection').copy().addObjects(@get('rangeSelection'))
switch selectionMode
when 'none' then null
when 'single' then (selection[0] or null)
when 'multiple' then selection
.property 'persistedSelection.[]', 'rangeSelection.[]', 'selectionMode'

# ---------------------------------------------------------------------------
# Internal properties
Expand Down Expand Up @@ -110,7 +119,7 @@ Ember.AddeparMixins.ResizeHandlerMixin,

# TODO(new-api): eliminate view alias
# specify the view class to use for rendering the table rows
tableRowView: 'Ember.Table.TableRow'
tableRowView: 'Ember.Table.TableRow'
tableRowViewClass: Ember.computed.alias 'tableRowView'

onColumnSort: (column, newIndex) ->
Expand All @@ -137,14 +146,14 @@ Ember.AddeparMixins.ResizeHandlerMixin,
.property()

fixedColumns: Ember.computed ->
columns = @get 'columns'
columns = @get 'columns'
return Ember.A() unless columns
numFixedColumns = @get('numFixedColumns') or 0
columns.slice(0, numFixedColumns) or []
.property 'columns.@each', 'numFixedColumns'

tableColumns: Ember.computed ->
columns = @get 'columns'
columns = @get 'columns'
return Ember.A() unless columns
numFixedColumns = @get('numFixedColumns') or 0
columns.slice(numFixedColumns, columns.get('length')) or []
Expand Down Expand Up @@ -377,7 +386,10 @@ Ember.AddeparMixins.ResizeHandlerMixin,
lastSelected: null

isSelected: (row) ->
@get('selection').contains row.get('content')
switch @get('selectionMode')
when 'none' then no
when 'single' then @get('selection') is row.get('content')
when 'multiple' then @get('selection').contains row.get('content')

setSelected: (row, val) ->
@persistSelection()
Expand All @@ -399,37 +411,39 @@ Ember.AddeparMixins.ResizeHandlerMixin,
row = @getRowForEvent event
item = row?.get 'content'
return unless item
return if @get('selectionMode') is 'none'
if @get('selectionMode') is 'single'
@get('persistedSelection').clear()
@get('persistedSelection').addObject item
else
if event.shiftKey
@get('rangeSelection').clear()
switch @get('selectionMode')
when 'none'
return
when 'single'
@get('persistedSelection').clear()
@get('persistedSelection').addObject item
when 'multiple'
if event.shiftKey
@get('rangeSelection').clear()

lastIndex = @rowIndex(@get('lastSelected'))
# If the last selected row is no longer in the table, use the
# first row in the table
lastIndex = 0 if lastIndex is -1
lastIndex = @rowIndex(@get('lastSelected'))
# If the last selected row is no longer in the table, use the
# first row in the table
lastIndex = 0 if lastIndex is -1

curIndex = @rowIndex(@getRowForEvent(event))
curIndex = @rowIndex(@getRowForEvent(event))

minIndex = Math.min(lastIndex, curIndex)
maxIndex = Math.max(lastIndex, curIndex)
minIndex = Math.min(lastIndex, curIndex)
maxIndex = Math.max(lastIndex, curIndex)

@get('rangeSelection').addObjects(
@get('bodyContent').slice(minIndex, maxIndex + 1).mapBy('content'))
else
if !event.ctrlKey && !event.metaKey
@get('persistedSelection').clear()
@get('rangeSelection').clear()
else
@persistSelection()
if @get('persistedSelection').contains item
@get('persistedSelection').removeObject item
@get('rangeSelection').addObjects(
@get('bodyContent').slice(minIndex, maxIndex + 1).mapBy('content'))
else
@get('persistedSelection').addObject item
@set('lastSelected', row)
if !event.ctrlKey && !event.metaKey
@get('persistedSelection').clear()
@get('rangeSelection').clear()
else
@persistSelection()
if @get('persistedSelection').contains item
@get('persistedSelection').removeObject item
else
@get('persistedSelection').addObject item
@set('lastSelected', row)

findRow: (content) ->
for row in @get('bodyContent')
Expand All @@ -445,7 +459,7 @@ Ember.AddeparMixins.ResizeHandlerMixin,

getRowForEvent: (event) ->
$rowView = $(event.target).parents('.ember-table-table-row')
view = Ember.View.views[$rowView.attr('id')]
view = Ember.View.views[$rowView.attr('id')]
view.get 'row' if view

Ember.Handlebars.helper('table-component', Ember.Table.EmberTableComponent)

0 comments on commit 074ad3d

Please sign in to comment.