diff options
| author | jpl | 2017-04-07 11:16:04 +0200 |
|---|---|---|
| committer | jpl | 2017-04-07 11:17:41 +0200 |
| commit | 97fa0c2405f23e701166e5c56eea684928b92373 (patch) | |
| tree | c623c4160374607ada39048fc92d5a0854ba50d7 | |
| parent | 7ac766fb016108429730248f1a8bfd5065e8c31b (diff) | |
| download | chouette-core-97fa0c2405f23e701166e5c56eea684928b92373.tar.bz2 | |
Refs #3076: mod calendar helper for timetables#show
| -rw-r--r-- | app/assets/stylesheets/components/_calendar.sass | 13 | ||||
| -rw-r--r-- | app/helpers/time_tables_helper.rb | 165 | ||||
| -rw-r--r-- | app/views/time_tables/_show_time_table.html.slim | 10 | ||||
| -rw-r--r-- | app/views/time_tables/show.html.slim | 4 | ||||
| -rw-r--r-- | db/schema.rb | 18 |
5 files changed, 199 insertions, 11 deletions
diff --git a/app/assets/stylesheets/components/_calendar.sass b/app/assets/stylesheets/components/_calendar.sass new file mode 100644 index 000000000..9f9330738 --- /dev/null +++ b/app/assets/stylesheets/components/_calendar.sass @@ -0,0 +1,13 @@ +//-----------// +// CALENDARS // +//-----------// + +table.calendar + display: table + width: 100% + table-layout: fixed + margin-bottom: 20px + + th, td + border: 1px solid + text-align: center diff --git a/app/helpers/time_tables_helper.rb b/app/helpers/time_tables_helper.rb index b1175d359..267c896bd 100644 --- a/app/helpers/time_tables_helper.rb +++ b/app/helpers/time_tables_helper.rb @@ -1,3 +1,5 @@ +require 'date' + module TimeTablesHelper def month_periode_enum(years) @@ -5,5 +7,166 @@ module TimeTablesHelper end_date = Date.today + years.years (start_date..end_date).map(&:beginning_of_month).uniq.map(&:to_s) end -end + def new_alt_calendar(options = {}, &block) + raise(ArgumentError, "No year given") unless options.has_key?(:year) + raise(ArgumentError, "No month given") unless options.has_key?(:month) + + block ||= Proc.new {|d| nil} + + month_names = (!defined?(I18n) || I18n.t("date.month_names").include?("missing")) ? Date::MONTHNAMES.dup : I18n.t("date.month_names") + + defaults = { + :table_id => "calendar-#{options[:year]}-#{"%02d" % options[:month]}", + :table_class => 'calendar', + :month_name_class => 'monthName', + :other_month_class => 'otherMonth', + :day_name_class => 'dayName', + :day_class => 'day', + :abbrev => true, + :first_day_of_week => 0, + :accessible => false, + :show_today => true, + :previous_month_text => nil, + :next_month_text => nil, + :month_header => true, + :calendar_title => month_names[options[:month]], + :summary => "Calendar for #{month_names[options[:month]]} #{options[:year]}" + } + options = defaults.merge options + + first = Date.civil(options[:year], options[:month], 1) + last = Date.civil(options[:year], options[:month], -1) + + first_weekday = first_day_of_week(options[:first_day_of_week]) + last_weekday = last_day_of_week(options[:first_day_of_week]) + + day_names = (!defined?(I18n) || I18n.t("date.day_names").include?("missing")) ? Date::DAYNAMES : I18n.t("date.day_names") + abbr_day_names = (!defined?(I18n) || I18n.t("date.abbr_day_names").include?("missing")) ? Date::ABBR_DAYNAMES : I18n.t("date.abbr_day_names") + week_days = (0..6).to_a + first_weekday.times do + week_days.push(week_days.shift) + end + + # TODO Use some kind of builder instead of straight HTML + cal = %(<table id="#{options[:table_id]}" class="#{options[:table_class]}" border="0" cellspacing="0" cellpadding="0" summary="#{options[:summary]}">) + cal << %(<thead>) + + if (options[:month_header]) + cal << %(<tr>) + if options[:previous_month_text] or options[:next_month_text] + cal << %(<th colspan="2">#{options[:previous_month_text]}</th>) + colspan=3 + else + colspan=7 + end + cal << %(<th colspan="#{colspan}" class="#{options[:month_name_class]}">#{options[:calendar_title]}</th>) + cal << %(<th colspan="2">#{options[:next_month_text]}</th>) if options[:next_month_text] + cal << %(</tr>) + end + + cal << %(<tr class="#{options[:day_name_class]}">) + + week_days.each do |wday| + cal << %(<th id="#{th_id(Date::DAYNAMES[wday], options[:table_id])}" scope="col">) + cal << (options[:abbrev] ? %(<abbr title="#{day_names[wday]}">#{t("calendars.days.#{Date::DAYNAMES[wday].downcase}")}</abbr>) : day_names[wday]) + cal << %(</th>) + end + + cal << "</tr></thead><tbody><tr>" + + # previous month + beginning_of_week(first, first_weekday).upto(first - 1) do |d| + cal << generate_other_month_cell(d, options) + end unless first.wday == first_weekday + + first.upto(last) do |cur| + cell_text, cell_attrs = block.call(cur) + cell_text ||= cur.mday + cell_attrs ||= {} + cell_attrs[:headers] = th_id(cur, options[:table_id]) + cell_attrs[:class] ||= options[:day_class] + cell_attrs[:class] += " weekendDay" if [0, 6].include?(cur.wday) + today = (Time.respond_to?(:zone) && !(zone = Time.zone).nil? ? zone.now.to_date : Date.today) + cell_attrs[:class] += " today" if (cur == today) and options[:show_today] + + cal << generate_cell(cell_text, cell_attrs) + cal << "</tr><tr>" if cur.wday == last_weekday + end + + # next month + (last + 1).upto(beginning_of_week(last + 7, first_weekday) - 1) do |d| + cal << generate_other_month_cell(d, options) + end unless last.wday == last_weekday + + cal << "</tr></tbody></table>" + cal.respond_to?(:html_safe) ? cal.html_safe : cal + end + + private + + def first_day_of_week(day) + day + end + + def last_day_of_week(day) + if day > 0 + day - 1 + else + 6 + end + end + + def days_between(first, second) + if first > second + second + (7 - first) + else + second - first + end + end + + def beginning_of_week(date, start = 1) + days_to_beg = days_between(start, date.wday) + date - days_to_beg + end + + def generate_cell(cell_text, cell_attrs) + cell_attrs = cell_attrs.map {|k, v| %(#{k}="#{v}") }.join(" ") + "<td #{cell_attrs}>#{cell_text}</td>" + end + + def generate_other_month_cell(date, options) + cell_attrs = {} + cell_attrs[:headers] = th_id(date, options[:table_id]) + cell_attrs[:class] = options[:other_month_class] + cell_attrs[:class] += " weekendDay" if weekend?(date) + + cell_text = date.day + if options[:accessible] + cell_text += %(<span class="hidden"> #{month_names[date.month]}</span>) + end + + generate_cell(date.day, cell_attrs) + end + + # Calculates id for th element. + # derived from calendar_id and dow. + # + # Params: + # `day` can be either Date or DOW('Sunday', 'Monday') + def th_id(day, calendar_id) + return th_id(Date::DAYNAMES[day.wday], calendar_id) if day.is_a?(Date) + "#{calendar_id}-#{day[0..2].downcase}" + end + + def weekend?(date) + [0, 6].include?(date.wday) + end + + class Engine < Rails::Engine # :nodoc: + ActiveSupport.on_load(:action_view) do + include CalendarHelper + end + end if defined? Rails::Engine + +end diff --git a/app/views/time_tables/_show_time_table.html.slim b/app/views/time_tables/_show_time_table.html.slim index f92f02b54..b1eeb690b 100644 --- a/app/views/time_tables/_show_time_table.html.slim +++ b/app/views/time_tables/_show_time_table.html.slim @@ -1,7 +1,6 @@ -.calendar_helper - - cal = "" - - (1..12).each do |month| - - cal << calendar(year: @year, month: month, first_day_of_week: 1, calendar_title: "#{I18n.t("date.month_names")[month]} #{@year}") do |d| +- (1..12).each do |month| + .col-lg-2.col-md-3.col-sm-3.col-xs-6 + = new_alt_calendar(year: @year, month: month, first_day_of_week: 1, calendar_title: "#{I18n.t("date.month_names")[month]} #{@year}") do |d| - if @time_table.excluded_date?(d) - [link_to(d.mday, edit_referential_time_table_path(@referential, @time_table) ), {class: "day excluded_date"}] - elsif @time_table.include_in_overlap_dates?(d) @@ -11,9 +10,6 @@ - elsif @time_table.include_in_periods?(d) - [link_to(d.mday, edit_referential_time_table_path(@referential, @time_table) ), {class: "day selected_period"}] - = cal.html_safe - - / wip - if @time_table.dates.where("in_out = true").present? h3.time_table_dates = @time_table.human_attribute_name("dates") diff --git a/app/views/time_tables/show.html.slim b/app/views/time_tables/show.html.slim index 54d26875e..18604dab9 100644 --- a/app/views/time_tables/show.html.slim +++ b/app/views/time_tables/show.html.slim @@ -43,9 +43,7 @@ = link_to '', referential_time_table_path(@referential, @time_table, year: (@year + 1)), class: 'next_page' .row - .col-lg-12 - / To update - = render 'show_time_table' + = render 'show_time_table' .row .col-lg-12 diff --git a/db/schema.rb b/db/schema.rb index 53e3ad57c..24bcf199c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -165,6 +165,22 @@ ActiveRecord::Schema.define(version: 20170404122930) do add_index "connection_links", ["objectid"], :name => "connection_links_objectid_key", :unique => true + create_table "delayed_jobs", force: true do |t| + t.integer "priority", default: 0 + t.integer "attempts", default: 0 + t.text "handler" + t.text "last_error" + t.datetime "run_at" + t.datetime "locked_at" + t.datetime "failed_at" + t.string "locked_by" + t.string "queue" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority" + create_table "exports", force: true do |t| t.integer "referential_id", limit: 8 t.string "status" @@ -825,6 +841,8 @@ ActiveRecord::Schema.define(version: 20170404122930) do add_index "workbenches", ["stop_area_referential_id"], :name => "index_workbenches_on_stop_area_referential_id" Foreigner.load + add_foreign_key "access_links", "access_points", name: "aclk_acpt_fkey", dependent: :delete + add_foreign_key "group_of_lines_lines", "group_of_lines", name: "groupofline_group_fkey", dependent: :delete add_foreign_key "journey_frequencies", "timebands", name: "journey_frequencies_timeband_id_fk", dependent: :nullify |
