MenuView.coffee 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. define [
  2. "libs/backbone"
  3. "libs/mustache"
  4. ], () ->
  5. MenuView = Backbone.View.extend
  6. tagName: "ul"
  7. className: "auto-complete-menu"
  8. templates:
  9. suggestion: $("#autoCompleteSuggestionTemplate").html()
  10. render: (fontFamily, fontSize) ->
  11. @$el.css
  12. position: "absolute"
  13. "font-family": fontFamily
  14. "font-size": fontSize
  15. return @$el
  16. setSuggestions: (suggestions) ->
  17. @$el.children().off()
  18. @$el.empty()
  19. @suggestions = []
  20. for suggestion in suggestions
  21. do (suggestion) =>
  22. el = $(Mustache.to_html(@templates.suggestion, suggestion))
  23. @$el.append(el)
  24. el.on "click", (e) => @trigger("click", e, suggestion)
  25. @suggestions.push suggestion: suggestion, el: el
  26. @selectSuggestionAtIndex 0
  27. selectSuggestionAtIndex: (index) ->
  28. if index >= 0 and index < @suggestions.length
  29. @$("li").removeClass "selected"
  30. @suggestions[index].el.addClass "selected"
  31. @selectedIndex = index
  32. moveSelectionDown: () ->
  33. if @selectedIndex? and @selectedIndex < @suggestions.length - 1
  34. @selectSuggestionAtIndex @selectedIndex + 1
  35. moveSelectionUp: () ->
  36. if @selectedIndex? and @selectedIndex > 0
  37. @selectSuggestionAtIndex @selectedIndex - 1
  38. getSelectedSuggestion: () ->
  39. if @selectedIndex? and @suggestions[@selectedIndex]?
  40. @suggestions[@selectedIndex].suggestion
  41. position: (pos) ->
  42. @$el.css
  43. top: pos.top
  44. left: pos.left
  45. show: () -> @$el.show()
  46. hide: () -> @$el.hide()