|
179 | 179 | * Helper function for getElement and getElements. |
180 | 180 | */ |
181 | 181 | p5.prototype._wrapElement = function(elt) { |
| 182 | + var children = Array.prototype.slice.call(elt.children); |
182 | 183 | if (elt.tagName === 'INPUT' && elt.type === 'checkbox') { |
183 | 184 | var converted = new p5.Element(elt); |
184 | 185 | converted.checked = function() { |
|
196 | 197 | return new p5.MediaElement(elt); |
197 | 198 | } else if (elt.tagName === 'SELECT') { |
198 | 199 | return this.createSelect(new p5.Element(elt)); |
| 200 | + } else if ( |
| 201 | + children.length > 0 && |
| 202 | + children.every(function(c) { |
| 203 | + return c.tagName === 'INPUT' || c.tagName === 'LABEL'; |
| 204 | + }) |
| 205 | + ) { |
| 206 | + return this.createRadio(new p5.Element(elt)); |
199 | 207 | } else { |
200 | 208 | return new p5.Element(elt); |
201 | 209 | } |
|
675 | 683 | * } |
676 | 684 | * </code></div> |
677 | 685 | */ |
678 | | - p5.prototype.createRadio = function() { |
| 686 | + p5.prototype.createRadio = function(existing_radios) { |
679 | 687 | p5._validateParameters('createRadio', arguments); |
| 688 | + // do some prep by counting number of radios on page |
680 | 689 | var radios = document.querySelectorAll('input[type=radio]'); |
681 | 690 | var count = 0; |
682 | 691 | if (radios.length > 1) { |
|
694 | 703 | } else if (radios.length === 1) { |
695 | 704 | count = 1; |
696 | 705 | } |
697 | | - var elt = document.createElement('div'); |
698 | | - var self = addElement(elt, this); |
| 706 | + // see if we got an existing set of radios from callee |
| 707 | + var elt, self; |
| 708 | + if (typeof existing_radios === 'object') { |
| 709 | + // use existing elements |
| 710 | + self = existing_radios; |
| 711 | + elt = this.elt = existing_radios.elt; |
| 712 | + } else { |
| 713 | + // create a set of radio buttons |
| 714 | + elt = document.createElement('div'); |
| 715 | + self = addElement(elt, this); |
| 716 | + } |
| 717 | + // setup member functions |
| 718 | + self._getInputChildrenArray = function() { |
| 719 | + return Array.prototype.slice.call(this.elt.children).filter(function(c) { |
| 720 | + return c.tagName === 'INPUT'; |
| 721 | + }); |
| 722 | + }; |
| 723 | + |
699 | 724 | var times = -1; |
700 | 725 | self.option = function(name, value) { |
701 | 726 | var opt = document.createElement('input'); |
702 | 727 | opt.type = 'radio'; |
703 | 728 | opt.innerHTML = name; |
704 | | - if (arguments.length > 1) opt.value = value; |
| 729 | + if (value) opt.value = value; |
705 | 730 | else opt.value = name; |
706 | 731 | opt.setAttribute('name', 'defaultradio' + count); |
707 | 732 | elt.appendChild(opt); |
|
715 | 740 | } |
716 | 741 | return opt; |
717 | 742 | }; |
718 | | - self.selected = function() { |
719 | | - var i, |
720 | | - length = this.elt.childNodes.length; |
721 | | - if (arguments.length === 1) { |
722 | | - for (i = 0; i < length; i += 2) { |
723 | | - if (this.elt.childNodes[i].value === arguments[0]) |
724 | | - this.elt.childNodes[i].checked = true; |
| 743 | + self.selected = function(value) { |
| 744 | + var i; |
| 745 | + var inputChildren = self._getInputChildrenArray(); |
| 746 | + if (value) { |
| 747 | + for (i = 0; i < inputChildren.length; i++) { |
| 748 | + if (inputChildren[i].value === value) inputChildren[i].checked = true; |
725 | 749 | } |
726 | 750 | return this; |
727 | 751 | } else { |
728 | | - for (i = 0; i < length; i += 2) { |
729 | | - if (this.elt.childNodes[i].checked) |
730 | | - return this.elt.childNodes[i].value; |
| 752 | + for (i = 0; i < inputChildren.length; i++) { |
| 753 | + if (inputChildren[i].checked === true) return inputChildren[i].value; |
731 | 754 | } |
732 | 755 | } |
733 | 756 | }; |
734 | | - self.value = function() { |
735 | | - var i, |
736 | | - length = this.elt.childNodes.length; |
737 | | - if (arguments.length === 1) { |
738 | | - for (i = 0; i < length; i += 2) { |
739 | | - if (this.elt.childNodes[i].value === arguments[0]) |
740 | | - this.elt.childNodes[i].checked = true; |
| 757 | + self.value = function(value) { |
| 758 | + var i; |
| 759 | + var inputChildren = self._getInputChildrenArray(); |
| 760 | + if (value) { |
| 761 | + for (i = 0; i < inputChildren.length; i++) { |
| 762 | + if (inputChildren[i].value === value) inputChildren[i].checked = true; |
741 | 763 | } |
742 | 764 | return this; |
743 | 765 | } else { |
744 | | - for (i = 0; i < length; i += 2) { |
745 | | - if (this.elt.childNodes[i].checked) |
746 | | - return this.elt.childNodes[i].value; |
| 766 | + for (i = 0; i < inputChildren.length; i++) { |
| 767 | + if (inputChildren[i].checked === true) return inputChildren[i].value; |
747 | 768 | } |
748 | 769 | return ''; |
749 | 770 | } |
|
0 commit comments