ezframe 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,238 +1,19 @@
1
1
  # frozen_string_literal: true
2
+ require_relative "main_page_kit"
3
+
2
4
  module Ezframe
3
5
  class MainEditor < PageBase
4
6
  include EditorCommon
5
-
6
- # 一覧ページ生成
7
- def public_default_get
8
- @id = get_id
9
- if @id
10
- return make_detail_page
11
- else
12
- data_a = list_for_index
13
- div = [ Ht.div(id: "detail-top-area", child: make_index_top), Ht.div(id: "index-area", child: make_index_table(data_a)) ]
14
- layout = index_layout(center: Ht.form(child: div))
15
- return show_base_template(title: Message[:index_page_title], body: Html.convert(layout))
16
- end
17
- end
18
-
19
- # 一覧ページの上部に表示するボタン等の生成
20
- def make_index_top
21
- make_create_button
22
- end
23
-
24
- # 新規登録フォーム表示
25
- def public_create_get
26
- @column_set.clear
27
- table = make_edit_form(:create)
28
- layout = main_layout(center: Ht.form(child: table), type: 2)
29
- show_base_template(title: Message[:parent_create_page_title], body: Html.convert(layout))
30
- end
31
-
32
- # 新規登録受信
33
- def public_create_post
34
- validation = @column_set.validate(@form)
35
- if @event[:branch] == "single_validate"
36
- EzLog.debug("public_create_post: single validate: event=#{@event}, form=#{@form}")
37
- return single_validation(validation, @event[:target_key])
38
- end
39
- unless @form
40
- return { inject: "#center-panel", body: Ht.form(child: make_edit_form(:create)) }
41
- else
42
- if count_errors(validation) > 0
43
- cmd_a = full_validation(validation)
44
- EzLog.debug("public_create_post: cmd_a=#{cmd_a}")
45
- return cmd_a if cmd_a.length > 0
46
- end
47
- # 値の保存
48
- id = create_data(@form)
49
- return { redirect: make_base_url(id) }
50
- end
51
- end
52
-
53
-
54
- # データ編集受信
55
- def public_edit_post
56
- EzLog.debug("public_edit_post: #{@form}")
57
- @id = get_id
58
- validation = @column_set.validate(@form)
59
- if @event[:branch] == "single_validate"
60
- EzLog.debug("public_edit_post: single validate:event=#{@event}, form=#{@form}")
61
- return single_validation(validation, @event[:target_key])
62
- end
63
- unless @form
64
- data = @column_set.set_from_db(@id)
65
- return show_message_page("no data", "data is not defined: #{@id}") unless data
66
- return { inject: "#center-panel", body: Html.convert(Ht.form(make_edit_form)) }
67
- else
68
- if count_errors(validation) > 0
69
- cmd_a = full_validation(validation)
70
- return cmd_a
71
- end
72
- # 値を保存
73
- update_data(@id, @form)
74
- return { redirect: make_base_url(@id) }
75
- end
76
- end
77
-
78
- private
79
-
80
- # 新規データの生成
81
- def create_data(form)
82
- @column_set.clear
83
- @column_set[:id].value = id = @column_set.create(form)
84
- return id
85
- end
86
-
87
- # データの更新
88
- def update_data(id, form)
89
- @column_set.update(id, form)
90
- end
91
-
92
- # 自動入力を行う
93
- def exec_completion
94
- return nil
95
- end
96
-
97
- # 一覧テーブルの生成
98
- def make_index_table(data_a)
99
- EzLog.debug("make_index_table: #{data_a.length}")
100
- target_keys = @index_keys
101
- unless target_keys
102
- target_keys = @column_set.keys.select {|k| !@column_set[k].no_view? }
103
- end
104
- tr_a = data_a.map do |data|
105
- # EzLog.debug("data=#{data}")
106
- @column_set.clear
107
- @column_set.set_values(data, from_db: true)
108
- line = target_keys.map do |key|
109
- view = @column_set[key].view
110
- Ht.td(Ht.a(href: "#{make_base_url(data[:id])}", child: view))
111
- end
112
- EzLog.debug("line=#{line}")
113
- Ht.tr(line)
114
- end
115
- th_a = target_keys.map {|key| Ht.th(@column_set[key.to_sym].label) }
116
- thead = Ht.thead(Ht.tr(th_a))
117
- tbody = Ht.tbody(tr_a)
118
- table_id = "enable_datatable_#{@class_snake}"
119
- return Ht.table(id: table_id, class: %w[enable_datatable], child: [ thead, tbody ], ezload: "command=enable_datatable:target=##{table_id}:size=10")
120
- end
121
-
122
- # 編集フォームの生成
123
- def make_edit_form(command = :edit)
124
- table = []
125
- matrix = @column_set.map do |column|
126
- next if column.no_edit?
127
- form = column.form
128
- table.push Ht.p([ Ht.small(column.label), form ]) if form
129
- end
130
- send_button = Ht.button(id: "edit-finish-button", child: Message[:edit_finish_button_label], class: %w[btn], event: "on=click:url=#{make_base_url}/#{command}:with=form")
131
- cancel_button = edit_cancel_button
132
- cancel_button[:event] = "on=click:command=redirect:url=#{make_base_url}"
133
- table.push(Ht.p([send_button, cancel_button]))
134
- return table
135
- end
136
-
137
- # 詳細表示
138
- def make_detail_page
139
- # EzLog.info "make_detail_page: #{@request.params.inspect}"
140
- id = get_id(@class_snake)
141
- unless @column_set.set_from_db(id)
142
- return show_message_page("no data", "data is not defined: #{id}")
143
- end
144
- right = nil
145
- right = right_tabs if @with_right_tabs
146
- layout = main_layout( center: make_detail_table, right: right)
147
- return show_base_template(title: Message[:customer_detail], body: Html.convert(layout))
148
- end
149
-
150
- private
151
-
152
- # 1カラムに対してだけバリデーションを行う。
153
- def single_validation(result, target_key)
154
- unless target_key
155
- raise "target_key is empty: #{result}"
156
- return []
157
- end
158
- cmd_a = []
159
- if result[target_key.to_sym]
160
- cmd_a = show_validate_result(result)
161
- end
162
- if count_errors(result) == 0
163
- cmd_a.unshift({ reset_error: "#error-box-#{target_key}"})
164
- end
165
- comp_a = exec_completion(@form)
166
- cmd_a += comp_a if comp_a
167
- EzLog.debug("reset_error: #error-box-#{target_key}")
168
- EzLog.debug("single_validation: target_key=#{target_key}, result=#{result}, count=#{count_errors(result)}, cmd_a=#{cmd_a}")
169
- return cmd_a
170
- end
171
-
172
- # 全てのカラムに対してバリデーションを行う
173
- def full_validation(result)
174
- cmd_a = show_validate_result(result)
175
- cmd_a.unshift({ reset_error: ".error-box" })
176
- EzLog.debug("full_validation: full, cmd_a=#{cmd_a}")
177
- return cmd_a
178
- end
179
-
180
- # フォームの値の有効性チェックし、ajax用返信ハッシュを生成
181
- def show_validate_result(validate_result)
182
- cmd_a = []
183
- validate_result.each do |key, status|
184
- norm, err = status
185
- EzLog.debug("norm=#{norm}, err=#{err}")
186
- if norm
187
- cmd_a.push({ set_value: "input[name=#{key}]", value: norm })
188
- end
189
- if err
190
- msg = Message[err.to_sym]||err
191
- cmd_a.push({ set_error: "#error-box-#{key}", value: msg })
192
- end
193
- end
194
- return cmd_a
195
- end
196
-
197
- # validate_resultの中のエラーの数を数える
198
- def count_errors(validate_result)
199
- return validate_result.count {|k, a| a[1] }
200
- end
201
-
202
- # 詳細ページの表の生成
203
- def make_detail_table
204
- table = []
205
- array = @column_set.map do |column|
206
- next if column.no_view?
207
- edit_btn = nil
208
- if column.type.to_s == "textarea"
209
- view = Ht.pre(id: "#{@class_snake}-#{column.key}-view", child: column.view)
210
- else
211
- view = Ht.span(id: "#{@class_snake}-#{column.key}-view", child: column.view)
212
- end
213
- table.push Ht.p(class: %w[hover-button-box], child: [ Ht.small(column.label), view, edit_btn ].compact)
214
- end
215
- edit_btn = Ht.button(id: "#{@class_snake}-detail-edit-button", class: %w[btn], child: [ Ht.icon("edit"), Message[:edit_button_label] ], event: "on=click:url=#{make_base_url}/edit")
216
- table.push edit_btn
217
- return table
218
- end
219
-
220
- # 一覧ページ用のデータリスト生成
221
- def list_for_index(where: nil)
222
- where ||= {}
223
- where[:deleted_at] = nil
224
- sql = @column_set.dataset.where(where).order(@sort_key).sql
225
- EzLog.debug("list_for_index: #{sql}")
226
- return @column_set.dataset.where(where).order(@sort_key).all
227
- end
228
-
229
- def edit_cancel_button
230
- Ht.span(class: %w[btn red small waves-effect waves-light switch-button], child: Ht.icon("clear"))
231
- end
232
-
233
- def make_create_button(event = nil)
234
- event ||= "on=click:command=redirect:url=#{make_base_url}/create"
235
- return Ht.button(class: %w[btn], child: [ Ht.icon("add"), Message[:create_button_label] ], event: event )
236
- end
7
+ include MainPageKit::Default
8
+ include MainPageKit::Index
9
+ include MainPageKit::Edit
10
+ include MainPageKit::Detail
11
+
12
+ def init_vars
13
+ super
14
+ @sort_key = :id
15
+ @event = @parsed_body[:ezevent] if @parsed_body
16
+ @dom_id = { create: "create-area", edit: "edit-area", index: "index-area", detail: "detail-area"}
17
+ end
237
18
  end
238
19
  end
@@ -0,0 +1,226 @@
1
+ module Ezframe
2
+ module MainPageKit
3
+ module Default
4
+ def public_default_get
5
+ @id = get_id
6
+ div = [Ht.div(id: @dom_id[:create], child: make_index_top), Ht.div(id: @dom_id[:index], child: "", ezload: "url=#{make_base_url}")]
7
+ layout = index_layout(center: make_form(make_base_url, div))
8
+ return show_base_template(title: Message[:index_page_title], body: Html.convert(layout))
9
+ end
10
+ end
11
+
12
+ # 一覧テーブルの生成
13
+ module Index
14
+ def public_default_post
15
+ return { inject: "#main-box", body: [ Ht.div(make_index_top), Ht.div(make_index_table)], set_url: make_base_url }
16
+ end
17
+
18
+ def make_index_table
19
+ data_a = list_for_index
20
+ EzLog.debug("make_index_table: #{data_a.length}")
21
+ target_keys = @index_keys
22
+ unless target_keys
23
+ target_keys = @column_set.keys.select { |k| !@column_set[k].no_view? }
24
+ end
25
+ tr_a = data_a.map do |data|
26
+ @column_set.clear
27
+ @column_set.set_values(data, from_db: true)
28
+ line = target_keys.map do |key|
29
+ view = @column_set[key].view
30
+ Ht.td(Ht.a(href: "#{make_base_url(data[:id])}", child: view))
31
+ end
32
+ Ht.tr(line)
33
+ end
34
+ th_a = target_keys.map { |key| Ht.th(@column_set[key.to_sym].label) }
35
+ thead = Ht.thead(Ht.tr(th_a))
36
+ tbody = Ht.tbody(tr_a)
37
+ table_id = "enable_datatable_#{@class_snake}"
38
+ return Ht.table(id: table_id, class: %w[enable_datatable], child: [thead, tbody], ezload: "command=enable_datatable:target=##{table_id}:size=10")
39
+ end
40
+
41
+ # 一覧ページの上部に表示するボタン等の生成
42
+ def make_index_top
43
+ make_create_button
44
+ end
45
+ end
46
+
47
+ module Edit
48
+ # 新規登録フォーム表示
49
+ def public_create_get
50
+ @column_set.clear
51
+ table = make_edit_form(:create)
52
+ layout = main_layout(center: make_form("#{make_base_url}/create", table), type: 2)
53
+ show_base_template(title: Message[:parent_create_page_title], body: Html.convert(layout))
54
+ end
55
+
56
+ # 新規登録受信
57
+ def public_create_post
58
+ validation = @column_set.validate(@form)
59
+ EzLog.debug("public_create_post: event=#{@event}, form=#{@form}")
60
+ if @event[:branch] == "single_validate"
61
+ EzLog.debug("public_create_post: single validate")
62
+ return single_validation(validation, @event[:target_key] || @form.keys[0])
63
+ end
64
+ unless @form
65
+ url = "#{make_base_url}/create"
66
+ return { inject: "#center-panel", body: Html.convert(make_form(url, make_edit_form(:create))), set_url: url }
67
+ else
68
+ if count_errors(validation) > 0
69
+ cmd_a = full_validation(validation)
70
+ EzLog.debug("public_create_post: cmd_a=#{cmd_a}")
71
+ return cmd_a if cmd_a.length > 0
72
+ end
73
+ # 値の保存
74
+ id = create_data(@form)
75
+ return { redirect: make_base_url(id) }
76
+ end
77
+ end
78
+
79
+ # データ編集受信
80
+ def public_edit_post
81
+ EzLog.debug("public_edit_post: #{@form}")
82
+ @id = get_id
83
+ validation = @column_set.validate(@form)
84
+ if @event[:branch] == "single_validate"
85
+ EzLog.debug("public_edit_post: single validate:event=#{@event}, form=#{@form}")
86
+ return single_validation(validation, @event[:target_key])
87
+ end
88
+ unless @form
89
+ data = @column_set.set_from_db(@id)
90
+ return show_message_page("no data", "data is not defined: #{@id}") unless data
91
+ return { inject: "#center-panel", body: Html.convert(make_form("#{make_base_url}/edit", make_edit_form)) }
92
+ else
93
+ if count_errors(validation) > 0
94
+ cmd_a = full_validation(validation)
95
+ return cmd_a
96
+ end
97
+ # 値を保存
98
+ update_data(@id, @form)
99
+ return { redirect: make_base_url(@id) }
100
+ end
101
+ end
102
+
103
+ # 自動入力を行う
104
+ def exec_completion
105
+ return nil
106
+ end
107
+
108
+ # 編集フォームの生成
109
+ def make_edit_form(command = :edit)
110
+ table = []
111
+ matrix = @column_set.map do |column|
112
+ next if column.no_edit?
113
+ form = column.form
114
+ table.push Ht.p([Ht.small(column.label), form]) if form
115
+ end
116
+ send_button = Ht.button(id: "edit-finish-button", child: Message[:edit_finish_button_label], class: %w[btn], ezevent: "on=click:url=#{make_base_url}/#{command}:with=form")
117
+ cancel_button = edit_cancel_button
118
+ cancel_button[:event] = "on=click:command=redirect:url=#{make_base_url}"
119
+ table.push(Ht.p([send_button, cancel_button]))
120
+ return table
121
+ end
122
+
123
+ # 1カラムに対してだけバリデーションを行う。
124
+ def single_validation(validate_h, target_key)
125
+ EzLog.debug("single_validation: validate_h=#{validate_h}, target_key=#{target_key}")
126
+ unless target_key
127
+ raise "target_key is empty: #{validate_h}"
128
+ return []
129
+ end
130
+ cmd_a = []
131
+ if validate_h[target_key.to_sym]
132
+ cmd_a = show_validate_result(validate_h)
133
+ end
134
+ if count_errors(validate_h) == 0
135
+ cmd_a.unshift({ reset_error: "#error-box-#{target_key}" })
136
+ end
137
+ comp_a = exec_completion(@form)
138
+ cmd_a += comp_a if comp_a
139
+ EzLog.debug("reset_error: #error-box-#{target_key}")
140
+ EzLog.debug("single_validation: target_key=#{target_key}, validate_h=#{validate_h}, count=#{count_errors(validate_h)}, cmd_a=#{cmd_a}")
141
+ return cmd_a
142
+ end
143
+
144
+ # 全てのカラムに対してバリデーションを行う
145
+ def full_validation(validate_h)
146
+ cmd_a = show_validate_result(validate_h)
147
+ cmd_a.unshift({ reset_error: ".error-box" })
148
+ EzLog.debug("full_validation: full, cmd_a=#{cmd_a}")
149
+ return cmd_a
150
+ end
151
+
152
+ # フォームの値の有効性チェックし、ajax用返信ハッシュを生成
153
+ def show_validate_result(validate_result)
154
+ cmd_a = []
155
+ validate_result.each do |key, status|
156
+ norm, err = status
157
+ EzLog.debug("norm=#{norm}, err=#{err}")
158
+ if norm
159
+ cmd_a.push({ set_value: "input[name=#{key}]", value: norm })
160
+ end
161
+ if err
162
+ msg = Message[err.to_sym] || err
163
+ cmd_a.push({ set_error: "#error-box-#{key}", value: msg })
164
+ end
165
+ end
166
+ return cmd_a
167
+ end
168
+
169
+ # validate_resultの中のエラーの数を数える
170
+ def count_errors(validate_result)
171
+ return validate_result.count { |k, a| a[1] }
172
+ end
173
+
174
+ def edit_cancel_button
175
+ Ht.span(class: %w[btn red small waves-effect waves-light switch-button], child: Ht.icon("clear"))
176
+ end
177
+
178
+ def make_create_button(event = nil)
179
+ event ||= "on=click:command=redirect:url=#{make_base_url}/create"
180
+ return Ht.button(class: %w[btn], child: [Ht.icon("add"), Message[:create_button_label]], ezevent: event)
181
+ end
182
+ end
183
+
184
+ # 詳細表示
185
+ module Detail
186
+ def make_detail_page
187
+ # EzLog.info "make_detail_page: #{@request.params.inspect}"
188
+ id = get_id(@class_snake)
189
+ unless @column_set.set_from_db(id)
190
+ return show_message_page("no data", "data is not defined: #{id}")
191
+ end
192
+ right = nil
193
+ right = right_tabs if @with_right_tabs
194
+ layout = main_layout(center: make_detail_table, right: right)
195
+ return show_base_template(title: Message[:customer_detail], body: Html.convert(layout))
196
+ end
197
+
198
+ # 詳細ページの表の生成
199
+ def make_detail_table
200
+ table = []
201
+ array = @column_set.map do |column|
202
+ next if column.no_view?
203
+ edit_btn = nil
204
+ if column.type.to_s == "textarea"
205
+ view = Ht.pre(id: "#{@class_snake}-#{column.key}-view", child: column.view)
206
+ else
207
+ view = Ht.span(id: "#{@class_snake}-#{column.key}-view", child: column.view)
208
+ end
209
+ table.push Ht.p(class: %w[hover-button-box], child: [Ht.small(column.label), view, edit_btn].compact)
210
+ end
211
+ edit_btn = Ht.button(id: "#{@class_snake}-detail-edit-button", class: %w[btn], child: [Ht.icon("edit"), Message[:edit_button_label]], ezevent: "on=click:url=#{make_base_url}/edit")
212
+ table.push edit_btn
213
+ return table
214
+ end
215
+
216
+ # 一覧ページ用のデータリスト生成
217
+ def list_for_index(where: nil)
218
+ where ||= {}
219
+ where[:deleted_at] = nil
220
+ sql = @column_set.dataset.where(where).order(@sort_key).sql
221
+ EzLog.debug("list_for_index: #{sql}")
222
+ return @column_set.dataset.where(where).order(@sort_key).all
223
+ end
224
+ end
225
+ end
226
+ end