// $Id$ /** * Requires ajaxsolr.support.js. */ /** * Accepts a container and returns a string of option tags. * *

If the container is an object, the object's properties serve as option * values and the values of the object's properties serve as option text.

* *

If the container is an array: Given a container where the elements are * two-element arrays, the first elements serve as option text and the second * elements serve as option values.

* *

If selected is specified, the matching option value or element * will get the selected option-tag. selected may also be an array of * values to be selected when using a multiple select.

* *

From Ruby on Rails.

* * @param {Array|Object} container * @param {Array|String} selected * @returns {String} The option tags. */ AjaxSolr.theme.prototype.options_for_select = function (container, selected) { var tags = []; var options = []; if (AjaxSolr.isArray(container)) { options = container; } else { for (var value in container) { options.push([ container[value], value ]); } } for (var i = 0, l = options.length; i < l; i++) { var text, value; if (AjaxSolr.isArray(options[i])) { text = options[i][0].toString(), value = options[i][1].toString(); } else { text = options[i].toString(), value = options[i].toString(); } var selectedAttribute = AjaxSolr.optionValueSelected(value, selected) ? ' selected="selected"' : ''; tags.push(''); } return tags.join('\n'); }; /** *

From Ruby on Rails.

*/ AjaxSolr.theme.prototype.select_tag = function (name, optionTags, options) { options = options || {}; var htmlName = options.multiple && !name.endsWith('[]') ? name + '[]' : name; options.name = options.name || htmlName; options.id = options.id || name.sanitizeToId(); return AjaxSolr.theme('content_tag_string', 'select', optionTags, options); }; /** *

From Ruby on Rails.

*/ AjaxSolr.theme.prototype.content_tag_string = function (name, content, options, escape) { var tagOptions = ''; if (escape === undefined) { escape = true; } if (options) { tagOptions = AjaxSolr.tagOptions(options, escape) } return '<' + name + tagOptions + '>' + content + ''; }; /** *

From Ruby on Rails.

* * @field * @private */ AjaxSolr.booleanAttributes = [ 'disabled', 'readonly', 'multiple', 'checked' ]; /** *

From Ruby on Rails.

* * @static */ AjaxSolr.optionValueSelected = function (value, selected) { if (AjaxSolr.isArray(selected)) { return AjaxSolr.inArray(value, selected) != -1; } else { return selected == value; } }; /** *

From Ruby on Rails.

* * @static */ AjaxSolr.tagOptions = function (options, escape) { options = options || {}; if (escape === undefined) { escape = true; } var attrs = []; if (escape) { for (var key in options) { if (AjaxSolr.inArray(key, AjaxSolr.booleanAttributes) != -1) { if (options[key]) { attrs.push(key + '="' + key + '"'); } } else { if (options[key]) { attrs.push(key + '="' + options[key].escapeOnce() + '"'); } } } } else { for (var key in options) { attrs.push(key + '="' + options[key] + '"'); } } if (attrs.length) { return ' ' + attrs.sort().join(' '); } return ''; };