diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/assets/javascripts/select2.coffee | 58 | ||||
| -rw-r--r-- | app/models/time_table_combination.rb | 14 | ||||
| -rw-r--r-- | app/views/autocomplete_time_tables/index.rabl | 1 | ||||
| -rw-r--r-- | app/views/time_table_combinations/_form.html.slim | 7 |
4 files changed, 58 insertions, 22 deletions
diff --git a/app/assets/javascripts/select2.coffee b/app/assets/javascripts/select2.coffee index e8d9c313e..510e6fe9b 100644 --- a/app/assets/javascripts/select2.coffee +++ b/app/assets/javascripts/select2.coffee @@ -1,20 +1,52 @@ +bind_select2 = (el, cfg = {}) -> + target = $(el) + default_cfg = + theme: 'bootstrap' + language: 'fr' + placeholder: target.data('select2ed-placeholder') + allowClear: true + + target.select2 $.extend({}, default_cfg, cfg) + +bind_select2_ajax = (el, cfg = {}) -> + target = $(el) + cfg = + ajax: + data: (params) -> + q: + "#{target.data('term')}": params.term + url: target.data('url'), + dataType: 'json', + delay: 125, + processResults: (data, params) -> results: data + minimumInputLength: 3 + templateResult: eval(target.data('formater')) + templateSelection: (item) -> + item.text + + bind_select2(el, cfg) + +select2_time_table = (item) -> + return item.text if item.loading + wrap = $('<div>', "class":'select2-result clearfix') + wrap.html(["<h5>Time table : #{item.comment}</h5>"].join("\n")) + +select2_calendar = (item) -> + return item.text if item.loading + wrap = $('<div>', "class":'select2-result clearfix') + wrap.html(["<h5>Calendar : #{item.name}</h5>"].join("\n")) + + @select_2 = -> $("[data-select2ed='true']").each -> - target = $(this) - target.select2 - theme: 'bootstrap' - language: 'fr' - placeholder: target.data('select2ed-placeholder') - allowClear: true + bind_select2(this) + + $("[data-select2-ajax='true']").each -> + bind_select2_ajax(this) $('select.form-control.tags').each -> - target = $(this) - target.select2 - theme: 'bootstrap' - language: 'fr' - placeholder: target.data('select2ed-placeholder') - allowClear: true - tags: true + bind_select2(this, {tags: true}) + $(document).on 'turbolinks:load', select_2 diff --git a/app/models/time_table_combination.rb b/app/models/time_table_combination.rb index f9b63340a..d7e16665c 100644 --- a/app/models/time_table_combination.rb +++ b/app/models/time_table_combination.rb @@ -3,15 +3,20 @@ class TimeTableCombination include ActiveModel::Conversion extend ActiveModel::Naming - attr_accessor :source_id, :combined_type, :target_id, :operation + attr_accessor :source_id, :combined_type, :operation, :time_table_id, :calendar_id + + validates_presence_of :source_id, :combined_type, :operation + + validates :time_table_id, presence: true, if: "calendar_id.blank?" + validates :calendar_id, presence: true, if: "time_table_id.blank?" - validates_presence_of :source_id, :combined_type, :operation, :target_id validates_inclusion_of :operation, :in => %w(union intersection disjunction), :allow_nil => true validates_inclusion_of :combined_type, :in => %w(time_table calendar) def clean self.source_id = nil - self.target_id = nil + self.time_table_id = nil + self.calendar = nil self.combined_type = nil self.operation = nil self.errors.clear @@ -32,8 +37,9 @@ class TimeTableCombination end def target + id = self.send("#{combined_type}_id") klass = combined_type == 'calendar' ? Calendar : Chouette::TimeTable - target = klass.find target_id + target = klass.find id target = target.convert_to_time_table unless target.is_a? Chouette::TimeTable target end diff --git a/app/views/autocomplete_time_tables/index.rabl b/app/views/autocomplete_time_tables/index.rabl index 8ec7b314e..bd478301d 100644 --- a/app/views/autocomplete_time_tables/index.rabl +++ b/app/views/autocomplete_time_tables/index.rabl @@ -5,6 +5,7 @@ node do |time_table| :time_table_bounding => time_table.presenter.time_table_bounding, :composition_info => time_table.presenter.composition_info, :tags => time_table.tags.join(','), + :text => time_table.comment, :day_types => %w(monday tuesday wednesday thursday friday saturday sunday).select{ |d| time_table.send(d) }.map{ |d| time_table.human_attribute_name(d).first(2)}.join('')} end diff --git a/app/views/time_table_combinations/_form.html.slim b/app/views/time_table_combinations/_form.html.slim index 47a5cee73..8f4924bd3 100644 --- a/app/views/time_table_combinations/_form.html.slim +++ b/app/views/time_table_combinations/_form.html.slim @@ -8,11 +8,8 @@ h1 = @time_table.comment = form.input :operation, as: :select, collection: TimeTableCombination.operations - = form.input :target_id, as: :select, 'data-select2ed': 'true', collection: Chouette::TimeTable.all.map{|t| [t.comment, t.id]} + = form.input :time_table_id, as: :select, input_html: { data: { select2_ajax: 'true', term: 'comment_cont', url: referential_autocomplete_time_tables_path(@referential, format: :json), formater: 'select2_time_table'}} - = form.input :target_id, as: :select, 'data-select2ed': 'true', collection: Calendar.all.map{|t| [t.name, t.id]}, disabled: true + = form.input :calendar_id, as: :select, input_html: { data: { select2_ajax: 'true', term: 'name_cont', url: referential_autocomplete_time_tables_path(@referential, format: :json), formater: 'select2_calendar'}} = form.button :submit, t('actions.submit') - / = form.input :combined_id, :label => t('.time_tables'), as: :search_time_table, :json => referential_autocomplete_time_tables_path(@referential, :format => :json), hint_text: t('search_hint'), no_result_text: t('no_result_text'), :searching_text => t('searching_term'), :tokenLimit => 1 - - |
