Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ sass_version = ENV["SASS_VERSION"] || ">= 3.1"

# jquery-rails is used by the dummy application
gem "jquery-rails"
gem "jquery-ui-rails"
gem "pg"
gem "ice_cube"

Expand Down
68 changes: 62 additions & 6 deletions app/assets/javascripts/recurring_select_dialog.js.coffee.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ window.RecurringSelectDialog =
@mainEventInit()
@freqInit()
@summaryInit()
@untilInit()

@freq_select.focus()
@outer_holder.trigger "recurring_select:dialog_opened"

Expand Down Expand Up @@ -79,6 +81,7 @@ window.RecurringSelectDialog =
@content.on 'click tap', 'h1 a', @cancel
@save_button = @content.find('input.rs_save').on "click tap", @save
@content.find('input.rs_cancel').on "click tap", @cancel
@content.find('#indefinite').on 'change tap', @indefiniteChanged

freqInit: ->
@freq_select = @outer_holder.find ".rs_frequency"
Expand Down Expand Up @@ -181,6 +184,32 @@ window.RecurringSelectDialog =
@summaryUpdate()
@content.css {"width": "auto"}

untilInit: =>
untilFormat = @recurring_selector.data 'until-datepicker-format'
$until = @content.find('#until')

@setRuleUntil = (date) =>
@current_rule.hash ||= {}
@current_rule.str = null
# Convert to unix time and set hash
@current_rule.hash.until = $.datepicker.parseDate(untilFormat, date).getTime() / 1000 if date
@summaryUpdate()
false

@setDatepickerUntil = (unix_time) =>
console.log unix_time
$until.val $.datepicker.formatDate(untilFormat, $.datepicker.parseDate('@', unix_time * 1000))

$until.datepicker
dateFormat: untilFormat
onSelect: @setRuleUntil
.blur (e) =>
@setRuleUntil(e.currentTarget.value)

@setDatepickerUntil(@current_rule.hash.until) if @current_rule.hash?.until

@content.find('#indefinite').click() if @current_rule.hash?.until?

init_calendar_days: (section) =>
monthly_calendar = section.find(".rs_calendar_day")
monthly_calendar.html ""
Expand Down Expand Up @@ -223,32 +252,34 @@ window.RecurringSelectDialog =
# ========================= Change callbacks ===============================

freqChanged: =>
old_until = @current_rule.hash?.until
@current_rule.hash = null unless $.isPlainObject(@current_rule.hash) # for custom values

@current_rule.hash ||= {}
@current_rule.hash.until = old_until
@current_rule.hash.interval = 1
@current_rule.hash.until = null
@current_rule.hash.count = null
@current_rule.hash.validations = null
@content.find(".freq_option_section").hide();
@content.find("input[type=radio], input[type=checkbox]").prop("checked", false)
@content.find("input[type=radio], input[type=checkbox]:not(#indefinite)").prop("checked", false)
switch @freq_select.val()
when "Weekly"
@current_rule.hash.rule_type = "IceCube::WeeklyRule"
@current_rule.str = "Weekly"
#@current_rule.str = "Weekly"
@initWeeklyOptions()
when "Monthly"
@current_rule.hash.rule_type = "IceCube::MonthlyRule"
@current_rule.str = "Monthly"
#@current_rule.str = "Monthly"
@initMonthlyOptions()
when "Yearly"
@current_rule.hash.rule_type = "IceCube::YearlyRule"
@current_rule.str = "Yearly"
#@current_rule.str = "Yearly"
@initYearlyOptions()
else
@current_rule.hash.rule_type = "IceCube::DailyRule"
@current_rule.str = "Daily"
#@current_rule.str = "Daily"
@initDailyOptions()
@current_rule.str = null
@summaryUpdate()
@positionDialogVert()

Expand Down Expand Up @@ -299,3 +330,28 @@ window.RecurringSelectDialog =
@summaryUpdate()
false

indefiniteChanged: (event) =>
$el = $(event.currentTarget)
$untilSection = $el.parent().next('.until_input')
$untilInput = $untilSection.find '#until'
if $el.is ':checked'
$untilInput.val('').blur()
$untilSection.hide()
else
if @current_rule.hash?.until
@setDatepickerUntil(@current_rule.hash.until)
else
$untilInput.datepicker('setDate', @getUntilDefaultDate())
$untilInput.blur()
$untilSection.show()

getUntilDefaultDate: =>
today = new Date
year = today.getFullYear()
month = today.getMonth()
day = today.getDate()
switch @current_rule.hash.rule_type
when "IceCube::MonthlyRule" then new Date year + 1, month, day
when "IceCube::YearlyRule" then new Date year + 5, month, day
when "IceCube::WeeklyRule" then new Date year, month + 1, day
else new Date year, month, day + 7
10 changes: 10 additions & 0 deletions app/assets/templates/recurring_select/dialog_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ <h1>Repeat <a href='#' title='Cancel' Alt='Cancel'></a> </h1>
year(s)
</p>
</div>
<div class="until_option">
<p class="indefinite_input">
<input id="indefinite" type="checkbox" checked />
<label for="indefinite">Repeats indefinitely</label>
</p>
<p class="until_input" style="display:none">
Until
<input id="until" type='text' name='rs_until' size='8' />
</p>
</div>


<p class='rs_summary'>
Expand Down
11 changes: 6 additions & 5 deletions app/helpers/recurring_select_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@ def recurring_options_for_select(currently_selected_rule = nil, default_schedule
options_array << custom_label
end

options_for_select(options_array, currently_selected_rule.to_json)
options_for_select(options_array, RecurringSelect.rule_to_option_json(currently_selected_rule))
end

private

def ice_cube_rule_to_option(supplied_rule, custom = false)
return supplied_rule unless RecurringSelect.is_valid_rule?(supplied_rule)

rule = RecurringSelect.dirty_hash_to_rule(supplied_rule)
ar = [rule.to_s, rule.to_hash.to_json]
ar = [rule.to_s, RecurringSelect.rule_to_option_json(rule)]

if custom
ar[0] << "*"
Expand All @@ -75,7 +74,7 @@ class InstanceTag < ActionView::Helpers::InstanceTag
include FormOptionsHelper

def to_recurring_select_tag(default_schedules, options, html_options)
html_options = recurring_select_html_options(html_options)
html_options = recurring_select_html_options(options, html_options)
add_default_name_and_id(html_options)
value = value(object)
content_tag("select",
Expand All @@ -88,9 +87,11 @@ def to_recurring_select_tag(default_schedules, options, html_options)

private

def recurring_select_html_options(html_options)
def recurring_select_html_options(options, html_options)
html_options = html_options.stringify_keys
html_options["class"] = ((html_options["class"] || "").split() + ["recurring_select"]).join(" ")
until_datepicker_format = options[:until_datepicker_format] || 'yy-mm-dd'
(html_options["data"] ||= {}).reverse_merge!(:until_datepicker_format => until_datepicker_format)
html_options
end
end
Expand Down
16 changes: 15 additions & 1 deletion lib/recurring_select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def to_s
end

module RecurringSelect

def self.dirty_hash_to_rule(params)
if params.is_a? IceCube::Rule
params
Expand Down Expand Up @@ -45,13 +44,28 @@ def self.is_valid_rule?(possible_rule)
false #only a hash or a string of a hash can be valid
end

# Convert until_time into int for comparison
def self.rule_to_option_json(rule)
return rule.to_json unless self.is_valid_rule?(rule)
rule = self.dirty_hash_to_rule(rule)
rule.to_hash.tap {|hash| hash[:until] = rule.until_time.to_i if rule.until_time}.to_json
end

private

def self.filter_params(params)
params.reject!{|key, value| value.blank? || value=="null" }

params[:interval] = params[:interval].to_i if params[:interval]
params[:week_start] = params[:week_start].to_i if params[:week_start]
begin
if params[:until]
# Set to 23:59:59 (in current TZ) to encompass all events on until day
params[:until] = Time.zone.at(params[:until].to_i).change(hour: 23, min: 59, sec: 59)
end
rescue ArgumentError
# Invalid date given, attempt to assign :until will fail silently
end

params[:validations] ||= {}
params[:validations].symbolize_keys!
Expand Down
1 change: 1 addition & 0 deletions recurring_select.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Gem::Specification.new do |s|

s.add_dependency "rails", ">= 3.1"
s.add_dependency "jquery-rails"
s.add_dependency "jquery-ui-rails"
s.add_dependency "ice_cube", ">= 0.8"
s.add_dependency "sass-rails", ">= 3.1"
s.add_dependency "coffee-rails", ">= 3.1"
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
//
//= require jquery
//= require jquery_ujs
//= require jquery.ui.datepicker
//= require recurring_select
//= require_tree .
1 change: 1 addition & 0 deletions spec/dummy/app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require recurring_select
*= require jquery.ui.datepicker
*= require_tree .
*/

Expand Down