// // JSRunWebviewDelegate.m // JSRun // // Created by Jonathan Saggau on 3/3/07. // Copyright 2007 Jonathan Saggau. All rights reserved. // #import "JSRunWebviewDelegate.h" #import "JSKVC.h" #import "JSCommonMacros.h" @interface JSRunWebviewDelegate (PrivateAPI) -(void) _manualBindings; -(void) _unbindManualBindings; @end @implementation JSRunWebviewDelegate - (void) awakeFromNib { NSAssert((nil != webView), @"Nib needs some connectin' of the webview delegate"); [webView setFrameLoadDelegate:self]; NSBundle *bundle = [NSBundle mainBundle]; NSString *htmlPath; NSString *htmlStr = [NSString stringWithString:@"Unable to load url"]; if (htmlPath = [bundle pathForResource:@"start" ofType:@"html"]) { NSURL *url = [NSURL fileURLWithPath:htmlPath]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [[webView mainFrame] loadRequest:request]; } else { [[webView mainFrame] loadHTMLString:htmlStr baseURL:nil]; } } -(void) _manualBindings { LogMethod(); NSNumber *nope = [NSNumber numberWithBool:NO]; NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionary]; [bindingOptions setValue:nope forKey:NSRaisesForNotApplicableKeysBindingOption]; [[boxInfoController selection] bind:@"content" toObject:runsController withKeyPath:@"selection.distance" options:bindingOptions]; // [[runsController selection] bind:@"distance" // toObject:boxInfoController // withKeyPath:@"selection.content" // options:bindingOptions]; } -(void) _unbindManualBindings { } - (NSString *)stringForResource:(NSString *)name ofType:(NSString *)extension { // return contents of resource file as string NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension]; return [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; } #pragma mark --- WebFrameLoadDelegate methods --- - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)windowScriptObject { // as soon as the window object is available, inject the code for the imageInfo object NSString *script = [self stringForResource:@"boxinfo" ofType:@"js"]; [windowScriptObject evaluateWebScript:script]; // add the key-value notification object to the script environment [windowScriptObject setValue:[[JSKVC alloc] init] forKey:@"JSKVC"]; } - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame { LogMethod(); // modify the DOM document each time a new page loads [[webView windowScriptObject] evaluateWebScript:@"installBoxInfo();"]; [self performSelector:(SEL)@selector(_manualBindings) withObject:(id)nil afterDelay:(NSTimeInterval).02]; } @end