alcatel/static/js/annotation-article.js
author obledc
Tue, 10 Sep 2013 13:28:30 +0200
changeset 44 3648b6dea2cc
permissions -rw-r--r--
new
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44
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
		alert('start'+start);
obledc
parents:
diff changeset
   108
		alert('end'+end);
obledc
parents:
diff changeset
   109
		alert('color'+color);
obledc
parents:
diff changeset
   110
obledc
parents:
diff changeset
   111
		alert('textinfo.text.substring(start, end)'+textinfo.text.substring(start, end));
obledc
parents:
diff changeset
   112
		alert('currentDocumentaryFile'+currentDocumentaryFile);
obledc
parents:
diff changeset
   113
		
obledc
parents:
diff changeset
   114
		
obledc
parents:
diff changeset
   115
        var annotation = {
obledc
parents:
diff changeset
   116
            startOffset: start,
obledc
parents:
diff changeset
   117
            length: end - start,
obledc
parents:
diff changeset
   118
            color: color,
obledc
parents:
diff changeset
   119
            comment: "",
obledc
parents:
diff changeset
   120
            creator: "cobled",
obledc
parents:
diff changeset
   121
            tags: [],
obledc
parents:
diff changeset
   122
            annotatedText: textinfo.text.substring(start, end),
obledc
parents:
diff changeset
   123
            beforeText: textinfo.text.substring(start - 40, start).replace(/^[\S]*\s+/,''),
obledc
parents:
diff changeset
   124
            afterText: textinfo.text.substring(end, end + 40).replace(/\s+[\S]*$/,''),
obledc
parents:
diff changeset
   125
            documentaryFile: currentDocumentaryFile
obledc
parents:
diff changeset
   126
        }
obledc
parents:
diff changeset
   127
			alert('highlightText');
obledc
parents:
diff changeset
   128
        annotations.push(annotation);
obledc
parents:
diff changeset
   129
			alert('highlightText');
obledc
parents:
diff changeset
   130
        showAnnotation(annotation, true);
obledc
parents:
diff changeset
   131
			alert('highlightText');
obledc
parents:
diff changeset
   132
        updateAnnotationCounts();
obledc
parents:
diff changeset
   133
			alert('highlightText');
obledc
parents:
diff changeset
   134
    }
obledc
parents:
diff changeset
   135
    
obledc
parents:
diff changeset
   136
    var frameTpl = _.template(
obledc
parents:
diff changeset
   137
        '<div class="annotation-frame" style="border-color: <%- annotation.color %>; top: <%-top %>px; left: <%- left %>px;">'
obledc
parents:
diff changeset
   138
        + '<div class="annotation-area" style="background-color: <%- annotation.color %>; height: <%- height %>px;"></div>'
obledc
parents:
diff changeset
   139
        + '<form class="annotation-form"><h3>Annoté par&nbsp;: <em><%- annotation.creator.name %></em></h3>'
obledc
parents:
diff changeset
   140
        + '<h3>Dossier documentaire&nbsp;: <em><%- annotation.documentaryFile.name %></em></h3><h3>Commentaire&nbsp;:</h3>'
obledc
parents:
diff changeset
   141
        + '<% if (editable) { %><textarea class="annotation-textarea" placeholder="Mon commentaire&hellip;"><%- annotation.comment || "" %></textarea>'
obledc
parents:
diff changeset
   142
        + '<% } else { %><p><%- annotation.comment || "(sans commentaire)" %></p><% } %>'
obledc
parents:
diff changeset
   143
        + '<h3>Mots-clés&nbsp;:</h3>'
obledc
parents:
diff changeset
   144
        + '<ul class="<%- editable ? "annotation-tags-form" : "" %>"><% _(annotation.tags).forEach(function(tag) { %><li><%- tag %></li><% }) %></ul>'
obledc
parents:
diff changeset
   145
        + '<% 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
   146
        + '</form></div>'
obledc
parents:
diff changeset
   147
    );
obledc
parents:
diff changeset
   148
    
obledc
parents:
diff changeset
   149
    var liTpl = _.template(
obledc
parents:
diff changeset
   150
        '<li style="border-color: <%- annotation.color %>;"><div class="annotation-longview"><h3>Texte annoté&nbsp;:</h3></div>'
obledc
parents:
diff changeset
   151
        + '<p class="annotation-text"><%- annotation.beforeText %><b><%- annotation.annotatedText %></b><%- annotation.afterText %></p>'
obledc
parents:
diff changeset
   152
        + '<h3>Annoté par&nbsp;: <em><%- annotation.creator.name %></em></h3>'
obledc
parents:
diff changeset
   153
        + '<div class="annotation-longview"><h3>Commentaire&nbsp;:</h3><p class="annotation-comment"><%- annotation.comment || "(Sans commentaire)" %></p>'
obledc
parents:
diff changeset
   154
        + '<h3>Mots-clés&nbsp;:</h3><p class="annotation-tags"><%- (annotation.tags || []).join(", ") || "(aucun mot-clé)" %></p></div>'
obledc
parents:
diff changeset
   155
        + '</li>'
obledc
parents:
diff changeset
   156
    );
obledc
parents:
diff changeset
   157
    
obledc
parents:
diff changeset
   158
    function showFrameBox() {
obledc
parents:
diff changeset
   159
        if (currentVisibleFrame) {
obledc
parents:
diff changeset
   160
            $(".annotation-frame-box").show();
obledc
parents:
diff changeset
   161
            var offset = currentVisibleFrame.offset(),
obledc
parents:
diff changeset
   162
                width = currentVisibleFrame.outerWidth(),
obledc
parents:
diff changeset
   163
                height = currentVisibleFrame.outerHeight();
obledc
parents:
diff changeset
   164
            $(".annotation-fb-top").css({
obledc
parents:
diff changeset
   165
                height: offset.top - 77
obledc
parents:
diff changeset
   166
            });
obledc
parents:
diff changeset
   167
            $(".annotation-fb-left").css({
obledc
parents:
diff changeset
   168
                top: offset.top,
obledc
parents:
diff changeset
   169
                height: height,
obledc
parents:
diff changeset
   170
                width: offset.left
obledc
parents:
diff changeset
   171
            });
obledc
parents:
diff changeset
   172
            $(".annotation-fb-right").css({
obledc
parents:
diff changeset
   173
                top: offset.top,
obledc
parents:
diff changeset
   174
                height: height,
obledc
parents:
diff changeset
   175
                left: offset.left + width
obledc
parents:
diff changeset
   176
            });
obledc
parents:
diff changeset
   177
            var fbbtop = offset.top + height;
obledc
parents:
diff changeset
   178
            $(".annotation-fb-bottom").css({
obledc
parents:
diff changeset
   179
                top: fbbtop,
obledc
parents:
diff changeset
   180
                height: ($("body").height() - fbbtop)
obledc
parents:
diff changeset
   181
            });
obledc
parents:
diff changeset
   182
            currentVisibleFrame.find(".annotation-textarea").focus();
obledc
parents:
diff changeset
   183
        } else {
obledc
parents:
diff changeset
   184
            $(".annotation-frame-box").hide();
obledc
parents:
diff changeset
   185
        }
obledc
parents:
diff changeset
   186
    }
obledc
parents:
diff changeset
   187
    
obledc
parents:
diff changeset
   188
    function hideAllFrames() {
obledc
parents:
diff changeset
   189
        if (currentVisibleFrame) {
obledc
parents:
diff changeset
   190
            currentVisibleFrame.hide();
obledc
parents:
diff changeset
   191
        }
obledc
parents:
diff changeset
   192
        
obledc
parents:
diff changeset
   193
        currentVisibleFrame = null;
obledc
parents:
diff changeset
   194
        showFrameBox();
obledc
parents:
diff changeset
   195
        $(".annotation-blocks li").removeClass("selected");
obledc
parents:
diff changeset
   196
    }
obledc
parents:
diff changeset
   197
    
obledc
parents:
diff changeset
   198
    function showAnnotation(annotation, editAfterShow) {
obledc
parents:
diff changeset
   199
        var start = annotation.startOffset,
obledc
parents:
diff changeset
   200
            end = annotation.length + start,
obledc
parents:
diff changeset
   201
            color = annotation.color;
obledc
parents:
diff changeset
   202
        var spans = [];
obledc
parents:
diff changeset
   203
        
obledc
parents:
diff changeset
   204
        for (var i = 0, l = textinfo.nodes.length; i < l; i++) {
obledc
parents:
diff changeset
   205
            var nodeinfo = textinfo.nodes[i];
obledc
parents:
diff changeset
   206
            if (nodeinfo.end > start && nodeinfo.start <= end) {
obledc
parents:
diff changeset
   207
                var r = document.createRange(),
obledc
parents:
diff changeset
   208
                    s = document.createElement('span'),
obledc
parents:
diff changeset
   209
                    rangestart = Math.max(0, start - nodeinfo.start),
obledc
parents:
diff changeset
   210
                    rangeend = Math.min(nodeinfo.length, end - nodeinfo.start);
obledc
parents:
diff changeset
   211
                s.style.backgroundColor = color;
obledc
parents:
diff changeset
   212
                r.setStart(nodeinfo.textNode, rangestart);
obledc
parents:
diff changeset
   213
                r.setEnd(nodeinfo.textNode, rangeend);
obledc
parents:
diff changeset
   214
                r.surroundContents(s);
obledc
parents:
diff changeset
   215
                spans.push(s);
obledc
parents:
diff changeset
   216
            }
obledc
parents:
diff changeset
   217
        }
obledc
parents:
diff changeset
   218
        
obledc
parents:
diff changeset
   219
        textinfo = parseContents(basenode);
obledc
parents:
diff changeset
   220
        var top = Math.min.apply(Math, spans.map(function(s) { return s.offsetTop })),
obledc
parents:
diff changeset
   221
            height = Math.max.apply(Math, spans.map(function(s) { return s.offsetHeight + s.offsetTop })) - top,
obledc
parents:
diff changeset
   222
            frame = $(frameTpl({
obledc
parents:
diff changeset
   223
                annotation: annotation,
obledc
parents:
diff changeset
   224
                editable: (currentUser.id === annotation.creator.id),
obledc
parents:
diff changeset
   225
                top: top,
obledc
parents:
diff changeset
   226
                height: height,
obledc
parents:
diff changeset
   227
                left: basenode.offsetLeft
obledc
parents:
diff changeset
   228
            })),
obledc
parents:
diff changeset
   229
            li = $(liTpl({
obledc
parents:
diff changeset
   230
                annotation: annotation
obledc
parents:
diff changeset
   231
            }));
obledc
parents:
diff changeset
   232
        
obledc
parents:
diff changeset
   233
        $(".annotation-frames").append(frame);
obledc
parents:
diff changeset
   234
        $(annotation.documentaryFile.id === currentDocumentaryFile.id ? ".annotation-file-list" : ".annotation-other-list").append(li);
obledc
parents:
diff changeset
   235
        
obledc
parents:
diff changeset
   236
        frame.find(".annotation-textarea").on("keyup paste input change", function() {
obledc
parents:
diff changeset
   237
            annotation.comment = $(this).val();
obledc
parents:
diff changeset
   238
            li.find(".annotation-comment").text(annotation.comment || "(Sans commentaire)");
obledc
parents:
diff changeset
   239
        });
obledc
parents:
diff changeset
   240
        
obledc
parents:
diff changeset
   241
        frame.find(".annotation-public").change(function() {
obledc
parents:
diff changeset
   242
            annotation.isPublic = $(this).is(":checked");
obledc
parents:
diff changeset
   243
        });
obledc
parents:
diff changeset
   244
        
obledc
parents:
diff changeset
   245
        frame.find("")
obledc
parents:
diff changeset
   246
        
obledc
parents:
diff changeset
   247
        var ontagchange = function(evt, ui) {
obledc
parents:
diff changeset
   248
            annotation.tags = $(this).tagit("assignedTags");
obledc
parents:
diff changeset
   249
            li.find(".annotation-tags").text((annotation.tags || []).join(", ") || "(aucun mot-clé)");
obledc
parents:
diff changeset
   250
        };
obledc
parents:
diff changeset
   251
        
obledc
parents:
diff changeset
   252
        frame.find(".annotation-tags-form").tagit({
obledc
parents:
diff changeset
   253
            afterTagAdded: ontagchange,
obledc
parents:
diff changeset
   254
            afterTagRemoved: ontagchange
obledc
parents:
diff changeset
   255
        });
obledc
parents:
diff changeset
   256
        
obledc
parents:
diff changeset
   257
        var show = function() {
obledc
parents:
diff changeset
   258
            if (mousedown) {
obledc
parents:
diff changeset
   259
                return;
obledc
parents:
diff changeset
   260
            }
obledc
parents:
diff changeset
   261
            shownByClick = false;
obledc
parents:
diff changeset
   262
            currentVisibleFrame = frame;
obledc
parents:
diff changeset
   263
            frame.show();
obledc
parents:
diff changeset
   264
            showFrameBox();
obledc
parents:
diff changeset
   265
            li.addClass("selected");
obledc
parents:
diff changeset
   266
        }
obledc
parents:
diff changeset
   267
                
obledc
parents:
diff changeset
   268
        $(spans).mouseenter(show);
obledc
parents:
diff changeset
   269
        
obledc
parents:
diff changeset
   270
        frame
obledc
parents:
diff changeset
   271
            .mouseleave(function() {
obledc
parents:
diff changeset
   272
                if (!shownByClick) {
obledc
parents:
diff changeset
   273
                    hideAllFrames();
obledc
parents:
diff changeset
   274
                }
obledc
parents:
diff changeset
   275
            })
obledc
parents:
diff changeset
   276
            .click(function() {
obledc
parents:
diff changeset
   277
                shownByClick = true;
obledc
parents:
diff changeset
   278
            });
obledc
parents:
diff changeset
   279
        
obledc
parents:
diff changeset
   280
        frame.find(".annotation-form").submit(function() {
obledc
parents:
diff changeset
   281
            hideAllFrames();
obledc
parents:
diff changeset
   282
            return false;
obledc
parents:
diff changeset
   283
        });
obledc
parents:
diff changeset
   284
        
obledc
parents:
diff changeset
   285
        frame.find(".annotation-remove").click(function() {
obledc
parents:
diff changeset
   286
            annotations = _(annotations).reject(function(a) {
obledc
parents:
diff changeset
   287
                return a === annotation
obledc
parents:
diff changeset
   288
            });
obledc
parents:
diff changeset
   289
            $(spans).css("background-color","").off("mouseenter",show);
obledc
parents:
diff changeset
   290
            li.remove();
obledc
parents:
diff changeset
   291
            frame.remove();
obledc
parents:
diff changeset
   292
            hideAllFrames();
obledc
parents:
diff changeset
   293
            updateAnnotationCounts();
obledc
parents:
diff changeset
   294
            return false;
obledc
parents:
diff changeset
   295
        });
obledc
parents:
diff changeset
   296
        
obledc
parents:
diff changeset
   297
        li
obledc
parents:
diff changeset
   298
            .mouseenter(function() {
obledc
parents:
diff changeset
   299
                $(spans).addClass("annotation-selected");
obledc
parents:
diff changeset
   300
                li.addClass("selected");
obledc
parents:
diff changeset
   301
                li.find(".annotation-longview").stop().slideDown();
obledc
parents:
diff changeset
   302
            })
obledc
parents:
diff changeset
   303
            .mouseleave(function() {
obledc
parents:
diff changeset
   304
                $(spans).removeClass("annotation-selected");
obledc
parents:
diff changeset
   305
                li.removeClass("selected");
obledc
parents:
diff changeset
   306
                li.find(".annotation-longview").stop().slideUp();
obledc
parents:
diff changeset
   307
            })
obledc
parents:
diff changeset
   308
            .click(function() {
obledc
parents:
diff changeset
   309
                show();
obledc
parents:
diff changeset
   310
                shownByClick = true;
obledc
parents:
diff changeset
   311
                $(window).scrollTop(currentVisibleFrame.offset().top - 100);
obledc
parents:
diff changeset
   312
            });
obledc
parents:
diff changeset
   313
        
obledc
parents:
diff changeset
   314
        if (editAfterShow) {
obledc
parents:
diff changeset
   315
            show();
obledc
parents:
diff changeset
   316
            shownByClick = true;
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
    function updateAnnotationCounts() {
obledc
parents:
diff changeset
   322
        $(".annotation-blocks .block").each(function() {
obledc
parents:
diff changeset
   323
            var $this = $(this), n = $this.find("li").length;
obledc
parents:
diff changeset
   324
            $this.find(".annotations-count").text(n || "aucune");
obledc
parents:
diff changeset
   325
            if (n > 1) {
obledc
parents:
diff changeset
   326
                $this.find(".annotation-plural").show();
obledc
parents:
diff changeset
   327
            } else {
obledc
parents:
diff changeset
   328
                $this.find(".annotation-plural").hide();
obledc
parents:
diff changeset
   329
            }
obledc
parents:
diff changeset
   330
        })
obledc
parents:
diff changeset
   331
    }
obledc
parents:
diff changeset
   332
    
obledc
parents:
diff changeset
   333
    annotations.forEach(function(annotation) {
obledc
parents:
diff changeset
   334
        showAnnotation(annotation);
obledc
parents:
diff changeset
   335
    });
obledc
parents:
diff changeset
   336
    updateAnnotationCounts();
obledc
parents:
diff changeset
   337
    
obledc
parents:
diff changeset
   338
    var range = null;
obledc
parents:
diff changeset
   339
        
obledc
parents:
diff changeset
   340
    $(".content").mouseup(function(e) {
obledc
parents:
diff changeset
   341
        range = document.getSelection().getRangeAt(0);
obledc
parents:
diff changeset
   342
        var addann = $(".add-annotation");
obledc
parents:
diff changeset
   343
        if (!range.collapsed && range.startContainer._nodeInfo && range.endContainer._nodeInfo && range.toString() !== " ") {
obledc
parents:
diff changeset
   344
            addann.show();
obledc
parents:
diff changeset
   345
            var doc = $(document), rect = range.getBoundingClientRect();
obledc
parents:
diff changeset
   346
            addann.css({
obledc
parents:
diff changeset
   347
                left: doc.scrollLeft() + rect.right + 5,
obledc
parents:
diff changeset
   348
                top: doc.scrollTop() + (rect.top + rect.bottom - addann.outerHeight()) / 2,
obledc
parents:
diff changeset
   349
            });
obledc
parents:
diff changeset
   350
        } else {
obledc
parents:
diff changeset
   351
            range = null;
obledc
parents:
diff changeset
   352
            $(".add-annotation").hide();
obledc
parents:
diff changeset
   353
        }
obledc
parents:
diff changeset
   354
    }).mousedown(function() {
obledc
parents:
diff changeset
   355
        $(".add-annotation").hide();
obledc
parents:
diff changeset
   356
    });
obledc
parents:
diff changeset
   357
    
obledc
parents:
diff changeset
   358
    $(".add-annotation").click(function() {
obledc
parents:
diff changeset
   359
		
obledc
parents:
diff changeset
   360
        $(".add-annotation").hide();
obledc
parents:
diff changeset
   361
        if (range) {
obledc
parents:
diff changeset
   362
			
obledc
parents:
diff changeset
   363
            var start = range.startOffset + range.startContainer._nodeInfo.start,
obledc
parents:
diff changeset
   364
                end = range.endOffset + range.endContainer._nodeInfo.start;
obledc
parents:
diff changeset
   365
				
obledc
parents:
diff changeset
   366
            highlightText(start, end, colors[ncol++ % colors.length]);
obledc
parents:
diff changeset
   367
            document.getSelection().removeAllRanges();
obledc
parents:
diff changeset
   368
        }
obledc
parents:
diff changeset
   369
    });
obledc
parents:
diff changeset
   370
            
obledc
parents:
diff changeset
   371
    $(window).mouseup(function() {
obledc
parents:
diff changeset
   372
        mousedown = false;
obledc
parents:
diff changeset
   373
        dragging = false;
obledc
parents:
diff changeset
   374
    });
obledc
parents:
diff changeset
   375
    
obledc
parents:
diff changeset
   376
    $(".annotation-frame-box").click(hideAllFrames);
obledc
parents:
diff changeset
   377
    
obledc
parents:
diff changeset
   378
    $(window).resize(function() {
obledc
parents:
diff changeset
   379
        showFrameBox();
obledc
parents:
diff changeset
   380
        $(".annotation-frame").css({
obledc
parents:
diff changeset
   381
            left: basenode.offsetLeft
obledc
parents:
diff changeset
   382
        })
obledc
parents:
diff changeset
   383
    });
obledc
parents:
diff changeset
   384
    
obledc
parents:
diff changeset
   385
    $(".add-annotation").css({
obledc
parents:
diff changeset
   386
        left: (basenode.offsetLeft + 500)
obledc
parents:
diff changeset
   387
    });
obledc
parents:
diff changeset
   388
obledc
parents:
diff changeset
   389
});