Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var pluck = require("lodash").pluck
- var groupBy = require("lodash").groupBy
- var guid = require("guid")
- var m = require("mithril")
- var horsey = require("horsey")
- var controller = function(label, data, property){
- this.grouped = groupBy( data, (suggestion) => suggestion[property][0])
- this.label = label
- this.oninput = ( value ) => value
- this.data = data
- this.property = property
- this.getter = ( suggestion ) => suggestion[ctrl.property]
- }
- var horseyConfig = (ctrl) => (el, isInit, context) => {
- var current_char = el.value.charAt(0).toUpperCase()
- var suggestions = ctrl.grouped[current_char] || []
- if(!context.horsey){
- context.horsey = horsey( el, {
- suggestions: suggestions,
- getValue: ctrl.getter,
- getText: ctrl.getter,
- limit: 10
- })
- }
- if(current_char != context.previous_char){
- context.horsey.clear()
- suggestions.forEach(
- ( s ) => context.horsey.add( s )
- )
- }
- context.previous_char = current_char
- }
- var input = ( id, ctrl ) =>
- m("input", {
- list: id,
- config: horseyConfig(ctrl),
- oninput: m.withAttr("value", ctrl.oninput )
- })
- var view = function(ctrl){
- var results_id = "search"
- return m("div",
- m("label",
- ctrl.label,
- input(results_id, ctrl)
- )
- )
- }
- module.exports = { view: view, controller: controller }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement