integration/annotation-article.js
author obledc
Wed, 14 Aug 2013 16:42:24 +0200
changeset 40 de926fe6c241
permissions -rw-r--r--
last
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40
obledc
parents:
diff changeset
     1
$(function() {
obledc
parents:
diff changeset
     2
obledc
parents:
diff changeset
     3
    $(".fancybox").fancybox();
obledc
parents:
diff changeset
     4
    $('.font-up a').click(function(){
obledc
parents:
diff changeset
     5
        var taille_police=parseFloat($('.content').css('font-size'),100)+2;
obledc
parents:
diff changeset
     6
        if(taille_police<30){
obledc
parents:
diff changeset
     7
            var taille_ligne=parseFloat($('.content').css('line-height'),100)+2;
obledc
parents:
diff changeset
     8
            $('.content').css({
obledc
parents:
diff changeset
     9
                'line-height':taille_ligne+'px',
obledc
parents:
diff changeset
    10
                'font-size':taille_police+'px'
obledc
parents:
diff changeset
    11
            });
obledc
parents:
diff changeset
    12
        }
obledc
parents:
diff changeset
    13
        return false;
obledc
parents:
diff changeset
    14
    });
obledc
parents:
diff changeset
    15
    $('.font-down a').click(function(){
obledc
parents:
diff changeset
    16
        var taille_police=parseFloat($('.content').css('font-size'),100)-2;
obledc
parents:
diff changeset
    17
        if(taille_police>11){
obledc
parents:
diff changeset
    18
            var taille_ligne=parseFloat($('.content').css('line-height'),100)-2;
obledc
parents:
diff changeset
    19
            $('.content').css({
obledc
parents:
diff changeset
    20
                'line-height':taille_ligne+'px',
obledc
parents:
diff changeset
    21
                'font-size':taille_police+'px'
obledc
parents:
diff changeset
    22
            });
obledc
parents:
diff changeset
    23
        }
obledc
parents:
diff changeset
    24
        return false;
obledc
parents:
diff changeset
    25
    });
obledc
parents:
diff changeset
    26
    
obledc
parents:
diff changeset
    27
    /* ANNOTATION HANDLING */
obledc
parents:
diff changeset
    28
    
obledc
parents:
diff changeset
    29
    var basenode = $(".content")[0],
obledc
parents:
diff changeset
    30
        cleanHtml = cleanTextNodes(basenode).innerHtml,
obledc
parents:
diff changeset
    31
        textinfo = parseContents(basenode);
obledc
parents:
diff changeset
    32
        
obledc
parents:
diff changeset
    33
    window.annotations = window.annotations || [];
obledc
parents:
diff changeset
    34
    
obledc
parents:
diff changeset
    35
    var colors = ["#ff8", "#f88", "#8f8", "#8ff", "#f8f", "#88f"],
obledc
parents:
diff changeset
    36
        currentVisibleFrame = null,
obledc
parents:
diff changeset
    37
        ncol = 0,
obledc
parents:
diff changeset
    38
        mousedown = false,
obledc
parents:
diff changeset
    39
        shownByClick = false,
obledc
parents:
diff changeset
    40
        dragging = false;
obledc
parents:
diff changeset
    41
    
obledc
parents:
diff changeset
    42
    function cleanText(txt, keepbefore, keepafter) {
obledc
parents:
diff changeset
    43
        var res = txt.replace(/[\n\r\t]+/gm,' ').replace(/ {2,}/g,' ');
obledc
parents:
diff changeset
    44
        if (!keepbefore) {
obledc
parents:
diff changeset
    45
            res = res.replace(/^ +/,'');
obledc
parents:
diff changeset
    46
        }
obledc
parents:
diff changeset
    47
        if (!keepafter) {
obledc
parents:
diff changeset
    48
            res = res.replace(/ +$/,'');
obledc
parents:
diff changeset
    49
        }
obledc
parents:
diff changeset
    50
        return res;
obledc
parents:
diff changeset
    51
    }
obledc
parents:
diff changeset
    52
    
obledc
parents:
diff changeset
    53
    function recursiveParse(node, info) {
obledc
parents:
diff changeset
    54
        var children = node.childNodes;
obledc
parents:
diff changeset
    55
        for (var i = 0, l = children.length; i < l; i++) {
obledc
parents:
diff changeset
    56
            var childnode = children[i];
obledc
parents:
diff changeset
    57
            switch(childnode.nodeType) {
obledc
parents:
diff changeset
    58
                case node.ELEMENT_NODE:
obledc
parents:
diff changeset
    59
                    recursiveParse(childnode, info);
obledc
parents:
diff changeset
    60
                break;
obledc
parents:
diff changeset
    61
                case node.TEXT_NODE:
obledc
parents:
diff changeset
    62
                    var startpos = info.text.length;
obledc
parents:
diff changeset
    63
                    info.text += childnode.textContent;
obledc
parents:
diff changeset
    64
                    var endpos = info.text.length,
obledc
parents:
diff changeset
    65
                        nodeinfo = {
obledc
parents:
diff changeset
    66
                            start: startpos,
obledc
parents:
diff changeset
    67
                            end: endpos,
obledc
parents:
diff changeset
    68
                            length: endpos - startpos,
obledc
parents:
diff changeset
    69
                            textNode: childnode
obledc
parents:
diff changeset
    70
                        };
obledc
parents:
diff changeset
    71
                    childnode._nodeInfo = nodeinfo;
obledc
parents:
diff changeset
    72
                    info.nodes.push(nodeinfo);
obledc
parents:
diff changeset
    73
                break;
obledc
parents:
diff changeset
    74
            }
obledc
parents:
diff changeset
    75
        }
obledc
parents:
diff changeset
    76
    }
obledc
parents:
diff changeset
    77
    
obledc
parents:
diff changeset
    78
    
obledc
parents:
diff changeset
    79
    function parseContents(node) {
obledc
parents:
diff changeset
    80
        var res = {
obledc
parents:
diff changeset
    81
            text: '',
obledc
parents:
diff changeset
    82
            nodes: []
obledc
parents:
diff changeset
    83
        };
obledc
parents:
diff changeset
    84
        recursiveParse(node, res);
obledc
parents:
diff changeset
    85
        return res;
obledc
parents:
diff changeset
    86
    }
obledc
parents:
diff changeset
    87
    
obledc
parents:
diff changeset
    88
    function cleanTextNodes(node) {
obledc
parents:
diff changeset
    89
        var children = node.childNodes;
obledc
parents:
diff changeset
    90
        for (var i = 0, l = children.length; i < l; i++) {
obledc
parents:
diff changeset
    91
            var childnode = children[i];
obledc
parents:
diff changeset
    92
            switch(childnode.nodeType) {
obledc
parents:
diff changeset
    93
                case node.ELEMENT_NODE:
obledc
parents:
diff changeset
    94
                    cleanTextNodes(childnode);
obledc
parents:
diff changeset
    95
                break;
obledc
parents:
diff changeset
    96
                case node.TEXT_NODE:
obledc
parents:
diff changeset
    97
                    var keepbefore = (i && children[i-1].nodeType == node.ELEMENT_NODE),
obledc
parents:
diff changeset
    98
                        keepafter = (i < l-1 && children[i+1].nodeType == node.ELEMENT_NODE);
obledc
parents:
diff changeset
    99
                    childnode.textContent = cleanText(childnode.textContent, keepbefore, keepafter);
obledc
parents:
diff changeset
   100
                break;
obledc
parents:
diff changeset
   101
            }
obledc
parents:
diff changeset
   102
        }
obledc
parents:
diff changeset
   103
        return node;
obledc
parents:
diff changeset
   104
    }
obledc
parents:
diff changeset
   105
    
obledc
parents:
diff changeset
   106
    function highlightText(start, end, color) {
obledc
parents:
diff changeset
   107
        var annotation = {
obledc
parents:
diff changeset
   108
            startOffset: start,
obledc
parents:
diff changeset
   109
            length: end - start,
obledc
parents:
diff changeset
   110
            color: color,
obledc
parents:
diff changeset
   111
            comment: "",
obledc
parents:
diff changeset
   112
            creator: currentUser,
obledc
parents:
diff changeset
   113
            tags: [],
obledc
parents:
diff changeset
   114
            annotatedText: textinfo.text.substring(start, end),
obledc
parents:
diff changeset
   115
            beforeText: textinfo.text.substring(start - 40, start).replace(/^[\S]*\s+/,''),
obledc
parents:
diff changeset
   116
            afterText: textinfo.text.substring(end, end + 40).replace(/\s+[\S]*$/,''),
obledc
parents:
diff changeset
   117
            documentaryFile: currentDocumentaryFile
obledc
parents:
diff changeset
   118
        }
obledc
parents:
diff changeset
   119
        annotations.push(annotation);
obledc
parents:
diff changeset
   120
        showAnnotation(annotation, true);
obledc
parents:
diff changeset
   121
        updateAnnotationCounts();
obledc
parents:
diff changeset
   122
    }
obledc
parents:
diff changeset
   123
    
obledc
parents:
diff changeset
   124
    var frameTpl = _.template(
obledc
parents:
diff changeset
   125
        '<div class="annotation-frame" style="border-color: <%- annotation.color %>; top: <%-top %>px; left: <%- left %>px;">'
obledc
parents:
diff changeset
   126
        + '<div class="annotation-area" style="background-color: <%- annotation.color %>; height: <%- height %>px;"></div>'
obledc
parents:
diff changeset
   127
        + '<form class="annotation-form"><h3>Annoté par&nbsp;: <em><%- annotation.creator.name %></em></h3>'
obledc
parents:
diff changeset
   128
        + '<h3>Dossier documentaire&nbsp;: <em><%- annotation.documentaryFile.name %></em></h3><h3>Commentaire&nbsp;:</h3>'
obledc
parents:
diff changeset
   129
        + '<% if (editable) { %><textarea class="annotation-textarea" placeholder="Mon commentaire&hellip;"><%- annotation.comment || "" %></textarea>'
obledc
parents:
diff changeset
   130
        + '<% } else { %><p><%- annotation.comment || "(sans commentaire)" %></p><% } %>'
obledc
parents:
diff changeset
   131
        + '<h3>Mots-clés&nbsp;:</h3>'
obledc
parents:
diff changeset
   132
        + '<ul class="<%- editable ? "annotation-tags-form" : "" %>"><% _(annotation.tags).forEach(function(tag) { %><li><%- tag %></li><% }) %></ul>'
obledc
parents:
diff changeset
   133
        + '<% if (editable) { %><h3><input class="annotation-public" type="checkbox" <% if (annotation.isPublic) {%>checked <% } %>/>Annotation publique</h3><div><a class="annotation-remove" href="#">Supprimer</a><input class="annotation-submit" type="submit" value="Enregistrer" /></div><% } %>'
obledc
parents:
diff changeset
   134
        + '</form></div>'
obledc
parents:
diff changeset
   135
    );
obledc
parents:
diff changeset
   136
    
obledc
parents:
diff changeset
   137
    var liTpl = _.template(
obledc
parents:
diff changeset
   138
        '<li style="border-color: <%- annotation.color %>;"><div class="annotation-longview"><h3>Texte annoté&nbsp;:</h3></div>'
obledc
parents:
diff changeset
   139
        + '<p class="annotation-text"><%- annotation.beforeText %><b><%- annotation.annotatedText %></b><%- annotation.afterText %></p>'
obledc
parents:
diff changeset
   140
        + '<h3>Annoté par&nbsp;: <em><%- annotation.creator.name %></em></h3>'
obledc
parents:
diff changeset
   141
        + '<div class="annotation-longview"><h3>Commentaire&nbsp;:</h3><p class="annotation-comment"><%- annotation.comment || "(Sans commentaire)" %></p>'
obledc
parents:
diff changeset
   142
        + '<h3>Mots-clés&nbsp;:</h3><p class="annotation-tags"><%- (annotation.tags || []).join(", ") || "(aucun mot-clé)" %></p></div>'
obledc
parents:
diff changeset
   143
        + '</li>'
obledc
parents:
diff changeset
   144
    );
obledc
parents:
diff changeset
   145
    
obledc
parents:
diff changeset
   146
    function showFrameBox() {
obledc
parents:
diff changeset
   147
        if (currentVisibleFrame) {
obledc
parents:
diff changeset
   148
            $(".annotation-frame-box").show();
obledc
parents:
diff changeset
   149
            var offset = currentVisibleFrame.offset(),
obledc
parents:
diff changeset
   150
                width = currentVisibleFrame.outerWidth(),
obledc
parents:
diff changeset
   151
                height = currentVisibleFrame.outerHeight();
obledc
parents:
diff changeset
   152
            $(".annotation-fb-top").css({
obledc
parents:
diff changeset
   153
                height: offset.top - 77
obledc
parents:
diff changeset
   154
            });
obledc
parents:
diff changeset
   155
            $(".annotation-fb-left").css({
obledc
parents:
diff changeset
   156
                top: offset.top,
obledc
parents:
diff changeset
   157
                height: height,
obledc
parents:
diff changeset
   158
                width: offset.left
obledc
parents:
diff changeset
   159
            });
obledc
parents:
diff changeset
   160
            $(".annotation-fb-right").css({
obledc
parents:
diff changeset
   161
                top: offset.top,
obledc
parents:
diff changeset
   162
                height: height,
obledc
parents:
diff changeset
   163
                left: offset.left + width
obledc
parents:
diff changeset
   164
            });
obledc
parents:
diff changeset
   165
            var fbbtop = offset.top + height;
obledc
parents:
diff changeset
   166
            $(".annotation-fb-bottom").css({
obledc
parents:
diff changeset
   167
                top: fbbtop,
obledc
parents:
diff changeset
   168
                height: ($("body").height() - fbbtop)
obledc
parents:
diff changeset
   169
            });
obledc
parents:
diff changeset
   170
            currentVisibleFrame.find(".annotation-textarea").focus();
obledc
parents:
diff changeset
   171
        } else {
obledc
parents:
diff changeset
   172
            $(".annotation-frame-box").hide();
obledc
parents:
diff changeset
   173
        }
obledc
parents:
diff changeset
   174
    }
obledc
parents:
diff changeset
   175
    
obledc
parents:
diff changeset
   176
    function hideAllFrames() {
obledc
parents:
diff changeset
   177
        if (currentVisibleFrame) {
obledc
parents:
diff changeset
   178
            currentVisibleFrame.hide();
obledc
parents:
diff changeset
   179
        }
obledc
parents:
diff changeset
   180
        
obledc
parents:
diff changeset
   181
        currentVisibleFrame = null;
obledc
parents:
diff changeset
   182
        showFrameBox();
obledc
parents:
diff changeset
   183
        $(".annotation-blocks li").removeClass("selected");
obledc
parents:
diff changeset
   184
    }
obledc
parents:
diff changeset
   185
    
obledc
parents:
diff changeset
   186
    function showAnnotation(annotation, editAfterShow) {
obledc
parents:
diff changeset
   187
        var start = annotation.startOffset,
obledc
parents:
diff changeset
   188
            end = annotation.length + start,
obledc
parents:
diff changeset
   189
            color = annotation.color;
obledc
parents:
diff changeset
   190
        var spans = [];
obledc
parents:
diff changeset
   191
        
obledc
parents:
diff changeset
   192
        for (var i = 0, l = textinfo.nodes.length; i < l; i++) {
obledc
parents:
diff changeset
   193
            var nodeinfo = textinfo.nodes[i];
obledc
parents:
diff changeset
   194
            if (nodeinfo.end > start && nodeinfo.start <= end) {
obledc
parents:
diff changeset
   195
                var r = document.createRange(),
obledc
parents:
diff changeset
   196
                    s = document.createElement('span'),
obledc
parents:
diff changeset
   197
                    rangestart = Math.max(0, start - nodeinfo.start),
obledc
parents:
diff changeset
   198
                    rangeend = Math.min(nodeinfo.length, end - nodeinfo.start);
obledc
parents:
diff changeset
   199
                s.style.backgroundColor = color;
obledc
parents:
diff changeset
   200
                r.setStart(nodeinfo.textNode, rangestart);
obledc
parents:
diff changeset
   201
                r.setEnd(nodeinfo.textNode, rangeend);
obledc
parents:
diff changeset
   202
                r.surroundContents(s);
obledc
parents:
diff changeset
   203
                spans.push(s);
obledc
parents:
diff changeset
   204
            }
obledc
parents:
diff changeset
   205
        }
obledc
parents:
diff changeset
   206
        
obledc
parents:
diff changeset
   207
        textinfo = parseContents(basenode);
obledc
parents:
diff changeset
   208
        var top = Math.min.apply(Math, spans.map(function(s) { return s.offsetTop })),
obledc
parents:
diff changeset
   209
            height = Math.max.apply(Math, spans.map(function(s) { return s.offsetHeight + s.offsetTop })) - top,
obledc
parents:
diff changeset
   210
            frame = $(frameTpl({
obledc
parents:
diff changeset
   211
                annotation: annotation,
obledc
parents:
diff changeset
   212
                editable: (currentUser.id === annotation.creator.id),
obledc
parents:
diff changeset
   213
                top: top,
obledc
parents:
diff changeset
   214
                height: height,
obledc
parents:
diff changeset
   215
                left: basenode.offsetLeft
obledc
parents:
diff changeset
   216
            })),
obledc
parents:
diff changeset
   217
            li = $(liTpl({
obledc
parents:
diff changeset
   218
                annotation: annotation
obledc
parents:
diff changeset
   219
            }));
obledc
parents:
diff changeset
   220
        
obledc
parents:
diff changeset
   221
        $(".annotation-frames").append(frame);
obledc
parents:
diff changeset
   222
        $(annotation.documentaryFile.id === currentDocumentaryFile.id ? ".annotation-file-list" : ".annotation-other-list").append(li);
obledc
parents:
diff changeset
   223
        
obledc
parents:
diff changeset
   224
        frame.find(".annotation-textarea").on("keyup paste input change", function() {
obledc
parents:
diff changeset
   225
            annotation.comment = $(this).val();
obledc
parents:
diff changeset
   226
            li.find(".annotation-comment").text(annotation.comment || "(Sans commentaire)");
obledc
parents:
diff changeset
   227
        });
obledc
parents:
diff changeset
   228
        
obledc
parents:
diff changeset
   229
        frame.find(".annotation-public").change(function() {
obledc
parents:
diff changeset
   230
            annotation.isPublic = $(this).is(":checked");
obledc
parents:
diff changeset
   231
        });
obledc
parents:
diff changeset
   232
        
obledc
parents:
diff changeset
   233
        frame.find("")
obledc
parents:
diff changeset
   234
        
obledc
parents:
diff changeset
   235
        var ontagchange = function(evt, ui) {
obledc
parents:
diff changeset
   236
            annotation.tags = $(this).tagit("assignedTags");
obledc
parents:
diff changeset
   237
            li.find(".annotation-tags").text((annotation.tags || []).join(", ") || "(aucun mot-clé)");
obledc
parents:
diff changeset
   238
        };
obledc
parents:
diff changeset
   239
        
obledc
parents:
diff changeset
   240
        frame.find(".annotation-tags-form").tagit({
obledc
parents:
diff changeset
   241
            afterTagAdded: ontagchange,
obledc
parents:
diff changeset
   242
            afterTagRemoved: ontagchange
obledc
parents:
diff changeset
   243
        });
obledc
parents:
diff changeset
   244
        
obledc
parents:
diff changeset
   245
        var show = function() {
obledc
parents:
diff changeset
   246
            if (mousedown) {
obledc
parents:
diff changeset
   247
                return;
obledc
parents:
diff changeset
   248
            }
obledc
parents:
diff changeset
   249
            shownByClick = false;
obledc
parents:
diff changeset
   250
            currentVisibleFrame = frame;
obledc
parents:
diff changeset
   251
            frame.show();
obledc
parents:
diff changeset
   252
            showFrameBox();
obledc
parents:
diff changeset
   253
            li.addClass("selected");
obledc
parents:
diff changeset
   254
        }
obledc
parents:
diff changeset
   255
                
obledc
parents:
diff changeset
   256
        $(spans).mouseenter(show);
obledc
parents:
diff changeset
   257
        
obledc
parents:
diff changeset
   258
        frame
obledc
parents:
diff changeset
   259
            .mouseleave(function() {
obledc
parents:
diff changeset
   260
                if (!shownByClick) {
obledc
parents:
diff changeset
   261
                    hideAllFrames();
obledc
parents:
diff changeset
   262
                }
obledc
parents:
diff changeset
   263
            })
obledc
parents:
diff changeset
   264
            .click(function() {
obledc
parents:
diff changeset
   265
                shownByClick = true;
obledc
parents:
diff changeset
   266
            });
obledc
parents:
diff changeset
   267
        
obledc
parents:
diff changeset
   268
        frame.find(".annotation-form").submit(function() {
obledc
parents:
diff changeset
   269
            hideAllFrames();
obledc
parents:
diff changeset
   270
            return false;
obledc
parents:
diff changeset
   271
        });
obledc
parents:
diff changeset
   272
        
obledc
parents:
diff changeset
   273
        frame.find(".annotation-remove").click(function() {
obledc
parents:
diff changeset
   274
            annotations = _(annotations).reject(function(a) {
obledc
parents:
diff changeset
   275
                return a === annotation
obledc
parents:
diff changeset
   276
            });
obledc
parents:
diff changeset
   277
            $(spans).css("background-color","").off("mouseenter",show);
obledc
parents:
diff changeset
   278
            li.remove();
obledc
parents:
diff changeset
   279
            frame.remove();
obledc
parents:
diff changeset
   280
            hideAllFrames();
obledc
parents:
diff changeset
   281
            updateAnnotationCounts();
obledc
parents:
diff changeset
   282
            return false;
obledc
parents:
diff changeset
   283
        });
obledc
parents:
diff changeset
   284
        
obledc
parents:
diff changeset
   285
        li
obledc
parents:
diff changeset
   286
            .mouseenter(function() {
obledc
parents:
diff changeset
   287
                $(spans).addClass("annotation-selected");
obledc
parents:
diff changeset
   288
                li.addClass("selected");
obledc
parents:
diff changeset
   289
                li.find(".annotation-longview").stop().slideDown();
obledc
parents:
diff changeset
   290
            })
obledc
parents:
diff changeset
   291
            .mouseleave(function() {
obledc
parents:
diff changeset
   292
                $(spans).removeClass("annotation-selected");
obledc
parents:
diff changeset
   293
                li.removeClass("selected");
obledc
parents:
diff changeset
   294
                li.find(".annotation-longview").stop().slideUp();
obledc
parents:
diff changeset
   295
            })
obledc
parents:
diff changeset
   296
            .click(function() {
obledc
parents:
diff changeset
   297
                show();
obledc
parents:
diff changeset
   298
                shownByClick = true;
obledc
parents:
diff changeset
   299
                $(window).scrollTop(currentVisibleFrame.offset().top - 100);
obledc
parents:
diff changeset
   300
            });
obledc
parents:
diff changeset
   301
        
obledc
parents:
diff changeset
   302
        if (editAfterShow) {
obledc
parents:
diff changeset
   303
            show();
obledc
parents:
diff changeset
   304
            shownByClick = true;
obledc
parents:
diff changeset
   305
        }
obledc
parents:
diff changeset
   306
        
obledc
parents:
diff changeset
   307
    }
obledc
parents:
diff changeset
   308
    
obledc
parents:
diff changeset
   309
    function updateAnnotationCounts() {
obledc
parents:
diff changeset
   310
        $(".annotation-blocks .block").each(function() {
obledc
parents:
diff changeset
   311
            var $this = $(this), n = $this.find("li").length;
obledc
parents:
diff changeset
   312
            $this.find(".annotations-count").text(n || "aucune");
obledc
parents:
diff changeset
   313
            if (n > 1) {
obledc
parents:
diff changeset
   314
                $this.find(".annotation-plural").show();
obledc
parents:
diff changeset
   315
            } else {
obledc
parents:
diff changeset
   316
                $this.find(".annotation-plural").hide();
obledc
parents:
diff changeset
   317
            }
obledc
parents:
diff changeset
   318
        })
obledc
parents:
diff changeset
   319
    }
obledc
parents:
diff changeset
   320
    
obledc
parents:
diff changeset
   321
    annotations.forEach(function(annotation) {
obledc
parents:
diff changeset
   322
        showAnnotation(annotation);
obledc
parents:
diff changeset
   323
    });
obledc
parents:
diff changeset
   324
    updateAnnotationCounts();
obledc
parents:
diff changeset
   325
    
obledc
parents:
diff changeset
   326
    var range = null;
obledc
parents:
diff changeset
   327
        
obledc
parents:
diff changeset
   328
    $(".content").mouseup(function(e) {
obledc
parents:
diff changeset
   329
        range = document.getSelection().getRangeAt(0);
obledc
parents:
diff changeset
   330
        var addann = $(".add-annotation");
obledc
parents:
diff changeset
   331
        if (!range.collapsed && range.startContainer._nodeInfo && range.endContainer._nodeInfo && range.toString() !== " ") {
obledc
parents:
diff changeset
   332
            addann.show();
obledc
parents:
diff changeset
   333
            var doc = $(document), rect = range.getBoundingClientRect();
obledc
parents:
diff changeset
   334
            addann.css({
obledc
parents:
diff changeset
   335
                left: doc.scrollLeft() + rect.right + 5,
obledc
parents:
diff changeset
   336
                top: doc.scrollTop() + (rect.top + rect.bottom - addann.outerHeight()) / 2,
obledc
parents:
diff changeset
   337
            });
obledc
parents:
diff changeset
   338
        } else {
obledc
parents:
diff changeset
   339
            range = null;
obledc
parents:
diff changeset
   340
            $(".add-annotation").hide();
obledc
parents:
diff changeset
   341
        }
obledc
parents:
diff changeset
   342
    }).mousedown(function() {
obledc
parents:
diff changeset
   343
        $(".add-annotation").hide();
obledc
parents:
diff changeset
   344
    });
obledc
parents:
diff changeset
   345
    
obledc
parents:
diff changeset
   346
    $(".add-annotation").click(function() {
obledc
parents:
diff changeset
   347
        $(".add-annotation").hide();
obledc
parents:
diff changeset
   348
        if (range) {
obledc
parents:
diff changeset
   349
            var start = range.startOffset + range.startContainer._nodeInfo.start,
obledc
parents:
diff changeset
   350
                end = range.endOffset + range.endContainer._nodeInfo.start;
obledc
parents:
diff changeset
   351
            highlightText(start, end, colors[ncol++ % colors.length]);
obledc
parents:
diff changeset
   352
            document.getSelection().removeAllRanges();
obledc
parents:
diff changeset
   353
        }
obledc
parents:
diff changeset
   354
    });
obledc
parents:
diff changeset
   355
            
obledc
parents:
diff changeset
   356
    $(window).mouseup(function() {
obledc
parents:
diff changeset
   357
        mousedown = false;
obledc
parents:
diff changeset
   358
        dragging = false;
obledc
parents:
diff changeset
   359
    });
obledc
parents:
diff changeset
   360
    
obledc
parents:
diff changeset
   361
    $(".annotation-frame-box").click(hideAllFrames);
obledc
parents:
diff changeset
   362
    
obledc
parents:
diff changeset
   363
    $(window).resize(function() {
obledc
parents:
diff changeset
   364
        showFrameBox();
obledc
parents:
diff changeset
   365
        $(".annotation-frame").css({
obledc
parents:
diff changeset
   366
            left: basenode.offsetLeft
obledc
parents:
diff changeset
   367
        })
obledc
parents:
diff changeset
   368
    });
obledc
parents:
diff changeset
   369
    
obledc
parents:
diff changeset
   370
    $(".add-annotation").css({
obledc
parents:
diff changeset
   371
        left: (basenode.offsetLeft + 500)
obledc
parents:
diff changeset
   372
    });
obledc
parents:
diff changeset
   373
obledc
parents:
diff changeset
   374
});