From b777af51705ccd0d4eccbb7ec29c0d272e8bab3a Mon Sep 17 00:00:00 2001 From: yasushiito Date: Sat, 24 Aug 2013 15:52:33 +0900 Subject: [PATCH] t#31896:refact elm --- app/assets/javascripts/panels.js.coffee | 102 +++++++++++++++------------ app/models/balloon.rb | 8 +++ app/models/ground_color.rb | 8 +++ app/models/panel_picture.rb | 11 +++ app/models/speech.rb | 8 +++ app/views/ground_colors/_element.html.erb | 2 +- app/views/ground_colors/_form.html.erb | 20 ++---- app/views/ground_pictures/_form.html.erb | 2 +- app/views/panel_pictures/_form.html.erb | 8 ++- app/views/panels/_color_helper.html.erb | 15 ++++ app/views/panels/_size_helper.html.erb | 1 + app/views/panels/_tail_angle_helper.html.erb | 5 ++ lib/element.rb | 20 ++++++ lib/element_part.rb | 20 ++++++ 14 files changed, 163 insertions(+), 67 deletions(-) create mode 100644 app/views/panels/_color_helper.html.erb create mode 100644 app/views/panels/_size_helper.html.erb create mode 100644 app/views/panels/_tail_angle_helper.html.erb diff --git a/app/assets/javascripts/panels.js.coffee b/app/assets/javascripts/panels.js.coffee index 1057fc3d..ec8547ae 100644 --- a/app/assets/javascripts/panels.js.coffee +++ b/app/assets/javascripts/panels.js.coffee @@ -40,6 +40,11 @@ $ -> return true else return false + element_is = (element_type, o) -> + if is_element(o) and o.attr('element_type') == element_type + return true + else + return false element_types = {'panel_picture': 'panel_pictures', 'speech_balloon': 'speech_balloons', 'ground_picture': 'ground_pictures', 'ground_color': 'ground_colors'} element_type_to_table = (element_type) -> element_types[element_type] @@ -160,15 +165,15 @@ $ -> } # sync view $('#panel' + pettanr_current_panel_id.toString() + 'width').focusout -> - trace = '#panel' + $(@).attr('panel_id') + trace = panel_tag_id($(@)) $(trace).width(parseInt( $(@).val())) $('#panel' + pettanr_current_panel_id.toString() + 'height').focusout -> - trace = '#panel' + $(@).attr('panel_id') + trace = panel_tag_id($(@)) $(trace).height(parseInt( $(@).val())) $('#panel' + pettanr_current_panel_id.toString() + 'border').focusout -> - trace = '#panel' + $(@).attr('panel_id') + trace = panel_tag_id($(@)) $(trace).css('border-width', parseInt($(@).val()).toString() + 'px') @@ -176,10 +181,10 @@ $ -> $('.pettanr-panel-picture-wrapper').map -> $(@).draggable { stop: ( event, ui ) -> - trace = '#panel' + $(@).attr('panel_id') + 'panel_picture' + $(@).attr('element_id') - img = '#panel' + $(@).attr('panel_id') + 'panel_picture' + $(@).attr('element_id') + 'img' - left = $(img).parent().position().left + $(@).position().left - top = $(img ).parent().position().top + $(@).position().top + trace = element_tag_id($(@)) + img = $(trace + 'img') + left = img.parent().position().left + $(@).position().left + top = img.parent().position().top + $(@).position().top $(trace + 'x').val(parseInt(left)) $(trace + 'y').val(parseInt(top)) } @@ -187,7 +192,7 @@ $ -> $('.panel-picture').map -> $(@).resizable { stop: ( event, ui ) -> - trace = '#panel' + $(@).parent().attr('panel_id') + 'panel_picture' + $(@).parent().attr('element_id') + trace = element_tag_id($(@).parent()) $(trace + 'width').val($(@).width()) $(trace + 'height').val($(@).height()) handles: 'all', @@ -210,7 +215,7 @@ $ -> # sync view $('input').map -> - if $(@).attr('element_type') and $(@).attr('element_type') == 'panel_picture' + if element_is('panel_picture', $(@)) $(@).focusout -> switch $(@).attr('column') when 'x' @@ -241,24 +246,26 @@ $ -> $('.pettanr-comic-balloon' ).map -> $(@).draggable { stop: ( event, ui ) -> - trace = '#panel' + $(@).attr('panel_id') + 'speech_balloon' + $(@).attr('element_id') + 'balloon' + $(@).attr('balloon_id') + img = $('.pettanr-balloon', $(@)) + trace = element_part_tag_id(img) left = $(@).position().left top = $(@).position().top $(trace + 'x').val(parseInt(left)) $(trace + 'y').val(parseInt(top)) } - $('.pettanr-balloon' ).map -> $(@).resizable { stop: ( event, ui ) -> - trace = '#panel' + $(@).parent().attr('panel_id') + 'speech_balloon' + $(@).parent().attr('element_id') + 'balloon' + $(@).parent().attr('balloon_id') + img = $('.pettanr-balloon', $(@)) + trace = element_part_tag_id(img) $(trace + 'width').val($(@).width()) $(trace + 'height').val($(@).height()) resize: ( event, ui ) -> - trace = '#panel' + $(@).parent().attr('panel_id') + 'speech_balloon' + $(@).parent().attr('element_id') - $(trace).width($(@).width()) - $(trace).height($(@).height()) + img = $('.pettanr-balloon', $(@)) + sb = element_tag_id(img) + $(sb).width($(@).width()) + $(sb).height($(@).height()) handles: 'all', autoHide: true } @@ -266,22 +273,22 @@ $ -> $(@).mouseover -> outer = $(@).parent() sb = outer.parent() - trace = '#panel' + sb.attr('panel_id') + 'speech_balloon' + sb.attr('element_id') + 'balloon' + sb.attr('balloon_id') - img = $(trace) + trace = element_tag_id(sb) + img = $('.pettanr-balloon', $(trace)) $('.ui-resizable-handle', img.parent()).map -> $(@).css('display', 'block') $(@).mouseout -> outer = $(@).parent() sb = outer.parent() - trace = '#panel' + sb.attr('panel_id') + 'speech_balloon' + sb.attr('element_id') + 'balloon' + sb.attr('balloon_id') - img = $(trace) + trace = element_tag_id(sb) + img = $('.pettanr-balloon', $(trace)) $('.ui-resizable-handle', img.parent()).map -> $(@).css('display', 'none') # sync view $('input').map -> - if $(@).attr('element_type') and $(@).attr('element_type') == 'speech_balloon' + if element_is('speech_balloon', $(@)) $(@).focusout -> switch $(@).attr('column') when 'x' @@ -296,41 +303,41 @@ $ -> trace = element_tag_id($(@)) v = parseInt($(@).val()) $(trace).width(Math.abs(v)) - trace = '#panel' + $(trace).attr('panel_id') + 'speech_balloon' + $(trace).attr('element_id') + 'balloon' + $(trace).attr('balloon_id') - $(trace).parent().width(v) - $(trace).width(v) + img = $('.pettanr-balloon', $(trace)) + img.parent().width(v) + img.width(v) when 'height' trace = element_tag_id($(@)) v = parseInt($(@).val()) $(trace).height(Math.abs(v)) - trace = '#panel' + $(trace).attr('panel_id') + 'speech_balloon' + $(trace).attr('element_id') + 'balloon' + $(trace).attr('balloon_id') - $(trace).parent().height(v) - $(trace).height(v) + img = $('.pettanr-balloon', $(trace)) + img.parent().height(v) + img.height(v) else else $('textarea').map -> - if $(@).attr('element_type') and $(@).attr('element_type') == 'speech_balloon' + if element_is('speech_balloon', $(@)) $(@).focusout -> switch $(@).attr('column') when 'content' - trace = '#panel' + $(@).attr('panel_id') + 'speech_balloon' + $(@).attr('element_id') + 'speech' + $(@).attr('element_part_id') + trace = element_part_tag_id($(@)) wf_sel = $(trace + 'writing_format_id') wf_id = parseInt(wf_sel.val()) v = writing_format_functions[writing_formats[wf_id]]($(@).val()) - $(trace).html(v) + $(trace).html('

' + v + '

') $('textarea').map -> - if $(@).attr('element_type') and $(@).attr('element_type') == 'speech_balloon' + if element_is('speech_balloon', $(@)) switch $(@).attr('column') when 'content' $(@).bind('textchange', (event, previousText) -> - trace = '#panel' + $(@).attr('panel_id') + 'speech_balloon' + $(@).attr('element_id') + 'speech' + $(@).attr('element_part_id') + trace = element_part_tag_id($(@)) wf_sel = $(trace + 'writing_format_id') wf_id = parseInt(wf_sel.val()) v = writing_format_functions[writing_formats[wf_id]]($(@).val()) $(trace).html('

' + v + '

') ) $('select').map -> - if $(@).attr('element_type') and $(@).attr('element_type') == 'speech_balloon' + if element_is('speech_balloon', $(@)) $(@).change -> switch $(@).attr('column') when 'font_size' @@ -342,24 +349,24 @@ $ -> $(trace).css('text-align', text_align_texts[v]) else $('input').map -> - if $(@).attr('element_type') and $(@).attr('element_type') == 'speech_balloon' and $(@).attr('column') == 'fore_color' - $(@).hide() + if element_is('speech_balloon', $(@)) + if $(@).attr('column') == 'fore_color' + $(@).hide() # ground-picture # sync view $('input').map -> - if $(@).attr('element_type') and $(@).attr('element_type') == 'ground_picture' + if element_is('ground_picture', $(@)) $(@).focusout -> switch $(@).attr('column') when 'x', 'y' trace = element_tag_id($(@)) - x = parseInt($(@).val()).toString() + 'px' - ytrace = '#panel' + $(@).attr('panel_id') + 'ground_picture' + $(@).attr('element_id') + 'y' - y = parseInt($(ytrace).val()).toString() + 'px' - $(trace).css('background-position', y + ' ' + x) + x = parseInt($(trace + 'x').val()).toString() + 'px' + y = parseInt($(trace + 'y').val()).toString() + 'px' + $(trace).css('background-position', x + ' ' + y) $('select').map -> - if $(@).attr('element_type') and $(@).attr('element_type') == 'ground_picture' + if element_is('ground_picture', $(@)) $(@).change -> switch $(@).attr('column') when 'repeat' @@ -372,8 +379,9 @@ $ -> # ground_color # sync view $('input').map -> - if $(@).attr('element_type') and $(@).attr('element_type') == 'ground_color' and $(@).attr('column') == 'code' - $(@).hide() + if element_is('ground_color', $(@)) + if $(@).attr('column') == 'code' + $(@).hide() color_slider_change = (trace) -> red = $(trace + 'code_red').slider('value') @@ -387,12 +395,12 @@ $ -> $(trace + 'code_hex').html('HEX: #' + h) $(trace + 'code_rgb').html('RGB: (' + red + ',' + green + ',' +blue + ')') v = '#' + h - if is_element_part($(trace)) - $(trace + 'fore_color').val(code) - $(trace).css('color', v) - else + if $(trace).attr('element_type') == 'ground_color' $(trace + 'code').val(code) $(trace).css('background-color', v) + else + $(trace + 'fore_color').val(code) + $(trace).css('color', v) $('.colorpicker').map -> if is_element_part($(@)) @@ -574,7 +582,7 @@ $ -> # disable form z t $('input').map -> - if $(@).attr('element_type') and $(@).attr('column') + if is_element($(@)) switch $(@).attr('column') when 'z' , 't' $(@).hide() diff --git a/app/models/balloon.rb b/app/models/balloon.rb index 1bf9ceaa..fbfc8bff 100644 --- a/app/models/balloon.rb +++ b/app/models/balloon.rb @@ -22,6 +22,14 @@ class Balloon < ActiveRecord::Base end end + def self.colum_structures + @@colum_structures ||= { + :r => { + :helper => 'panels/tail_angle_helper' + } + } + end + def url '/system_pictures/' + self.system_picture.filename end diff --git a/app/models/ground_color.rb b/app/models/ground_color.rb index bbf59f8d..7f1a3f06 100644 --- a/app/models/ground_color.rb +++ b/app/models/ground_color.rb @@ -18,6 +18,14 @@ class GroundColor < ActiveRecord::Base end end + def self.colum_structures + @@colum_structures ||= { + :code => { + :helper => 'panels/color_helper' + } + } + end + def self.list_opt_for_panel { :ground_colors => { diff --git a/app/models/panel_picture.rb b/app/models/panel_picture.rb index 4d0356e5..32468bcc 100644 --- a/app/models/panel_picture.rb +++ b/app/models/panel_picture.rb @@ -23,6 +23,17 @@ class PanelPicture < ActiveRecord::Base end end + def self.colum_structures + @@colum_structures ||= { + :width => { + :helper => 'panels/size_helper' + }, + :height => { + :helper => 'panels/size_helper' + } + } + end + def self.list_opt_for_panel { :panel_pictures => { diff --git a/app/models/speech.rb b/app/models/speech.rb index 68b85620..3622ff91 100644 --- a/app/models/speech.rb +++ b/app/models/speech.rb @@ -26,6 +26,14 @@ class Speech < ActiveRecord::Base end end + def self.colum_structures + @@colum_structures ||= { + :fore_color => { + :helper => 'panels/color_helper' + } + } + end + def supply_default self.x = 0 self.y = 0 diff --git a/app/views/ground_colors/_element.html.erb b/app/views/ground_colors/_element.html.erb index a6295284..5d1d59f3 100644 --- a/app/views/ground_colors/_element.html.erb +++ b/app/views/ground_colors/_element.html.erb @@ -1,3 +1,3 @@ -
class="pettanr-comic-ground-color" style="<%= elm.opt_div_style(spot) -%>"> +
class="pettanr-comic-ground-color" style="<%= elm.opt_div_style(spot) -%>">
diff --git a/app/views/ground_colors/_form.html.erb b/app/views/ground_colors/_form.html.erb index 1a0cc892..55f51a24 100644 --- a/app/views/ground_colors/_form.html.erb +++ b/app/views/ground_colors/_form.html.erb @@ -7,22 +7,10 @@
<%= f.label :code %> - <%= f.text_field :code, elm.field_tag_attributes(:code, no_attr, :size => 8 ) %> -
-
> -
-
-
-
-
-
-
-
-
-
-
-
-
+ <%= f.text_field :code, elm.field_tag_attributes(:code, no_attr, :size => 8) %> + <% if elm.has_helper?(:code) %> + <%= render elm.form_helper_template(:code), :elm => elm, :no_attr => no_attr %> + <% end %>
<%= f.label :z %> diff --git a/app/views/ground_pictures/_form.html.erb b/app/views/ground_pictures/_form.html.erb index f3f6c28f..5422d57e 100644 --- a/app/views/ground_pictures/_form.html.erb +++ b/app/views/ground_pictures/_form.html.erb @@ -7,7 +7,7 @@
<%= f.label :repeat %> - <%= f.collection_select :repeat, t_select_items(MagicNumber['ground_picture_repeat_items']), :last, :first, {:html => {:selected => elm.repeat}}, elm.field_tag_attributes(:repeat, no_attr) %> + <%= f.collection_select :repeat, t_select_items(MagicNumber['ground_picture_repeat_items']), *elm.select_tag_attributes(elm.repeat, :repeat, no_attr) %>
<%= f.label :x %> diff --git a/app/views/panel_pictures/_form.html.erb b/app/views/panel_pictures/_form.html.erb index f351d610..e0c7047c 100644 --- a/app/views/panel_pictures/_form.html.erb +++ b/app/views/panel_pictures/_form.html.erb @@ -22,12 +22,16 @@
<%= f.label :width %> <%= f.number_field :width, elm.field_tag_attributes(:width, no_attr, :size => 5) %> - <%= content_tag :span, '', elm.tag_attributes('', :class => "panel_picture_width_tool") %> + <% if elm.has_helper?(:width) %> + <%= render elm.form_helper_template(:width), :elm => elm, :no_attr => no_attr, :opt => {:class => "panel_picture_width_tool"} %> + <% end %>
<%= f.label :height %> <%= f.number_field :height, elm.field_tag_attributes(:height, no_attr, :size => 5) %> - <%= content_tag :span, '', elm.tag_attributes('', :class => "panel_picture_height_tool") %> + <% if elm.has_helper?(:height) %> + <%= render elm.form_helper_template(:height), :elm => elm, :no_attr => no_attr, :opt => {:class => "panel_picture_height_tool"} %> + <% end %>
<%= f.label :link %> diff --git a/app/views/panels/_color_helper.html.erb b/app/views/panels/_color_helper.html.erb new file mode 100644 index 00000000..a2ecf68f --- /dev/null +++ b/app/views/panels/_color_helper.html.erb @@ -0,0 +1,15 @@ +
+
> +
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/views/panels/_size_helper.html.erb b/app/views/panels/_size_helper.html.erb new file mode 100644 index 00000000..f27574ac --- /dev/null +++ b/app/views/panels/_size_helper.html.erb @@ -0,0 +1 @@ +<%= content_tag :span, '', elm.tag_attributes('', :class => opt[:class]) %> diff --git a/app/views/panels/_tail_angle_helper.html.erb b/app/views/panels/_tail_angle_helper.html.erb new file mode 100644 index 00000000..be9300c7 --- /dev/null +++ b/app/views/panels/_tail_angle_helper.html.erb @@ -0,0 +1,5 @@ + diff --git a/lib/element.rb b/lib/element.rb index 4f7a4ecd..dc9b2c6b 100644 --- a/lib/element.rb +++ b/lib/element.rb @@ -5,6 +5,10 @@ module Element end module ClassMethods + def colum_structures + raise + end + def list_opt_for_panel {} end @@ -40,6 +44,10 @@ module Element @parts ||= [] end + def has_helper? column_name + self.class.colum_structures[column_name] and self.class.colum_structures[column_name][:helper] + end + def new_index @new_index end @@ -100,6 +108,10 @@ module Element self.path_name(with_engine) + '/element_face' end + def form_helper_template(colum_name) + self.class.colum_structures[colum_name][:helper] + end + def tag_attributes column = nil, opt = {} { :id => self.field_tag_id(column), :panel_id => self.tag_panel_id, @@ -122,6 +134,14 @@ module Element r.merge(opt) end + def select_tag_attributes(selected, column, no_attr) + [ + :last, :first, + {:html => {:selected => selected}}, + self.field_tag_attributes(column, no_attr) + ] + end + def tag_attr column = nil, opt = {} self.tag_attributes(column, opt).to_attr end diff --git a/lib/element_part.rb b/lib/element_part.rb index ee685800..0a1a69f1 100644 --- a/lib/element_part.rb +++ b/lib/element_part.rb @@ -5,6 +5,10 @@ module ElementPart end module ClassMethods + def colum_structures + raise + end + def list_opt_for_panel {} end @@ -32,6 +36,10 @@ module ElementPart false end + def has_helper? column_name + self.class.colum_structures[column_name] and self.class.colum_structures[column_name][:helper] + end + def new_parent @new_parent end @@ -93,6 +101,10 @@ module ElementPart end end + def form_helper_template(colum_name) + self.class.colum_structures[colum_name][:helper] + end + def tag_attributes column = nil, opt = {} { :id => self.field_tag_id(column), :panel_id => self.tag_panel_id, @@ -116,6 +128,14 @@ module ElementPart r.merge(opt) end + def select_tag_attributes(selected, column, no_attr) + [ + :last, :first, + {:html => {:selected => selected}}, + self.field_tag_attributes(column, no_attr) + ] + end + def tag_attr column = nil, opt = {} self.tag_attributes(column, opt).to_attr end -- 2.11.0