Skip to content

Instantly share code, notes, and snippets.

@lsmith
Forked from stlsmiths/gist:2480907
Created April 24, 2012 15:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsmith/2480921 to your computer and use it in GitHub Desktop.
Save lsmith/2480921 to your computer and use it in GitHub Desktop.
POC DataTable.Selection class extension
// Class extension to DataTable ...
Y.DataTable.Selection = function () {}
Y.DataTable.Selection.ATTRS = {
// selectionMode: for supporting multiple selection could be its own extension
selectionType: {
value: null, // Feature should not change base behavior by default
validator: '_validateSelectionType'
},
selection: {
setter: '_setSelection'
}
};
Y.mix(Y.DataTable.Selection.prototype, {
initializer: function () {
this.after('selectionTypeChange', this._afterSelectionTypeChange);
this._initSelectionType(this.get('selectionType'));
},
_initSelectionType: function (mode) {
var rendered = this.get('rendered');
if (!this.get('rendered')) {
if (mode && !this._initSelectionHandle) {
this._initSelectionHandle = this.after('renderTable', this._bindSelectionUI)
} else if (!mode && this._initSelectionHandle) {
this._initSelectionHandle.detach();
delete this._initSelectionHandle;
}
} else {
this._bindSelectionUI();
}
},
_afterSelectionTypeChange: function (e) {
this._initSelectionType(e.newVal);
},
_bindSelectionUI: function () {
if (!this._selectionHandle) {
// Not a delegate because a click will always result in something being selected
this._selectionHandle = this.get('contentBox').on('click', this._onClickSelect, this);
}
},
_onClickSelect: function (e) {
this.set('selection', e.target);
},
_setSelection: function (val) {
var type;
if (val) {
type = this.get('selectionType');
if (type === 'cell') {
val = val.ancestor('.' + this.getClassName('cell'));
} else if (type === 'row') {
val = val.ancestor('.' + this.getClassName('data') + '> tr');
} else if (type === 'column') {
val = this.getColumn(val);
}
}
return val || null;
},
_validateSelectionType: function (val) {
return val === 'cell' || val === 'row' || val === 'column' || val === null;
}
});
// Mix the class extension into Y.DataTable
Y.Base.mix(Y.DataTable, [ Y.DataTable.Selection ]);
var newDT = new Y.DataTable({
selectionType: 'cell'
});
newDT.on("selectionChange", function(e){
// do something ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment