define('jira/ajs/select/select-model', ['jira/ajs/control', 'jira/ajs/list/group-descriptor', 'jira/ajs/list/item-descriptor', 'jira/util/objects', 'jquery'], function (Control, GroupDescriptor, ItemDescriptor, Objects, jQuery) { 'use strict'; /** * Gives a JSON interface to a <select> list. Allowing you to add elements via JSON descriptors. It also * provides utility methods to retrieve collections of elements as JSON, for example selected options. * * @class SelectModel * @extends Control */ return Control.extend({ /** * Sets some defaults and parses all options, and option group into JSON. This json representation is bound * to its corresponding DOM element, and can be accessed using jQuery([OPTION OR OPTGROUP HERE]).data("descriptor") * * @constructs * @param {jQuery | Selector | HTMLElement} options - <select&rt; tag for the in dom representation of model */ init: function init(options) { var instance = this; var opts = {}; if (options) { if (options instanceof jQuery) { opts.element = options; } else if (options instanceof Element || typeof options === "string") { opts.element = jQuery(options); } else { opts = options; } } this._setOptions(opts); this.$element = this.options.element.hide(); this.type = this.$element.attr("multiple") ? "multiple" : "single"; this.id = this.$element.attr("id") || this.$element.attr("name"); if (this.type === "single") { this.$element.attr("multiple", "multiple"); } // provide a way for people to dynamically update/populate the this._parseDescriptors(); }, /** * * @param value * @returns {boolean} */ hasOptionByValue: function hasOptionByValue(value) { return this.$element.find('option[value="' + value + '"]').length > 0; }, /** * Returns text content of every selected option as array * * @returns {Array} */ getSelectedTexts: function getSelectedTexts() { var selectedTexts = []; var selected = this.getDisplayableSelectedDescriptors(); for (var i = 0; i < selected.length; i++) { selectedTexts.push(selected[i].label()); } return selectedTexts; }, /** * * @param {String} value * @param {String} [text] - text to put in if not existing option is added * @param {boolean} [hidden=false] */ putOption: function putOption(value, text, hidden) { var option; if (this.hasOptionByValue(value)) { var descriptor = this.getDescriptor(value); if (!text) { text = descriptor.label(); } option = descriptor.model(); } else { option = jQuery('").addClass(descriptor.styleClass()).attr("label", descriptor.label()); descriptor.model(elem); elem.data("descriptor", descriptor); if (descriptor.id()) { elem.attr("id", descriptor.id()); } return elem; } } }); }); AJS.namespace('AJS.SelectModel', null, require('jira/ajs/select/select-model'));