1 // $Id$ 2 3 /** 4 * Requires ajaxsolr.support.js. 5 */ 6 7 /** 8 * Accepts a container and returns a string of option tags. 9 * 10 * <p>If the container is an object, the object's properties serve as option 11 * values and the values of the object's properties serve as option text.</p> 12 * 13 * <p>If the container is an array: Given a container where the elements are 14 * two-element arrays, the first elements serve as option text and the second 15 * elements serve as option values.</p> 16 * 17 * <p>If <tt>selected</tt> is specified, the matching option value or element 18 * will get the selected option-tag. <tt>selected</tt> may also be an array of 19 * values to be selected when using a multiple select.</p> 20 * 21 * <p>From Ruby on Rails.</p> 22 * 23 * @param {Array|Object} container 24 * @param {Array|String} selected 25 * @returns {String} The option tags. 26 */ 27 AjaxSolr.theme.prototype.options_for_select = function (container, selected) { 28 var tags = []; 29 30 var options = []; 31 if (AjaxSolr.isArray(container)) { 32 options = container; 33 } 34 else { 35 for (var value in container) { 36 options.push([ container[value], value ]); 37 } 38 } 39 40 for (var i = 0, l = options.length; i < l; i++) { 41 var text, value; 42 43 if (AjaxSolr.isArray(options[i])) { 44 text = options[i][0].toString(), value = options[i][1].toString(); 45 } 46 else { 47 text = options[i].toString(), value = options[i].toString(); 48 } 49 50 var selectedAttribute = AjaxSolr.optionValueSelected(value, selected) ? ' selected="selected"' : ''; 51 tags.push('<option value="' + value.htmlEscape() +'"' + selectedAttribute + '>' + text.htmlEscape() + '</option>'); 52 } 53 54 return tags.join('\n'); 55 }; 56 57 /** 58 * <p>From Ruby on Rails.</p> 59 */ 60 AjaxSolr.theme.prototype.select_tag = function (name, optionTags, options) { 61 options = options || {}; 62 var htmlName = options.multiple && !name.endsWith('[]') ? name + '[]' : name; 63 options.name = options.name || htmlName; 64 options.id = options.id || name.sanitizeToId(); 65 return AjaxSolr.theme('content_tag_string', 'select', optionTags, options); 66 }; 67 68 /** 69 * <p>From Ruby on Rails.</p> 70 */ 71 AjaxSolr.theme.prototype.content_tag_string = function (name, content, options, escape) { 72 var tagOptions = ''; 73 74 if (escape === undefined) { 75 escape = true; 76 } 77 78 if (options) { 79 tagOptions = AjaxSolr.tagOptions(options, escape) 80 } 81 82 return '<' + name + tagOptions + '>' + content + '</' + name + '>'; 83 }; 84 85 /** 86 * <p>From Ruby on Rails.</p> 87 * 88 * @field 89 * @private 90 */ 91 AjaxSolr.booleanAttributes = [ 'disabled', 'readonly', 'multiple', 'checked' ]; 92 93 /** 94 * <p>From Ruby on Rails.</p> 95 * 96 * @static 97 */ 98 AjaxSolr.optionValueSelected = function (value, selected) { 99 if (AjaxSolr.isArray(selected)) { 100 return AjaxSolr.inArray(value, selected) != -1; 101 } 102 else { 103 return selected == value; 104 } 105 }; 106 107 /** 108 * <p>From Ruby on Rails.</p> 109 * 110 * @static 111 */ 112 AjaxSolr.tagOptions = function (options, escape) { 113 options = options || {}; 114 115 if (escape === undefined) { 116 escape = true; 117 } 118 119 var attrs = []; 120 121 if (escape) { 122 for (var key in options) { 123 if (AjaxSolr.inArray(key, AjaxSolr.booleanAttributes) != -1) { 124 if (options[key]) { 125 attrs.push(key + '="' + key + '"'); 126 } 127 } 128 else { 129 if (options[key]) { 130 attrs.push(key + '="' + options[key].escapeOnce() + '"'); 131 } 132 } 133 } 134 } 135 else { 136 for (var key in options) { 137 attrs.push(key + '="' + options[key] + '"'); 138 } 139 } 140 141 if (attrs.length) { 142 return ' ' + attrs.sort().join(' '); 143 } 144 145 return ''; 146 }; 147