Allow for empty catalogue.Book.project value in forms
[redakcja.git] / redakcja / static / js / wiki / view_annotations.js
index 071e7ee..76a0ae9 100644 (file)
 
     AnnotationsPerspective.prototype = new $.wiki.Perspective();
 
+    AnnotationsPerspective.prototype.updateAnnotationIds = function(self){
+       self.annotationToAnchor = {};
+       $('#html-view .annotation-inline-box').each(
+           function(i, annoBox) {
+               var $annoBox = $(annoBox);
+               var $anchor = $("a[name|=anchor]", $annoBox);
+               var htmlContent = $('span', $annoBox).html();
+               // TBD: perhaps use a hash of htmlContent as key
+               self.annotationToAnchor[htmlContent] = $anchor.attr('name');
+               });
+    }
+
+    AnnotationsPerspective.prototype.goToAnnotation = function(self, srcNode){
+       var content = $(srcNode).html();
+       content = content.replace('&gt;', '>', 'g').replace('&lt;', '<', 'g').replace('&amp;', '&', 'g');
+       xml2html({
+           xml: '<root>'+content+'</root>',
+           success: function(txt) {
+               content = $(txt).html();
+               },
+            error: function(text) {
+                $.unblockUI();
+                self.$error.html(text);
+                self.$spinner.hide();
+                self.$error.show();
+            }
+       });
+
+       var anchor = self.annotationToAnchor[content];
+       if (anchor != undefined) {
+           var $htmlView = $("#html-view");
+           var top = $htmlView.offset().top + 
+               $("[name=" + anchor + "]", $htmlView).offset().top - 
+               $htmlView.children().eq(0).offset().top
+
+           $htmlView.animate({scrollTop: top}, 250);
+       }
+    }
+
     AnnotationsPerspective.prototype.refresh = function(self, atype) {
         var xml;
 
@@ -59,6 +98,7 @@
                     self.$error.show();
                 }
             });
+           self.updateAnnotationIds(self);
         }
         else {
             xml = this.doc.text;
                             elem.sortby = $(elem).text().trim();
                             $(elem).append("<div class='src'>"+ xml_text.replace("&", "&amp;", "g").replace("<", "&lt;", "g") +"</div>")
                             anno_list.push(elem);
+                           $(".src", elem).click(function() { self.goToAnnotation(self, this); });
                             counter--;
 
                             if (!counter) {
                                 anno_list.sort(function(a, b){return a.sortby.localeCompare(b.sortby);});
-                                self.$annos.append(anno_list);
+                                for (i in anno_list)
+                                    self.$annos.append(anno_list[i]);
                                 self.$spinner.hide();
                                 self.$annos.show();
                             }