]> err.no Git - sope/blob - sope-appserver/WOExtensions/JSAlertPanel.m
added svn:keywords and svn:ignore where appropriate. removed CVS artifacts.
[sope] / sope-appserver / WOExtensions / JSAlertPanel.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #import <NGObjWeb/WODynamicElement.h>
24
25 @interface JSAlertPanel : WODynamicElement
26 {
27   WOAssociation *action;
28   WOAssociation *javaScriptFunction;
29   WOAssociation *pageName;
30   WOAssociation *alertMessage;
31   WOAssociation *altTag;
32   WOAssociation *filename;
33   WOAssociation *targetWindow;
34   WOAssociation *string;
35   
36   WOElement     *template;
37 }
38
39 @end
40
41 #include "common.h"
42
43 @implementation JSAlertPanel
44
45 + (int)version {
46   return [super version] + 0 /* v2 */;
47 }
48 + (void)initialize {
49   NSAssert2([super version] == 2,
50             @"invalid superclass (%@) version %i !",
51             NSStringFromClass([self superclass]), [super version]);
52 }
53
54 - (id)initWithName:(NSString *)_name
55   associations:(NSDictionary *)_config
56   template:(WOElement *)_subs
57 {
58   if ((self = [super initWithName:_name associations:_config template:_subs]))
59   {
60     int funcCount;
61
62     self->action             = WOExtGetProperty(_config,@"action");
63     self->javaScriptFunction = WOExtGetProperty(_config,@"javaScriptFunction");
64     self->pageName           = WOExtGetProperty(_config,@"pageName");
65     self->alertMessage       = WOExtGetProperty(_config,@"alertMessage");
66     self->altTag             = WOExtGetProperty(_config,@"altTag");
67     self->filename           = WOExtGetProperty(_config,@"filename");
68     self->targetWindow       = WOExtGetProperty(_config,@"targetWindow");
69     self->string             = WOExtGetProperty(_config,@"string");
70     
71     funcCount = 0;
72     if (self->action) funcCount++;
73     if (self->pageName) funcCount++;
74     if (self->javaScriptFunction) funcCount++;
75
76     if (funcCount > 1) {
77       NSLog(@"WARNING: JSAlertPanel: choose only one of "
78             @"action | pageName | javaScriptFunction");
79     }
80     if (funcCount < 1) {
81       NSLog(@"WARNING: JSAlertPanel: no function declared - choose one of"
82             @"action | pageName | javaScriptFunction");
83     }
84     if (!self->alertMessage) {
85       NSLog(@"WARNING: JSAlertPanel: no value for 'alertMessage'"
86             @" - using default");
87     }
88     
89     self->template = RETAIN(_subs);
90   }
91   return self;
92 }
93
94 - (void)dealloc {
95   RELEASE(self->action);
96   RELEASE(self->javaScriptFunction);
97   RELEASE(self->pageName);
98   RELEASE(self->alertMessage);
99   RELEASE(self->altTag);
100   RELEASE(self->filename);
101   RELEASE(self->targetWindow);
102   RELEASE(self->string);
103   RELEASE(self->template);
104   [super dealloc];
105 }
106
107 /* processing requests */
108
109 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
110   [self->template takeValuesFromRequest:_rq inContext:_ctx];
111 }
112
113 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
114   if (self->pageName) {
115     NSString *name;
116     
117     name = [self->pageName stringValueInComponent: [_ctx component]];
118     return [[_ctx application] pageWithName:name inContext:_ctx];
119   }
120   if (self->action)
121     return [self->action valueInComponent:[_ctx component]];
122   
123   return [self->template invokeActionForRequest:_rq inContext:_ctx];
124 }
125
126
127 - (void)appendToResponse:(WOResponse *)_response
128   inContext:(WOContext *)_ctx
129 {
130   WOComponent     *comp;
131   NSString        *tmp;
132   NSArray         *languages;
133   
134   comp = [_ctx component];
135   
136   // link
137   [_response appendContentString:@"<a onClick=\"javascript:alert('"];
138   tmp = (self->alertMessage)
139     ? [self->alertMessage stringValueInComponent: comp]
140     : @"Press OK.";
141   [_response appendContentHTMLString:tmp];
142   [_response appendContentString:@"');\""];
143   [_response appendContentString:@" href=\""];
144   
145   if (self->javaScriptFunction) {
146     [_response appendContentString:@"javascript:"];
147     [_response appendContentHTMLAttributeValue:
148                  [self->javaScriptFunction stringValueInComponent:comp]];
149   }
150   else {
151     [_response appendContentString:[_ctx componentActionURL]];
152   }
153   [_response appendContentString:@"\" "];
154   if (self->targetWindow) {
155     [_response appendContentString:@" target=\""];
156     [_response appendContentHTMLAttributeValue:
157                  [self->targetWindow stringValueInComponent: comp]];
158     [_response appendContentString:@"\" "];
159   }
160   [self appendExtraAttributesToResponse:_response inContext:_ctx];
161   [_response appendContentString:@" >"];
162
163   // link content  
164   if (self->filename) {
165     WOResourceManager *rm;
166
167     rm = [[_ctx application] resourceManager];
168     
169     languages = [_ctx hasSession]
170       ? [[_ctx session] languages]
171       : [[_ctx request] browserLanguages];
172     
173     tmp = [rm urlForResourceNamed:[self->filename stringValueInComponent:comp]
174               inFramework:[comp frameworkName]
175               languages:languages
176               request:[_ctx request]];
177     
178     [_response appendContentString:@"<img border=\"0\" src=\""];
179     [_response appendContentString:tmp];
180     [_response appendContentString:@"\""];
181     
182     if (self->altTag) {
183       [_response appendContentString:@" alt=\""];
184       [_response appendContentString:
185         [self->altTag stringValueInComponent:comp]];
186       [_response appendContentString:@"\" "];
187     }
188     [_response appendContentString:@" />"];
189   }
190   [self->template appendToResponse:_response inContext:_ctx];
191   if (self->string) 
192     [_response appendContentString:[self->string stringValueInComponent:comp]];
193
194   /* close link */
195   [_response appendContentString:@"</a>"];
196 }
197
198 @end /* JSAlertPanel */