OSDN Git Service

5f9f9e528563e2d331b459cf5c32ed40874cc721
[pettanr/pettanr.git] / lib / peta / element.rb
1 module Peta
2   class Element < ElementNestableContent
3     self.abstract_class = true
4     
5     # Dynamic ClassMethods
6     
7     def self.load_manifest
8       super
9       # Class Methods
10       pm = Manifest.manifest.models[self.my_peta.parent_model_name].classify
11       define_singleton_method("parent_model") do 
12         pm
13       end
14       # Instance Methods
15     end
16     
17     def self.element?
18       true
19     end
20     
21     def self.root_model
22       if self.parent_model and self.parent_model.element?
23         self.parent_model.root_model
24       else
25         self
26       end
27     end
28     
29     # Instance Methods
30     
31     def visible? operators
32       return false unless super
33       true
34     end
35     
36     def self.list_opt_for_panel
37       {}
38     end
39     
40     def self.show_opt_for_panel
41       {}
42     end
43     
44     def self.json_opt_for_panel
45       {}
46     end
47     
48     def self.panelize elements_attributes
49       elements_attributes = [elements_attributes] unless elements_attributes.is_a?(Array)
50       hash = {}
51       index = 0
52       elements_attributes.each do |element_attributes|
53         hash[self.to_s.tableize + '_attributes'] ||= {}
54         n = if element_attributes['id']
55           element_attributes['id'].to_s
56         else
57           index += 1
58           'new' + index.to_s 
59         end
60         hash[self.to_s.tableize + '_attributes'][n] = element_attributes
61       end
62       hash
63     end
64     
65     def has_picture?
66       false
67     end
68     
69     def has_part?
70       false
71     end
72     
73     def parts
74       @parts ||= []
75     end
76     
77     def extend_column
78       nil
79     end
80     
81     def extend_element_name
82       self.extend_column ? self.attributes[extend_column] : self.element_name
83     end
84     
85     def scenario_template with_engine = false
86       self.path_name(with_engine) + '/scenario'
87     end
88     
89     def element_face_template with_engine = false
90       self.path_name(with_engine) + '/element_face'
91     end
92     
93     def tag_attributes column = nil, opt = {}
94       r = super
95       r.merge({'data-parent_dom_id' => self.parent.dom_item_id}) if self.editize?
96       r
97     end
98     
99     def merge_opacity s, opacity
100       s.merge({
101         'opacity' => opacity.to_f/100, 
102         'filter' => "alpha(opacity=#{opacity})"
103       })
104     end
105     
106     def copy_attributes opt = {}
107       r = self.attributes
108       r.delete 'id'
109     r.delete 'panel_id'
110       r.delete 't' unless opt[:all]
111       r.delete 'z' unless opt[:all]
112       r.delete 'created_at'
113       r.delete 'updated_at'
114       r
115     end
116     
117   end
118 end
119