// // MyDocument.m // JavaScriptBindings // // Created by James G. Speth on 10/29/06. // Copyright Jimco 2006 . All rights reserved. // #import "MyDocument.h" #import "JSKVC.h" @implementation MyDocument - (id)init { self = [super init]; if (self) { } return self; } - (NSString *)windowNibName { return @"MyDocument"; } - (NSData *)dataRepresentationOfType:(NSString *)aType { return nil; } - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType { return YES; } - (void)windowControllerDidLoadNib:(NSWindowController *) aController { [super windowControllerDidLoadNib:aController]; // setup NSOutlineView double-click action [outlineView setTarget:self]; [outlineView setDoubleAction:@selector(editNode:)]; // load the starting page from a resource file NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"start" ofType:@"html"]]; [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:url]]; } - (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]; } // --- WebFrameLoadDelegate methods --- - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)windowScriptObject { // as soon as the window object is available, inject the code for the boxInfo object NSString *stringForBoxInfo = [self stringForResource:@"boxinfo" ofType:@"js"]; [windowScriptObject evaluateWebScript:stringForBoxInfo]; // add the key-value notification object to the script environment [windowScriptObject setValue:[[JSKVC alloc] init] forKey:@"JSKVC"]; } - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame { // modify the DOM document each time a new page loads [[webView windowScriptObject] evaluateWebScript:@"installBoxInfo();"]; } // --- actions --- - (IBAction)editNode:(id)sender; { // get the node that the user double-clicked on id obj = [[treeController selectedObjects] lastObject]; if ([[obj valueForKey:@"nodeName"] isEqualToString:@"INPUT"]) { // tell the boxInfo object to use the selected box node WebScriptObject *boxInfo = [[webView windowScriptObject] evaluateWebScript:@"document.boxInfo"]; [boxInfo callWebScriptMethod:@"setBox" withArguments:[NSArray arrayWithObject:obj]]; } } - (IBAction)openLocation:(id)sender { // handle menu command by moving focus to the location field [locationField selectText:sender]; } @end