OSDN Git Service

t#31945:separate js
[pettanr/pettanr.git] / app / models / panel_picture.rb
index 3f496c7..32468bc 100644 (file)
@@ -1,4 +1,6 @@
 class PanelPicture < ActiveRecord::Base
+  include Element
+  include ElementInspire
   belongs_to :panel
   belongs_to :picture
   
@@ -21,6 +23,43 @@ 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 => {
+        :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)
@@ -31,6 +70,23 @@ class PanelPicture < ActiveRecord::Base
     self.panel.visible? roles
   end
   
+  def supply_default
+    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  pid
+    self.panel_id = pid
+  end
+  
   def flip
     res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h')
     res += '/' unless res.empty?
@@ -45,8 +101,16 @@ class PanelPicture < ActiveRecord::Base
     '/pictures/' + self.filename
   end
   
-  def opt_img_tag
-    {: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}; "}
+  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});" : ''
+    {: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
@@ -54,6 +118,13 @@ class PanelPicture < ActiveRecord::Base
     {:src => self.url, :width => tw, :height => th, :alt => self.caption}
   end
   
+  def boost
+  end
+  
+  def tag_element_type
+    'panel_picture'
+  end
+  
   def self.default_page_size
     25
   end
@@ -75,32 +146,44 @@ class PanelPicture < ActiveRecord::Base
     page_size
   end
   
+  def self.list_where
+    'panels.publish > 0'
+  end
+  
+  def self.mylist_where au
+    ['panels.author_id = ?', au.id]
+  end
+  
+  def self.himlist_where au
+    ['panels.author_id = ? and panels.publish > 0', au.id]
+  end
+  
   def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(PanelPicture.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => 'panels.publish > 0', :order => 'panel_pictures.updated_at desc'})
-    PanelPicture.find(:all, opt)
+    PanelPicture.where(self.list_where()).includes(PanelPicture.list_opt).order('panel_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
   end
   
   def self.mylist au, page = 1, page_size = Author.default_panel_picture_page_size
-    opt = {}
-    opt.merge!(PanelPicture.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'panel_pictures.updated_at desc'})
-    PanelPicture.find(:all, opt)
+    PanelPicture.where(self.mylist_where(au)).includes(PanelPicture.list_opt).order('panel_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
   end
   
   def self.himlist au, page = 1, page_size = Author.default_panel_picture_page_size
-    opt = {}
-    opt.merge!(self.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['panels.author_id = ? and panels.publish > 0', au.id], :order => 'panel_pictures.updated_at desc'})
-    PanelPicture.find(:all, opt)
+    PanelPicture.where(self.himlist_where(au)).includes(PanelPicture.list_opt).order('panel_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.list_paginate page = 1, page_size = self.default_page_size
+    Kaminari.paginate_array(Array.new(PanelPicture.where(self.list_where()).includes(PanelPicture.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.mylist_paginate au, page = 1, page_size = Author.default_panel_picture_page_size
+    Kaminari.paginate_array(Array.new(PanelPicture.where(self.mylist_where(au)).includes(PanelPicture.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.himlist_paginate au, page = 1, page_size = Author.default_panel_picture_page_size
+    Kaminari.paginate_array(Array.new(PanelPicture.where(self.himlist_where(au)).includes(PanelPicture.list_opt).count, nil)).page(page).per(page_size)
   end
   
   def self.list_opt
-    {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
+    {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
   end
   
   def self.list_json_opt
@@ -123,6 +206,25 @@ class PanelPicture < ActiveRecord::Base
     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
   end
   
+  def store au
+    if self.new_record?
+      self.panel.panel_pictures.build(self.attributes)
+    else
+      self.panel.panel_pictures.each do |panel_picture|
+        next unless panel_picture == self
+        attr = self.attributes
+        attr.delete 'id'
+        panel_picture.attributes = attr
+        break
+      end
+    end
+    self.panel.store({}, au)
+  end
+  
+  def remove au
+    self.panel.remove_element(self, au)
+  end
+  
   def scenario
     if caption.blank?
       ''