2 Copyright (C) 2005 Helge Hess
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 #import <NGObjWeb/WODynamicElement.h>
27 Inspired by the link_to_remote() RoR function.
30 @interface WELiveLink : WODynamicElement
32 WOAssociation *string;
33 WOAssociation *updateID;
34 WOAssociation *position;
36 WOAssociation *actionClass;
37 WOAssociation *directActionName;
40 WOAssociation *confirmText;
47 #include "WEPrototypeScript.h"
50 @implementation WELiveLink
52 - (id)initWithName:(NSString *)_name
53 associations:(NSDictionary *)_config
54 template:(WOElement *)_tmp
56 if ((self = [super initWithName:_name associations:_config template:_tmp])) {
57 self->template = [_tmp retain];
59 self->string = WEPExtGetProperty(_config, @"string");
60 self->updateID = WEPExtGetProperty(_config, @"updateID");
61 self->position = WEPExtGetProperty(_config, @"position");
62 self->actionClass = WEPExtGetProperty(_config, @"actionClass");
63 self->directActionName = WEPExtGetProperty(_config, @"directActionName");
64 self->href = WEPExtGetProperty(_config, @"href");
65 self->confirmText = WEPExtGetProperty(_config, @"confirmText");
71 [self->updateID release];
72 [self->position release];
73 [self->actionClass release];
74 [self->directActionName release];
76 [self->confirmText release];
77 [self->string release];
78 [self->template release];
82 /* generating response */
84 - (NSString *)linkInContext:(WOContext *)_ctx {
85 if (self->directActionName != nil) {
88 ac = [self->actionClass stringValueInComponent:[_ctx component]];
89 da = [self->directActionName stringValueInComponent:[_ctx component]];
92 da = [[ac stringByAppendingString:@"/"] stringByAppendingString:da];
94 return [_ctx directActionURLForActionNamed:da queryDictionary:nil];
97 if (self->href != nil)
98 return [self->href stringValueInComponent:[_ctx component]];
100 [self logWithFormat:@"ERROR: no binding for link!"];
104 - (void)appendJavaScriptToResponse:(WOResponse *)_response
105 inContext:(WOContext *)_ctx
108 new Ajax.Updater('time_div',
109 '/hello_world/say_when',
110 {insertion:Insertion.After, asynchronous:true});
113 WOComponent *sComponent;
115 BOOL closeBracket = NO, isDOM = NO;
117 sComponent = [_ctx component];
119 /* check for confirm panel */
121 s = [self->confirmText stringValueInComponent:sComponent];
122 if ([s length] > 0) {
123 [_response appendContentString:@"if (confirm('"];
124 [_response appendContentHTMLAttributeValue:s];
125 [_response appendContentString:@"')) { "];
129 /* whether to update an element */
131 s = [self->updateID stringValueInComponent:sComponent];
132 if ([s length] > 0) {
133 [_response appendContentString:@"new Ajax.Updater('"];
134 [_response appendContentHTMLAttributeValue:s];
135 [_response appendContentString:@"', '"];
139 [_response appendContentString:@"new Ajax.Request('"];
144 if ((s = [self linkInContext:_ctx]) != nil)
145 [_response appendContentHTMLAttributeValue:s];
146 [_response appendContentString:@"'"];
150 [_response appendContentString:@", {"];
153 s = [self->position stringValueInComponent:sComponent];
154 if ([s length] > 0) {
155 [_response appendContentString:@"insertion: Insertion."];
156 [_response appendContentString:[s capitalizedString]];
157 [_response appendContentString:@", "];
161 [_response appendContentString:@"asynchronous: true }"];
165 [_response appendContentString:@");"];
167 /* close confirm panel if */
169 if (closeBracket) [_response appendContentString:@"}"];
171 /* always: do not follow link target */
172 [_response appendContentString:@" return false;"];
175 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
178 /* first ensure that prototype is loaded */
179 [WEPrototypeScript appendToResponse:_response inContext:_ctx];
183 [_response appendContentString:@"<a href=\"#\" onclick=\""];
185 /* generate JavaScript */
187 [self appendJavaScriptToResponse:_response inContext:_ctx];
189 /* finish link start tag */
191 [_response appendContentString:@"\""];
193 [self appendExtraAttributesToResponse:_response inContext:_ctx];
194 if (self->otherTagString != nil) {
195 [_response appendContentString:@" "];
196 s = [self->otherTagString stringValueInComponent:[_ctx component]];
197 [_response appendContentString:s];
199 [_response appendContentString:@">"];
201 /* generate content */
203 if ((s = [self->string stringValueInComponent:[_ctx component]]) != nil)
204 [_response appendContentHTMLString:s];
206 [self->template appendToResponse:_response inContext:_ctx];
209 [_response appendContentString:@"</a>"];
212 @end /* WELiveLink */