Leftbar.coffee 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. class Leftbar extends Class
  2. constructor: ->
  3. @contacts = []
  4. @folder_active = "inbox"
  5. @reload_contacts = true
  6. @
  7. handleContactClick: (e) =>
  8. Page.message_create.show(e.currentTarget.querySelector(".name").textContent)
  9. return false
  10. handleNewMessageClick: (e) =>
  11. Page.message_create.show()
  12. return false
  13. handleFolderClick: (e) =>
  14. folder_name = e.currentTarget.href.replace(/.*\?/, "")
  15. @folder_active = folder_name.toLowerCase()
  16. Page.cmd "wrapperReplaceState", [{}, "", folder_name]
  17. return false
  18. handleLogoutClick: (e) =>
  19. Page.cmd "certSelect", [["zeroid.bit"]]
  20. # Reset localstorage
  21. Page.local_storage = {}
  22. Page.saveLocalStorage ->
  23. Page.getLocalStorage()
  24. return false
  25. loadContacts: (cb) ->
  26. addresses = (address for address of Page.local_storage.parsed.my_secret)
  27. query = """
  28. SELECT directory, value AS cert_user_id
  29. FROM json
  30. LEFT JOIN keyvalue USING (json_id)
  31. WHERE ? AND file_name = 'content.json' AND key = 'cert_user_id'
  32. """
  33. Page.cmd "dbQuery", [query, {directory: addresses}], (rows) ->
  34. contacts = ([row.cert_user_id, row.directory] for row in rows)
  35. cb contacts
  36. getContacts: ->
  37. if @reload_contacts
  38. @reload_contacts = false
  39. @log "Reloading contacts"
  40. Page.on_local_storage.then =>
  41. known_addresses = (address for [username, address] in @contacts)
  42. unknown_addresses = (address for address of Page.local_storage.parsed.my_secret when address not in known_addresses)
  43. if unknown_addresses.length > 0
  44. @loadContacts (contacts) =>
  45. @log "Unknown contacts found, reloaded."
  46. @contacts = contacts
  47. Page.projector.scheduleRender()
  48. @contacts
  49. render: =>
  50. contacts = if Page.site_info?.cert_user_id then @getContacts() else []
  51. h("div.Leftbar", [
  52. h("a.logo", {href: "?Main"}, ["ZeroMail_"])
  53. h("a.button-create.newmessage", {href: "#New+message", onclick: @handleNewMessageClick}, ["New message"])
  54. h("div.folders", [
  55. h("a", {key: "Inbox", href: "?Inbox", classes: {"active": @folder_active == "inbox"}, onclick: @handleFolderClick}, ["Inbox"])
  56. h("a", {key: "Sent", href: "?Sent", classes: {"active": @folder_active == "sent"}, onclick: @handleFolderClick}, ["Sent"])
  57. ])
  58. if contacts.length > 0
  59. [
  60. h("h2", ["Contacts"]),
  61. h("div.contacts", contacts.map ([username, address]) =>
  62. h("a.username", {key: username, href: Page.createUrl("to", username.replace("@zeroid.bit", "")), onclick: @handleContactClick, "enterAnimation": Animation.show}, [
  63. h("span.bullet", {"style": "color: #{Text.toColor(address)}"}, ["•"]),
  64. h("span.name", [username.replace("@zeroid.bit", "")])
  65. ])
  66. )
  67. ]
  68. if Page.site_info?.cert_user_id then h("a.logout.icon.icon-logout", {href: "?Logout", title: "Logout", onclick: @handleLogoutClick})
  69. ])
  70. onSiteInfo: (site_info) ->
  71. if site_info.event
  72. [action, inner_path] = site_info.event
  73. if action == "file_done" and inner_path.endsWith "data.json"
  74. @reload_contacts = true
  75. window.Leftbar = Leftbar