- (NSArray *)properlySplitAddresses:(NSArray *)_addresses;
-- (NSArray *)getAddressesOfType:(NSString *)_type
- fromFormValues:(NSArray *)_values;
+- (void)getAddressesFromFormValues:(NSDictionary *)_dict;
+- (NSString *)getIndexFromIdentifier:(NSString *)_identifier;
@end
[ma addObject:self->cc];
if(self->bcc)
[ma addObject:self->bcc];
+ /* ensure that min 1 object is available */
+ if([ma count] == 0) {
+ NSArray *tmp = [NSArray arrayWithObject:@""];
+ ASSIGN(self->to, tmp);
+ [ma addObject:self->to];
+ }
return ma;
}
/* identifiers */
- (NSString *)currentRowId {
- if(currentIndex == 0)
- return @"addr_proto";
return [NSString stringWithFormat:@"row_%d", self->currentIndex];
}
/* handling requests */
-- (NSArray *)getAddressesOfType:(NSString *)_type
- fromFormValues:(NSArray *)_values
-{
+- (void)getAddressesFromFormValues:(NSDictionary *)_dict {
+ NSMutableArray *rawTo, *rawCc, *rawBcc;
+ NSArray *keys;
unsigned i, count;
- NSMutableArray *result;
+
+ rawTo = [NSMutableArray array];
+ rawCc = [NSMutableArray array];
+ rawBcc = [NSMutableArray array];
- count = [_values count];
- result = [NSMutableArray arrayWithCapacity:count];
- for (i = 0; i < count; i++) {
- NSString *s;
+ keys = [_dict allKeys];
+ count = [keys count];
+ for(i = 0; i < count; i++) {
+ NSString *key;
- s = [_values objectAtIndex:i];
- if ([s length] == 0)
- continue;
+ key = [keys objectAtIndex:i];
+ if([key hasPrefix:@"addr_"]) {
+ NSString *idx, *addr, *popupKey, *popupValue;
+
+ addr = [[_dict objectForKey:key] lastObject];
+ idx = [self getIndexFromIdentifier:key];
+ popupKey = [NSString stringWithFormat:@"popup_%@", idx];
+ popupValue = [[_dict objectForKey:popupKey] lastObject];
+ if([popupValue isEqualToString:@"0"])
+ [rawTo addObject:addr];
+ else if([popupValue isEqualToString:@"1"])
+ [rawCc addObject:addr];
+ else
+ [rawBcc addObject:addr];
+ }
}
- return result;
+
+ [self setTo:rawTo];
+ [self setCc:rawCc];
+ [self setBcc:rawBcc];
+}
+
+- (NSString *)getIndexFromIdentifier:(NSString *)_identifier {
+ NSRange r = [_identifier rangeOfString:@"_"];
+ return [_identifier substringFromIndex:NSMaxRange(r)];
}
- (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
/* OK, we have a special form value processor */
- NSArray *tmp;
-
+ NSDictionary *d;
+
+ d = [_rq formValues];
+ if(!d)
+ return;
+
+#if 0
[self debugWithFormat:@"Note: will take values ..."];
-
NSLog(@"%s formValues: %@",
__PRETTY_FUNCTION__,
- [_rq formValues]);
-
- tmp = [self getAddressesOfType:@"to"
- fromFormValues:[_rq formValuesForKey:@"to"]];
- [self setTo:tmp];
- tmp = [self getAddressesOfType:@"cc"
- fromFormValues:[_rq formValuesForKey:@"cc"]];
- [self setCc:tmp];
- tmp = [self getAddressesOfType:@"bcc"
- fromFormValues:[_rq formValuesForKey:@"bcc"]];
- [self setBcc:tmp];
+ d);
+#endif
+ [self getAddressesFromFormValues:d];
}
/* response generation */
@"}\n"
@"\n"
@"function rememberAddress(email) {\n"
- @" var list, e;\n"
+ @" var list, span, idx;\n"
@" \n"
- @" list = document.getElementById(\"addr_addresses\");\n"
- @" e = document.createElement(\"span\");\n"
- @" e.id = email;\n"
- @" list.appendChild(e);\n"
+ @" list = document.getElementById('addr_addresses');\n"
+ @" span = document.createElement('span');\n"
+ @" span.id = email;\n"
+ @" idx = document.createTextNode(currentIndex);\n"
+ @" span.appendChild(idx);\n"
+ @" list.appendChild(span);\n"
@"}\n"
@"\n"
@"function addAddress(type, cn, dn, email, uid, sn) {\n"
- @" var shouldAddRow, s;\n"
+ @" var shouldAddRow, s, e;\n"
@" \n"
@" shouldAddRow = true;\n"
- @" s = cn + \" <\" + email + \">\";\n"
+ @" s = cn + ' <' + email + '>';\n"
@"\n"
@" if(this.hasAddress(email))\n"
@" return;\n"
@"\n"
- @" this.rememberAddress(email);\n"
- @"\n"
- @" var e = document.getElementById(\"addr_0\");\n"
+ @" e = document.getElementById('addr_0');\n"
@" if(e.value == '') {\n"
@" e.value = s;\n"
@" shouldAddRow = false;\n"
@" if(shouldAddRow) {\n"
@" this.fancyAddRow(false, s);\n"
@" }\n"
+ @" this.rememberAddress(email);\n"
@"}\n"
@"\n"
@"function fancyAddRow(shouldEdit, text) {\n"
- @" var table = document.getElementById(\"addr_table\").childNodes[1];\n"
- @" var lastChild = document.getElementById(\"addr_lastentry\");\n"
- @" var proto, row, select, input;\n"
+ @" var table, lastChild, proto, row, select, input;\n"
+ @"\n"
+ @" table = this.getTable();\n"
+ @" lastChild = document.getElementById('row_last');\n"
@"\n"
@" currentIndex++;\n"
@"\n"
- @" proto = document.getElementById(\"addr_proto\");\n"
+ @" proto = document.getElementById('row_0');\n"
@" row = proto.cloneNode(true);\n"
- @" row.id = \"\";\n"
+ @" row.id = 'row_' + currentIndex;\n"
@"\n"
@" select = row.childNodes[1].childNodes[1];\n"
- @" select.name = \"popup_\" + currentIndex;\n"
+ @" select.name = 'popup_' + currentIndex;\n"
@" input = row.childNodes[3].childNodes[1];\n"
- @" input.name = \"addr_\" + currentIndex;\n"
- @" input.id = \"addr_\" + currentIndex;\n"
+ @" input.name = 'addr_' + currentIndex;\n"
+ @" input.id = 'addr_' + currentIndex;\n"
@" input.value = text;\n"
@"\n"
@" table.insertBefore(row, lastChild);\n"
@" input.select();\n"
@" }\n"
@"}\n"
- @"\n";
+ @"\n"
+ @"function removeIfNecessary(sender) {\n"
+ @" var idx, addr, table, senderRow;\n"
+ @"\n"
+ @" if(sender.value != '')\n"
+ @" return;\n"
+ @" if(sender.id == 'addr_0')\n"
+ @" return;\n"
+ @" idx = this.getIndexFromIdentifier(sender.id);\n"
+ @" addr = this.findAddressWithIndex(idx);\n"
+ @" if(addr) {\n"
+ @" var addresses = document.getElementById('addr_addresses');\n"
+ @" addresses.removeChild(addr);\n"
+ @" }\n"
+ @" table = this.getTable();\n"
+ @" senderRow = this.findRowWithIndex(idx);\n"
+ @" table.removeChild(senderRow);\n"
+ @"}\n"
+ @"\n"
+ @"function findAddressWithIndex(idx) {\n"
+ @" var list, i, count, addr, idx\n"
+ @" list = document.getElementById('addr_addresses').childNodes;\n"
+ @" count = list.length;\n"
+ @" for(i = 0; i < count; i++) {\n"
+ @" addr = list[i];\n"
+ @" if(addr.innerHTML == idx)\n"
+ @" return addr;\n"
+ @" }\n"
+ @" return null;\n"
+ @"}\n"
+ @"\n"
+ @"function findRowWithIndex(idx) {\n"
+ @" var id = 'row_' + idx;\n"
+ @" return document.getElementById(id);\n"
+ @"}\n"
+ @"\n"
+ @"function getIndexFromIdentifier(id) {\n"
+ @" return id.split('_')[1];\n"
+ @"}\n"
+ @"\n"
+ @"function getTable() {\n"
+ @" return document.getElementById('addr_table').childNodes[1];\n"
+ @"}\n";
+ unsigned idx;
+
+ idx = [self->to count] + [self->cc count] + [self->bcc count];
+
return [NSString stringWithFormat:
script,
- self->currentIndex];
+ idx];
}
@end