Comments.coffee 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. class Comments extends Class
  2. pagePost: (post_id, cb=false) ->
  3. @post_id = post_id
  4. @rules = {}
  5. $(".button-submit-comment").off("click").on "click", =>
  6. @submitComment()
  7. return false
  8. @loadComments("noanim", cb)
  9. @autoExpand $(".comment-textarea")
  10. $(".certselect").off("click").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_header").text(comments.length + if comments.length > 1 then " Comments:" else " Comment:")
  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. $(".reply", elem).off("click").on "click", (e) => # Reply link
  35. return @buttonReply $(e.target).parents(".comment")
  36. @applyCommentData(elem, comment)
  37. elem.appendTo(".comments")
  38. setTimeout (->
  39. Page.addInlineEditors(".comments")
  40. ), 1000
  41. applyCommentData: (elem, comment) ->
  42. [user_name, cert_domain] = comment.cert_user_id.split("@")
  43. user_address = comment.directory.replace("users/", "")
  44. $(".comment-body", elem).html Text.renderMarked(comment.body, {"sanitize": true})
  45. $(".user_name", elem).text(user_name).css("color": Text.toColor(comment.cert_user_id)).attr("title", "#{user_name}@#{cert_domain}: #{user_address}")
  46. $(".added", elem).text(Time.since(comment.date_added)).attr("title", Time.date(comment.date_added, "long"))
  47. #$(".cert_domain", elem).html("@#{cert_domain}").css("display", "none")
  48. # Add inline editor
  49. if user_address == Page.site_info.auth_address
  50. $(elem).attr("data-object", "Comment:#{comment.comment_id}").attr("data-deletable", "yes")
  51. $(".comment-body", elem).attr("data-editable", "body").data("content", comment.body)
  52. buttonReply: (elem) ->
  53. @log "Reply to", elem
  54. user_name = $(".user_name", elem).text()
  55. post_id = elem.attr("id")
  56. body_add = "> [#{user_name}](\##{post_id}): "
  57. elem_quote = $(".comment-body", elem).clone()
  58. $("blockquote", elem_quote).remove() # Remove other people's quotes
  59. body_add+= elem_quote.text().trim("\n").replace(/\n/g, "\n> ")
  60. body_add+= "\n\n"
  61. $(".comment-new .comment-textarea").val( $(".comment-new .comment-textarea").val()+body_add )
  62. $(".comment-new .comment-textarea").trigger("input").focus() # Autosize
  63. return false
  64. submitComment: ->
  65. if not Page.site_info.cert_user_id # Not registered
  66. Page.cmd "wrapperNotification", ["info", "Please, select your account."]
  67. return false
  68. body = $(".comment-new .comment-textarea").val()
  69. if not body
  70. $(".comment-new .comment-textarea").focus()
  71. return false
  72. $(".comment-new .button-submit").addClass("loading")
  73. inner_path = "data/users/#{Page.site_info.auth_address}/data.json"
  74. Page.cmd "fileGet", {"inner_path": inner_path, "required": false}, (data) =>
  75. if data
  76. data = JSON.parse(data)
  77. else # Default data
  78. data = {"next_comment_id": 1, "comment": [], "comment_vote": {}, "topic_vote": {} }
  79. data.comment.push {
  80. "comment_id": data.next_comment_id,
  81. "body": body,
  82. "post_id": @post_id,
  83. "date_added": Time.timestamp()
  84. }
  85. data.next_comment_id += 1
  86. json_raw = unescape(encodeURIComponent(JSON.stringify(data, undefined, '\t')))
  87. Page.writePublish inner_path, btoa(json_raw), (res) =>
  88. $(".comment-new .button-submit").removeClass("loading")
  89. @loadComments()
  90. setTimeout (->
  91. Page.loadLastcomments()
  92. ), 1000
  93. @checkCert("updaterules")
  94. @log "Writepublish result", res
  95. if res != false
  96. $(".comment-new .comment-textarea").val("")
  97. checkCert: (type) ->
  98. last_cert_user_id = $(".comment-new .user_name").text()
  99. if Page.site_info.cert_user_id
  100. $(".comment-new").removeClass("comment-nocert")
  101. $(".comment-new .user_name").text(Page.site_info.cert_user_id)
  102. else
  103. $(".comment-new").addClass("comment-nocert")
  104. $(".comment-new .user_name").text("Please sign in")
  105. if $(".comment-new .user_name").text() != last_cert_user_id or type == "updaterules" # User changed
  106. # Update used/allowed space
  107. if Page.site_info.cert_user_id
  108. Page.cmd "fileRules", "data/users/#{Page.site_info.auth_address}/content.json", (rules) =>
  109. @rules = rules
  110. if rules.max_size
  111. @setCurrentSize(rules.current_size)
  112. else
  113. @setCurrentSize(0)
  114. else
  115. @setCurrentSize(0)
  116. setCurrentSize: (current_size) ->
  117. if current_size
  118. current_size_kb = current_size/1000
  119. $(".user-size").text("used: #{current_size_kb.toFixed(1)}k/#{Math.round(@rules.max_size/1000)}k")
  120. $(".user-size-used").css("width", Math.round(70*current_size/@rules.max_size))
  121. else
  122. $(".user-size").text("")
  123. autoExpand: (elem) ->
  124. editor = elem[0]
  125. # Autoexpand
  126. if elem.height() > 0 then elem.height(1)
  127. elem.off("input").on "input", =>
  128. if editor.scrollHeight > elem.height()
  129. old_height = elem.height()
  130. elem.height(1)
  131. new_height = editor.scrollHeight
  132. new_height += parseFloat elem.css("borderTopWidth")
  133. new_height += parseFloat elem.css("borderBottomWidth")
  134. new_height -= parseFloat elem.css("paddingTop")
  135. new_height -= parseFloat elem.css("paddingBottom")
  136. min_height = parseFloat(elem.css("lineHeight"))*2 # 2 line minimum
  137. if new_height < min_height then new_height = min_height+4
  138. elem.height(new_height-4)
  139. # Update used space
  140. if @rules.max_size
  141. if elem.val().length > 0
  142. current_size = @rules.current_size + elem.val().length + 90
  143. else
  144. current_size = @rules.current_size
  145. @setCurrentSize(current_size)
  146. if elem.height() > 0 then elem.trigger "input"
  147. else elem.height("48px")
  148. window.Comments = new Comments()