// TODO: might be better to use -stringByReplacingBindings: etc
static NSString *script = \
@"var currentIndex = %d;\n"
+ @"var lastIndex = currentIndex;\n"
@"\n"
@"function hasAddress(email) {\n"
@" var e = document.getElementById(email);\n"
@"}\n"
@"\n"
@"function fancyAddRow(shouldEdit, text) {\n"
- @" var table, lastChild, proto, row, select, input;\n"
+ @" var addr, table, lastChild, proto, row, select, input;\n"
@"\n"
+ @" addr = document.getElementById('addr_' + lastIndex);\n"
+ @" if (addr && addr.value == '') {\n"
+ @" input = document.getElementById('compose_subject_input');\n"
+ @" if (input) {\n"
+ @" input.focus();\n"
+ @" input.select();\n"
+ @" return;\n"
+ @" }\n"
+ @" }\n"
@" table = this.getTable();\n"
@" lastChild = document.getElementById('row_last');\n"
@"\n"
@" currentIndex++;\n"
@"\n"
- @" proto = document.getElementById('row_0');\n"
+ @" proto = document.getElementById('row_' + lastIndex);\n"
@" row = proto.cloneNode(true);\n"
@" row.id = 'row_' + currentIndex;\n"
@"\n"
+ @" // select popup\n"
@" select = row.childNodes[1].childNodes[1];\n"
@" select.name = 'popup_' + currentIndex;\n"
+ @" select.value = proto.childNodes[1].childNodes[1].value;\n"
@" input = row.childNodes[3].childNodes[1];\n"
@" input.name = 'addr_' + currentIndex;\n"
@" input.id = 'addr_' + currentIndex;\n"
@" }\n"
@"}\n"
@"\n"
+ @"function addressFieldGotFocus(sender) {\n"
+ @" var idx;\n"
+ @"\n"
+ @" idx = this.getIndexFromIdentifier(sender.id);\n"
+ @" if ((lastIndex == idx) || (idx == 0)) return;\n"
+ @" this.removeLastEditedRowIfEmpty();\n"
+ @"}\n"
+ @"function addressFieldLostFocus(sender) {\n"
+ @" lastIndex = this.getIndexFromIdentifier(sender.id);\n"
+ @"}\n"
+ @"\n"
@"function removeIfNecessary(sender) {\n"
+ @"/*"
@" var idx, addr, table, senderRow;\n"
@"\n"
@" if(sender.value != '')\n"
@" table = this.getTable();\n"
@" senderRow = this.findRowWithIndex(idx);\n"
@" table.removeChild(senderRow);\n"
+ @"*/"
+ @"}\n"
+ @"\n"
+ @"function removeLastEditedRowIfEmpty() {\n"
+ @" var idx, addr, table, senderRow;\n"
+ @"\n"
+ @" idx = lastIndex;\n"
+ @" addr = document.getElementById('addr_' + idx);\n"
+ @" if (!addr) return;\n"
+ @" if (addr.value != '') return;\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"