Add default_time_settings feature.

This commit is contained in:
BoHung Chiu 2021-10-23 10:34:18 +08:00
parent a6caffd67f
commit 74881746ab
7 changed files with 175 additions and 6 deletions

View File

@ -131,8 +131,27 @@ class Admin::PropertyHiresController < OrbitAdminController
@settings = PropertyHireSetting.first @settings = PropertyHireSetting.first
end end
if request.request_method == "PATCH" if request.request_method == "PATCH"
@settings.update_attributes(settings_params) @settings.update_attributes(settings_params.except("property_day_settings"))
@settings.save @settings.save
if settings_params["property_day_settings"]
property_day_settings = settings_params["property_day_settings"].values
property_day_settings.each do |setting|
setting["property_id"] = "default_settings"
s = nil
if setting["id"].present?
s = PropertyDaySetting.where(:id=>setting["id"]).first
end
if s
if setting["_destroy"] == "1"
s.destroy
else
s.update_attributes(setting.except("id","_destroy"))
end
else
s = PropertyDaySetting.create(setting.except("id","_destroy"))
end
end
end
@saved = true @saved = true
else else
@saved = false @saved = false

View File

@ -14,4 +14,7 @@ class PropertyHireSetting
after_save do after_save do
Property.init_class_variables Property.init_class_variables
end end
def default_time_settings
PropertyDaySetting.where(:property_id=>"default_settings").asc(:key)
end
end end

View File

@ -60,6 +60,9 @@
.card-header h4 { .card-header h4 {
margin: 0; margin: 0;
} }
.apply_default_time_settings{
margin-bottom: 1em;
}
</style> </style>
<% content_for :page_specific_css do %> <% content_for :page_specific_css do %>
<%= stylesheet_link_tag "lib/main-forms" %> <%= stylesheet_link_tag "lib/main-forms" %>
@ -438,6 +441,9 @@
</div> </div>
<div class="collapse" id="<%=weekday%>_setting"> <div class="collapse" id="<%=weekday%>_setting">
<div class="card card-body"> <div class="card card-body">
<div>
<button type="button" class="btn btn-primary apply_default_time_settings pull-right" data-target="#add_target_weekday_<%=i%>" data-count="0" data-day="<%=i%>"><%=t("property_hire.apply_default_time_settings")%></button>
</div>
<div id="add_target_weekday_<%=i%>"> <div id="add_target_weekday_<%=i%>">
<% if all_day_settings[i.to_s] %> <% if all_day_settings[i.to_s] %>
<% all_day_settings[i.to_s].each_with_index do |setting,j| %> <% all_day_settings[i.to_s].each_with_index do |setting,j| %>
@ -765,9 +771,59 @@
} }
}) })
var setting_count = <%=setting_count%>; var setting_count = <%=setting_count%>;
var default_time_settings = <%= PropertyHireSetting.first.default_time_settings.map{|s| s.attributes.except("_id","created_at","updated_at")}.to_s.gsub("=>",": ").html_safe rescue "[]" %>
$(".apply_default_time_settings").click(function(){
var target = $($(this).data("target"));
var key = $(this).data("count");
var day = $(this).data("day");
var new_key = $(this).prev().attr('value');
var old_key = new RegExp("new_key", "g");
var new_day = day;
var old_day = new RegExp("new_day", "g");
var new_key = key;
target.empty();
default_time_settings.forEach(function(setting){
var old_index = new RegExp("new_index", "g");
var new_index = setting_count;
setting_count += 1;
<% property_day_setting = f.object.property_day_settings.new(:id=>nil) %>
<%= f.fields_for :property_day_settings,property_day_setting,:child_index => "new_index" do |f| %>
var template_html = "<%= escape_javascript(render(:partial=>"time_form",:locals=>{:f=>f})) %>";
<%end%>
var tmp = $(template_html.replace(old_index,new_index).replace(old_key,new_key).replace(old_day,new_day));
Object.keys(setting).forEach(function(k){
if(tmp.find("[name*='["+k+"]'").length != 0){
tmp.find("[name*='["+k+"]']").val(setting[k]);
console.log([k,setting[k]])
}
});
target.append(tmp);
key += 1;
})
$(this).data("count",key);
target.find("input[data-format]").each(function(i,input){
var $input = $(input);
var format = $input.data("format"),
timeOnly = !(format.match(/Y|M|d/)), timeFormat, dateFormat = "";
if(timeOnly){
timeFormat = format;
}else{
dateFormat = format.match(/yy(\/|-|)(mm|)(\/|-|)(dd|)/i)[0];
timeFormat = $.trim(format.replace(dateFormat,""));
}
var options = {dateFormat: dateFormat,timeFormat: timeFormat,timeOnly: timeOnly};
var additionalOptions = $input.data();
$.extend(options, additionalOptions);
$input.ui_datetimepicker(options);
$input.siblings('.clearDate').click(function(){
$input.val('');
$input.trigger('change');
});
})
})
$(".add_weekday_setting").click(function(){ $(".add_weekday_setting").click(function(){
var target = $($(this).data("target")); var target = $($(this).data("target"));
var key = $(this).data("key"); var key = $(this).data("count");
var day = $(this).data("day"); var day = $(this).data("day");
var new_key = $(this).prev().attr('value'); var new_key = $(this).prev().attr('value');
var old_key = new RegExp("new_key", "g"); var old_key = new RegExp("new_key", "g");
@ -775,7 +831,7 @@
var old_day = new RegExp("new_day", "g"); var old_day = new RegExp("new_day", "g");
var new_key = key; var new_key = key;
key += 1; key += 1;
$(this).data("key",key); $(this).data("count",key);
var old_index = new RegExp("new_index", "g"); var old_index = new RegExp("new_index", "g");
var new_index = setting_count; var new_index = setting_count;
setting_count += 1; setting_count += 1;

View File

@ -24,13 +24,13 @@
<div class="control-group"> <div class="control-group">
<%= f.label :start_time, t("property_hire.limit_start_time"), :class => "control-label muted" %> <%= f.label :start_time, t("property_hire.limit_start_time"), :class => "control-label muted" %>
<div class="controls"> <div class="controls">
<%= f.datetime_picker :start_time, :picker_type => "time", :no_label => true, :new_record => @property.new_record? %> <%= f.datetime_picker :start_time, :picker_type => "time", :no_label => true, :new_record => (@property.new_record? rescue true) %>
</div> </div>
</div> </div>
<div class="control-group"> <div class="control-group">
<%= f.label :end_time, t("property_hire.limit_end_time"), :class => "control-label muted" %> <%= f.label :end_time, t("property_hire.limit_end_time"), :class => "control-label muted" %>
<div class="controls"> <div class="controls">
<%= f.datetime_picker :end_time, :picker_type => "time", :no_label => true, :new_record => @property.new_record? %> <%= f.datetime_picker :end_time, :picker_type => "time", :no_label => true, :new_record => (@property.new_record? rescue true) %>
</div> </div>
</div> </div>
<%= f.hidden_field :key, :value => key %> <%= f.hidden_field :key, :value => key %>

View File

@ -6,6 +6,18 @@
<% content_for :page_specific_javascript do %> <% content_for :page_specific_javascript do %>
<%= javascript_include_tag "lib/module-area" %> <%= javascript_include_tag "lib/module-area" %>
<% end %> <% end %>
<style>
.time_setting_form, .image_group{
border: 0.2em solid #333;
padding-top: 0;
}
.add-on.iconbtn{
display: none;
}
.time_setting_form:last-child, .image_group:last-child {
margin-bottom: 1em;
}
</style>
<%= form_for @settings , :url => settings_admin_property_hires_path, html: {class: "form-horizontal main-forms"} do |f| %> <%= form_for @settings , :url => settings_admin_property_hires_path, html: {class: "form-horizontal main-forms"} do |f| %>
<fieldset> <fieldset>
<% if @saved %> <% if @saved %>
@ -54,11 +66,31 @@
</div> </div>
</div> </div>
<div class="control-group"> <div class="control-group">
<%= f.label :carousel_image_width, t("announcement.default_carousel_image_width"), :class => "control-label muted" %> <%= f.label :carousel_image_width, t("property_hire.default_carousel_image_width"), :class => "control-label muted" %>
<div class="controls"> <div class="controls">
<%= f.text_field :carousel_image_width %> <%= f.text_field :carousel_image_width %>
</div> </div>
</div> </div>
<div class="control-group">
<%= f.label :default_time_settings, t("property_hire.default_time_settings"), :class => "control-label muted" %>
<% default_time_settings = f.object.default_time_settings %>
<% setting_count = 0 %>
<div class="controls">
<div id="default_time_settings">
<% if default_time_settings %>
<% default_time_settings.each_with_index do |setting,j| %>
<%= f.fields_for :property_day_settings do |f| %>
<%= f.fields_for setting_count.to_s, setting do |f| %>
<%= render :partial => "time_form", :locals=>{:key=>j,:day=>0,:f=>f} %>
<% setting_count += 1 %>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
<a class="btn btn-primary add_weekday_setting" data-target="#default_time_settings" data-count="<%= default_time_settings.count %>" data-day="0"><%=t(:add)%></a>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>
@ -67,4 +99,59 @@
<%= f.submit t('submit'), class: 'btn btn-primary' %> <%= f.submit t('submit'), class: 'btn btn-primary' %>
</div> </div>
</fieldset> </fieldset>
<script>
var setting_count = <%=setting_count%>;
$(".add_weekday_setting").click(function(){
var target = $($(this).data("target"));
var key = $(this).data("count");
var day = $(this).data("day");
var new_key = $(this).prev().attr('value');
var old_key = new RegExp("new_key", "g");
var new_day = day;
var old_day = new RegExp("new_day", "g");
var new_key = key;
key += 1;
$(this).data("count",key);
var old_index = new RegExp("new_index", "g");
var new_index = setting_count;
setting_count += 1;
<% property_day_setting = PropertyDaySetting.new(:id=>nil,:property_id=>"default_settings") %>
<%= f.fields_for :property_day_settings do |f| %>
<%= f.fields_for "new_index",property_day_setting do |f| %>
var template_html = "<%= escape_javascript(render(:partial=>"time_form",:locals=>{:f=>f})) %>";
<%end%>
<%end%>
var tmp = $(template_html.replace(old_index,new_index).replace(old_key,new_key).replace(old_day,new_day));
target.append(tmp);
tmp.find("input[data-format]").each(function(i,input){
var $input = $(input);
var format = $input.data("format"),
timeOnly = !(format.match(/Y|M|d/)), timeFormat, dateFormat = "";
if(timeOnly){
timeFormat = format;
}else{
dateFormat = format.match(/yy(\/|-|)(mm|)(\/|-|)(dd|)/i)[0];
timeFormat = $.trim(format.replace(dateFormat,""));
}
var options = {dateFormat: dateFormat,timeFormat: timeFormat,timeOnly: timeOnly};
var additionalOptions = $input.data();
$.extend(options, additionalOptions);
$input.ui_datetimepicker(options);
$input.siblings('.clearDate').click(function(){
$input.val('');
$input.trigger('change');
});
})
})
$(document).on('click', '.setting-form-remove', function(){
if($(this).find(".remove_existing_record").length != 0){
if(confirm("<%= I18n.t(:sure?)%>")){
$(this).find('.should_destroy').attr('value', 1);
$(this).parents('.time_setting_form').hide();
}
}else{
$(this).parents('.time_setting_form').remove();
}
});
</script>
<% end %> <% end %>

View File

@ -5,6 +5,8 @@ en:
restful_actions: restful_actions:
fields_display_order: "Fields display order" fields_display_order: "Fields display order"
property_hire: property_hire:
apply_default_time_settings: Apply default time settings
default_time_settings: Default time settings
the_reservation_was_successfully_sent: "The reservation was successfully sent!Please see email to get more information" the_reservation_was_successfully_sent: "The reservation was successfully sent!Please see email to get more information"
go_to_infos_page: Go to infos page go_to_infos_page: Go to infos page
display_in_reason_for_hire: "Display in reason for hire" display_in_reason_for_hire: "Display in reason for hire"

View File

@ -5,6 +5,8 @@ zh_tw:
restful_actions: restful_actions:
fields_display_order: "欄位顯示順序" fields_display_order: "欄位顯示順序"
property_hire: property_hire:
apply_default_time_settings: 套用預設時段設定
default_time_settings: 預設時段設定
the_reservation_was_successfully_sent: "預約成功送出請查看Email確認預約資訊!" the_reservation_was_successfully_sent: "預約成功送出請查看Email確認預約資訊!"
go_to_infos_page: 前往資訊頁面 go_to_infos_page: 前往資訊頁面
display_in_reason_for_hire: "顯示於使用用途" display_in_reason_for_hire: "顯示於使用用途"