diff --git a/app/assets/javascripts/recurring_select_dialog.js.coffee.erb b/app/assets/javascripts/recurring_select_dialog.js.coffee.erb index baeccd6a..c05c9c2a 100644 --- a/app/assets/javascripts/recurring_select_dialog.js.coffee.erb +++ b/app/assets/javascripts/recurring_select_dialog.js.coffee.erb @@ -1,75 +1,61 @@ -window.RecurringSelectDialog = - class RecurringSelectDialog - constructor: (@recurring_selector) -> - @current_rule = @recurring_selector.recurring_select('current_rule') - @initDialogBox() +window.RecurringSelectComponent = + class RecurringSelectComponent + constructor: (@opts) -> + if @opts.value && @opts.value.val() + @current_rule = { hash : JSON.parse(@opts.value.val()) } + else + @current_rule = { hash: {}, str: '' } + + @render() if not @current_rule.hash? or not @current_rule.hash.rule_type? @freqChanged() - else - setTimeout @positionDialogVert, 10 # allow initial render - initDialogBox: -> - $(".rs_dialog_holder").remove() + render: -> + $(".recurring-component-container").remove() - open_in = $("body") - open_in = $(".ui-page-active") if $(".ui-page-active").length + open_in = $(@opts.container) open_in.append @template() - @outer_holder = $(".rs_dialog_holder") - @inner_holder = @outer_holder.find ".rs_dialog" - @content = @outer_holder.find ".rs_dialog_content" - @positionDialogVert(true) + @outer_holder = $(".recurring-component-container") + @content = @outer_holder.find ".recurring-component-content" + @content.hide() + @trigger = @outer_holder.find('.recurring-component-trigger-input') + @end_date_selects = @outer_holder.find('.recurring-component-end-date select') @mainEventInit() @freqInit() @summaryInit() - @outer_holder.trigger "recurring_select:dialog_opened" @freq_select.focus() - - positionDialogVert: (initial_positioning) => - window_height = $(window).height() - window_width = $(window).width() - dialog_height = @content.outerHeight() - if dialog_height < 80 - dialog_height = 80 - margin_top = (window_height - dialog_height)/2 - 30 - margin_top = 10 if margin_top < 10 - # if dialog_height > window_height - 20 - # dialog_height = window_height - 20 - - new_style_hash = - "margin-top" : margin_top+"px" - "min-height" : dialog_height+"px" - - if initial_positioning? - @inner_holder.css new_style_hash - @inner_holder.trigger "recurring_select:dialog_positioned" - else - @inner_holder.addClass "animated" - @inner_holder.animate new_style_hash, 200, => - @inner_holder.removeClass "animated" - @content.css {"width": "auto"} - @inner_holder.trigger "recurring_select:dialog_positioned" + @initUntilSelects() cancel: => @outer_holder.remove() - @recurring_selector.recurring_select('cancel') outerCancel: (event) => - if $(event.target).hasClass("rs_dialog_holder") + if $(event.target).hasClass("recurring-component-container") @cancel() save: => return if !@current_rule.str? - @outer_holder.remove() - @recurring_selector.recurring_select('save', @current_rule) - + @persistToForm() + + persistToForm: => + new_rule_hash_json = JSON.stringify(@current_rule.hash) + @opts.value.val(new_rule_hash_json) + + initUntilSelects: => + defaultDate = new Date + @outer_holder.find('.recurring-component-end-date-year').val(defaultDate.getFullYear()) + @outer_holder.find('.recurring-component-end-date-month').val(defaultDate.getMonth()) + @outer_holder.find('.recurring-component-end-date-day').val(defaultDate.getDate()) + @untilChanged() # ========================= Init Methods =============================== mainEventInit: -> # Tap hooks are for jQueryMobile @outer_holder.on 'click tap', @outerCancel - @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 + @trigger.click( => @content.toggle() ) + @end_date_selects.on('change', @untilChanged) + freqInit: -> @freq_select = @outer_holder.find ".rs_frequency" @@ -148,18 +134,19 @@ window.RecurringSelectDialog = # ========================= render methods =============================== + update: => + @summaryUpdate() + summaryUpdate: (new_string) => @summary.width @content.width() if @current_rule.hash? && @current_rule.str? @summary.removeClass "fetching" - @save_button.removeClass("disabled") rule_str = @current_rule.str.replace("*", "") if rule_str.length < 20 rule_str = "#{$.fn.recurring_select.texts["summary"]}: "+rule_str @summary.find("span").html rule_str else @summary.addClass "fetching" - @save_button.addClass("disabled") @summary.find("span").html "" @summaryFetch() @@ -176,6 +163,7 @@ window.RecurringSelectDialog = @current_rule.str = data @summaryUpdate() @content.css {"width": "auto"} + @save() init_calendar_days: (section) => monthly_calendar = section.find(".rs_calendar_day") @@ -249,8 +237,6 @@ window.RecurringSelectDialog = @current_rule.hash.rule_type = "IceCube::DailyRule" @current_rule.str = $.fn.recurring_select.texts["daily"] @initDailyOptions() - @summaryUpdate() - @positionDialogVert() intervalChanged: (event) => @current_rule.str = null @@ -258,7 +244,17 @@ window.RecurringSelectDialog = @current_rule.hash.interval = parseInt($(event.currentTarget).val()) if @current_rule.hash.interval < 1 || isNaN(@current_rule.hash.interval) @current_rule.hash.interval = 1 - @summaryUpdate() + @update() + + untilChanged: () => + year = +@outer_holder.find('.recurring-component-end-date-year').val() + month = +@outer_holder.find('.recurring-component-end-date-month').val() + day = +@outer_holder.find('.recurring-component-end-date-day').val() + + @current_rule.str = null + @current_rule.hash ||= {} + @current_rule.hash.until = (new Date(year, month - 1, day + 1)).toISOString(); + @update() daysChanged: (event) => $(event.currentTarget).toggleClass("selected") @@ -267,7 +263,7 @@ window.RecurringSelectDialog = @current_rule.hash.validations = {} raw_days = @content.find(".day_holder a.selected").map -> parseInt($(this).data("value")) @current_rule.hash.validations.day = raw_days.get() - @summaryUpdate() + @update() false # this prevents default and propogation dateOfMonthChanged: (event) => @@ -280,7 +276,7 @@ window.RecurringSelectDialog = res @current_rule.hash.validations.day_of_week = {} @current_rule.hash.validations.day_of_month = raw_days.get() - @summaryUpdate() + @update() false weekOfMonthChanged: (event) => @@ -295,78 +291,138 @@ window.RecurringSelectDialog = instance = parseInt($(elm).attr("instance")) @current_rule.hash.validations.day_of_week[day] ||= [] @current_rule.hash.validations.day_of_week[day].push instance - @summaryUpdate() + @update() false # ========================= Change callbacks =============================== template: () -> str = " -
- - +
+ + +
++ + +
+ ++ <%= f.label :start_date, "Start date" %> + <%= f.date_select :start_date, input_html: {class: 'select2'} %> +
++ <%= f.label :start_time, "Start time" %> + <%= f.time_select :start_time, input_html: {class: 'select2'} %> +
++ <%= f.label :end_time, "End time" %> + <%= f.time_select :end_time, input_html: {class: 'select2'} %> +
+ ++ <%= f.hidden_field :repeat_rule, class: 'appointment-system-repeat-rule-value', value: '' %> +
+ + + ++ <%= f.submit %> +
+ + +<% end %> + + \ No newline at end of file diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index 0eae5fa1..bc6dbb7c 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -1,3 +1,5 @@ Dummy::Application.routes.draw do root to: 'sample#index' + + get '/jt', to: 'sample#jt' end