Class: Ezframe::SinglePageEditor

Inherits:
PageBase show all
Includes:
EditorCommon
Defined in:
lib/ezframe/single_page_editor.rb

Overview

ページ遷移無しでデータを編集する仕組み

Instance Attribute Summary

Attributes inherited from PageBase

#request

Instance Method Summary collapse

Methods included from EditorCommon

#get_id, #make_cancel_button, #make_create_button, #make_delete_button, #make_edit_button, #show_label_edit, #show_label_view, #show_message_page, #update_value

Methods inherited from PageBase

#initialize, #login?, #make_base_url, #parse_json_body, #session, #set_request, #show_base_template, #user, #warden

Constructor Details

This class inherits a constructor from Ezframe::PageBase

Instance Method Details

#act_after_cancelObject



117
118
119
# File 'lib/ezframe/single_page_editor.rb', line 117

def act_after_cancel
  return public_detail_post
end

#act_after_editObject



113
114
115
# File 'lib/ezframe/single_page_editor.rb', line 113

def act_after_edit
  return [public_default_post, public_detail_post]
end

#button_for_detail_box(data) ⇒ Object

詳細ページ用ボタン



176
177
178
179
180
181
182
# File 'lib/ezframe/single_page_editor.rb', line 176

def button_for_detail_box(data)
  buttons = [Ht.button(class: %w[btn right], event: "on=click:url=#{make_base_url(data[:id])}/edit", child: [Ht.icon("edit"), Message[:edit_button_label]]) ]
  if @show_delete_button
    buttons.push(make_delete_button)
  end
  return Ht.div(class: %w[button-box], child: buttons)
end

#button_for_index_line(data) ⇒ Object

一覧ページ用ボタン



171
172
173
# File 'lib/ezframe/single_page_editor.rb', line 171

def button_for_index_line(data)
  Ht.button(class: %w[btn right], event: "on=click:url=#{make_base_url(data[:id])}/edit", child: [Ht.icon("edit"), Message[:edit_button_label]])
end

#init_varsObject



8
9
10
11
12
13
14
# File 'lib/ezframe/single_page_editor.rb', line 8

def init_vars
  super
  @sort_key = :id
  @event = @parsed_body[:event] if @parsed_body
  @dom_id = { create: "create-area", edit: "edit-area", index: "index-area", detail: "detail-area"}
  # @show_delete_button = nil
end

#list_for_indexObject

一覧ページ用のデータリスト生成



166
167
168
# File 'lib/ezframe/single_page_editor.rb', line 166

def list_for_index
  return @column_set.dataset.where(deleted_at: nil).order(@sort_key).all
end

#make_detail_line(column) ⇒ Object

詳細表示欄の一行を生成



104
105
106
107
108
109
110
111
# File 'lib/ezframe/single_page_editor.rb', line 104

def make_detail_line(column)
  view = column.view
  if view
    view = Ht.pre(view) if view.strip.index("\n")
    return Ht.p([Ht.small(column.label), view])
  end
  return nil
end

#make_edit_form(command = :edit) ⇒ Object

編集フォームの生成



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ezframe/single_page_editor.rb', line 185

def make_edit_form(command = :edit)
  @id ||= get_id
  target_keys = @edit_keys || @column_set.keys
  list = target_keys.map do |colkey|
    column = @column_set[colkey.to_sym]
    unless column
      EzLog.error("undefined column entry: #{colkey}")
      next
    end
    form = column.form
    Ht.p(class: %w[form-line], child: [Ht.small(column.label), form]) if form
  end
  send_button = Ht.button(id: "#{@class_snake}-#{command}-finish-button", class: %w[btn], child: [Ht.icon("check"), Message[:edit_finish_button_label]], event: "on=click:url=#{make_base_url(@id)}/#{command}:with=form")
  cancel_button = make_cancel_button("on=click:url=#{make_base_url(@id)}/#{command}:cancel=true:with=form")
  list.push(Ht.p(class: %w[edit-finish-buttons], child: [send_button, cancel_button]))
  return Ht.form(list)
end

#make_index_column(key) ⇒ Object

一覧表示の1カラムを生成



155
156
157
158
159
160
161
162
163
# File 'lib/ezframe/single_page_editor.rb', line 155

def make_index_column(key)
  column = @column_set[key.to_sym]
#      if @with_label
#        child = [Ht.small(column.label), column.view(force: true)]
#        return Ht.p(id: "edit-#{@class_snake}-#{@column_set[:id].value}-column-#{column.key}", child: child)
#      else
    return column.view(force: true)
#      end
end

#make_index_line(target_keys, data) ⇒ Object

一覧表示の1行を生成



148
149
150
151
152
# File 'lib/ezframe/single_page_editor.rb', line 148

def make_index_line(target_keys, data)
  @column_set.clear
  @column_set.values = data
  return target_keys.map { |key| make_index_column(key) }
end

#make_index_tableObject

一覧表の生成



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ezframe/single_page_editor.rb', line 122

def make_index_table
  list = list_for_index
  target_keys = @index_keys
  unless target_keys
    target_keys = @column_set.keys.select {|k| !@column_set[k].no_view?}
  end
  labels = @table_labels
  unless labels
    labels = target_keys.map {|k| @column_set[k].label(force: true) || " "}
  end
  # 項目名欄の生成
  thead = Ht.thead(Ht.tr(labels.map {|label| Ht.th(label||" ")}))

  tr_a = list.map do |data|
    view_a = make_index_line(target_keys, data)
    td_a = view_a.map {|view| Ht.td(view)}
    Ht.tr(id: "tr-#{@class_snake}-#{data[:id]}", child: td_a, event: "on=click:url=#{make_base_url(data[:id])}/detail")
  end
  tbody = Ht.tbody(tr_a)
  return [
           Ht.table(id: "enable_datatable_#{@class_snake}", child: [thead, tbody], ezload: "command=enable_datatable:target=#enable_datatable_#{@class_snake}"),
           Ht.div(id: @dom_id[:detail], child: ""),
         ]
end

#make_index_topObject



27
28
29
# File 'lib/ezframe/single_page_editor.rb', line 27

def make_index_top
  make_create_button("on=click:url=#{make_base_url}/create")
end

#public_create_postObject

新規データ登録



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ezframe/single_page_editor.rb', line 36

def public_create_post
  @form = @event[:form]
  EzLog.debug("public_create_post: event=#{@event}")
  if @event[:cancel]
    return { inject: "##{@dom_id[:create]}", body: Html.convert(make_index_top) }
  elsif !@form
    return { inject: "##{@dom_id[:create]}", body: Html.convert(make_edit_form(:create)) }
  else
    # 値の保存
    @column_set.clear
    form_values = @form
    form_values.update(@env["url_params"])
    # @column_set.values = form_values
    @column_set[:id].value = @id = @column_set.create(form_values)
    return { redirect: make_base_url(@id) }
    # return public_default_post
  end
end

#public_default_getObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/ezframe/single_page_editor.rb', line 16

def public_default_get
  @id = get_id
#      if @id
#        return public_detail_post
#      else
    div = [ Ht.div(id: @dom_id[:create], child: make_index_top), Ht.div(id: @dom_id[:index], child: make_index_table) ]
    layout = index_layout(center: Ht.form(child: div))
    return show_base_template(title: Message[:index_page_title], body: Html.convert(layout))
#      end
end

#public_default_postObject



31
32
33
# File 'lib/ezframe/single_page_editor.rb', line 31

def public_default_post
  return { inject: "##{@dom_id[:index]}", body: Html.convert(make_index_table) }
end

#public_delete_postObject



96
97
98
99
100
101
# File 'lib/ezframe/single_page_editor.rb', line 96

def public_delete_post
  @id = get_id
  dataset = DB.dataset(@column_set.name)
  DB.delete(dataset, @id)
  return public_default_post
end

#public_detail_postObject

データ詳細表示



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ezframe/single_page_editor.rb', line 79

def public_detail_post
  @id ||= get_id
  data = @column_set.set_from_db(@id)
  target_keys = @detail_keys || @column_set.keys.select { |key| !@column_set[key].attribute[:no_view] }
  line_a = []
  target_keys.each do |key|
    column = @column_set[key]
    v = make_detail_line(column)
    line_a.push(v) if v
  end
  table = Ht.div(line_a)
  collection = Materialize::Collection.new
  # 詳細表示用のblockを追加
  collection.push(Ht.div(id: @dom_id[:detail], child: [button_for_detail_box(data), table]))
  return { inject: "##{@dom_id[:detail]}", body: Html.convert(collection.to_h) }
end

#public_edit_postObject

データ編集受信



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ezframe/single_page_editor.rb', line 56

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
    # フォームの表示
    form = make_edit_form
    found_a = Ht.search(form, tag: "input")
    found_a.each { |h| h.add_class("#{@class_snake}-edit-box") if h[:size] }
    return { inject: "##{@dom_id[:detail]}", body: Html.convert(form) }
  else
    if @event[:cancel]
      data = @column_set.set_from_db(@id)
      return act_after_cancel
    else
      # 値を保存
      @column_set.update(@id, @event[:form])
    end
    return act_after_edit
  end
end