Fix js not reload bug and refine constant variables.

This commit is contained in:
邱博亞 2024-08-22 07:59:37 +08:00
parent c049ad8050
commit 66fcc22d0f
5 changed files with 101 additions and 39 deletions

View File

@ -321,29 +321,23 @@ class Admin::Coloncancerpredict2sController < OrbitAdminController
@form_to_show.auto_write_predict_js
end
end
@index = 0
Dir.chdir("public") do
while File.exist?('cancerfield_back'+@index.to_s+'.txt')
@index += 1
end
end
Dir.chdir("public") do
@site_locales = Site.last.in_use_locales.each do |locale|
I18n.with_locale(locale) do
@file_tmp = File.new('colon_cancer_tool_table_tmp_'+locale.to_s+'2.txt', 'w')
File.open(Coloncancerpredictfields2s::ToolTableMap[I18n.locale], 'w') do |f|
tmp_table_texts = create_table(locale)
@file_tmp.write(tmp_table_texts)
@file_tmp.close
f.write(tmp_table_texts)
end
end
end
Dir.chdir("public") do
@file_back = File.open('colon_cancerfield_back'+@index.to_s+'2.txt', 'w')
@file_back.write(@form_to_show.attributes)
@file_back.close
@file_org = File.open('colon_cancerfield_org'+@index.to_s+'2.txt', 'w')
@file_org.write(Coloncancerpredictfields2s.where("title"=>@app_title+'_back').first.attributes)
@file_org.close
idx = 0
while File.exist?("public/colon_cancerfield2_back#{idx}.txt")
idx += 1
end
File.open("public/colon_cancerfield2_back#{idx}.txt", 'w') do |f|
f.write(@form_to_show.attributes)
end
File.open("public/colon_cancerfield2_org#{idx}.txt", 'w') do |f|
f.write(Coloncancerpredictfields1s.where("title"=>@app_title+'_back').first.attributes)
end
redirect_to admin_coloncancerpredict2s_path
end
@ -631,12 +625,10 @@ class Admin::Coloncancerpredict2sController < OrbitAdminController
#FileUtils.rm_r(dir_path, :force => true) if Dir.exist?(dir_path)
FileUtils.mkdir dir_path if !Dir.exist?(dir_path) #create dir for storing tmp_file if dir doesn't exist
@filename = ("#{Time.now.year}_%02s_%02s_export_cancer_tool_record.xlsx"%[Time.now.month,Time.now.day]).gsub(" ","0")
Dir.chdir(dir_path) do
File.open(@filename, 'w') do |f|
File.open("#{dir_path}#{@filename}", 'w') do |f|
f.write render_to_string( :handlers=> [:axlsx], :formats=> [:xlsx] ,:partial=> 'export_cancer_tool_record.xlsx',:locals=> {:results=>cancer_records} )
end
end
tmp_filename_data = File.read(dir_path +@filename)
tmp_filename_data = File.read("#{dir_path}#{@filename}")
send_data(tmp_filename_data, type: 'application/xlsx', disposition: 'attachment', filename: @filename)
end
def create_first_field
@ -651,13 +643,11 @@ class Admin::Coloncancerpredict2sController < OrbitAdminController
@form_to_show.title=@app_title
@form_to_show.save
@form_to_show = Coloncancerpredictfields2s.where("title"=>@app_title).first
Dir.chdir("public") do
@site_locales = Site.last.in_use_locales.each do |locale|
I18n.with_locale(locale) do
@file_tmp = File.new('colon_cancer_tool_table_tmp_'+locale.to_s+'2.txt', 'w')
File.open(Coloncancerpredictfields2s::ToolTableMap[I18n.locale], 'w') do |f|
tmp_table_texts = create_table(locale)
@file_tmp.write(tmp_table_texts)
@file_tmp.close
f.write(tmp_table_texts)
end
end
end

View File

@ -147,20 +147,40 @@ class Coloncancerpredict2sController < ApplicationController
uid = OrbitHelper.params[:uid] rescue ""
tags = OrbitHelper.widget_tags
categories = OrbitHelper.widget_categories || []
@table_str = File.read('public/colon_cancer_tool_table_tmp_'+I18n.locale.to_s+'2.txt')
@table_str = File.read(Coloncancerpredictfields2s::ToolTableMap[I18n.locale])
preidct_js_url = "/assets/#{Coloncancerpredictfields2s::JS}"
if File.exist?(Coloncancerpredictfields2s::JSFileName)
js_filename = File.read(Coloncancerpredictfields2s::JSFileName)
if js_filename.include?(Coloncancerpredictfields2s::JS)
asset = Rails.application.assets[js_filename]
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{asset.digest_path}"
else
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{File.basename(js_filename)}"
end
end
{
"coloncancerpredict2" => [],
"extras"=>{"table"=> @table_str}
"extras"=>{"table"=> @table_str,'preidct_js_url'=>preidct_js_url}
}
end
def widget
uid = OrbitHelper.params[:uid] rescue ""
tags = OrbitHelper.widget_tags
categories = OrbitHelper.widget_categories || []
@table_str = File.read('public/colon_cancer_tool_table_tmp_'+I18n.locale.to_s+'2.txt')
@table_str = File.read(Coloncancerpredictfields2s::ToolTableMap[I18n.locale])
preidct_js_url = "/assets/#{Coloncancerpredictfields2s::JS}"
if File.exist?(Coloncancerpredictfields2s::JSFileName)
js_filename = File.read(Coloncancerpredictfields2s::JSFileName)
if js_filename.include?(Coloncancerpredictfields2s::JS)
asset = Rails.application.assets[js_filename]
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{asset.digest_path}"
else
preidct_js_url = "#{Rails.application.config.assets.prefix}/#{File.basename(js_filename)}"
end
end
{
"coloncancerpredict2" => [],
"extras"=>{"table"=> @table_str}
"extras"=>{"table"=> @table_str,'preidct_js_url'=>preidct_js_url}
}
end
def create_first_field

View File

@ -9,6 +9,12 @@ class Coloncancerpredictfields2s
AdvanceFields = ["revert_value","map_values","coloncancer_predict_mapping_file2"]
TherapyFields = ["variable","name","hint","comment_text","choice_fields","lpv_impact","active_choice","disable_condition"]
TherapyOnly = ["lpv_impact","active_choice","disable_condition"]
ModuleAppPath = Pathname.new(File.expand_path(__dir__)).dirname.dirname.to_s.freeze
JS = "colon_cancer_predict2.js"
JSFileName = "public/cc2_tool_js_filename.txt".freeze
ToolTableMap = I18n.available_locales.map do |locale|
[locale, "public/colon_cancer_tool_table_tmp_#{locale}2.txt".freeze]
end.to_h
field :title ,type:String ,default:""
field :advance_mode, type: Boolean, default: false
field :form_show , :type=> Hash ,default: {
@ -218,6 +224,50 @@ class Coloncancerpredictfields2s
self.lpv_calc = get_years_settings_dict
self.generate_eval_formula
end
def reload_any_asset(path, type=nil, force_reload=false)
env = Rails.application.assets
new_env = Sprockets::Environment.new(Rails.root.to_s) do |env|
env.version = ::Rails.env
tmp_path = "#{Rails.application.config.root}/tmp/cache/assets/#{::Rails.env}"
env.cache = ::Sprockets::Cache::FileStore.new(tmp_path)
env.context_class.class_eval do
include ::Sprockets::Rails::Helper
end
end
new_env.config = env.config
stats = env.cached.instance_variable_get(:@stats)
new_path = path.sub(/\.erb$/,'')
if force_reload || (stats && stats[path]) #Need reload asset
if type.nil?
ext = File.extname(new_path)
env.mime_types.each do |t, h|
if h[:extensions].include?(ext)
type = t
break
end
end
end
if type
full_path = 'file://'+path+'?type='+type
uris = env.cached.instance_variable_get(:@uris)
keys = uris.keys.select{|k| k.include?(full_path)}
asset = new_env.load(full_path)
if keys.count != 0
keys.each do |k|
uris[k] = asset
end
end
new_path = new_path.sub(/\.[^.]+$/){|ext| '-' + asset.digest + ext}
File.binwrite(new_path, asset.to_s)
yield new_path if block_given?
end
end
end
def reload_js_asset(path, force_reload=false)
reload_any_asset(path, 'application/javascript', force_reload) do |new_path|
File.open(JSFileName, 'w+'){|f| f.write(new_path)}
end
end
def generate_eval_formula
eval_hidden_variables = "def eval_hidden_variables(result); #{self.tmp_hidden_variables_for_ruby}; end"
Coloncancerpredict2sController.module_eval(eval_hidden_variables)
@ -343,10 +393,9 @@ class Coloncancerpredictfields2s
org_str.insert(st, replace_str)
org_str
end
def auto_write_predict_js
def auto_write_predict_js(force_reload=true)
js_codes = generate_jscode
module_app_path = Pathname.new(File.expand_path(__dir__)).dirname.dirname.to_s
save_path = module_app_path + '/app/assets/javascripts/colon_cancer_predict2.js'
save_path = "#{ModuleAppPath}/app/assets/javascripts/#{JS}"
file_texts = File.read(save_path)
need_write = false
str1 = "/* auto add start */"
@ -409,6 +458,9 @@ class Coloncancerpredictfields2s
end
if need_write
File.write(save_path,file_texts)
reload_js_asset(save_path, force_reload)
elsif !(File.exist?(JSFileName))
File.open(JSFileName, 'w+'){|f| f.write(save_path)}
end
end
def get_years_settings_dict

View File

@ -1,6 +1,6 @@
<script type="text/javascript" src="/assets/jquery.smartmenus.min.js"></script>
<script type="text/javascript" src="/assets/jquery.smartmenus.bootstrap.min.js"></script>
<script type="text/javascript" src="/assets/colon_cancer_predict2.js"></script>
<script type="text/javascript" src="{{preidct_js_url}}"></script>
{{table}}
<%= stylesheet_link_tag "colon_cancer_predict2", media: "screen" %>
<%= stylesheet_link_tag "bootstrap.min.print", media: "print" %>

View File

@ -1,6 +1,6 @@
<script type="text/javascript" src="/assets/jquery.smartmenus.min.js"></script>
<script type="text/javascript" src="/assets/jquery.smartmenus.bootstrap.min.js"></script>
<script type="text/javascript" src="/assets/colon_cancer_predict2.js"></script>
<script type="text/javascript" src="{{preidct_js_url}}"></script>
{{table}}
<%= stylesheet_link_tag "colon_cancer_predict2", media: "screen" %>
<%= stylesheet_link_tag "bootstrap.min.print", media: "print" %>