1
0

Comments.coffee 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. class Comments extends Class
  2. pagePost: (post_id, cb=false) ->
  3. @post_id = post_id
  4. @rules = {}
  5. $(".button-submit-comment").on "click", =>
  6. @submitComment()
  7. return false
  8. @loadComments("noanim", cb)
  9. @autoExpand $(".comment-textarea")
  10. $(".certselect").on "click", =>
  11. if Page.server_info.rev < 160
  12. Page.cmd "wrapperNotification", ["error", "Comments requires at least ZeroNet 0.3.0 Please upgade!"]
  13. else
  14. Page.cmd "certSelect", [["zeroid.bit"]]
  15. return false
  16. loadComments: (type="show", cb=false) ->
  17. query = "SELECT comment.*, json_content.json_id AS content_json_id, keyvalue.value AS cert_user_id, json.directory,
  18. (SELECT COUNT(*) FROM comment_vote WHERE comment_vote.comment_uri = comment.comment_id || '@' || json.directory)+1 AS votes
  19. FROM comment
  20. LEFT JOIN json USING (json_id)
  21. LEFT JOIN json AS json_content ON (json_content.directory = json.directory AND json_content.file_name='content.json')
  22. LEFT JOIN keyvalue ON (keyvalue.json_id = json_content.json_id AND key = 'cert_user_id')
  23. WHERE post_id = #{@post_id} ORDER BY date_added DESC"
  24. Page.cmd "dbQuery", query, (comments) =>
  25. $(".comments-num").text(comments.length)
  26. for comment in comments
  27. user_address = comment.directory.replace("users/", "")
  28. comment_address = "#{comment.comment_id}_#{user_address}"
  29. elem = $("#comment_"+comment_address)
  30. if elem.length == 0 # Create if not exits
  31. elem = $(".comment.template").clone().removeClass("template").attr("id", "comment_"+comment_address).data("post_id", @post_id)
  32. if type != "noanim"
  33. elem.cssSlideDown()
  34. @applyCommentData(elem, comment)
  35. elem.appendTo(".comments")
  36. applyCommentData: (elem, comment) ->
  37. [user_name, cert_domain] = comment.cert_user_id.split("@")
  38. user_address = comment.directory.replace("users/", "")
  39. $(".comment-body", elem).html Text.toMarked(comment.body, {"sanitize": true})
  40. $(".user_name", elem).text(user_name).css("color": Text.toColor(comment.cert_user_id)).attr("title", "#{user_name}@#{cert_domain}: #{user_address}")
  41. $(".added", elem).text(Time.since(comment.date_added)).attr("title", Time.date(comment.date_added, "long"))
  42. #$(".cert_domain", elem).html("@#{cert_domain}").css("display", "none")
  43. submitComment: ->
  44. if not Page.site_info.cert_user_id # Not registered
  45. Page.cmd "wrapperNotification", ["info", "Please, select your account."]
  46. return false
  47. body = $(".comment-new .comment-textarea").val()
  48. if not body
  49. $(".comment-new .comment-textarea").focus()
  50. return false
  51. $(".comment-new .button-submit").addClass("loading")
  52. inner_path = "data/users/#{Page.site_info.auth_address}/data.json"
  53. Page.cmd "fileGet", {"inner_path": inner_path, "required": false}, (data) =>
  54. if data
  55. data = JSON.parse(data)
  56. else # Default data
  57. data = {"next_comment_id": 1, "comment": [], "comment_vote": {} }
  58. data.comment.push {
  59. "comment_id": data.next_comment_id,
  60. "body": body,
  61. "post_id": @post_id,
  62. "date_added": Time.timestamp()
  63. }
  64. data.next_comment_id += 1
  65. json_raw = unescape(encodeURIComponent(JSON.stringify(data, undefined, '\t')))
  66. Page.writePublish inner_path, btoa(json_raw), (res) =>
  67. $(".comment-new .button-submit").removeClass("loading")
  68. @loadComments()
  69. @checkCert("updaterules")
  70. @log "Writepublish result", res
  71. if res != false
  72. $(".comment-new .comment-textarea").val("")
  73. checkCert: (type) ->
  74. last_cert_user_id = $(".comment-new .user_name").text()
  75. if Page.site_info.cert_user_id
  76. $(".comment-new").removeClass("comment-nocert")
  77. $(".comment-new .user_name").text(Page.site_info.cert_user_id)
  78. else
  79. $(".comment-new").addClass("comment-nocert")
  80. $(".comment-new .user_name").text("Please sign in")
  81. if $(".comment-new .user_name").text() != last_cert_user_id or type == "updaterules" # User changed
  82. # Update used/allowed space
  83. if Page.site_info.cert_user_id
  84. Page.cmd "fileRules", "data/users/#{Page.site_info.auth_address}/content.json", (rules) =>
  85. @rules = rules
  86. if rules.max_size
  87. @setCurrentSize(rules.current_size)
  88. else
  89. @setCurrentSize(0)
  90. else
  91. @setCurrentSize(0)
  92. setCurrentSize: (current_size) ->
  93. if current_size
  94. current_size_kb = current_size/1000
  95. $(".user-size").text("used: #{current_size_kb.toFixed(1)}k/#{Math.round(@rules.max_size/1000)}k")
  96. $(".user-size-used").css("width", Math.round(70*current_size/@rules.max_size))
  97. else
  98. $(".user-size").text("")
  99. autoExpand: (elem) ->
  100. editor = elem[0]
  101. # Autoexpand
  102. if elem.height() > 0 then elem.height(1)
  103. elem.on "input", =>
  104. if editor.scrollHeight > elem.height()
  105. old_height = elem.height()
  106. elem.height(1)
  107. new_height = editor.scrollHeight
  108. new_height += parseFloat elem.css("borderTopWidth")
  109. new_height += parseFloat elem.css("borderBottomWidth")
  110. new_height -= parseFloat elem.css("paddingTop")
  111. new_height -= parseFloat elem.css("paddingBottom")
  112. min_height = parseFloat(elem.css("lineHeight"))*2 # 2 line minimum
  113. if new_height < min_height then new_height = min_height+4
  114. elem.height(new_height-4)
  115. # Update used space
  116. if @rules.max_size
  117. if elem.val().length > 0
  118. current_size = @rules.current_size + elem.val().length + 90
  119. else
  120. current_size = @rules.current_size
  121. @setCurrentSize(current_size)
  122. if elem.height() > 0 then elem.trigger "input"
  123. else elem.height("48px")
  124. window.Comments = new Comments()