| 1 |
class SelectParser |
| 2 |
|
| 3 |
constructor: -> |
| 4 |
@options_index = 0 |
| 5 |
@parsed = [] |
| 6 |
|
| 7 |
add_node: (child) -> |
| 8 |
if child.nodeName.toUpperCase() is "OPTGROUP" |
| 9 |
this.add_group child |
| 10 |
else |
| 11 |
this.add_option child |
| 12 |
|
| 13 |
add_group: (group) -> |
| 14 |
group_position = @parsed.length |
| 15 |
@parsed.push |
| 16 |
array_index: group_position |
| 17 |
group: true |
| 18 |
label: group.label |
| 19 |
title: group.title if group.title |
| 20 |
children: 0 |
| 21 |
disabled: group.disabled, |
| 22 |
classes: group.className |
| 23 |
this.add_option( option, group_position, group.disabled ) for option in group.childNodes |
| 24 |
|
| 25 |
add_option: (option, group_position, group_disabled) -> |
| 26 |
if option.nodeName.toUpperCase() is "OPTION" |
| 27 |
if option.text != "" |
| 28 |
if group_position? |
| 29 |
@parsed[group_position].children += 1 |
| 30 |
@parsed.push |
| 31 |
array_index: @parsed.length |
| 32 |
options_index: @options_index |
| 33 |
value: option.value |
| 34 |
text: option.text |
| 35 |
html: option.innerHTML |
| 36 |
title: option.title if option.title |
| 37 |
selected: option.selected |
| 38 |
disabled: if group_disabled is true then group_disabled else option.disabled |
| 39 |
group_array_index: group_position |
| 40 |
group_label: if group_position? then @parsed[group_position].label else null |
| 41 |
classes: option.className |
| 42 |
style: option.style.cssText |
| 43 |
else |
| 44 |
@parsed.push |
| 45 |
array_index: @parsed.length |
| 46 |
options_index: @options_index |
| 47 |
empty: true |
| 48 |
@options_index += 1 |
| 49 |
|
| 50 |
SelectParser.select_to_array = (select) -> |
| 51 |
parser = new SelectParser() |
| 52 |
parser.add_node( child ) for child in select.childNodes |
| 53 |
parser.parsed |