lib/ezframe/editor.rb in ezframe-0.1.0 vs lib/ezframe/editor.rb in ezframe-0.1.1
- old
+ new
@@ -1,188 +1,176 @@
# frozen_string_literal: true
module Ezframe
class DataEditor < PageBase
-
- def initialize(request=nil, model=nil)
- super(request, model)
- decide_target
- if model
- @column_set = @model.column_sets[@target]
- unless @column_set
- raise "[ERROR] no such column set: #{@target}"
- end
- @dataset = @column_set.dataset
+ # 一覧ページ生成
+ def public_default_get
+ id = get_id
+ if id
+ return show_detail_page
+ else
+ data_a = @dataset.all
+ htb = make_index_table(data_a)
+ layout = index_layout(center: Ht.form(child: htb))
+ return show_base_template(title: Message[:index_page_title], body: Html.convert(layout))
end
- if @parsed_body
- @event = @parsed_body[:event] || {}
- @target_id = @event[@target]
- end
- @auth = false
- init_vars
end
- def init_vars
- end
-
- def decide_target
- @target = self.class.to_s
- if @target.index("::")
- @target = $1 if /::(\w+)$/ =~ @target
+ # 一覧テーブルの生成
+ def make_index_table(data_a)
+ # @column_set.each { |col| col.attribute.delete(:hidden) }
+ idx_keys = @index_keys || @column_set.keys
+ puts "idx_keys=#{idx_keys.inspect}"
+ a_element = Proc.new { |key, id, text|
+ if key.to_s.index("_name")
+ Ht.a(href: "#{make_base_url(id)}", child: text)
+ else
+ text
+ end
+ }
+ tr_a = data_a.map do |data|
+ @column_set.clear
+ @column_set.values = data
+ line = idx_keys.map do |key|
+ view = @column_set[key].view
+ Ht.td(Ht.a(href: "#{make_base_url(data[:id])}", child: view))
+ end
+ Ht.tr(line)
end
- @target.downcase!
- @target = @target.to_sym
- return @target
+ th_a = idx_keys.map {|key| Ht.th(@column_set[key.to_sym].label) }
+ thead = Ht.thead(Ht.tr(th_a))
+ tbody = Ht.tbody(tr_a)
+ table_id = "enable_datatable_#{@class_snake}"
+ return Ht.table(id: table_id, class: %w[enable_datatable], child: [ thead, tbody ], event: "on=load:command=enable_datatable:target=##{table_id}:size=10")
end
-# def public_login_get
-# flash_area = ""
-# mylog "public_login_get: #{@request}"
-# if @request
-# mylog "flash=#{@request.env['x-rack.flash']}"
-# flash_area = Ht.div(class: %w[teal], child: @request['env']['x-rack.flash'].error)
-# end
-# form = Ht.multi_div([ %w[container], %w[row]],
-# Ht.form(class: "col s6 offset-s3", action: "/#{@target}/login", method: "post", child: [
-# Materialize.input(type: "text", name: "account", label: "User ID"),
-# Materialize.input(type: "password", name: "password", label: "Password"),
-# Ht.button(type: "submit", class: %w[btn], child: "login")
-# ]))
-# show_base_template(title: "Login", body: Html.convert(Materialize.convert([flash_area, form])))
-# end
-
-# def public_login_post
-# mylog "public_login_post: #{@params.inspect}, #{@parsed_body}"
-# warden.authenticate
-# public_index_get
-# end
-
- #--------------------------------------------------------------------------------------------------------
- # add new parts
+ # 新規登録フォーム表示
def public_create_get
- matrix = @column_set.map do |column|
- [column.label, column.form]
- end
- matrix.push([Ht.button(child: Message[:create_finish_button_label], class: %w[btn], event: "on=click:url=/#{@target}/create:with=form")])
- tb = Ht::Table.new(matrix)
- layout = main_layout(left: sidenav, center: Ht.form(child: tb.to_h))
- show_base_template(title: Message[:create_page_title], body: Html.convert(Materialize.convert(layout)))
+ @column_set.clear
+ table = make_edit_form(:create)
+ layout = main_layout(center: Ht.form(child: table), type: 2)
+ show_base_template(title: Message[:parent_create_page_title], body: Html.convert(layout))
end
+ # 新規登録受信
def public_create_post
- @column_set.values = @event[:form]
- @column_set[:id].value = @target_id = @column_set.create
- { redirect: "/#{@target}/detail?id=#{@target_id}" }
+ unless @event[:form]
+ { inject: "#center-panel", body: Ht.form(child: make_edit_form(:create)) }
+ else
+ # 値の保存
+ @column_set.clear
+ @column_set.values = @event[:form]
+ @column_set[:id].value = id = @column_set.create
+ return { redirect: make_base_url(id) }
+ end
end
- #--------------------------------------------------------------------------------------------------------
- # index parts
- def public_index_get
- data_a = @dataset.all
- htb = make_index_table(data_a)
- layout = index_layout(left: sidenav, center: Ht.form(child: htb))
- show_base_template(title: Message[:index_page_title], body: Html.convert(Materialize.convert(layout)))
+ # データ編集受信
+ def public_edit_post
+ id = get_id
+ unless @event[:form]
+ data = @column_set.set_from_db(id)
+ return show_message_page("no data", "data is not defined: #{id}") unless data
+ { inject: "#center-panel", body: Html.convert(Ht.form(make_edit_form)) }
+ else
+ # 値を保存
+ @column_set.update(id, @event[:form])
+ { redirect: make_base_url(id) }
+ end
end
- def make_index_table(data_a)
- column_header = [:id, :name, :email, :zipcode, :prefecture]
- @column_set.each { |col| col.attribute.delete(:hidden) }
- a_element = Proc.new { |key, id, text|
- # mylog "proc executed"
- if key == :name
- Ht.a(href: "/#{@target}/detail?id=#{id}", child: text)
- else
- text
- end
- }
- table = PageKit::IndexTable.new(column_header: column_header, column_set: @column_set, add_checkbox: :id,
- decorate_column: a_element)
- table.make_table(data_a)
+ # 編集フォームの生成
+ def make_edit_form(command = :edit)
+ @id = get_id
+ table = []
+ matrix = @column_set.map do |column|
+ form = column.form
+ table.push Ht.p([ Ht.small_text(column.label), form ]) if form
+ end
+ send_button = Ht.button(child: Message[:edit_finish_button_label], class: %w[btn], event: "on=click:url=#{make_base_url(@id)}/#{command}:with=form")
+ table.push(send_button)
+ return Ht.table(Ht.tr(table))
end
- alias_method :public_default_get, :public_index_get
-
#--------------------------------------------------------------------------------------------------------
- # search parts
+ # 検索
def public_search_post
mylog "public_search_post: #{@parsed_body.inspect}"
- word = @event[:form][:word]
+ sch_keys = @search_keys || @column_set.keys
+ word = @params["word"]
pattern = "%#{word}%"
- data_a = @dataset.where(Sequel.|(Sequel.like(:name_kana, pattern), Sequel.like(:name, pattern))).all
- make_index_table(data_a)
+ pattern_a = sch_keys.map {|key| Sequel.like(key, pattern) }
+ data_a = @dataset.where(Sequel.|(*pattern_a)).all
+ puts data_a.inspect
+ layout = index_layout(center: make_index_table(data_a))
+ show_base_template(title: Message[:search_result_page_title], body: Html.convert(layout))
end
- #--------------------------------------------------------------------------------------------------------
- # detail parts
- def public_detail_get
- mylog "pubilc_detail_get: #{@request.params.inspect}"
- @target_id ||= @request.params["id"]
- data = @column_set.set_from_db(@target_id)
- return show_base_template(title: "no data", body: "no customer data: #{@target_id}") unless data
- show_base_template(title: Message[:index_page_title], body: Html.convert(make_detail_get))
+ # 詳細表示
+ def show_detail_page
+ mylog "show_detail_page: #{@request.params.inspect}"
+ id = get_id(@class_snake)
+ unless @column_set.set_from_db(id)
+ return show_message_page("no data", "data is not defined: #{id}")
+ end
+ layout = main_layout(center: make_detail_table)
+ return show_base_template(title: Message[:detail_page_title], body: Html.convert(layout))
end
- def make_detail_get
- layout = main_layout( left: sidenav, center: detail_table )
- @request.env['rack.session'][@target] = @target_id
- layout[:event] = "on=load:command=set_global@target=#{@target_id}"
- Materialize.convert(layout)
- end
+ private
- def public_detail_post
- mylog "public_detail_post: #{@request.params.inspect}: #{@parsed_body}"
- if @parsed_body[:global]
- @target_id ||= @parsed_body[:global][@target]
+ # 詳細ページの表の生成
+ def make_detail_table
+ @id = get_id
+ table = []
+ array = @column_set.map do |column|
+ edit_btn = nil
+ if @column_edit_mode
+ edit_btn = edit_button(column)
+ edit_btn[:event] = "on=click:branch=edit_column:key=#{column.key}" if edit_btn
+ end
+ table.push(Ht.p(class: %w[hover-button-box], child: [ Ht.small_text(column.label), column.view, edit_btn ].compact))
end
- @column_set.set_from_db(@target_id)
- case @event[:branch]
- when "update_value"
- update_value
+ unless @column_edit_mode
+ edit_btn = Ht.button(class: %w[btn], child: [ Ht.icon("edit"), Message[:edit_button_label] ], event: "on=click:url=#{make_base_url(@id)}/edit")
+ table.push(edit_btn)
end
- { inject: "#center-panel", is_html: true, body: Html.convert(Materialize.convert(detail_table)) }
+ return Ht.table(Ht.tr(table))
end
- private
-
- def update_value
- form = @event[:form]
- @column_set.update(@target_id, form)
+ def edit_cancel_button
+ Ht.span(class: %w[btn red small waves-effect waves-light switch-button], child: Ht.icon("cancel"))
end
- def detail_table
- row_a = @column_set.map do |column|
- # column.attribute.delete(:hidden) # Todo: remove this
- Ht.tr(class: %w[hover-button-box], child: [
- Ht.td(child: column.label),
- Ht.td(child: Ht.div(class: %w[switch-box], child: [ detail_value_part(column), detail_form_part(column)].compact))
- ])
- end
- return Ht.table(child: row_a)
+ # URLからのIDの取得
+ def get_id(class_name = nil)
+ class_name ||= @class_snake
+ params = @request.env['url_params']
+ return nil unless params
+ # mylog "get_id: #{params.inspect}, #{class_name}"
+ return params[class_name.to_sym]
end
- def detail_value_part(column)
- return Ht.span(class: %w[switch-element], child: [ Ht.span(child: column.view) , edit_button(column)])
+ # 値の更新
+ def update_value
+ form = @event[:form]
+ @column_set.update(get_id, form)
end
- def detail_form_part(column)
- return nil if column.attribute[:no_edit]
- form = column.form
- if (form)
- form = Ht.form(class: %w[switch-element hide], child: [ Ht.span(child: column.form) , edit_ok_button(column), edit_cancel_button ])
- end
- return form
+ # ラベル付きで1カラムのviewを表示
+ def show_label_view(key)
+ col = @column_set[key]
+ Ht.span([Ht.small_text(col.label), col.view(force: true)])
end
- def edit_button(column)
- return nil if column.attribute[:no_edit]
- return Ht.button(class: %w[btn-floating switch-button hover-button hide right], child: Ht.icon("edit"))
+ # ラベル付きで1カラムのformを表示
+ def show_label_edit(key)
+ col = @column_set[key]
+ Ht.span([Ht.small_text(col.label), col.form(force: true)])
end
- def edit_ok_button(column)
- return Ht.span(class: %w[btn small teal waves-effect waves-light], event: "on=click:branch=update_value:url=/#{@target}/detail:key=#{column.key}:with=form", child: Ht.icon("check"))
- end
-
- def edit_cancel_button
- Ht.span(class: %w[btn red small waves-effect waves-light switch-button], child: Ht.icon("clear"))
+ # エラーページの表示
+ def show_message_page(title, body)
+ return show_base_template(title: title, body: Html.convert(body))
end
end
end