-
-
Save lsmith/2480921 to your computer and use it in GitHub Desktop.
POC DataTable.Selection class extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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