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