Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Forked from WreckedAvent/select option (es6).js
Created July 29, 2016 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barneycarroll/69bc0581507995fa8fa215b9f66ae328 to your computer and use it in GitHub Desktop.
Save barneycarroll/69bc0581507995fa8fa215b9f66ae328 to your computer and use it in GitHub Desktop.
var m = require( 'mithril' )
var isSelected = require( '../functions/isSelected' )
function action(option, selection, e){
if (isSelected(ctrl.option, ctrl.selection)) {
var i = ctrl.selection.indexOf(ctrl.option)
ctrl.selection.splice(i, 1)
} else {
if (Array.isArray(ctrl.selection)) {
ctrl.selection.push(ctrl.option)
} else {
ctrl.selection(ctrl.option)
}
}
e.stopPropagation()
m.redraw()
}
function setClasses(option, selection, highlighted, index){
var selected = isSelected(option, selection);
if (selected && highlighted() === index){
return "selected highlighted";
} else if (selected && highlighted() !== index){
return "selected";
} else if (!selected && highlighted() === index){
return 'highlighted';
} else {
return '';
}
}
var selectOption = {};
selectOption.view = function( vnode ){
return m('.item[tabindex=0]', {
class: setClasses(option, selection, highlighted, index),
style: {
display: search() && option.toLowerCase().indexOf(search().toLowerCase()) === -1 ? 'none' : 'block'
},
onclick: function( e ){} action(option, selection, e) },
onfocus: function(){ ctrl.highlighted(index) },
onmouseover: function(){ ctrl.highlighted(index) }
}, option )
}
module.exports = selectOption
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment