-
-
Save barneycarroll/69bc0581507995fa8fa215b9f66ae328 to your computer and use it in GitHub Desktop.
This file contains 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
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