Revision: 21947
          http://sourceforge.net/p/bibdesk/svn/21947
Author:   hofman
Date:     2018-02-17 18:49:11 +0000 (Sat, 17 Feb 2018)
Log Message:
-----------
Register parsers and publications for subframe of webview, remove them when 
reloading just the frame.

Modified Paths:
--------------
    trunk/bibdesk/BDSKWebGroup.h
    trunk/bibdesk/BDSKWebGroup.m

Modified: trunk/bibdesk/BDSKWebGroup.h
===================================================================
--- trunk/bibdesk/BDSKWebGroup.h        2018-02-17 17:27:50 UTC (rev 21946)
+++ trunk/bibdesk/BDSKWebGroup.h        2018-02-17 18:49:11 UTC (rev 21947)
@@ -45,6 +45,8 @@
     NSString *label;
     BDSKWebView *webView;
     NSMutableArray *parsers;
+    NSMapTable *publicationsForFrame;
+    NSMapTable *parserForFrame;
 }
 
 - (NSString *)label;

Modified: trunk/bibdesk/BDSKWebGroup.m
===================================================================
--- trunk/bibdesk/BDSKWebGroup.m        2018-02-17 17:27:50 UTC (rev 21946)
+++ trunk/bibdesk/BDSKWebGroup.m        2018-02-17 18:49:11 UTC (rev 21947)
@@ -56,6 +56,13 @@
 @interface BDSKWebGroup () <BDSKWebParserDelegate>
 @end
 
+@interface BDSKWebGroup (BDSKPrivate)
+- (void)removePublications:(NSArray *)oldPublications;
+- (void)addPublications:(NSArray *)items forFrame:(WebFrame *)frame;
+- (void)setParser:(BDSKWebParser *)parser forFrame:(WebFrame *)frame;
+- (WebFrame *)frameForParser:(BDSKWebParser *)frame;
+@end
+
 @implementation BDSKWebGroup
 
 static NSString *BDSKWebLocalizedString = nil;
@@ -189,9 +196,23 @@
     if (frame == [sender mainFrame]) {
         [parsers makeObjectsPerformSelector:@selector(cancel)];
         [parsers removeAllObjects];
+        [parserForFrame removeAllObjects];
+        [publicationsForFrame removeAllObjects];
         [self setPublications:nil];
     } else {
-        [self addPublications:nil];
+        BDSKWebParser *parser = [parserForFrame objectForKey:frame];
+        if (parser) {
+            [parser cancel];
+            [parsers removeObject:parser];
+            [parserForFrame removeObjectForKey:frame];
+        }
+        NSArray *pubs = [publicationsForFrame objectForKey:frame];
+        if (pubs) {
+            [self removePublications:pubs];
+            [publicationsForFrame removeObjectForKey:frame];
+        } else {
+            [self addPublications:nil];
+        }
     }
 }
 
@@ -201,12 +222,16 @@
     
     NSError *error = nil;
     BDSKWebParser *parser = [BDSKWebParser parserForDocument:domDocument 
fromURL:url error:&error];
-
+    
     if (parser) {
         
         if (parsers == nil)
             parsers = [[NSMutableArray alloc] init];
         [parsers addObject:parser];
+        
+        if (frame != [sender mainFrame])
+            [self setParser:parser forFrame:frame];
+        
         // the parser can finish immediately
         [parser startWithDelegate:self];
         
@@ -246,6 +271,8 @@
         }
         
         [self addPublications:newPubs ?: [NSArray array]];
+        if (frame != [sender mainFrame] && [newPubs count] > 0)
+            [self addPublications:newPubs forFrame:frame];
     }
 }
 
@@ -257,13 +284,64 @@
 
 - (void)webParser:(BDSKWebParser *)parser didFindItems:(NSArray *)items  {
     [self addPublications:items];
+    
+    if ([items count] && parserForFrame) {
+        WebFrame *frame = [self frameForParser:parser];
+        if (frame)
+            [self addPublications:items forFrame:frame];
+    }
 }
 
 - (void)webParser:(BDSKWebParser *)parser didFinishWithSuccess:(BOOL)success 
error:(NSError *)error {
     if (success == NO)
         NSLog(@"-[%@ %@] %@", [self class], NSStringFromSelector(_cmd), error);
+    WebFrame *frame = [self frameForParser:parser];
+    if (frame)
+        [parserForFrame removeObjectForKey:frame];
     [parsers removeObject:parser];
+    
     [self addPublications:[NSArray array]];
 }
 
 @end
+
+#pragma mark -
+
+@implementation BDSKWebGroup (BDSKPrivate)
+
+- (void)removePublications:(NSArray *)oldPublications {
+    if ([oldPublications count]) {
+        NSIndexSet *indexes = [publications 
indexesOfObjectsIdenticalTo:oldPublications];
+        [publications removeObjectsAtIndexes:indexes];
+        [searchIndexes removePublications:oldPublications];
+        
+        [self setCount:[publications count]];
+        
+        [self notifyUpdateForSuccess:YES];
+    }
+}
+
+- (void)addPublications:(NSArray *)newPubs forFrame:(WebFrame *)frame {
+    NSMutableArray *pubs = [publicationsForFrame objectForKey:frame];
+    if (pubs == nil) {
+        pubs = [NSMutableArray array];
+        publicationsForFrame = [[NSMapTable alloc] 
initWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableStrongMemory 
capacity:0];
+        [publicationsForFrame setObject:pubs forKey:frame];
+    }
+    [pubs addObjectsFromArray:newPubs];
+}
+
+- (void)setParser:(BDSKWebParser *)parser forFrame:(WebFrame *)frame {
+    if (parserForFrame == nil)
+        parserForFrame = [[NSMapTable alloc] 
initWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableStrongMemory 
capacity:0];
+    [parserForFrame setObject:parser forKey:frame];
+}
+
+- (WebFrame *)frameForParser:(BDSKWebParser *)parser {
+    for (WebFrame *frame in parserForFrame)
+        if ([parserForFrame objectForKey:frame] == parser)
+            return frame;
+    return nil;
+}
+
+@end

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to