]> err.no Git - sope/blob - sope-appserver/WEExtensions/JSShiftClick.m
fix for SoProductResourceManager.m
[sope] / sope-appserver / WEExtensions / JSShiftClick.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/NGObjWeb.h>
24 #include "WEClientCapabilities.h"
25
26 /*
27
28   < scriptName   (obligatory !!!)
29   > identifier
30   > prefix
31
32   Generates an ShiftClick JavaScript for CheckBoxes.
33
34   Example:
35
36   // wod:
37   ShiftClickScript: JSShiftClick {
38     scriptName = scriptName;
39   }
40
41   Repetition: WORepetition {
42     list = (1, 2, 3, 4, 5, 6, 7, 8, 9);
43     item = index;
44   }
45   CheckBox: WOCheckBox {
46     checked = checked;
47     value   = index;      // = index"              this must be done !!!
48     onClick = scriptCall; // = "scriptName(index)" this must be done !!!
49   }
50
51   // html:
52
53   <FORM....>
54   
55     <#ShiftClickScript />
56     <#Repetition>
57       <#CheckBox />
58     </#Repetition>
59
60   </FORM>
61   
62 */
63
64 @interface JSShiftClick : WODynamicElement
65 {
66   WOAssociation *identifier;
67   WOAssociation *prefix;
68   WOAssociation *scriptName;
69 }
70 @end
71
72 static NSString *JSShiftClick_Script =
73       @"<script language=\"JavaScript\">\n"
74       @"<!--\n"
75       @"var ns = (document.layers) ? true : false;\n"
76       @"var ie = (document.all) ? true : false;\n"
77       @"var last = -1;\n"
78       @"function shiftClick%@SearchElement(el) { \n"
79       @"  for (i = 0; i < document.forms.length; i++) { \n"
80       @"    for (j = 0; j < document.forms[i].elements.length; j++) { \n"
81       @"      if (document.forms[i].elements[j].value == el) { \n"
82       @"        return document.forms[i].elements[j]; \n"
83       @"      } \n"
84       @"    } \n"
85       @"  } \n"
86       @"  return false; \n"
87       @"} \n\n"
88       @"function shiftClick%@(z) {\n"
89       @"  if (ie) {\n"
90       @"    var plusShift = window.event.shiftKey;\n"
91       @"    if (plusShift && last >= 0) {\n"
92       @"      var actEl    = shiftClick%@SearchElement('%@'+last); \n"
93       @"      if (actEl) { \n "
94       @"        var actState = actEl.checked;\n"
95       @"        if (z<last) { var e1 = z; var e2 = last; }\n"
96       @"        else { var e1 = last; var e2 = z; }\n"
97       @"        for (idx = e1; idx<= e2; idx++) {\n"
98       @"          actEl = shiftClick%@SearchElement('%@' + idx); \n"
99       @"          actEl.checked = actState;\n"
100       @"        }\n"
101       @"      } \n"
102       @"    }\n"
103       @"    last = z;\n"
104       @"  }\n"
105       @"}\n"
106       @"//-->\n"
107       @"</script>";
108
109 #include "common.h"
110
111 @implementation JSShiftClick
112
113 - (id)initWithName:(NSString *)_name
114   associations:(NSDictionary *)_config
115   template:(WOElement *)_tmp
116 {
117   if ((self = [super initWithName:_name associations:_config template:_tmp])) {
118     self->identifier = WOExtGetProperty(_config, @"identifier");
119     self->prefix     = WOExtGetProperty(_config, @"prefix");
120     self->scriptName = WOExtGetProperty(_config, @"scriptName");
121   }
122   return self;
123 }
124
125 - (void)dealloc {
126   [self->identifier release];
127   [self->prefix     release];
128   [self->scriptName release];
129   [super dealloc];
130 }
131
132 /* response generation */
133
134 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
135   WEClientCapabilities *ccaps = nil;
136   NSString *eid  = nil;
137   NSString *prfx = nil;
138
139   ccaps = [[_ctx request] clientCapabilities];
140
141   eid = [self->identifier stringValueInComponent:[_ctx component]];
142   eid = (eid) ? eid : [_ctx elementID];
143   eid = [[eid componentsSeparatedByString:@"."]
144               componentsJoinedByString:@"_"];
145   
146   prfx = [self->prefix stringValueInComponent:[_ctx component]];
147   prfx = (prfx) ? prfx : @"";
148
149   if ([ccaps isJavaScriptBrowser]) {
150     NSString *s;
151     
152     s = [[NSString alloc] initWithFormat:JSShiftClick_Script,
153                           eid, eid, eid, prfx, eid, prfx];
154     [_response appendContentString:s];
155     [s release];
156   }
157   if ([self->scriptName isValueSettable]) {
158     NSString *sName = nil;
159
160     sName = [@"shiftClick" stringByAppendingString:eid];
161     [self->scriptName setValue:sName inComponent:[_ctx component]];
162   }
163 #if DEBUG
164   else {
165     NSLog(@"Warning: JSShiftClick: 'scriptName' is not settable!!!");
166   }
167 #endif
168 }
169
170 @end /* JSShiftClick */