2 // PBWebGitController.m
5 // Created by Pieter de Bie on 14-06-08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
9 #import "PBWebHistoryController.h"
11 @implementation PBWebHistoryController
17 startFile = @"history";
18 repository = historyController.repository;
20 [historyController addObserver:self forKeyPath:@"webCommit" options:0 context:@"ChangedCommit"];
26 [self changeContentTo: historyController.webCommit];
29 - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
31 if ([(NSString *)context isEqualToString: @"ChangedCommit"])
32 [self changeContentTo: historyController.webCommit];
34 [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
37 - (void) changeContentTo: (PBGitCommit *) content
39 if (content == nil || !finishedLoading)
42 // The sha is the same, but refs may have changed.. reload it lazy
43 if ([currentSha isEqualToString: [content realSha]])
45 [[self script] callWebScriptMethod:@"reload" withArguments: nil];
48 currentSha = [content realSha];
50 NSArray *arguments = [NSArray arrayWithObjects:content, [[[historyController repository] headRef] simpleRef], nil];
51 [[self script] callWebScriptMethod:@"loadCommit" withArguments: arguments];
54 - (void) selectCommit: (NSString*) sha
56 [historyController selectCommit:sha];
59 - (void) sendKey: (NSString*) key
61 id script = [view windowScriptObject];
62 [script callWebScriptMethod:@"handleKeyFromCocoa" withArguments: [NSArray arrayWithObject:key]];
67 NSString *source = [(DOMHTMLElement *)[[[view mainFrame] DOMDocument] documentElement] outerHTML];
68 NSPasteboard *a =[NSPasteboard generalPasteboard];
69 [a declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
70 [a setString:source forType: NSStringPboardType];
73 - (NSArray *) webView:(WebView *)sender
74 contextMenuItemsForElement:(NSDictionary *)element
75 defaultMenuItems:(NSArray *)defaultMenuItems
77 DOMNode *node = [element valueForKey:@"WebElementDOMNode"];
80 // Every ref has a class name of 'refs' and some other class. We check on that to see if we pressed on a ref.
81 if ([[node className] hasPrefix:@"refs "]) {
82 NSString *selectedRefString = [[[node childNodes] item:0] textContent];
83 for (PBGitRef *ref in historyController.webCommit.refs)
85 if ([[ref shortName] isEqualToString:selectedRefString])
86 return [contextMenuDelegate menuItemsForRef:ref commit:historyController.webCommit];
88 NSLog(@"Could not find selected ref!");
89 return defaultMenuItems;
91 if ([node hasAttributes] && [[node attributes] getNamedItem:@"representedFile"])
92 return [historyController menuItemsForPaths:[NSArray arrayWithObject:[[[node attributes] getNamedItem:@"representedFile"] value]]];
94 node = [node parentNode];
97 return defaultMenuItems;
101 // Open external links in the default browser
102 - (void)webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)actionInformation
103 request:(NSURLRequest *)request
104 newFrameName:(NSString *)frameName
105 decisionListener:(id < WebPolicyDecisionListener >)listener
107 [[NSWorkspace sharedWorkspace] openURL:[request URL]];
110 - getConfig:(NSString *)config
112 return [historyController valueForKeyPath:[@"repository.config." stringByAppendingString:config]];
117 [historyController removeObserver:self forKeyPath:@"webCommit"];
121 - (void) preferencesChanged
123 [[self script] callWebScriptMethod:@"enableFeatures" withArguments:nil];