1
0

MessageList.coffee 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. class MessageList extends Class
  2. constructor: (@message_lists) ->
  3. @title = "Unknown"
  4. @loading = false
  5. @loaded = false
  6. @has_more = false
  7. @loading_message = "Loading..."
  8. @messages = []
  9. @selected = []
  10. @message_db = {}
  11. getMessages: ->
  12. return @messages
  13. setActiveMessage: (message) ->
  14. if @message_lists.message_active
  15. @message_lists.message_active.active = false
  16. message.active = true
  17. @message_lists.message_active = message
  18. @deselectMessages()
  19. deselectMessages: ->
  20. for message in @selected
  21. message.selected = false
  22. @updateSelected()
  23. updateSelected: ->
  24. @selected = (message for message in @messages when message.selected)
  25. addMessage: (message_row, index=-1) ->
  26. message = new Message(@, message_row)
  27. @message_db[message_row.key] = message
  28. if index >= 0
  29. @messages.splice index, 0, message
  30. else
  31. @messages.push message
  32. return message
  33. deleteMessage: (message) ->
  34. message.deleted = true
  35. index = @messages.indexOf(message)
  36. if index > -1
  37. @messages.splice(index, 1)
  38. syncMessages: (message_rows) ->
  39. # @messages = []
  40. last_obj = null
  41. for message_row in message_rows
  42. current_obj = @message_db[message_row.key]
  43. if current_obj
  44. current_obj.row = message_row
  45. last_obj = current_obj
  46. # @messages.push current_obj
  47. else
  48. if last_obj # Add after last found obj in list
  49. last_obj = @addMessage(message_row, @messages.indexOf(last_obj)+1)
  50. else
  51. last_obj = @addMessage(message_row, 0)
  52. setLoadingMessage: (@loading_message) ->
  53. Page.projector.scheduleRender()
  54. handleMoreClick: =>
  55. @reload = true
  56. @getMessages("nolimit")
  57. return false
  58. render: =>
  59. messages = if Page.site_info?.cert_user_id then @getMessages() else []
  60. if messages.length > 0
  61. return h("div.MessageList", {"key": @title, "enterAnimation": Animation.show},
  62. messages.map (message) ->
  63. message.renderList()
  64. h("a.more", {href: "#More", classes: {"visible": @has_more, "loading": @loading}, onclick: @handleMoreClick}, "Load more messages")
  65. )
  66. else if @loading
  67. return h("div.MessageList.empty", {"key": @title+".loading", "enterAnimation": Animation.show, "afterCreate": Animation.show, "delay": 1}, [
  68. "#{@title}: #{@loading_message}",
  69. h("span.cursor", ["_"])
  70. ])
  71. else
  72. return h("div.MessageList.empty", {"key": @title+".empty", "enterAnimation": Animation.show, "afterCreate": Animation.show, "delay": 0.5}, [
  73. "#{@title}: No messages",
  74. h("span.cursor", ["_"])
  75. ])
  76. window.MessageList = MessageList