integration/js/annotation-article.js
changeset 33 8d7e6a9018fc
parent 32 3612737630f4
child 35 63dae3d25255
equal deleted inserted replaced
32:3612737630f4 33:8d7e6a9018fc
   104     }
   104     }
   105     
   105     
   106     function highlightText(start, end, color) {
   106     function highlightText(start, end, color) {
   107         var annotation = {
   107         var annotation = {
   108             startOffset: start,
   108             startOffset: start,
   109             endOffset: end,
   109             length: end - start,
   110             color: color,
   110             color: color,
   111             comment: "",
   111             comment: "",
       
   112             creator: username,
   112             tags: [],
   113             tags: [],
   113             annotatedText: textinfo.text.substring(start, end),
   114             annotatedText: textinfo.text.substring(start, end),
   114             beforeText: textinfo.text.substring(start - 40, start).replace(/^[\S]*\s+/,''),
   115             beforeText: textinfo.text.substring(start - 40, start).replace(/^[\S]*\s+/,''),
   115             afterText: textinfo.text.substring(end, end + 40).replace(/\s+[\S]*$/,'')
   116             afterText: textinfo.text.substring(end, end + 40).replace(/\s+[\S]*$/,'')
   116         }
   117         }
   117         annotations.push(annotation);
   118         annotations.push(annotation);
   118         showAnnotation(annotation);
   119         showAnnotation(annotation, true);
   119     }
   120     }
   120     
   121     
   121     var frameTpl = _.template(
   122     var frameTpl = _.template(
   122         '<div class="annotation-frame" style="border-color: <%- annotation.color %>; top: <%-top %>px; left: <%- left %>px;">'
   123         '<div class="annotation-frame" style="border-color: <%- annotation.color %>; top: <%-top %>px; left: <%- left %>px;">'
   123         + '<div class="annotation-area" style="background-color: <%- annotation.color %>; height: <%- height %>px;"></div>'
   124         + '<div class="annotation-area" style="background-color: <%- annotation.color %>; height: <%- height %>px;"></div>'
   124         + '<form class="annotation-form"><h3>Commentaire&nbsp;:</h3>'
   125         + '<form class="annotation-form"><h3>Annoté par&nbsp;: <em><%- annotation.creator %></em></h3><h3>Commentaire&nbsp;:</h3>'
   125         + '<textarea class="annotation-textarea" placeholder="Mon commentaire&hellip;"><%- annotation.comment || "" %></textarea>'
   126         + '<% if (editable) { %><textarea class="annotation-textarea" placeholder="Mon commentaire&hellip;"><%- annotation.comment || "" %></textarea>'
       
   127         + '<% } else { %><p><%- annotation.comment || "(sans commentaire)" %></p><% } %>'
   126         + '<h3>Mots-clés&nbsp;:</h3>'
   128         + '<h3>Mots-clés&nbsp;:</h3>'
   127         + '<ul class="annotation-tags-form"><% _(annotation.tags).forEach(function(tag) { %><li><%- tag %></li><% }) %></ul>'
   129         + '<ul class="<%- editable ? "annotation-tags-form" : "" %>"><% _(annotation.tags).forEach(function(tag) { %><li><%- tag %></li><% }) %></ul>'
   128         + '<div><button class="annotation-remove">Supprimer</button><input class="annotation-submit" type="submit" value="Enregistrer" /></div>'
   130         + '<% if (editable) { %><div><a class="annotation-remove" href="#">Supprimer</a><input class="annotation-submit" type="submit" value="Enregistrer" /></div><% } %>'
   129         + '</form></div>'
   131         + '</form></div>'
   130     );
   132     );
   131     
   133     
   132     var liTpl = _.template(
   134     var liTpl = _.template(
   133         '<li style="border-color: <%- annotation.color %>;"><h3>Texte annoté</h3>'
   135         '<li style="border-color: <%- annotation.color %>;"><h3>Texte annoté</h3>'
   134         + '<p class="annotation-text"><%- annotation.beforeText %><b><%- annotation.annotatedText %></b><%- annotation.afterText %></p>'
   136         + '<p class="annotation-text"><%- annotation.beforeText %><b><%- annotation.annotatedText %></b><%- annotation.afterText %></p>'
       
   137         + '<h3>Annoté par&nbsp;: <em><%- annotation.creator %></em></h3>'
   135         + '<h3>Commentaire&nbsp;:</h3><p class="annotation-comment"><%- annotation.comment || "(Sans commentaire)" %></p>'
   138         + '<h3>Commentaire&nbsp;:</h3><p class="annotation-comment"><%- annotation.comment || "(Sans commentaire)" %></p>'
   136         + '<h3>Mots-clés&nbsp;:</h3><p class="annotation-tags"><%- (annotation.tags || []).join(", ") || "(aucun mot-clé)" %></p>'
   139         + '<h3>Mots-clés&nbsp;:</h3><p class="annotation-tags"><%- (annotation.tags || []).join(", ") || "(aucun mot-clé)" %></p>'
   137         + '</li>'
   140         + '</li>'
   138     );
   141     );
   139     
   142     
   175         currentVisibleFrame = null;
   178         currentVisibleFrame = null;
   176         showFrameBox();
   179         showFrameBox();
   177         $(".annotation-list li").removeClass("selected");
   180         $(".annotation-list li").removeClass("selected");
   178     }
   181     }
   179     
   182     
   180     function showAnnotation(annotation) {
   183     function showAnnotation(annotation, editAfterShow) {
   181         var start = annotation.startOffset,
   184         var start = annotation.startOffset,
   182             end = annotation.endOffset,
   185             end = annotation.length + start,
   183             color = annotation.color;
   186             color = annotation.color;
   184         var spans = [];
   187         var spans = [];
   185         
   188         
   186         for (var i = 0, l = textinfo.nodes.length; i < l; i++) {
   189         for (var i = 0, l = textinfo.nodes.length; i < l; i++) {
   187             var nodeinfo = textinfo.nodes[i];
   190             var nodeinfo = textinfo.nodes[i];
   195                 r.setEnd(nodeinfo.textNode, rangeend);
   198                 r.setEnd(nodeinfo.textNode, rangeend);
   196                 r.surroundContents(s);
   199                 r.surroundContents(s);
   197                 spans.push(s);
   200                 spans.push(s);
   198             }
   201             }
   199         }
   202         }
       
   203         
   200         textinfo = parseContents(basenode);
   204         textinfo = parseContents(basenode);
   201         var top = Math.min.apply(Math, spans.map(function(s) { return s.offsetTop })),
   205         var top = Math.min.apply(Math, spans.map(function(s) { return s.offsetTop })),
   202             height = Math.max.apply(Math, spans.map(function(s) { return s.offsetHeight + s.offsetTop })) - top,
   206             height = Math.max.apply(Math, spans.map(function(s) { return s.offsetHeight + s.offsetTop })) - top,
   203             frame = $(frameTpl({
   207             frame = $(frameTpl({
   204                 annotation: annotation,
   208                 annotation: annotation,
       
   209                 editable: (username === annotation.creator),
   205                 top: top,
   210                 top: top,
   206                 height: height,
   211                 height: height,
   207                 left: basenode.offsetLeft
   212                 left: basenode.offsetLeft
   208             })),
   213             })),
   209             li = $(liTpl({
   214             li = $(liTpl({
   279             .click(function() {
   284             .click(function() {
   280                 show();
   285                 show();
   281                 shownByClick = true;
   286                 shownByClick = true;
   282                 $(window).scrollTop(currentVisibleFrame.offset().top - 100);
   287                 $(window).scrollTop(currentVisibleFrame.offset().top - 100);
   283             });
   288             });
       
   289         
       
   290         if (editAfterShow) {
       
   291             show();
       
   292             shownByClick = true;
       
   293         }
       
   294         
   284     }
   295     }
   285     
   296     
   286     annotations.forEach(function(annotation) {
   297     annotations.forEach(function(annotation) {
   287         showAnnotation(annotation);
   298         showAnnotation(annotation);
   288     });
   299     });
   289     
   300     
   290     $(".content")
   301     var range = null;
   291         .mousedown(function() {
   302         
   292             mousedown = true;
   303     $(".content").mouseup(function(e) {
   293             dragging = false;
   304         range = document.getSelection().getRangeAt(0);
   294         })
   305         var addann = $(".add-annotation");
   295         .mousemove(function() {
   306         if (!range.collapsed && range.startContainer._nodeInfo && range.endContainer._nodeInfo && range.toString() !== " ") {
   296             if (mousedown) {
   307             addann.show();
   297                 dragging = true;
   308             var doc = $(document), rect = range.getBoundingClientRect();
   298             }
   309             addann.css({
   299         })
   310                 left: doc.scrollLeft() + rect.right + 5,
   300         .mouseup(function() {
   311                 top: doc.scrollTop() + (rect.top + rect.bottom - addann.outerHeight()) / 2,
   301             if (!dragging) {
   312             });
   302                 return;
   313         } else {
   303             }
   314             range = null;
   304             var range = document.getSelection().getRangeAt(0);
   315             $(".add-annotation").hide();
   305             if (!range.collapsed && range.startContainer._nodeInfo && range.endContainer._nodeInfo) {
   316         }
   306                 var start = range.startOffset + range.startContainer._nodeInfo.start,
   317     }).mousedown(function() {
   307                     end = range.endOffset + range.endContainer._nodeInfo.start;
   318         $(".add-annotation").hide();
   308                 highlightText(start, end, colors[ncol++ % colors.length]);
   319     });
   309                 document.getSelection().removeAllRanges();
   320     
   310             }
   321     $(".add-annotation").click(function() {
   311         });
   322         $(".add-annotation").hide();
   312         
   323         if (range) {
       
   324             var start = range.startOffset + range.startContainer._nodeInfo.start,
       
   325                 end = range.endOffset + range.endContainer._nodeInfo.start;
       
   326             highlightText(start, end, colors[ncol++ % colors.length]);
       
   327             document.getSelection().removeAllRanges();
       
   328         }
       
   329     });
       
   330             
   313     $(window).mouseup(function() {
   331     $(window).mouseup(function() {
   314         mousedown = false;
   332         mousedown = false;
   315         dragging = false;
   333         dragging = false;
   316     });
   334     });
   317     
   335     
   320     $(window).resize(function() {
   338     $(window).resize(function() {
   321         showFrameBox();
   339         showFrameBox();
   322         $(".annotation-frame").css({
   340         $(".annotation-frame").css({
   323             left: basenode.offsetLeft
   341             left: basenode.offsetLeft
   324         })
   342         })
   325     })
   343     });
       
   344     
       
   345     $(".add-annotation").css({
       
   346         left: (basenode.offsetLeft + 500)
       
   347     });
   326 
   348 
   327 });
   349 });