OSDN Git Service

581cfa92f5ca33e02088eef98f840e92b3b5d7da
[pettanr/pettanr.git] / app / models / ground_color.rb
1 class GroundColor < Peta::Element
2   load_manifest
3   belongs_to :panel
4   belongs_to :color
5   
6   validates :panel_id, :numericality => {:allow_blank => true}
7   validates :code, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
8   validates :orientation, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
9   validates :xy, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true}
10   validates :wh, :numericality => {:greater_than_or_equal_to => 0, :allow_blank => true}
11   validates :z, :presence => true, :numericality => {:greater_than => 0}
12   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
13   
14   def self.list_opt_for_panel
15     {
16       :ground_colors => {
17       }
18     }
19   end
20   
21   def self.show_opt_for_panel
22     {
23       :ground_colors => {
24       }
25     }
26   end
27   
28   def self.json_opt_for_panel
29     {
30     }
31   end
32   
33   def self.has_picture?
34     false
35   end
36   
37   def supply_default
38     self.code ||= 0
39     if self.panel
40       self.t = self.panel.new_t 
41       self.z = self.panel.new_z 
42     end
43   end
44   
45   def overwrite pid
46     self.panel_id = pid
47   end
48   
49   def visible? operators
50     return false unless super
51     self.owner_model.visible? operators
52   end
53   
54   def div_offset
55     xy ? xy : 0
56   end
57   
58   def div_size
59     wh ? wh : 100 - self.div_offset
60   end
61   
62   def div_x
63     if self.orientation == 0
64       0
65     else
66       self.div_offset
67     end
68   end
69   
70   def div_y
71     if self.orientation == 0
72       self.div_offset
73     else
74       0
75     end
76   end
77   
78   def div_width
79     if self.orientation == 0
80       100
81     else
82       self.div_size
83     end
84   end
85   
86   def div_height 
87     if self.orientation == 0
88       self.div_size
89     else
90       100
91     end
92   end
93   
94   def style spot = nil, opacity = 20
95     r = {
96       'position' => 'absolute', 'z-index' => self.z, 
97       'top' => self.div_y.to_s + '%', 'left' => self.div_x.to_s + '%', 
98       'width' => self.div_width.to_s + '%', 'height' => self.div_height.to_s + '%', 
99       'background-color' => '#' + format("%06x", self.code)
100     }
101     self.merge_opacity(r, opacity) if spot and spot != self
102     r
103   end
104   
105   def self.list_where
106     'panels.publish > 0'
107   end
108   
109   def self.list_order
110     'ground_colors.updated_at desc'
111   end
112   
113   def self.list_opt
114     {:panel => {:author => {}} }
115   end
116   
117   def self.list_json_opt
118     {:include => {:panel => {:include => {:author => {}}} }}
119   end
120   
121   def self.show_opt
122     {:include => {:panel => {:author => {}} }}
123   end
124   
125   def self.show_json_opt
126     {:include => {:panel => {:include => {:author => {}}} }}
127   end
128   
129   def store operators
130     if self.new_record?
131       self.panel.ground_colors.build(self.attributes)
132     else
133       self.panel.ground_colors.each do |ground_color|
134         next unless ground_color == self
135         attr = self.attributes
136         attr.delete 'id'
137         ground_color.attributes = attr
138         break
139       end
140     end
141     self.panel.store({}, operators)
142   end
143   
144   def remove operators
145     self.panel.remove_element(self, operators)
146   end
147   
148   def scenario
149     if caption.blank?
150       ''
151     else
152       '<p>' + ERB::Util.html_escape(self.caption) + '</p>'
153     end
154   end
155   
156   def plain_scenario
157     if caption.blank?
158       ''
159     else
160       self.caption + "\n"
161     end
162   end
163   
164 end