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);
});
@tcitworld
Copy link
Author

Je vois le souci, je vais essayer la conversion en int.

Si tu peux sauvegarder un attribut sur ton commentaire, tu pourrais sauvegarder l'attribut name du paragraphe.

C'était juste un exemple de paragraphes en fait, du coup je n'ai pas toujours des name ou des id ainsi.
Sinon je pourrais en effet avoir un travail en amont côté serveur qui donne des id ou name uniques à mes paragraphes. J'espère que php peut manipuler un DOM sans trop faire de bêtises.

PS un id pour être exploitable doit commencer par !Number.

Que veux-tu dire ?

Merci pour ton aide.

@dhoko
Copy link

dhoko commented Sep 22, 2015

Tu peux aisément le faire via domdocument et domelement qui sont des classes native PHP, t'as une liste ici pour bosser avec le dom en php.

Any string, with the following restrictions:

  • must be at least one character long
  • must not contain any space characters

Previous versions of HTML placed greater restrictions on the content of ID values (for example, they did not permit ID values to begin with a number).

cf html5 spec

@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