2019-01-28 09:05:56 -05:00
|
|
|
require_relative 'wait_for_requests'
|
|
|
|
|
2013-07-29 06:46:00 -04:00
|
|
|
# Select2 ajax programmatic helper
|
2013-03-14 04:16:27 -04:00
|
|
|
# It allows you to select value from select2
|
|
|
|
#
|
|
|
|
# Params
|
|
|
|
# value - real value of selected item
|
|
|
|
# opts - options containing css selector
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# select2(2, from: '#user_ids')
|
|
|
|
#
|
|
|
|
|
|
|
|
module Select2Helper
|
2019-01-25 19:39:28 -05:00
|
|
|
include WaitForRequests
|
|
|
|
|
2016-08-05 22:03:01 -04:00
|
|
|
def select2(value, options = {})
|
2017-02-22 12:25:50 -05:00
|
|
|
raise ArgumentError, 'options must be a Hash' unless options.is_a?(Hash)
|
2013-03-14 04:16:27 -04:00
|
|
|
|
2019-01-25 19:39:28 -05:00
|
|
|
wait_for_requests unless options[:async]
|
|
|
|
|
2015-06-22 14:52:41 -04:00
|
|
|
selector = options.fetch(:from)
|
2013-03-14 04:16:27 -04:00
|
|
|
|
2017-10-16 13:37:52 -04:00
|
|
|
first(selector, visible: false)
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2013-03-14 04:16:27 -04:00
|
|
|
if options[:multiple]
|
2016-10-20 04:48:38 -04:00
|
|
|
execute_script("$('#{selector}').select2('val', ['#{value}']).trigger('change');")
|
2013-03-14 04:16:27 -04:00
|
|
|
else
|
2016-10-20 04:48:38 -04:00
|
|
|
execute_script("$('#{selector}').select2('val', '#{value}').trigger('change');")
|
2013-03-14 04:16:27 -04:00
|
|
|
end
|
|
|
|
end
|
2017-03-30 08:14:32 -04:00
|
|
|
|
|
|
|
def open_select2(selector)
|
|
|
|
execute_script("$('#{selector}').select2('open');")
|
|
|
|
end
|
|
|
|
|
|
|
|
def scroll_select2_to_bottom(selector)
|
|
|
|
evaluate_script "$('#{selector}').scrollTop($('#{selector}')[0].scrollHeight); $('#{selector}');"
|
|
|
|
end
|
2013-03-14 04:16:27 -04:00
|
|
|
end
|