ContentCreateProfile.coffee 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. class ContentCreateProfile extends Class
  2. constructor: ->
  3. @loaded = true
  4. @hubs = []
  5. @default_hubs = []
  6. @need_update = true
  7. @creation_status = []
  8. @downloading = {}
  9. handleDownloadClick: (e) =>
  10. hub = e.target.attributes.address.value
  11. @downloading[hub] = true
  12. Page.needSite hub, =>
  13. @update()
  14. return false
  15. handleJoinClick: (e) =>
  16. hub_address = e.target.attributes.address.value
  17. if Page.user?.hub
  18. hub_name = (hub.content.title for hub in @hubs when hub.address == Page.user.hub)?[0]
  19. hub_name ?= Page.user.hub
  20. Page.cmd "wrapperConfirm", ["You already have profile on hub <b>#{hub_name}</b>,<br>are you sure you want to create a new one?", "Create new profile"], =>
  21. @joinHub(hub_address)
  22. else
  23. @joinHub(hub_address)
  24. joinHub: (hub) =>
  25. user = new User({hub: hub, auth_address: Page.site_info.auth_address})
  26. @creation_status.push "Checking user on selected hub..."
  27. Page.cmd "fileGet", {"inner_path": user.getPath()+"/content.json", "required": false}, (found) =>
  28. if found
  29. Page.cmd "wrapperNotification", ["error", "User #{Page.site_info.cert_user_id} already exists on this hub"]
  30. @creation_status = []
  31. return
  32. # Create new profile
  33. user_name = Page.site_info.cert_user_id.replace(/@.*/, "")
  34. data = user.getDefaultData()
  35. data.avatar = "generate"
  36. data.user_name = user_name.charAt(0).toUpperCase()+user_name.slice(1)
  37. data.hub = hub
  38. @creation_status.push "Creating new profile..."
  39. user.save data, hub, =>
  40. @creation_status = []
  41. Page.checkUser()
  42. Page.setUrl("?Home")
  43. return false
  44. updateHubs: =>
  45. Page.cmd "mergerSiteList", true, (sites) =>
  46. # Get userlist
  47. Page.cmd "dbQuery", "SELECT * FROM json", (users) =>
  48. site_users = {}
  49. for user in users
  50. site_users[user.hub] ?= []
  51. site_users[user.hub].push(user)
  52. hubs = []
  53. for address, site of sites
  54. if address == Page.userdb
  55. continue
  56. site["users"] = site_users[site.address] or []
  57. hubs.push(site)
  58. @hubs = hubs
  59. Page.projector.scheduleRender()
  60. @default_hubs = []
  61. for address, content of Page.site_info.content.settings.default_hubs
  62. if not sites[address] and not @downloading[address]
  63. @default_hubs.push {
  64. users: [],
  65. address: address,
  66. content: content,
  67. type: "available"
  68. }
  69. renderHub: (hub) =>
  70. rendered = 0
  71. h("div.hub.card", {key: hub.address+hub.type, enterAnimation: Animation.slideDown, exitAnimation: Animation.slideUp}, [
  72. if hub.type == "available"
  73. h("a.button.button-join", {href: "#Download:#{hub.address}", address: hub.address, onclick: @handleDownloadClick}, "Download")
  74. else
  75. h("a.button.button-join", {href: "#Join:#{hub.address}", address: hub.address, onclick: @handleJoinClick}, "Join!")
  76. h("div.avatars", [
  77. hub.users.map (user) =>
  78. if user.avatar not in ["jpg", "png"] or rendered >= 4
  79. return ""
  80. avatar = "merged-ZeroMe/#{hub.address}/#{user.directory}/avatar.#{user.avatar}"
  81. rendered += 1
  82. h("a.avatar", {key: user.user_name, title: user.user_name, style: "background-image: url('#{avatar}')"})
  83. if hub.users.length - rendered > 0
  84. h("a.avatar.empty", "+#{hub.users.length - rendered}")
  85. ])
  86. h("div.name", hub.content.title),
  87. h("div.intro", hub.content.description)
  88. ])
  89. renderSeededHubs: =>
  90. h("div.hubs.hubs-seeded", @hubs.map(@renderHub))
  91. renderDefaultHubs: =>
  92. h("div.hubs.hubs-default", @default_hubs.map(@renderHub))
  93. handleSelectUserClick: ->
  94. Page.cmd "certSelect", {"accepted_domains": ["zeroid.bit"], "accept_any": true}
  95. return false
  96. render: =>
  97. if @loaded and not Page.on_loaded.resolved then Page.on_loaded.resolve()
  98. if @need_update
  99. @updateHubs()
  100. @need_update = false
  101. h("div#Content.center.content-signup", [
  102. h("h1", "Create new profile"),
  103. h("a.button.button-submit.button-certselect.certselect", {href: "#Select+user", onclick: @handleSelectUserClick}, [
  104. h("div.icon.icon-profile"),
  105. if Page.site_info?.cert_user_id
  106. "As: #{Page.site_info.cert_user_id}"
  107. else
  108. "Select ID..."
  109. ])
  110. if @creation_status.length > 0
  111. h("div.creation-status", {enterAnimation: Animation.slideDown, exitAnimation: Animation.slideUp}, [
  112. @creation_status.map (creation_status) =>
  113. h("h3", {key: creation_status, enterAnimation: Animation.slideDown, exitAnimation: Animation.slideUp}, creation_status)
  114. ])
  115. else if Page.site_info.cert_user_id
  116. h("div.hubs", {enterAnimation: Animation.slideDown, exitAnimation: Animation.slideUp}, [
  117. if @hubs.length
  118. h("div.hubselect.seeded", {enterAnimation: Animation.slideDown, exitAnimation: Animation.slideUp}, [
  119. h("h2", "Seeded HUBs")
  120. @renderSeededHubs()
  121. ])
  122. if @default_hubs.length
  123. h("div.hubselect.default", {enterAnimation: Animation.slideDown, exitAnimation: Animation.slideUp}, [
  124. h("h2", "Available HUBs")
  125. @renderDefaultHubs()
  126. ])
  127. h("h5", "(With this you choose where is your profile stored. There is no difference on content and you will able to reach all users from any hub)")
  128. ])
  129. ])
  130. update: =>
  131. @need_update = true
  132. Page.projector.scheduleRender()
  133. window.ContentCreateProfile = ContentCreateProfile