]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Less redrawing of bits in the link thumbnail preview
authorBrion Vibber <brion@pobox.com>
Tue, 16 Nov 2010 23:20:37 +0000 (15:20 -0800)
committerBrion Vibber <brion@pobox.com>
Tue, 16 Nov 2010 23:20:37 +0000 (15:20 -0800)
plugins/LinkPreview/linkpreview.js

index 37b0241a5ddd61b18668fcd613aac6d16367e022..5dca3a8071d84f1434f3c073da1d4a9eee5c8612 100644 (file)
          */
         previewLinks: function(text)
         {
+            var old = LinkPreview.links;
             var links = LinkPreview.findLinks(text);
-            $('#link-preview').html('');
-            for (var i = 0; i < links.length; i++) {
-                var id = 'link-preview-' + i;
-                $('#link-preview').append('<span id="' + id + '"></span>');
-                LinkPreview.prepLinkPreview(id, links[i]);
+
+            // Check for existing common elements...
+            for (var i = 0; i < old.length && i < links.length; i++) {
+                if (links[i] != old[i]) {
+                    // Change an existing entry!
+                    var id = 'link-preview-' + i;
+                    $('#' + id).html('');
+                    LinkPreview.prepLinkPreview(id, links[i]);
+                }
+            }
+            if (links.length > old.length) {
+                // Adding new entries, whee!
+                for (var i = old.length; i < links.length; i++) {
+                    var id = 'link-preview-' + i;
+                    $('#link-preview').append('<span id="' + id + '"></span>');
+                    LinkPreview.prepLinkPreview(id, links[i]);
+                }
+            } else if (old.length > links.length) {
+                // Remove preview entries for links that have been removed.
+                for (var i = links.length; i < old.length; i++) {
+                    var id = 'link-preview-' + i;
+                    $('#' + id).remove();
+                }
             }
+
+            LinkPreview.links = links;
         }
     };