<commit_before>###
 * coffeescript-ui - Coffeescript User Interface System (CUI)
 * Copyright (c) 2013 - 2016 Programmfabrik GmbH
 * MIT Licence
 * https://github.com/programmfabrik/coffeescript-ui, http://www.coffeescript-ui.org
###

class CUI.Select extends CUI.Checkbox
	@defaults:
		not_found: "- not found -"

	initOpts: ->
		super()
		@addOpts
			empty_text:
				check: String
			not_found_text:
				default: CUI.Select.defaults.not_found
				check: String
			options:
				mandatory: true
				check: (v) ->
					CUI.util.isArray(v) or CUI.util.isFunction(v)
			#group can be used for buttonbars to specify a group css style
			group:
				check: String
			onShow:
				check: Function
			onHide:
				check: Function
			menu_class:
				check: String


	init: ->
		@__value = null
		if not CUI.util.isFunction(@_options)
			@__loadOptions()

		# @DOM.prop("title", @getName()+":"+@__uniqueId)

	disable: (@__wasAutomaticDisabled = false) ->
		super()

	isDisabled: ->
		if @__wasAutomaticDisabled
			false
		else
			super()

	enableOption: (value) ->
		opt = @__getOptionByValue(value)
		if not opt
			return
		opt.disabled = false
		@displayValue()

	disableOption: (value) ->
		opt = @__getOptionByValue(value)
		if not opt
			return
		opt.disabled = true
		@displayValue()

	__getOptionByValue: (value) ->
		for opt in @getOptions()
			if opt.value == value
				return opt
		return null

	enable: (automatic = false) ->
		if automatic
			if @__wasAutomaticDisabled
				super()
			else
				# the select was disabled automatically but
				# during the deferred, disabled manually
				# so we ignore the automatic enable
		else
			super()

	__loadOptions: (event) ->
		if @__optionsPromise?.state() == "pending"
			return @__optionsPromise

		ret = @getArrayFromOpt("options", event, true)

		if CUI.util.isPromise(ret)
			@__optionsPromise = ret
			btn = @getButton()

			if btn
				icon_right = btn.getIconRight()
				btn.setIconRight("spinner")

				@__optionsPromise.always =>
					btn.setIconRight(icon_right)
		else
			@__optionsPromise = CUI.resolvedPromise(ret)

		# if not @isDisabled()
		# 	@disable(true)
		# 	@__optionsPromise.always =>
		# 		@enable(true)

		@__optionsPromise.done (@__options) =>
			first_value_opt = undefined

			# adjust options, so we always have a text and value
			for opt, idx in @__options
				opt._idx = idx
				if CUI.util.isUndef(opt.text) and not CUI.util.isUndef(opt.value) and not opt.icon
					opt.text = ""+opt.value

				if CUI.util.isUndef(opt.value) and not CUI.util.isUndef(opt.text)
					opt.value = opt.text

				if not CUI.util.isUndef(opt.value) and first_value_opt == undefined
					first_value_opt = opt

				if @_ui
					uiValue = opt.value
					if CUI.util.isPlainObject(opt.value)
						uiValue = idx
					opt.ui = "#{@_ui}.option:#{uiValue}"

			# auto - select first opt, if value unset
			# and no empty text allowed
			@_default_opt = undefined
			if first_value_opt
				@default_opt = first_value_opt<commit_msg>Select: Fixed the UI attribute when the value is an object.<commit_after>###
 * coffeescript-ui - Coffeescript User Interface System (CUI)
 * Copyright (c) 2013 - 2016 Programmfabrik GmbH
 * MIT Licence
 * https://github.com/programmfabrik/coffeescript-ui, http://www.coffeescript-ui.org
###

class CUI.Select extends CUI.Checkbox
	@defaults:
		not_found: "- not found -"

	initOpts: ->
		super()
		@addOpts
			empty_text:
				check: String
			not_found_text:
				default: CUI.Select.defaults.not_found
				check: String
			options:
				mandatory: true
				check: (v) ->
					CUI.util.isArray(v) or CUI.util.isFunction(v)
			#group can be used for buttonbars to specify a group css style
			group:
				check: String
			onShow:
				check: Function
			onHide:
				check: Function
			menu_class:
				check: String


	init: ->
		@__value = null
		if not CUI.util.isFunction(@_options)
			@__loadOptions()

		# @DOM.prop("title", @getName()+":"+@__uniqueId)

	disable: (@__wasAutomaticDisabled = false) ->
		super()

	isDisabled: ->
		if @__wasAutomaticDisabled
			false
		else
			super()

	enableOption: (value) ->
		opt = @__getOptionByValue(value)
		if not opt
			return
		opt.disabled = false
		@displayValue()

	disableOption: (value) ->
		opt = @__getOptionByValue(value)
		if not opt
			return
		opt.disabled = true
		@displayValue()

	__getOptionByValue: (value) ->
		for opt in @getOptions()
			if opt.value == value
				return opt
		return null

	enable: (automatic = false) ->
		if automatic
			if @__wasAutomaticDisabled
				super()
			else
				# the select was disabled automatically but
				# during the deferred, disabled manually
				# so we ignore the automatic enable
		else
			super()

	__loadOptions: (event) ->
		if @__optionsPromise?.state() == "pending"
			return @__optionsPromise

		ret = @getArrayFromOpt("options", event, true)

		if CUI.util.isPromise(ret)
			@__optionsPromise = ret
			btn = @getButton()

			if btn
				icon_right = btn.getIconRight()
				btn.setIconRight("spinner")

				@__optionsPromise.always =>
					btn.setIconRight(icon_right)
		else
			@__optionsPromise = CUI.resolvedPromise(ret)

		# if not @isDisabled()
		# 	@disable(true)
		# 	@__optionsPromise.always =>
		# 		@enable(true)

		@__optionsPromise.done (@__options) =>
			first_value_opt = undefined

			# adjust options, so we always have a text and value
			for opt, idx in @__options
				opt._idx = idx
				if CUI.util.isUndef(opt.text) and not CUI.util.isUndef(opt.value) and not opt.icon
					opt.text = ""+opt.value

				if CUI.util.isUndef(opt.value) and not CUI.util.isUndef(opt.text)
					opt.value = opt.text

				if not CUI.util.isUndef(opt.value) and first_value_opt == undefined
					first_value_opt = opt

				if @_ui
					uiValue = opt.value
					if not CUI.util.isInteger(opt.value) and not CUI.util.isString(opt.value)
						uiValue = idx
					opt.ui = "#{@_ui}.option:#{uiValue}"

			# auto - select first opt, if value unset
			# and no empty text allowed
			@_default_opt = undefined
			if first_value_opt
				@default_opt = first_value_opt
{'fraction_non_alphanumeric': 0.11140539645754015, 'fraction_numerical': 0.002648568117861281, 'mean_word_length': 3.1411925976696367, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3202672', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2044, 'n_words': 527}