OSDN Git Service

t#31834:new base
[pettanr/pettanr.git] / app / models / ground_color.rb
index e1452d3..7117e4b 100644 (file)
@@ -1,12 +1,52 @@
 class GroundColor < ActiveRecord::Base
+  include Element
+  include ElementInspire
   belongs_to :panel
   belongs_to :color
   
   validates :panel_id, :numericality => {:allow_blank => true}
   validates :code, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
   validates :z, :presence => true, :numericality => {:greater_than => 0}
+  validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
+  
+  before_validation :valid_encode
+  
+  def valid_encode
+    ['caption'].each do |a|
+      next if attributes[a] == nil
+      raise Pettanr::BadRequest unless attributes[a].valid_encoding?
+    end
+  end
+  
+  def self.list_opt_for_panel
+    {
+      :ground_colors => {
+      }
+    }
+  end
+  
+  def self.show_opt_for_panel
+    {
+      :ground_colors => {
+      }
+    }
+  end
+  
+  def self.json_opt_for_panel
+    {
+    }
+  end
+  
+  def self.has_picture?
+    false
+  end
   
   def supply_default
+    self.code ||= 0
+    if self.panel
+      self.t = self.panel.new_t 
+      self.z = self.panel.new_z 
+    end
   end
   
   def overwrite pid
@@ -23,6 +63,55 @@ class GroundColor < ActiveRecord::Base
     self.panel.visible? roles
   end
   
+  def opt_div_style spot = nil, opacity = 20
+    o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
+    "position: absolute; width:#{self.get_panel.width}px; height:#{self.get_panel.height}px; z-index:#{self.z}; background-color:##{format("%06x", self.code)}; #{o}"
+  end
+  
+  def new_index
+    @new_index
+  end
+  
+  def new_index= v
+    @new_index = v
+  end
+  
+  def new_panel
+    @new_panel
+  end
+  
+  def new_panel= v
+    @new_panel = v
+  end
+  
+  def get_panel
+    self.panel || @new_panel
+  end
+  
+  def tag_id c = nil
+    'panel' + tag_panel_id + 'ground_color' + tag_element_id + c.to_s
+  end
+  
+  def field_tag_id f
+    self.tag_id + f.to_s
+  end
+  
+  def tag_panel_id
+    self.get_panel.new_record? ? '0' : self.get_panel.id.to_s
+  end
+  
+  def tag_element_id
+    self.new_record? ? '0' : self.id.to_s
+  end
+  
+  def tag_element_type
+    'ground_color'
+  end
+  
+  def tag_new_index
+    self.new_index.to_s
+  end
+  
   def self.default_page_size
     25
   end
@@ -120,29 +209,23 @@ class GroundColor < ActiveRecord::Base
   end
   
   def remove au
-    PanelPicture.transaction do
-      d = false
-      panel_pictures_attributes = {}
-      
-      self.panel.panel_pictures.each do |panel_picture|
-        attr = panel_picture.attributes
-        if panel_picture == self
-          attr['_destroy'] = true
-          d = true
-        else
-          if d
-            attr['t']  -= 1 
-          end
-        end
-        panel_pictures_attributes[panel_picture.id] = attr
-      end
-      self.panel.attributes = {:panel_pictures_attributes => panel_pictures_attributes}
-      self.panel.store({}, au)
-    end
+    self.panel.remove_element(self, au)
   end
   
   def scenario
-    ''
+    if caption.blank?
+      ''
+    else
+      '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
+    end
+  end
+  
+  def plain_scenario
+    if caption.blank?
+      ''
+    else
+      self.caption + "\n"
+    end
   end
   
 end