]> err.no Git - sope/blob - sope-appserver/WEExtensions/WEDragContainer.m
added strict OSX bundle dependencies
[sope] / sope-appserver / WEExtensions / WEDragContainer.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 /*
25   renders this:
26
27   <...small script...>
28   <SPAN onDragStart="fnSetInfo($tag,$effectsAllowed)">
29   $content
30   </SPAN>
31
32 */
33
34 @interface WEDragContainer : WODynamicElement
35 {
36   WOElement     *template;
37   WOAssociation *tag;
38   WOAssociation *effectsAllowed;
39   WOAssociation *elementName;
40   WOAssociation *isDraggable;
41   
42   WOAssociation *object;
43   WOAssociation *droppedObject;
44 }
45 @end
46
47 @interface WEDragScript : WODynamicElement
48 + (void)appendDragScriptToResponse:(WOResponse *)_response
49   inContext:(WOContext *)_ctx;
50 @end
51
52 #include <NGObjWeb/WEClientCapabilities.h>
53 #include "common.h"
54
55 //#define DEBUG_TAKEVALUES 1
56
57 @implementation WEDragContainer
58
59 static BOOL debugTakeValues = NO;
60
61 + (int)version {
62   return 0 + [super version];
63 }
64
65 - (id)initWithName:(NSString *)_name
66   associations:(NSDictionary *)_config
67   template:(WOElement *)_t
68 {
69   if ((self = [super initWithName:_name associations:_config template:_t])) {
70     self->tag            = WOExtGetProperty(_config, @"tag");
71     self->effectsAllowed = WOExtGetProperty(_config, @"effectsAllowed");
72     self->elementName    = WOExtGetProperty(_config, @"elementName");
73     self->isDraggable    = WOExtGetProperty(_config, @"isDraggable");
74     
75     self->object         = WOExtGetProperty(_config, @"object");
76     self->droppedObject  = WOExtGetProperty(_config, @"droppedObject");
77     
78     self->template = [_t retain];
79   }
80   return self;
81 }
82
83 - (void)dealloc {
84   [self->isDraggable    release];
85   [self->object         release];
86   [self->droppedObject  release];
87   [self->tag            release];
88   [self->elementName    release];
89   [self->effectsAllowed release];
90   [self->template       release];
91   [super dealloc];
92 }
93
94 /* processing request values */
95
96 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
97   id formValue;
98   
99   if ((formValue = [_rq formValueForKey:[_ctx elementID]]) != nil) {
100     id obj;
101
102     if (debugTakeValues) {
103       [[_ctx component]
104              debugWithFormat:@"WEDragContainer: got value '%@' for id '%@'",
105                formValue, [_ctx elementID]];
106     }
107
108     obj = [self->object valueInComponent:[_ctx component]];
109     
110     if (debugTakeValues)
111       NSLog(@"DRAG MATCH => ok, obj is %@",obj);
112     
113     if ([self->droppedObject isValueSettable])
114       [self->droppedObject setValue:obj inComponent:[_ctx component]];
115     
116     if (obj) {
117       [_ctx takeValue:obj forKey:@"WEDragContainer_DropObject"];
118     }
119   }
120   else if (debugTakeValues) {
121     [[_ctx component]
122            debugWithFormat:@"WEDragContainer: got no value for id '%@'",
123              [_ctx elementID]];
124   }
125   
126   [self->template takeValuesFromRequest:_rq inContext:_ctx];
127 }
128
129 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
130   return [self->template invokeActionForRequest:_rq inContext:_ctx];
131 }
132
133 /* generating response */
134
135 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
136   NSString *tmp = nil;
137   NSString *ttag;
138   BOOL     doDnD;
139   
140   doDnD = [[[_ctx request] clientCapabilities] doesSupportDHTMLDragAndDrop];
141     
142   if (doDnD) {
143     if (self->isDraggable)
144       doDnD = [self->isDraggable boolValueInComponent:[_ctx component]];
145   }
146   
147   [WEDragScript appendDragScriptToResponse:_response inContext:_ctx];
148   
149   ttag = [self->tag stringValueInComponent:[_ctx component]];
150
151   if (doDnD) {
152     NSString *teffect, *tdragContent;
153
154     teffect = self->effectsAllowed
155       ? [self->effectsAllowed stringValueInComponent:[_ctx component]]
156       : @"all";
157     
158     tdragContent = @"this.innerHTML";
159     
160     tmp = @"fnSetInfo('%@?%@','%@', %@)";
161     tmp = [NSString stringWithFormat:tmp,
162                       [_ctx elementID], ttag,
163                       teffect,
164                       tdragContent];
165   }
166
167   if (self->elementName || doDnD) {
168     /* Note: not using lowercase names since this might break JS? */
169     [_response appendContentString:@"<SPAN "];
170     [_response appendContentString:@"ID=\"span_"];
171     [_response appendContentString:[_ctx elementID]];
172     [_response appendContentString:@"\" "];
173   }
174   
175   if (doDnD) {
176     [_response appendContentString:@" onDragStart=\""];
177     [_response appendContentString:tmp];
178     [_response appendContentString: @"\""];
179     [_response appendContentString:@" onDrag=\"fnDrag()\""];
180     [_response appendContentString:@" onDragEnd=\"fnDragEnd()\""];
181   }
182   
183   if (self->elementName || doDnD) {
184     [self appendExtraAttributesToResponse:_response inContext:_ctx];
185     [_response appendContentString:@">"];
186   }
187   
188   /* add template */
189   [self->template appendToResponse:_response inContext:_ctx];
190   
191   /* close container */
192   if (self->elementName || doDnD)
193     [_response appendContentString:@"</SPAN>"];
194 }
195
196 /* accessors */
197
198 - (id)template {
199   return self->template;
200 }
201
202 @end /* WEDragContainer */
203
204
205 @implementation WEDragScript
206
207 + (void)appendDragScriptToResponse:(WOResponse *)_response
208   inContext:(WOContext *)_ctx
209 {
210   NSString *dragScript;
211   BOOL     doDnD;
212   
213   doDnD = [[[_ctx request] clientCapabilities] doesSupportDHTMLDragAndDrop];
214   
215   if (![[_ctx valueForKey: @"WEDragContainerScriptDone"] boolValue] && doDnD) {
216     dragScript =
217       @"<DIV ID=\"DragDIV\" STYLE=\"position: absolute; visibility: hidden; width: 150;\"></DIV>"
218       @"<SCRIPT LANGUAGE=\"JScript\">\n"
219       @"<!--\n"
220       @"function fnSetInfo(objData, effects, dragContent) {\n"
221       @"  event.dataTransfer.clearData(\"Text\");\n"
222       @"  event.dataTransfer.setData(\"Text\", objData);\n"
223       @"  event.dataTransfer.effectAllowed = effects;\n "
224       @"  DragDIV.innerHTML = dragContent;\n"
225       @"  DragDIV.style.visibility = \"visible\";\n "
226       @"  DragDIV.style.top  =window.event.clientY+document.body.scrollTop;\n"
227       @"  DragDIV.style.left =window.event.clientX+document.body.scrollLeft;\n"
228       @"  DragDIV.style.zIndex += 20; \n"
229       @"}\n"
230       @"function fnDrag() {\n"
231       @"  DragDIV.style.top  =window.event.clientY+document.body.scrollTop;\n"
232       @"  DragDIV.style.left =window.event.clientX+document.body.scrollLeft;\n"
233       @"}\n"
234       @"function fnDragEnd() {\n"
235       @"  DragDIV.innerHTML = \"\";\n"
236       @"  DragDIV.style.visibility = \"hidden\";\n"
237       @"}\n"
238       @"// -->\n"
239       @"</SCRIPT>";
240
241     [_response appendContentString: dragScript];
242     
243     [_ctx takeValue: [NSNumber numberWithBool: YES] forKey:
244           @"WEDragContainerScriptDone"];
245   }
246 }
247
248 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
249   [[self class] appendDragScriptToResponse:_response inContext:_ctx];
250 }
251
252 @end /* WEDragScript */