]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOJavaScript.m
improved query string handling
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOJavaScript.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "WOHTMLDynamicElement.h"
23
24 @interface WOJavaScript : WOHTMLDynamicElement
25 {
26   // WODynamicElement: extraAttributes
27   // WODynamicElement: otherTagString
28 @protected
29   WOAssociation *scriptFile;
30   WOAssociation *scriptString;
31   WOAssociation *scriptSource;
32   WOAssociation *hideInComment;
33   
34   WOAssociation *type;
35 }
36
37 @end /* WOJavaScript */
38
39 #include "WOElement+private.h"
40 #include <NGObjWeb/WOResourceManager.h>
41 #include <NGObjWeb/WOApplication.h>
42 #include "decommon.h"
43
44 @implementation WOJavaScript
45
46 - (id)initWithName:(NSString *)_name
47   associations:(NSDictionary *)_config
48   template:(WOElement *)_tmp
49 {
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");
56   }
57   return self;
58 }
59
60 - (void)dealloc {
61   [self->type          release];
62   [self->scriptFile    release];
63   [self->scriptString  release];
64   [self->scriptSource  release];
65   [self->hideInComment release];
66   [super dealloc];
67 }
68
69 /* response generation */
70
71 - (void)appendScriptFileToResponse:(WOResponse *)_response
72   inContext:(WOContext *)_ctx 
73 {
74   NSString *s;
75
76   if (self->scriptFile == nil)
77     return;
78   if ((s = [self->scriptFile stringValueInComponent:[_ctx component]]) == nil)
79     return;
80
81   /* load file */
82
83   if ([s isAbsolutePath]) {
84     s = [[NSString alloc] initWithContentsOfFile:s];
85   }
86   else {
87     WOResourceManager *rm;
88     NSArray           *languages;
89           
90     if ((rm = [[_ctx component] resourceManager]) == nil)
91       rm = [[_ctx application] resourceManager];
92     
93     languages = [_ctx resourceLookupLanguages];
94     s         = [rm pathForResourceNamed:s inFramework:nil languages:languages];
95     if (s)  s = [[NSString alloc] initWithContentsOfFile:s];
96   }
97
98   /* append to response */
99   
100   if (s) WOResponse_AddString(_response, s);
101   [s release];
102 }
103
104 - (void)appendScriptContentToResponse:(WOResponse *)_response
105   inContext:(WOContext *)_ctx 
106 {
107   if (self->scriptString) {
108     NSString *s;
109
110     if ((s = [self->scriptString stringValueInComponent:[_ctx component]]))
111       WOResponse_AddString(_response, s);
112   }
113   
114   if (self->scriptFile)
115     [self appendScriptFileToResponse:_response inContext:_ctx];
116 }
117
118 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
119   WOComponent *sComponent;
120   NSString *st;
121   BOOL hide;
122   
123   if ([[_ctx request] isFromClientComponent])
124     return;
125   
126   sComponent = [_ctx component];
127   hide = [self->hideInComment boolValueInComponent:sComponent];
128   
129   WOResponse_AddCString(_response, "<script");
130   
131   if ((st = [self->type stringValueInComponent:sComponent]) != nil) {
132     WOResponse_AddCString(_response, " type=\"");
133     [_response appendContentHTMLAttributeValue:st];
134     WOResponse_AddCString(_response, "\"");
135   }
136   else {
137     WOResponse_AddCString(_response, " type=\"text/javascript\"");
138   }
139   
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, "\"");
146   }
147   
148   [self appendExtraAttributesToResponse:_response inContext:_ctx];
149   if (self->otherTagString != nil) {
150     WOResponse_AddChar(_response, ' ');
151     WOResponse_AddString(_response,
152                          [self->otherTagString stringValueInComponent:
153                            sComponent]);
154   }
155   WOResponse_AddChar(_response, '>');
156   if (hide) 
157     WOResponse_AddCString(_response, "<!-- hide from older browsers\n");
158   
159   [self appendScriptContentToResponse:_response inContext:_ctx];
160   
161   if (hide) 
162     WOResponse_AddCString(_response, "// hide from older browsers -->");
163   WOResponse_AddCString(_response, "</script>");
164 }
165
166 /* description */
167
168 - (NSString *)associationDescription {
169   NSMutableString *str = [[NSMutableString alloc] init];
170   
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];
177   
178   return str;
179 }
180
181 @end /* WOJavaScript */