GitIndex: Add a few notifications
[GitX.git] / PBWebHistoryController.m
blob35979a4537d74ebae82d699440579d20e9bcaef3
1 //
2 //  PBWebGitController.m
3 //  GitTest
4 //
5 //  Created by Pieter de Bie on 14-06-08.
6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
9 #import "PBWebHistoryController.h"
11 @implementation PBWebHistoryController
13 @synthesize diff;
15 - (void) awakeFromNib
17         startFile = @"history";
18         repository = historyController.repository;
19         [super awakeFromNib];
20         [historyController addObserver:self forKeyPath:@"webCommit" options:0 context:@"ChangedCommit"];
23 - (void) didLoad
25         currentSha = @"";
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];
33         else
34                 [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
37 - (void) changeContentTo: (PBGitCommit *) content
39         if (content == nil || !finishedLoading)
40                 return;
42         // The sha is the same, but refs may have changed.. reload it lazy
43         if ([currentSha isEqualToString: [content realSha]])
44         {
45                 [[self script] callWebScriptMethod:@"reload" withArguments: nil];
46                 return;
47         }
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]];
65 - (void) copySource
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"];
79         while (node) {
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)
84                         {
85                                 if ([[ref shortName] isEqualToString:selectedRefString])
86                                         return [contextMenuDelegate menuItemsForRef:ref commit:historyController.webCommit];
87                         }
88                         NSLog(@"Could not find selected ref!");
89                         return defaultMenuItems;
90                 }
91                 if ([node hasAttributes] && [[node attributes] getNamedItem:@"representedFile"])
92                         return [historyController menuItemsForPaths:[NSArray arrayWithObject:[[[node attributes] getNamedItem:@"representedFile"] value]]];
94                 node = [node parentNode];
95         }
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]];
115 - (void) finalize
117         [historyController removeObserver:self forKeyPath:@"webCommit"];
118         [super finalize];
121 - (void) preferencesChanged
123         [[self script] callWebScriptMethod:@"enableFeatures" withArguments:nil];
126 @end