OSDN Git Service

t#31725:add panel inspire
[pettanr/pettanr.git] / app / models / ground_color.rb
index 59bdbf6..810ffb5 100644 (file)
@@ -1,12 +1,28 @@
 class GroundColor < ActiveRecord::Base
+  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 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 +39,35 @@ 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.panel.width}px; height:#{self.panel.height}px; z-index:#{self.z}; background-color:##{format("%06x", self.code)}; #{o}"
+  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.panel.new_record? ? '0' : self.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 field_tree f
+    'panels-' + self.tag_panel_id + '-ground_colors_attributes-' + self.tag_element_id + '-' + f.to_s
+  end
+  
   def self.default_page_size
     25
   end
@@ -120,26 +165,23 @@ class GroundColor < ActiveRecord::Base
   end
   
   def remove au
-    d = false
-    ground_colors_attributes = {}
-    self.panel.ground_colors.each do |ground_color|
-      attr = ground_color.attributes
-      if ground_color == self
-        attr['_destroy'] = true
-        d = true
-      else
-        if d
-          attr['t']  -= 1 
-        end
-      end
-      ground_colors_attributes[ground_color.id] = attr
-    end
-    self.panel.attributes = {:ground_colors_attributes => ground_colors_attributes}
-    self.panel.store({}, au)
+    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