forked from dgrijalva/gitx
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathPBWebChangesController.m
142 lines (116 loc) · 3.76 KB
/
PBWebChangesController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// PBWebChangesController.m
// GitX
//
// Created by Pieter de Bie on 22-09-08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "PBWebChangesController.h"
#import "PBGitIndexController.h"
#import "PBGitIndex.h"
@implementation PBWebChangesController
- (void) dealloc
{
selectedFile = Nil;
actHunk = Nil;
}
- (void) awakeFromNib
{
selectedFile = nil;
selectedFileIsCached = NO;
startFile = @"commit";
[super awakeFromNib];
[unstagedFilesController addObserver:self forKeyPath:@"selection" options:0 context:@"UnstagedFileSelected"];
[cachedFilesController addObserver:self forKeyPath:@"selection" options:0 context:@"cachedFileSelected"];
}
- (void)closeView
{
[[self script] removeWebScriptKey:@"Index"];
[unstagedFilesController removeObserver:self forKeyPath:@"selection"];
[cachedFilesController removeObserver:self forKeyPath:@"selection"];
[super closeView];
}
- (void) didLoad
{
[[self script] setValue:controller.index forKey:@"Index"];
[self refresh];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSArrayController *otherController;
otherController = object == unstagedFilesController ? cachedFilesController : unstagedFilesController;
int count = [[object selectedObjects] count];
if (count == 0) {
if([[otherController selectedObjects] count] == 0 && selectedFile) {
selectedFile = nil;
selectedFileIsCached = NO;
[self refresh];
}
return;
}
// TODO: Move this to commitcontroller
[otherController setSelectionIndexes:[NSIndexSet indexSet]];
if (count > 1) {
[self showMultiple: [object selectedObjects]];
return;
}
selectedFile = [[object selectedObjects] objectAtIndex:0];
selectedFileIsCached = object == cachedFilesController;
[self refresh];
}
- (void) showMultiple: (NSArray *)objects
{
[[self script] callWebScriptMethod:@"showMultipleFilesSelection" withArguments:[NSArray arrayWithObject:objects]];
}
- (void) refresh
{
if (!finishedLoading)
return;
id script = [view windowScriptObject];
[script callWebScriptMethod:@"showFileChanges"
withArguments:[NSArray arrayWithObjects:selectedFile ?: (id)[NSNull null],
[NSNumber numberWithBool:selectedFileIsCached], nil]];
}
- (void)stageHunk:(NSString *)hunk reverse:(BOOL)reverse
{
[controller.index applyPatch:hunk stage:YES reverse:reverse];
// FIXME: Don't need a hard refresh
[self refresh];
}
- (void) discardHunk:(NSString *)hunk
{
[controller.index applyPatch:hunk stage:NO reverse:YES];
[self refresh];
}
- (void) discardHunkAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
[[alert window] orderOut:nil];
if (returnCode == NSAlertDefaultReturn)
[self discardHunk:actHunk];
}
- (void)discardHunk:(NSString *)hunk altKey:(BOOL)altKey
{
if (!altKey) {
NSAlert *alert = [NSAlert alertWithMessageText:@"Discard hunk"
defaultButton:nil
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@"Are you sure you wish to discard the changes in this hunk?\n\nYou cannot undo this operation."];
[alert beginSheetModalForWindow:[[controller view] window]
modalDelegate:self
didEndSelector:@selector(discardHunkAlertDidEnd:returnCode:contextInfo:)
contextInfo:Nil];
actHunk = hunk;
} else {
[self discardHunk:hunk];
}
}
- (void) setStateMessage:(NSString *)state
{
id script = [view windowScriptObject];
[script callWebScriptMethod:@"setState" withArguments: [NSArray arrayWithObject:state]];
}
@end