OSDN Git Service

fix leaf sort
[pettanr/pettanr.git] / app / models / panel_picture.rb
1 class PanelPicture < Peta::Element
2   load_manifest
3   belongs_to :panel
4   belongs_to :picture
5   
6   validates :panel_id, :numericality => {:allow_blank => true}
7   validates :picture_id, :numericality => true, :existence => {:both => false}
8   validates :link, :length => {:maximum => 200}, :url => {:allow_blank => true, :message => I18n.t('errors.messages.url')}
9   validates :x, :presence => true, :numericality => true
10   validates :y, :presence => true, :numericality => true
11   validates :width, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :sync_vh => true
12   validates :height, :presence => true, :numericality => true, :not_zero => true, :reverse => true, :sync_vh => true
13   validates :z, :presence => true, :numericality => {:greater_than => 0}
14   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
15   
16   def self.by_author_list_includes
17     {
18       :panel => {
19         :author => {}
20       }
21     }
22   end
23   
24   def self.list_opt_for_panel
25     {
26       :panel_pictures => {
27         :picture => {:artist => {}, :license => {}}
28       }
29     }
30   end
31   
32   def self.show_opt_for_panel
33     {
34       :panel_pictures => {
35         :picture => {:artist => {}, :license => {}}
36       }
37     }
38   end
39   
40   def self.json_opt_for_panel
41     {
42       :picture => {:artist => {}, :license => {}}
43     }
44   end
45   
46   def self.has_picture?
47     true
48   end
49   
50   def supply_default
51     self.x = 0
52     self.y = 0
53     if self.picture
54       self.width = self.picture.width 
55       self.height = self.picture.height 
56     end
57     if self.panel
58       self.t = self.panel.new_t 
59       self.z = self.panel.new_z 
60     end
61   end
62   
63   def overwrite  pid
64     self.panel_id = pid
65   end
66   
67   def flip
68     res = (self.height > 0 ? '' : 'v') + (self.width > 0 ? '' : 'h')
69     res = res # format of /1.png?subdir=v
70     # res += '/' unless res.empty? # format of /v/1.png
71     res
72   end
73   
74   def filename
75     self.picture.filename + '?subdir=' + self.flip
76   end
77   
78   def url
79     '/pictures/' + self.filename
80   end
81   
82   def opt_div_style
83     "top:#{self.y}px; left:#{self.x}px; z-index:#{self.z}; position: absolute;"
84   end
85   
86   def opt_img_tag spot = nil, opacity = 20
87     o = (spot and spot != self) ? "opacity: #{opacity.to_f/100}; filter:alpha(opacity=#{opacity});" : ''
88     self.tag_attributes(:img, {
89       :class => "panel-picture", :vPicture => self.id, 
90       :src => self.url, :width => self.width.abs, :height => self.height.abs, :picture_id => self.picture_id, :ext => self.picture.ext, :alt => self.caption, 
91       :style => "#{o}"
92     })
93   end
94   
95   def tmb_opt_img_tag
96     tw, th = PettanImager.thumbnail_size(self.width.abs, self.height.abs)
97     {:src => self.url, :width => tw, :height => th, :alt => self.caption}
98   end
99   
100   def symbol_option
101     self.picture.symbol_option
102   end
103   
104   def self.public_list_where
105     'panels.publish > 0'
106   end
107   
108   def self.list_order
109     'panel_pictures.updated_at desc'
110   end
111   
112   def self.list_opt
113     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
114   end
115   
116   def self.list_json_opt
117     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
118   end
119   
120   def self.show_opt
121     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
122   end
123   
124   def self.show_json_opt
125     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
126   end
127   
128   def store operators
129     if self.new_record?
130       self.panel.panel_pictures.build(self.attributes)
131     else
132       self.panel.panel_pictures.each do |panel_picture|
133         next unless panel_picture == self
134         attr = self.attributes
135         attr.delete 'id'
136         panel_picture.attributes = attr
137         break
138       end
139     end
140     self.panel.store({}, operators)
141   end
142   
143   def remove operators
144     self.panel.remove_element(self, operators)
145   end
146   
147   def scenario
148     if caption.blank?
149       ''
150     else
151       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
152     end
153   end
154   
155   def plain_scenario
156     if caption.blank?
157       ''
158     else
159       self.caption + "\n"
160     end
161   end
162   
163 end