Skip to content

Instantly share code, notes, and snippets.

@tcitworld
Created September 21, 2015 20:41
Show Gist options
  • Save tcitworld/47d86bfa263a2546e26c to your computer and use it in GitHub Desktop.
Save tcitworld/47d86bfa263a2546e26c to your computer and use it in GitHub Desktop.
/*
* In that case, I don't insert comment in my text. Instead, they are shown on bottom and they have an anchor that
* links to the paragraph on which they were created.
*/
var paragraphsSelector = $('p');
var commentselector = $('.comment');
var comment = new Array();
var commentdomints = $('.commentdom');
// iteration on each comment coming from commentselector
for (var i = 0; i < commentselector.length; i++) {
// define paragraph for comment : get the index number for the paragraph stored in database.
// It's the same as domPath (see below)
comment['dom'] = commentdomints.eq(i).text();
// get paragraph id associated to comment
// This is where the problem happens.
comment['paragraphid'] = paragraphsSelector.eq(comment['dom']).attr('id');
// set anchor for return button to paragraph
commentselector.eq(i).find('.return-to-paragraph').attr('href', '#' + comment['paragraphid']);
};
/*
* Show comment-adding button when hoovering a paragraph
*/
$('article p, article blockquote, article aside').mouseover(function(e) {
paragraph = $(this);
offsetcom = paragraph.offset();
domPath = $(this).index();
$("#new-comment-button").show();
$("#new-comment-button").offset({ top: offsetcom.top, left: (offsetcom.left + em(55)) });
});
/*
* Add the comment input textarea when clicking on button
* The index of the paragraph is put in a hidden filed to be sent to server with comment content
*/
$('#new-comment-button').click(function(e) {
$('.nav-panel-add-comment').show();
$(".nav-panel-add-comment").offset({ top: offsetcom.top, left: offsetcom.left + em(57)});
$("#comment_content").focus();
$(".nav-panel-add-comment #comment_dom").val(domPath);
});
@dhoko
Copy link

dhoko commented Sep 22, 2015

Quand ça marche si tu veux je peux refaire une passe sur ton JS pour l'opti un peu :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment