Skip to content

Commit

Permalink
Improvements to behaviors/spreadsheet-select
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Luc Carmel Biron committed Mar 11, 2017
1 parent 8d52949 commit 48eed45
Show file tree
Hide file tree
Showing 15 changed files with 917 additions and 140 deletions.
169 changes: 169 additions & 0 deletions addon/behaviors/common/row-range.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import Ember from 'ember';

const { Evented, on, run } = Ember;

export default Ember.Object.extend(Evented, {

// passed-in
a: null, // the default anchor point
b: null, // the other point
anchorIsB: false,
anchorAdjustment: 0,

/*
anchor: Ember.computed('a', 'b', 'anchorIsB', function() {
return this.get('anchorAdjustment') + (this.get('anchorIsB') ? this.get('b') : this.get('a'));
}),
other: Ember.computed('a', 'b', 'anchorIsB', function() {
return this.get('anchorIsB') ? this.get('a') : this.get('b');
}),
*/

realA: Ember.computed('anchorIsB', 'a', 'anchorAdjustment', {
get() {
let a = this.get('a');
return this.get('anchorIsB') ? a : a + this.get('anchorAdjustment');
},
set(key, value) {
this.set('a', this.get('anchorIsB') ? value : value - this.get('anchorAdjustment'));
return value;
}
}),

realB: Ember.computed('anchorIsB', 'b', 'anchorAdjustment', {
get() {
let b = this.get('b');
return !this.get('anchorIsB') ? b : b + this.get('anchorAdjustment');
},
set(key, value) {
this.set('b', !this.get('anchorIsB') ? value : value - this.get('anchorAdjustment'));
return value;
}
}),

first: Ember.computed('a', 'b', {
get() {
return Math.min(this.get('a'), this.get('b'));
},
set(key, value) {
if (this.get('a') <= this.get('b')) {
this.set('a', value);
} else {
this.set('b', value);
}
return value;
}
}),

last: Ember.computed('a', 'b', {
get() {
return Math.max(this.get('a'), this.get('b'));
},
set(key, value) {
if (this.get('a') > this.get('b')) {
this.set('a', value);
} else {
this.set('b', value);
}
return value;
}
}),

realFirst: Ember.computed('realA', 'realB', {
get() {
return Math.min(this.get('realA'), this.get('realB'));
},
set(key, value) {
if (this.get('realA') <= this.get('realB')) {
this.set('realA', value);
} else {
this.set('realB', value);
}
return value;
}
}),

realLast: Ember.computed('realA', 'realB', {
get() {
return Math.max(this.get('realA'), this.get('realB'));
},
set(key, value) {
if (this.get('realA') > this.get('realB')) {
this.set('realA', value);
} else {
this.set('realB', value);
}
return value;
}
}),

normalize() {
this.setProperties({
a: this.get('realA'),
b: this.get('realB'),
anchorAdjustment: 0
});
if (this.get('anchorIsB')) {
let a = this.get('a');
let b = this.get('b');
this.setProperties({
a: b,
b: a,
anchorIsB: false
});
}
},

move(ltBody, pivot, direction) {
let a = this.get('a');
let b = this.get('b');
let n = ltBody.get('table.rows.length');
let clip = (i) => Math.min(Math.max(0, i), n - 1);
let i;
if (a === b) {
i = clip(b + direction);
this.set('b', i);
} else {
if (pivot === -1) {
pivot = a;
}
if (pivot === a) {
i = clip(b + direction);
this.set('b', i);
} else if (pivot === b) {
i = clip(a + direction);
this.set('a', i);
} else if (direction > 0) {
i = clip(this.get('last') + direction);
this.set('last', i);
} else {
i = clip(this.get('first') + direction);
this.set('first', i);
}
}
run.schedule('afterRender', null, () => ltBody.makeRowAtVisible(i));
},

applyDomModifications(ltBody) {
ltBody.get('scrollableDecorations').pushObject({
component: 'lt-row-range',
namedArgs: { range: this }
});
},

revertDomModifications(ltBody) {
let decorations = ltBody.get('scrollableDecorations');
decorations.removeObject(decorations.findBy('namedArgs.range', this));
},

_onHandleMove: on('move', function() {
this.trigger('handleMove', ...arguments);
}),

_onHandleDrop: on('drop', function() {
this.trigger('handleDrop', ...arguments);
})

});

Loading

0 comments on commit 48eed45

Please sign in to comment.