]> err.no Git - sope/blob - sope-appserver/WOExtensions/JSModalWindow.m
PCH support
[sope] / sope-appserver / WOExtensions / JSModalWindow.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 <NGObjWeb/WODynamicElement.h>
23
24 @interface JSModalWindow : WODynamicElement
25 {
26   WOAssociation *action;
27   WOAssociation *pageName;
28   WOAssociation *href;      /* non - WO API */
29   
30   WOAssociation *height;
31   WOAssociation *width;
32   WOAssociation *windowName;
33   WOAssociation *isResizable;
34   WOAssociation *showLocation;
35   WOAssociation *showMenuBar;
36   WOAssociation *showScrollbars;
37   WOAssociation *showStatus;
38   WOAssociation *showToolbar;
39
40   /* non - WO API */
41   WOAssociation *top;
42   WOAssociation *left;
43   WOAssociation *isCenter;
44   WOAssociation *filename;
45   WOAssociation *string;
46   WOAssociation *framework;
47   
48   WOElement     *template;
49 }
50
51 @end
52
53 #include "common.h"
54
55 @implementation JSModalWindow
56
57 + (int)version {
58   return [super version] + 0 /* v2 */;
59 }
60 + (void)initialize {
61   NSAssert2([super version] == 2,
62             @"invalid superclass (%@) version %i !",
63             NSStringFromClass([self superclass]), [super version]);
64 }
65
66 - (id)initWithName:(NSString *)_name
67   associations:(NSDictionary *)_config
68   template:(WOElement *)_subs
69 {
70   if ((self = [super initWithName:_name associations:_config template:_subs])){
71     int funcCount;
72
73     self->action             = WOExtGetProperty(_config, @"action");
74     self->pageName           = WOExtGetProperty(_config, @"pageName");
75     self->href               = WOExtGetProperty(_config, @"href");
76     
77     self->height             = WOExtGetProperty(_config, @"height");
78     self->width              = WOExtGetProperty(_config, @"width");
79     self->windowName         = WOExtGetProperty(_config, @"windowName");
80     self->isResizable        = WOExtGetProperty(_config, @"isResizable");
81     self->showLocation       = WOExtGetProperty(_config, @"showLocation");
82     self->showMenuBar        = WOExtGetProperty(_config, @"showMenuBar");
83     self->showScrollbars     = WOExtGetProperty(_config, @"showScrollbars");
84     self->showStatus         = WOExtGetProperty(_config, @"showStatus");
85     self->showToolbar        = WOExtGetProperty(_config, @"showToolbar");
86
87     self->top                = WOExtGetProperty(_config, @"top");
88     self->left               = WOExtGetProperty(_config, @"left");
89     self->isCenter           = WOExtGetProperty(_config, @"isCenter");
90     self->filename           = WOExtGetProperty(_config, @"filename");
91     self->string             = WOExtGetProperty(_config, @"string");
92     self->framework          = WOExtGetProperty(_config, @"framework");
93
94     funcCount = 0;
95     if (self->action)   funcCount++;
96     if (self->pageName) funcCount++;
97     if (self->href)     funcCount++;
98
99     if (funcCount > 1)
100       NSLog(@"WARNING: JSModalWindow: choose only one of "
101             @"action | pageName | href");
102     if (funcCount < 1)
103       NSLog(@"WARNING: JSModalWindow: no function declared - choose one of"
104             @"action | pageName | href");
105
106 #define SetAssociationValue(_assoc_, _value_)                               \
107              if (_assoc_ == nil) {                                          \
108                _assoc_ = [WOAssociation associationWithValue:_value_];      \
109                [_assoc_ retain];                                            \
110              }                                                              \
111
112     SetAssociationValue(self->height,         @"300");
113     SetAssociationValue(self->width,          @"300");
114     SetAssociationValue(self->isResizable,    @"Yes");
115     SetAssociationValue(self->showLocation,   @"Yes");
116     SetAssociationValue(self->showMenuBar,    @"Yes");
117     SetAssociationValue(self->showScrollbars, @"Yes");
118     SetAssociationValue(self->showStatus,     @"Yes");
119     SetAssociationValue(self->showToolbar,    @"Yes");
120     SetAssociationValue(self->top,            @"500");
121     SetAssociationValue(self->left,           @"500");
122
123 #undef SetAssociationValue
124
125     self->template = [_subs retain];
126   }
127   return self;
128 }
129
130 - (void)dealloc {
131   [self->action   release];
132   [self->pageName release];
133   [self->href     release];
134   
135   [self->height         release];
136   [self->width          release];
137   [self->windowName     release];
138   [self->isResizable    release];
139   [self->showLocation   release];
140   [self->showMenuBar    release];
141   [self->showScrollbars release];
142   [self->showStatus     release];
143   [self->showToolbar    release];
144
145   [self->top       release];
146   [self->left      release];
147   [self->isCenter  release];
148   [self->filename  release];
149   [self->string    release];
150   [self->framework release];
151   
152   [self->template release];
153   [super dealloc];
154 }
155
156 /* handling requests */
157
158 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
159   [self->template takeValuesFromRequest:_rq inContext:_ctx];
160 }
161
162 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
163   if (self->pageName != nil) {
164     NSString *name;
165     
166     name = [self->pageName stringValueInComponent: [_ctx component]];
167     return [[_ctx application] pageWithName:name inContext:_ctx];
168   }
169   
170   if (self->action != nil)
171     return [self->action valueInComponent:[_ctx component]];
172
173   return [self->template invokeActionForRequest:_rq inContext:_ctx];
174 }
175
176 /* generating response */
177
178 - (void)appendToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
179   WOComponent *comp;
180   NSString    *tmp;
181   NSArray     *languages;
182   
183   comp = [_ctx component];
184   
185   // link
186   [_resp appendContentString:
187              @"<a onclick=\"window.showModalDialog('"];
188   if (self->href != nil)
189     [_resp appendContentString:[self->href stringValueInComponent:comp]];
190   else
191     [_resp appendContentString:@"http://sope.opengroupware.org/"];
192   
193   [_resp appendContentString:@"', 'a short text', '"];
194
195   /* configure modal panel */
196   if (self->height != nil) {
197     [_resp appendContentString:@" dialogHeight: "];
198     [_resp appendContentString:[self->height stringValueInComponent:comp]];
199     [_resp appendContentString:@"px;"];
200   }
201   if (self->width != nil) {
202     [_resp appendContentString:@" dialogWidth: "];
203     [_resp appendContentString:[self->width stringValueInComponent:comp]];
204     [_resp appendContentString:@"px;"];
205   }
206   if (self->top != nil) {
207     [_resp appendContentString:@" dialogTop: "];
208     [_resp appendContentString:[self->top stringValueInComponent:comp]];
209     [_resp appendContentString:@"px;"];
210   }
211   if (self->left != nil) {
212     [_resp appendContentString:@" dialogLeft: "];
213     [_resp appendContentString:[self->left stringValueInComponent:comp]];
214     [_resp appendContentString:@"px;"];
215   }
216   if (self->isResizable != nil) {
217     [_resp appendContentString:@" resizable: "];
218     [_resp appendContentString:
219              [self->isResizable stringValueInComponent:comp]];
220     [_resp appendContentCharacter:';'];
221   }
222   if (self->showStatus != nil) {
223     [_resp appendContentString:@" status: "];
224     [_resp appendContentString:[self->showStatus stringValueInComponent:comp]];
225     [_resp appendContentCharacter:';'];
226   }
227   if (self->isCenter != nil) {
228     [_resp appendContentString:@" center: "];
229     [_resp appendContentString:[self->isCenter stringValueInComponent:comp]];
230     [_resp appendContentCharacter:';'];
231   }
232
233   [_resp appendContentString:@"')\""];
234   
235   [_resp appendContentString:@" href=\""];
236   [_resp appendContentString:[_ctx componentActionURL]];
237   [_resp appendContentString:@"\" "];
238   
239   [self appendExtraAttributesToResponse:_resp inContext:_ctx];
240   [_resp appendContentString:@" >"];
241
242   /* link content */
243   if (self->filename != nil) {
244     WOResourceManager *rm;
245     NSString          *frameworkName;
246       
247     rm        = [[_ctx application] resourceManager];
248     languages = [_ctx resourceLookupLanguages];
249     frameworkName = (self->framework != nil)
250       ? [self->framework stringValueInComponent:comp] 
251       : [comp frameworkName];
252     
253     tmp = [rm urlForResourceNamed:[self->filename stringValueInComponent:comp]
254               inFramework:frameworkName
255               languages:languages
256               request:[_ctx request]];
257     
258     [_resp appendContentString:@"<img border=\"0\" src=\""];
259     [_resp appendContentString:tmp];
260     [_resp appendContentString:@"\" "];
261     
262     [_resp appendContentString:
263              (_ctx->wcFlags.xmlStyleEmptyElements ? @" />" : @">")];
264   }
265   
266   [self->template appendToResponse:_resp inContext:_ctx];
267   
268   if (self->string != nil)
269     [_resp appendContentString:[self->string stringValueInComponent:comp]];
270   
271   /* close link */
272   [_resp appendContentString:@"</a>"];
273 }
274
275 @end /* JSModalWindow */