OSDN Git Service

t#31896:refact elm
authoryasushiito <yas@pen-chan.jp>
Sat, 24 Aug 2013 06:52:33 +0000 (15:52 +0900)
committeryasushiito <yas@pen-chan.jp>
Sat, 24 Aug 2013 06:52:33 +0000 (15:52 +0900)
14 files changed:
app/assets/javascripts/panels.js.coffee
app/models/balloon.rb
app/models/ground_color.rb
app/models/panel_picture.rb
app/models/speech.rb
app/views/ground_colors/_element.html.erb
app/views/ground_colors/_form.html.erb
app/views/ground_pictures/_form.html.erb
app/views/panel_pictures/_form.html.erb
app/views/panels/_color_helper.html.erb [new file with mode: 0644]
app/views/panels/_size_helper.html.erb [new file with mode: 0644]
app/views/panels/_tail_angle_helper.html.erb [new file with mode: 0644]
lib/element.rb
lib/element_part.rb

index 1057fc3..ec8547a 100644 (file)
@@ -40,6 +40,11 @@ $ ->
       return true\r
     else\r
       return false\r
+  element_is = (element_type, o) ->\r
+    if is_element(o) and o.attr('element_type') == element_type\r
+      return true\r
+    else\r
+      return false\r
   element_types  = {'panel_picture': 'panel_pictures', 'speech_balloon': 'speech_balloons', 'ground_picture': 'ground_pictures', 'ground_color': 'ground_colors'}\r
   element_type_to_table = (element_type) ->\r
     element_types[element_type]\r
@@ -160,15 +165,15 @@ $ ->
       }\r
   # sync view \r
   $('#panel' + pettanr_current_panel_id.toString() +  'width').focusout ->\r
-    trace = '#panel' + $(@).attr('panel_id')\r
+    trace = panel_tag_id($(@))\r
     $(trace).width(parseInt( $(@).val()))\r
   \r
   $('#panel' + pettanr_current_panel_id.toString() + 'height').focusout ->\r
-    trace = '#panel' + $(@).attr('panel_id')\r
+    trace = panel_tag_id($(@))\r
     $(trace).height(parseInt( $(@).val()))\r
   \r
   $('#panel' + pettanr_current_panel_id.toString() + 'border').focusout ->\r
-    trace = '#panel' + $(@).attr('panel_id')\r
+    trace = panel_tag_id($(@))\r
     $(trace).css('border-width', parseInt($(@).val()).toString() + 'px')\r
   \r
   \r
@@ -176,10 +181,10 @@ $ ->
   $('.pettanr-panel-picture-wrapper').map ->\r
     $(@).draggable {\r
       stop: ( event, ui ) ->\r
-        trace = '#panel' + $(@).attr('panel_id') + 'panel_picture' + $(@).attr('element_id')\r
-        img = '#panel' + $(@).attr('panel_id') + 'panel_picture' + $(@).attr('element_id') + 'img'\r
-        left = $(img).parent().position().left + $(@).position().left\r
-        top = $(img ).parent().position().top + $(@).position().top\r
+        trace = element_tag_id($(@))\r
+        img = $(trace + 'img')\r
+        left = img.parent().position().left + $(@).position().left\r
+        top = img.parent().position().top + $(@).position().top\r
         $(trace + 'x').val(parseInt(left))\r
         $(trace + 'y').val(parseInt(top))\r
     }\r
@@ -187,7 +192,7 @@ $ ->
   $('.panel-picture').map ->\r
     $(@).resizable {\r
       stop: ( event, ui ) ->\r
-        trace = '#panel' + $(@).parent().attr('panel_id') + 'panel_picture' + $(@).parent().attr('element_id')\r
+        trace = element_tag_id($(@).parent())\r
         $(trace + 'width').val($(@).width())\r
         $(trace + 'height').val($(@).height())\r
       handles: 'all',\r
@@ -210,7 +215,7 @@ $ ->
   \r
   # sync view \r
   $('input').map  ->\r
-    if $(@).attr('element_type') and $(@).attr('element_type') == 'panel_picture'\r
+    if element_is('panel_picture', $(@))\r
       $(@).focusout ->\r
         switch $(@).attr('column')\r
           when 'x'\r
@@ -241,24 +246,26 @@ $ ->
   $('.pettanr-comic-balloon' ).map ->\r
     $(@).draggable {\r
       stop: ( event, ui ) ->\r
-        trace = '#panel' + $(@).attr('panel_id') + 'speech_balloon' + $(@).attr('element_id') + 'balloon' + $(@).attr('balloon_id')\r
+        img = $('.pettanr-balloon', $(@))\r
+        trace = element_part_tag_id(img)\r
         left = $(@).position().left\r
         top = $(@).position().top\r
         $(trace + 'x').val(parseInt(left))\r
         $(trace + 'y').val(parseInt(top))\r
     }\r
-\r
   \r
   $('.pettanr-balloon' ).map ->\r
     $(@).resizable {\r
       stop: ( event, ui ) ->\r
-        trace = '#panel' + $(@).parent().attr('panel_id') + 'speech_balloon' + $(@).parent().attr('element_id') + 'balloon' + $(@).parent().attr('balloon_id')\r
+        img = $('.pettanr-balloon', $(@))\r
+        trace = element_part_tag_id(img)\r
         $(trace + 'width').val($(@).width())\r
         $(trace + 'height').val($(@).height())\r
      resize: ( event, ui ) ->\r
-        trace = '#panel' + $(@).parent().attr('panel_id') + 'speech_balloon' + $(@).parent().attr('element_id')\r
-        $(trace).width($(@).width())\r
-        $(trace).height($(@).height())\r
+        img = $('.pettanr-balloon', $(@))\r
+        sb = element_tag_id(img)\r
+        $(sb).width($(@).width())\r
+        $(sb).height($(@).height())\r
       handles: 'all',\r
       autoHide: true\r
     }\r
@@ -266,22 +273,22 @@ $ ->
     $(@).mouseover -> \r
       outer = $(@).parent()\r
       sb = outer.parent()\r
-      trace = '#panel' + sb.attr('panel_id') + 'speech_balloon' + sb.attr('element_id') + 'balloon' + sb.attr('balloon_id')\r
-      img = $(trace)\r
+      trace = element_tag_id(sb)\r
+      img = $('.pettanr-balloon', $(trace))\r
       $('.ui-resizable-handle', img.parent()).map ->\r
         $(@).css('display', 'block')\r
     $(@).mouseout -> \r
       outer = $(@).parent()\r
       sb = outer.parent()\r
-      trace = '#panel' + sb.attr('panel_id') + 'speech_balloon' + sb.attr('element_id') + 'balloon' + sb.attr('balloon_id')\r
-      img = $(trace)\r
+      trace = element_tag_id(sb)\r
+      img = $('.pettanr-balloon', $(trace))\r
       $('.ui-resizable-handle', img.parent()).map ->\r
         $(@).css('display', 'none')\r
         \r
   \r
   # sync view \r
   $('input').map  ->\r
-    if $(@).attr('element_type') and $(@).attr('element_type') == 'speech_balloon'\r
+    if element_is('speech_balloon', $(@))\r
       $(@).focusout ->\r
         switch $(@).attr('column')\r
           when 'x'\r
@@ -296,41 +303,41 @@ $ ->
             trace = element_tag_id($(@))\r
             v = parseInt($(@).val())\r
             $(trace).width(Math.abs(v))\r
-            trace = '#panel' + $(trace).attr('panel_id') + 'speech_balloon' + $(trace).attr('element_id') + 'balloon' + $(trace).attr('balloon_id')\r
-            $(trace).parent().width(v)\r
-            $(trace).width(v)\r
+            img = $('.pettanr-balloon', $(trace))\r
+            img.parent().width(v)\r
+            img.width(v)\r
           when 'height'\r
             trace = element_tag_id($(@))\r
             v = parseInt($(@).val())\r
             $(trace).height(Math.abs(v))\r
-            trace = '#panel' + $(trace).attr('panel_id') + 'speech_balloon' + $(trace).attr('element_id') + 'balloon' + $(trace).attr('balloon_id')\r
-            $(trace).parent().height(v)\r
-            $(trace).height(v)\r
+            img = $('.pettanr-balloon', $(trace))\r
+            img.parent().height(v)\r
+            img.height(v)\r
           else\r
     else\r
   $('textarea').map  ->\r
-    if $(@).attr('element_type') and $(@).attr('element_type') == 'speech_balloon'\r
+    if element_is('speech_balloon', $(@))\r
       $(@).focusout ->\r
         switch $(@).attr('column')\r
           when 'content'\r
-            trace = '#panel' + $(@).attr('panel_id') + 'speech_balloon' + $(@).attr('element_id') + 'speech' + $(@).attr('element_part_id')\r
+            trace = element_part_tag_id($(@))\r
             wf_sel = $(trace + 'writing_format_id')\r
             wf_id = parseInt(wf_sel.val())\r
             v = writing_format_functions[writing_formats[wf_id]]($(@).val())\r
-            $(trace).html(v)\r
+            $(trace).html('<p>' + v + '</p>')\r
   $('textarea').map  ->\r
-    if $(@).attr('element_type') and $(@).attr('element_type') == 'speech_balloon'\r
+    if element_is('speech_balloon', $(@))\r
       switch $(@).attr('column')\r
         when 'content'\r
           $(@).bind('textchange', (event, previousText) ->\r
-            trace = '#panel' + $(@).attr('panel_id') + 'speech_balloon' + $(@).attr('element_id') + 'speech' + $(@).attr('element_part_id')\r
+            trace = element_part_tag_id($(@))\r
             wf_sel = $(trace + 'writing_format_id')\r
             wf_id = parseInt(wf_sel.val())\r
             v = writing_format_functions[writing_formats[wf_id]]($(@).val())\r
             $(trace).html('<p>' + v + '</p>')\r
           )\r
   $('select').map  ->\r
-    if $(@).attr('element_type') and $(@).attr('element_type') == 'speech_balloon'\r
+    if element_is('speech_balloon', $(@))\r
       $(@).change ->\r
         switch $(@).attr('column')\r
           when 'font_size'\r
@@ -342,24 +349,24 @@ $ ->
             $(trace).css('text-align', text_align_texts[v])\r
     else\r
   $('input').map  ->\r
-    if $(@).attr('element_type') and $(@).attr('element_type') == 'speech_balloon' and $(@).attr('column') == 'fore_color'\r
-      $(@).hide()\r
+    if element_is('speech_balloon', $(@))\r
+      if $(@).attr('column') == 'fore_color'\r
+        $(@).hide()\r
 \r
   \r
   # ground-picture\r
   # sync view \r
   $('input').map  ->\r
-    if $(@).attr('element_type') and $(@).attr('element_type') == 'ground_picture'\r
+    if element_is('ground_picture', $(@))\r
       $(@).focusout ->\r
         switch $(@).attr('column')\r
           when 'x', 'y'\r
             trace = element_tag_id($(@))\r
-            x = parseInt($(@).val()).toString() + 'px'\r
-            ytrace = '#panel' + $(@).attr('panel_id') + 'ground_picture' + $(@).attr('element_id') + 'y'\r
-            y = parseInt($(ytrace).val()).toString() + 'px'\r
-            $(trace).css('background-position', y + ' ' + x)\r
+            x = parseInt($(trace + 'x').val()).toString() + 'px'\r
+            y = parseInt($(trace + 'y').val()).toString() + 'px'\r
+            $(trace).css('background-position', x + ' ' + y)\r
   $('select').map  ->\r
-    if $(@).attr('element_type') and $(@).attr('element_type') == 'ground_picture'\r
+    if element_is('ground_picture', $(@))\r
       $(@).change ->\r
         switch $(@).attr('column')\r
           when 'repeat'\r
@@ -372,8 +379,9 @@ $ ->
   # ground_color\r
   # sync view \r
   $('input').map  ->\r
-    if $(@).attr('element_type') and $(@).attr('element_type') == 'ground_color' and $(@).attr('column') == 'code'\r
-      $(@).hide()\r
+    if element_is('ground_color', $(@))\r
+      if $(@).attr('column') == 'code'\r
+        $(@).hide()\r
 \r
   color_slider_change = (trace) -> \r
     red = $(trace + 'code_red').slider('value')\r
@@ -387,12 +395,12 @@ $ ->
     $(trace + 'code_hex').html('HEX: #' + h)\r
     $(trace + 'code_rgb').html('RGB: (' + red + ',' + green + ',' +blue + ')')\r
     v = '#' + h\r
-    if is_element_part($(trace))\r
-      $(trace + 'fore_color').val(code)\r
-      $(trace).css('color', v)\r
-    else\r
+    if $(trace).attr('element_type') == 'ground_color'\r
       $(trace + 'code').val(code)\r
       $(trace).css('background-color', v)\r
+    else\r
+      $(trace + 'fore_color').val(code)\r
+      $(trace).css('color', v)\r
   \r
   $('.colorpicker').map -> \r
     if is_element_part($(@))\r
@@ -574,7 +582,7 @@ $ ->
   \r
   # disable form z t\r
   $('input').map  ->\r
-    if $(@).attr('element_type') and $(@).attr('column')\r
+    if is_element($(@))\r
       switch $(@).attr('column')\r
         when 'z' , 't'\r
           $(@).hide()\r
index 1bf9cea..fbfc8bf 100644 (file)
@@ -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
index bbf59f8..7f1a3f0 100644 (file)
@@ -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 => {
index 4d0356e..32468bc 100644 (file)
@@ -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 => {
index 68b8562..3622ff9 100644 (file)
@@ -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
index a629528..5d1d59f 100644 (file)
@@ -1,3 +1,3 @@
-<div <%= raw elm.any_tag_attr(:div) -%> class="pettanr-comic-ground-color" style="<%= elm.opt_div_style(spot) -%>">
+<div <%= raw elm.any_tag_attr() -%> class="pettanr-comic-ground-color" style="<%= elm.opt_div_style(spot) -%>">
 
 </div>
index 1a0cc89..55f51a2 100644 (file)
@@ -7,22 +7,10 @@
   </div>
   <div class="field">
     <%= f.label :code %>
-    <%= f.text_field :code, elm.field_tag_attributes(:code, no_attr, :size => 8 ) %>
-    <div class="colorpicker-wrap">
-      <div class="colorpicker" style="float: left;" <%= raw elm.field_tag_attr(:code_picker, no_attr) -%>>
-        <div id="<%= elm.tag_id(:code_red) -%>" class="colorpicker-red"></div>
-        <div id="<%= elm.tag_id(:code_green) -%>" class="colorpicker-green"></div>
-        <div id="<%= elm.tag_id(:code_blue) -%>" class="colorpicker-blue"></div>
-      </div>
-      <div style="float: left;">
-        <div id="<%= elm.tag_id(:code_swatch) -%>" class="ui-widget-content ui-corner-all code_swatch"></div>
-      </div>
-      <div style="float: left;">
-        <div id="<%= elm.tag_id(:code_hex) -%>"></div>
-        <div id="<%= elm.tag_id(:code_rgb) -%>"></div>
-      </div>
-      <div style="clear: both;"></div>
-    </div>
+    <%= 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 %>
   </div>
   <div class="field">
     <%= f.label :z %>
index f3f6c28..5422d57 100644 (file)
@@ -7,7 +7,7 @@
   </div>
   <div class="field">
     <%= 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) %>
   </div>
   <div class="field">
     <%= f.label :x %>
index f351d61..e0c7047 100644 (file)
   <div class="field">
     <%= 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 %>
   </div>
   <div class="field">
     <%= 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 %>
   </div>
   <div class="field">
     <%= 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 (file)
index 0000000..a2ecf68
--- /dev/null
@@ -0,0 +1,15 @@
+<div class="colorpicker-wrap">
+  <div class="colorpicker" style="float: left;" <%= raw elm.field_tag_attr(:code_picker, no_attr) -%>>
+    <div id="<%= elm.tag_id(:code_red) -%>" class="colorpicker-red"></div>
+    <div id="<%= elm.tag_id(:code_green) -%>" class="colorpicker-green"></div>
+    <div id="<%= elm.tag_id(:code_blue) -%>" class="colorpicker-blue"></div>
+  </div>
+  <div style="float: left;">
+    <div id="<%= elm.tag_id(:code_swatch) -%>" class="ui-widget-content ui-corner-all code_swatch"></div>
+  </div>
+  <div style="float: left;">
+    <div id="<%= elm.tag_id(:code_hex) -%>"></div>
+    <div id="<%= elm.tag_id(:code_rgb) -%>"></div>
+  </div>
+  <div style="clear: both;"></div>
+</div>
diff --git a/app/views/panels/_size_helper.html.erb b/app/views/panels/_size_helper.html.erb
new file mode 100644 (file)
index 0000000..f27574a
--- /dev/null
@@ -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 (file)
index 0000000..be9300c
--- /dev/null
@@ -0,0 +1,5 @@
+<span class="r-wrap" style="display: none;" <%= raw elm.tag_attr -%> r_step="<%= elm.r_step -%>">
+  <button class="r-down" value="0"><</button>
+  <button class="r-up" value="0">></button>
+  <button class="r-reset" value="0">reset</button>
+</span>
index 4f7a4ec..dc9b2c6 100644 (file)
@@ -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
index ee68580..0a1a69f 100644 (file)
@@ -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