2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "WOHTMLDynamicElement.h"
24 @interface WOJavaScript : WOHTMLDynamicElement
26 // WODynamicElement: extraAttributes
27 // WODynamicElement: otherTagString
29 WOAssociation *scriptFile;
30 WOAssociation *scriptString;
31 WOAssociation *scriptSource;
32 WOAssociation *hideInComment;
37 @end /* WOJavaScript */
39 #include "WOElement+private.h"
40 #include <NGObjWeb/WOResourceManager.h>
41 #include <NGObjWeb/WOApplication.h>
44 @implementation WOJavaScript
46 - (id)initWithName:(NSString *)_name
47 associations:(NSDictionary *)_config
48 template:(WOElement *)_tmp
50 if ((self = [super initWithName:_name associations:_config template:_tmp])) {
51 self->scriptFile = OWGetProperty(_config, @"scriptFile");
52 self->scriptString = OWGetProperty(_config, @"scriptString");
53 self->scriptSource = OWGetProperty(_config, @"scriptSource");
54 self->hideInComment = OWGetProperty(_config, @"hideInComment");
55 self->type = OWGetProperty(_config, @"type");
62 [self->scriptFile release];
63 [self->scriptString release];
64 [self->scriptSource release];
65 [self->hideInComment release];
69 /* response generation */
71 - (void)appendScriptFileToResponse:(WOResponse *)_response
72 inContext:(WOContext *)_ctx
76 if (self->scriptFile == nil)
78 if ((s = [self->scriptFile stringValueInComponent:[_ctx component]]) == nil)
83 if ([s isAbsolutePath]) {
84 s = [[NSString alloc] initWithContentsOfFile:s];
87 WOResourceManager *rm;
90 if ((rm = [[_ctx component] resourceManager]) == nil)
91 rm = [[_ctx application] resourceManager];
93 languages = [_ctx resourceLookupLanguages];
94 s = [rm pathForResourceNamed:s inFramework:nil languages:languages];
95 if (s) s = [[NSString alloc] initWithContentsOfFile:s];
98 /* append to response */
100 if (s) WOResponse_AddString(_response, s);
104 - (void)appendScriptContentToResponse:(WOResponse *)_response
105 inContext:(WOContext *)_ctx
107 if (self->scriptString) {
110 if ((s = [self->scriptString stringValueInComponent:[_ctx component]]))
111 WOResponse_AddString(_response, s);
114 if (self->scriptFile)
115 [self appendScriptFileToResponse:_response inContext:_ctx];
118 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
119 WOComponent *sComponent;
123 if ([[_ctx request] isFromClientComponent])
126 sComponent = [_ctx component];
127 hide = [self->hideInComment boolValueInComponent:sComponent];
129 WOResponse_AddCString(_response, "<script");
131 if ((st = [self->type stringValueInComponent:sComponent]) != nil) {
132 WOResponse_AddCString(_response, " type=\"");
133 [_response appendContentHTMLAttributeValue:st];
134 WOResponse_AddCString(_response, "\"");
137 WOResponse_AddCString(_response, " type=\"text/javascript\"");
140 /* add URL to script */
141 if (self->scriptSource != nil) {
142 st = [self->scriptSource stringValueInComponent:sComponent];
143 WOResponse_AddCString(_response, " src=\"");
144 [_response appendContentHTMLAttributeValue:st];
145 WOResponse_AddCString(_response, "\"");
148 [self appendExtraAttributesToResponse:_response inContext:_ctx];
149 if (self->otherTagString != nil) {
150 WOResponse_AddChar(_response, ' ');
151 WOResponse_AddString(_response,
152 [self->otherTagString stringValueInComponent:
155 WOResponse_AddChar(_response, '>');
157 WOResponse_AddCString(_response, "<!-- hide from older browsers\n");
159 [self appendScriptContentToResponse:_response inContext:_ctx];
162 WOResponse_AddCString(_response, "// hide from older browsers -->");
163 WOResponse_AddCString(_response, "</script>");
168 - (NSString *)associationDescription {
169 NSMutableString *str = [[NSMutableString alloc] init];
171 str = [NSMutableString stringWithCapacity:64];
172 if (self->scriptFile) [str appendFormat:@" file=%@", self->scriptFile];
173 if (self->scriptString) [str appendFormat:@" string=%@", self->scriptString];
174 if (self->scriptSource) [str appendFormat:@" source=%@", self->scriptSource];
175 if (self->hideInComment)
176 [str appendFormat:@" hide=%@", self->hideInComment];
181 @end /* WOJavaScript */