OSDN Git Service

t#31779:element lib
[pettanr/pettanr.git] / app / models / panel_picture.rb
index e960e3e..64f990c 100644 (file)
@@ -1,4 +1,6 @@
 class PanelPicture < ActiveRecord::Base
+  include Element
+  include ElementInspire
   belongs_to :panel
   belongs_to :picture
   
@@ -21,6 +23,32 @@ class PanelPicture < ActiveRecord::Base
     end
   end
   
+  def self.list_opt_for_panel
+    {
+      :panel_pictures => {
+        :picture => {:artist => {}, :license => {}}
+      }
+    }
+  end
+  
+  def self.show_opt_for_panel
+    {
+      :panel_pictures => {
+        :picture => {:artist => {}, :license => {}}
+      }
+    }
+  end
+  
+  def self.json_opt_for_panel
+    {
+      :picture => {:artist => {}, :license => {}}
+    }
+  end
+  
+  def self.has_picture?
+    true
+  end
+  
   def visible? roles
     if MagicNumber['run_mode'] == 0
       return false unless guest_role_check(roles)
@@ -32,15 +60,20 @@ class PanelPicture < ActiveRecord::Base
   end
   
   def supply_default
-    self.x = 0 if self.x.blank?
-    self.y = 0 if self.y.blank?
-    self.width = self.picture.width if self.width.blank? and self.picture
-    self.height = self.picture.height if self.height.blank? and self.picture
-    self.t = self.new_t if self.t.blank?
-    self.z = self.new_z if self.z.blank?
+    self.x = 0
+    self.y = 0
+    if self.picture
+      self.width = self.picture.width 
+      self.height = self.picture.height 
+    end
+    if self.panel
+      self.t = self.panel.new_t 
+      self.z = self.panel.new_z 
+    end
   end
   
-  def overwrite 
+  def overwrite  pid
+    self.panel_id = pid
   end
   
   def flip
@@ -57,9 +90,16 @@ class PanelPicture < ActiveRecord::Base
     '/pictures/' + self.filename
   end
   
+  def opt_div_style
+    "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; position: absolute;"
+  end
+  
   def opt_img_tag spot = nil, opacity = 20
-    o = spot and spot != self ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
-    {:src => self.url, :vPicture => self.id, :width => self.width.abs, :height => self.height.abs, :alt => self.caption, :style => "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; #{o}"}
+    o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
+    {:id => self.tag_id(:img), :panel_id => self.tag_panel_id, :element_id => self.tag_element_id, :element_type => self.tag_element_type,
+    :class => "panel-picture", :vPicture => self.id, 
+    :src => self.url, :width => self.width.abs, :height => self.height.abs, :picture_id => self.picture_id, :ext => self.picture.ext, :alt => self.caption, 
+    :style => "#{o}"}
   end
   
   def tmb_opt_img_tag
@@ -67,12 +107,28 @@ class PanelPicture < ActiveRecord::Base
     {:src => self.url, :width => tw, :height => th, :alt => self.caption}
   end
   
-  def new_t
-    self.panel.parts.size
+  def tag_id c = nil
+    'panel' + tag_panel_id + 'panel_picture' + tag_element_id + c.to_s
   end
   
-  def new_z
-    self.panel.panel_elements.size + 1
+  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
+    'panel_picture'
+  end
+  
+  def field_tree f
+    'panels-' + self.tag_panel_id + '-panel_pictures_attributes-' + self.tag_element_id + '-' + f.to_s
   end
   
   def self.default_page_size
@@ -172,22 +228,7 @@ class PanelPicture < ActiveRecord::Base
   end
   
   def remove au
-    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)
+    self.panel.remove_element(self, au)
   end
   
   def scenario