Skip to content

Instantly share code, notes, and snippets.

@taburetkin
Created September 12, 2018 17:48
Show Gist options
  • Save taburetkin/bd319297b36feb66ecdd047dddd27cff to your computer and use it in GitHub Desktop.
Save taburetkin/bd319297b36feb66ecdd047dddd27cff to your computer and use it in GitHub Desktop.
selector
import BaseObject from 'base/object';
export default BaseObject.extend({
initialize(opts){
this.mergeOptions(opts,[
'collection', 'multiple',
'getItemKey', 'getItemValue', 'getItemIndex', 'getItemByIndex', 'children',
'initialValue'
]);
this.store = {};
this.selectInitialValues();
},
selectInitialValues(silent = false){
let values = this.initialValue;
if(values == null) return;
if(!_.isArray(values))
values = [values];
let children = this.children;
if(_.isFunction(children))
children = children();
_(children._views).each((f) => {
let val = this.getItemKey(f);
if(values.indexOf(val) > -1)
this.select(f, silent);
});
},
setCollection(col){
this.collection = col;
},
getItemKey(item){
return item.model.id;
},
getItemValue(item){
return item.model;
},
getItemIndex(item){
return this.children().findIndexByView(item);
//return item.collection.indexOf(item);
},
getItemByIndex(index){
return this.children().findByIndex(index);
//return this.collection.models[index];
},
getItemsRange(leftIndex, rightIndex, ignore)
{
let result = [];
let get = this.getItemByIndex;
for(var x = leftIndex; x <= rightIndex; x++){
if(x == ignore) continue;
let item = get.call(this, x);
if(item != null)
result.push(item);
}
return result;
},
set(key, item, silent){
this.store[key] = item;
if(!silent)
this.trigger('select', key, item);
if (item && item.trigger) {
item.trigger('select');
}
return { [key]:item };
},
unset(key, item, silent){
delete this.store[key];
if(!silent)
this.trigger('unselect', key, item);
if (item && item.trigger) {
item.trigger('unselect');
}
return { [key]:item };
},
unsetAll({ silent }){
let keyPairs = _.extend({},this.store);
_.each(keyPairs, (item, key) => {
this.unset(key, item, silent);
});
/*this.store = {};
let changes = {
unselected: keyPairs,
selected:{}
};
!silent && this.triggerChange(changes);*/
},
has(key){
return key in this.store;
},
select(item, silent){
this.set(this.getItemKey(item), item, silent);
},
unselect(item){
this.unset(this.getItemKey(item), item);
},
toggle(item, silent){
let key = this.getItemKey(item);
if(this.has(key)) {
this.unset(key, item, silent);
return false;
} else {
this.set(key, item, silent);
return true;
}
// let result = {
// selected:{},
// unselected:{},
// };
// _.extend(result.unselected, this.unset(key, item, silent));
// else
// _.extend(result.selected, this.set(key, item, silent));
// return result;
},
clicked(item, opts = {}){
if(this.multiple){
if(opts.event && opts.event.shiftKey && this.tryToggleRange(item)){
delete this.lastItemIndex;
return;
}
this.lastItemIndex = this.getItemIndex(item);
this.toggle(item);
}else{
this.unsetAll({ silent: true });
this.select(item);
}
this.trigger('change', this.getValue());
},
isSelected(item){
let key = this.getItemKey(item);
return this.has(key);
},
tryToggleRange(item){
if(this.lastItemIndex == null) return;
let i1 = this.lastItemIndex;
let ignoreIndex = i1;
let i2 = this.getItemIndex(item);
let left = _([i1,i2]).min();
let right = _([i1,i2]).max();
let range = this.getItemsRange(left, right, ignoreIndex);
// let result = {
// selected:{},
// unselected:{},
// };
_(range).each((item) =>{
//let t =
this.toggle(item);
// _.extend(result.selected, t.selected);
// _.extend(result.unselected, t.unselected);
});
//this.triggerChange(result);
return true;
},
triggerChange(changes){
//Console.log('triggering changes', changes);
this.trigger('change', changes);
},
getValue(){
let values = _(this.store).map((m) => m.model);
if(this.multiple)
return values;
else
return values[0];
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment