Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. var pluck = require("lodash").pluck
  2. var groupBy = require("lodash").groupBy
  3. var guid = require("guid")
  4. var m = require("mithril")
  5. var horsey = require("horsey")
  6.  
  7. var controller = function(label, data, property){
  8.  
  9. this.grouped = groupBy( data, (suggestion) => suggestion[property][0])
  10. this.label = label
  11.  
  12. this.oninput = ( value ) => value
  13. this.data = data
  14. this.property = property
  15. this.getter = ( suggestion ) => suggestion[ctrl.property]
  16. }
  17.  
  18. var horseyConfig = (ctrl) => (el, isInit, context) => {
  19. var current_char = el.value.charAt(0).toUpperCase()
  20. var suggestions = ctrl.grouped[current_char] || []
  21.  
  22. if(!context.horsey){
  23. context.horsey = horsey( el, {
  24. suggestions: suggestions,
  25. getValue: ctrl.getter,
  26. getText: ctrl.getter,
  27. limit: 10
  28. })
  29. }
  30. if(current_char != context.previous_char){
  31. context.horsey.clear()
  32. suggestions.forEach(
  33. ( s ) => context.horsey.add( s )
  34. )
  35. }
  36.  
  37. context.previous_char = current_char
  38. }
  39.  
  40. var input = ( id, ctrl ) =>
  41. m("input", {
  42. list: id,
  43. config: horseyConfig(ctrl),
  44. oninput: m.withAttr("value", ctrl.oninput )
  45. })
  46.  
  47. var view = function(ctrl){
  48. var results_id = "search"
  49.  
  50. return m("div",
  51. m("label",
  52. ctrl.label,
  53. input(results_id, ctrl)
  54. )
  55. )
  56. }
  57.  
  58. module.exports = { view: view, controller: controller }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement