-
Notifications
You must be signed in to change notification settings - Fork 0
/
BCDailyDataWebView.m
259 lines (188 loc) · 8.95 KB
/
BCDailyDataWebView.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// BCDailyDataWebView.m
// Bartender
//
// Created by Tom Houpt on 12/7/16.
// Copyright 2012 Behavioral Cybernetics. All rights reserved.
//
#import "BCDailyDataWebView.h"
#import "BarExperiment.h"
#import "BCTableHtmlFormatter.h"
#import "DailyDataMeansHtmlFormatter.h"
@implementation BCDailyDataWebView
-(id)initWithDailyData:(DailyData *)data andTable:(NSTableView *)table; {
NSRect frameRect = NSMakeRect(0,0,10,10);
// set initial frame very small, so that web view has to increase document size
// when rendering html
// we get new size from frame load delegate call
self = [super initWithFrame:frameRect frameName:nil groupName:nil];
if (self) {
dailyData = data;
tableView = table;
htmlString = [[NSMutableString alloc] init];
// set up the webpreferences to print backgrounds
[self setPreferencesIdentifier:@"DailyDataWebPreferences"];
WebPreferences *prefs = [self preferences];
[prefs setShouldPrintBackgrounds:YES];
// turn off the scroll bars
[[[self mainFrame] frameView] setAllowsScrolling:NO];
// make us our own load delegate
[self setFrameLoadDelegate:self];
// construct and load the html
// [htmlString appendString:@"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"];
[self appendCssToString:htmlString];
// append daily data expt info
[self appendDailyDataInfoToString:htmlString];
// append the daily data table
BCTableHtmlFormatter *htmlFormatter = [[BCTableHtmlFormatter alloc] initWithTableView:tableView andTableID:@"weightTable"];
[htmlFormatter appendHtmlToString:htmlString];
// append the comment (if any)
if (nil != [data comment]) {
if ([[data comment] length] > 0) {
[htmlString appendString:@"<BR>\r"];
[htmlString appendString:@"<DIV class = \"dailydata\" >\r"];
[htmlString appendString:[data comment]];
[htmlString appendString:@"\r</DIV>\r"];
}
}
// append the group means table
if (kWeighingOff == [data currentState] || kUserEditing == [data currentState]) {
DailyDataMeansHtmlFormatter *meanFormatter = [[DailyDataMeansHtmlFormatter alloc] initWithDailyData:data andTableID:@"meanTable"];
[meanFormatter appendHtmlToString:htmlString];
}
[[self mainFrame] loadHTMLString:htmlString baseURL:nil];
// NSString *testFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0] stringByAppendingPathComponent:@"dailyDataWebViewTest.html"];
// [self writeHtmlToPath:testFilePath];
// NSView *frameView = [[self mainFrame] frameView];
// NSView *docView = [frameView documentView];
// NSRect webViewRect = [self frame];
// NSRect frameViewRect = [frameView frame];
// NSRect docViewRect = [docView frame];
//
// NSLog(@"before load? page: %@",NSStringFromRect(docViewRect));
}
return self;
}
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)webFrame; {
if ([webFrame isEqual:[self mainFrame]]) {
// NSView *frameView = [[self mainFrame] frameView];
// NSView *docView = [frameView documentView];
// NSRect webViewRect = [self frame];
// NSRect frameViewRect = [frameView frame];
// NSRect docViewRect = [docView frame];
//
NSRect docViewRect = [[[[self mainFrame] frameView] documentView] frame];
NSLog(@"after loading page: %@",NSStringFromRect(docViewRect));
// NOTE: we should make sure the frame is a minimum width, like the width of the paper page,
if (docViewRect.size.width < 526) { docViewRect.origin.x = 0; docViewRect.size.width = 526; }
docViewRect.origin.y = 0;
[self setFrame:docViewRect];
}
// if (nil != printOp){
//
// [printOp setShowsPrintPanel:showPrintPanels];
//
// if (showPrintPanels) {
// // Add accessory view, if needed
// }
//
// // Run operation, which shows the Print panel if showPanels was YES
//
// [printSender runModalPrintOperation:printOp
// delegate:printSender
// didRunSelector:@selector(documentDidRunModalPrintOperation:success:contextInfo:)
// contextInfo:NULL];
//
// // zero out the print op and the sender
//// printOp = nil;
//// printSender = nil;
// }
}
//-(void)print:(BOOL)showPanels fromDocument:(NSDocument *)sender; {
// //
// // If you have
// //
// // WebView *myView;
// //
// // Don't print myView, but print [[[myView mainFrame] frameView] documentView].
// //
// // Consider WebView and frameView like NSScrollView. You don't want to
// // print the scroll view but its content.
// //
//
//
// showPrintPanels = showPanels;
// printSender = sender;
//
// NSView *printView = [[[self mainFrame] frameView] documentView];
//
// // Construct the print operation and setup Print panel
// printOp = [NSPrintOperation
// printOperationWithView:printView
// printInfo:[printSender printInfo]];
//
//
// // move rest of printing operation into the BCDailyDataWebView View:didFinishLoadForFrame: method,
// // so that printing is completed only after the webview finishes loading the html
// // (apparently has time to load in debug mode, but in release mode the printing starts
// // without the html loading completely, so we get a blank document...
//
//
//}
//<HEAD>
//<TITLE>How to Carve Wood</TITLE>
//<STYLE type="text/css">
//BODY {text-align: center}
//</STYLE>
//<BODY>
//...the body is centered...
//</BODY>
-(void)appendCssToString:(NSMutableString *)buffer; { }
-(void) appendDailyDataInfoToString:(NSMutableString *)buffer; {
// expt_code: expt_name
[buffer appendFormat:@"<P><STRONG><BIG>%@</BIG></STRONG></P>\r",[[dailyData theExperiment] codeName]];
[buffer appendString:@"<Style> \r"];
[buffer appendFormat: @".dailydata td {font-family: Helvetica, sans-serif; font-size:9pt;} \r"];
[buffer appendString:@"</Style> \r"];
[buffer appendString:@"<TABLE class = \"dailydata\" >\r"];
// Days completed: expt_days
[buffer appendFormat:@"\t<TR><TD>Experiment Day %ld </TD></TR>\r", [dailyData exptDayIndex] + 1];
// increment by 1 to make 1-indexed
// this is the actual day of the current dailyData, e.g. it's position in the dailyData Array
// if for weighing on (i.e. weighing off is still pending), the exptDayIndex is the index of the next day to be added
// Phase: phase name; Day phase_day
NSString *phaseLabel;
if ([[dailyData phaseName] isEqualToString:@"<none>"]) {phaseLabel = kNoDataCellText; }
else { phaseLabel = [NSString stringWithFormat: @"Phase: %@, Day %ld", [dailyData phaseName], [dailyData phaseDayIndex]+1]; }
[buffer appendFormat:@"\t<TR><TD>%@ </TD></TR>\r", phaseLabel];
// weighed on:
[buffer appendFormat:@"\t<TR><TD>Weighed on: %@</TD></TR>\r", [dailyData onTimeString]];
// weighed off:
[buffer appendFormat:@"\t<TR><TD>Weighed off: %@</TD></TR>\r", [dailyData offTimeString]];
[buffer appendString:@"</TABLE>\r"];
[buffer appendString:@"<BR>\r"];
}
-(void)writeHtmlToPath:(NSString *)path; {
NSError *error;
if (nil == htmlString || nil == path) return;
[htmlString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (nil != error) {
NSLog(@"BCDailyDataWebView writeHtmlToPath Error Desc: %@",[error localizedDescription]);
NSLog(@"BCDailyDataWebView writeHtmlToPath Error Sugg: %@",[error localizedRecoverySuggestion]);
}
}
// from http://cocoadev.com/wiki/NSStringCategory
// Replaces all XML reserved chars with entities; replaces \n or \r with <BR>
// for all entities see http://www.w3.org/TR/html4/sgml/entities.html
- (NSString *)stringSafeForXML:(NSString *)aString; {
NSMutableString *str = [NSMutableString stringWithString:aString];
[str replaceOccurrencesOfString:@"&" withString:@"&" options:NSLiteralSearch range:NSMakeRange (0, [str length])];
[str replaceOccurrencesOfString:@"<" withString:@"<" options:NSLiteralSearch range:NSMakeRange (0, [str length])];
[str replaceOccurrencesOfString:@">" withString:@">" options:NSLiteralSearch range:NSMakeRange (0, [str length])];
[str replaceOccurrencesOfString:@"\"" withString:@""" options:NSLiteralSearch range:NSMakeRange (0, [str length])];
[str replaceOccurrencesOfString:@"'" withString:@"'" options:NSLiteralSearch range:NSMakeRange (0, [str length])];
[str replaceOccurrencesOfString:@"\n" withString:@"<BR>\r" options:NSLiteralSearch range:NSMakeRange (0, [str length])];
[str replaceOccurrencesOfString:@"\r" withString:@"<BR>\r" options:NSLiteralSearch range:NSMakeRange (0, [str length])];
return str;
}
@end