OSDN Git Service

2e4c39256ada1206e650e69180b9a87f200cd954
[pettanr/pettanr.git] / app / assets / javascripts / peta / element_nestable_content.js.coffee
1 class Peta.ElementNestableContent extends Peta.Content\r
2   element_items: (element_model) ->\r
3     n = @my_class().my_manifest().associations.child_element_name(element_model)\r
4     @get(n)\r
5   \r
6   elements_items: () ->\r
7     r = _.map @my_class().child_models(), (element_model) =>\r
8       @element_items(element_model)\r
9     _.compact(_.flatten(r))\r
10   \r
11   boosts: (level) ->\r
12     super(level)\r
13     _.each @elements_items(), (item) =>\r
14       item.boosts(level) if item\r
15   \r
16   dig_elements: () ->\r
17     items = []\r
18     @_dig_elements(items, @attributes)\r
19     items\r
20   \r
21   _dig_elements: (items, attributes) ->\r
22     _.each attributes, (attribute, name) =>\r
23       a = @my_class().my_manifest().associations\r
24       if a.has_many[name]\r
25         _.each attribute, (attr) =>\r
26           items.push(attr)\r
27           Array.prototype.push.apply(items, attr.dig_elements())\r
28       if a.has_one[name]\r
29         items.push(attribute)\r
30         Array.prototype.push.apply(items, attribute.dig_elements())\r
31   \r
32   load_elements: (options) ->\r
33     @_load_elements(@dig_elements(), options)\r
34   \r
35   _load_elements: (items, options) ->\r
36     if _.isEmpty(items)\r
37       options.success.call(options.context, this)\r
38       return\r
39     item = items.shift()\r
40     item.fetch().done =>\r
41       @_load_elements(items, options)\r
42   \r
43   decoded_attributes: () ->\r
44     attributes = {}\r
45     _.each @attributes, (attribute, name) =>\r
46       a = @my_class().my_manifest().associations\r
47       decoded = if a.has_many[name]\r
48         # has many association\r
49         model = a.has_many[name].model()\r
50         _.map attribute, (attr) =>\r
51           child = @cache_child(model, attr)\r
52           child.attributes = child.decoded_attributes()\r
53           child\r
54       else if a.has_one[name]\r
55         # has one association\r
56         model = a.has_one[name].model()\r
57         child = @cache_child(model, attribute)\r
58         child.attributes = child.decoded_attributes()\r
59         child\r
60       else\r
61         attribute\r
62       attributes[name] = decoded\r
63     attributes\r
64   \r
65   replaced_attributes: (options = {}) ->\r
66     attributes = {}\r
67     _.each @attributes, (attribute, name) =>\r
68       console.log name\r
69       console.log attribute\r
70       a = @my_class().my_manifest().associations\r
71       replaced = if a.has_many[name]\r
72         # has many association\r
73         _.map attribute, (decoded) =>\r
74           child = if options.hold\r
75             @replace_and_hold_element(decoded)\r
76           else\r
77             @replace_element(decoded)\r
78           console.log child\r
79           child.attributes = child.replaced_attributes(options)\r
80           child\r
81       else if a.has_one[name]\r
82         # has one association\r
83         child = if options.hold\r
84           @replace_and_hold_element(attribute)\r
85         else\r
86           @replace_element(attribute)\r
87         child.attributes = child.replaced_attributes(options)\r
88         child\r
89       else\r
90         attribute\r
91       console.log replaced\r
92       attributes[name] = replaced\r
93     attributes\r
94   \r
95   replace_element: (decoded_child) ->\r
96     cached_child = Pettanr.cache.restore(decoded_child)\r
97     item = if cached_child\r
98       cached_child\r
99     else\r
100       model = decoded_child.my_class()\r
101       caching_item = new model()\r
102       Pettanr.cache.store(caching_item)\r
103     item\r
104   \r
105   cache_child: (model, attr) ->\r
106     attr_without_element = {}\r
107     _.each attr, (a, n) ->\r
108       if !_.isObject(a) and !_.isArray(a)\r
109         attr_without_element[n] = a\r
110     decoded_child = new model(attr_without_element)\r
111     cached_child = Pettanr.cache.restore(decoded_child)\r
112     holden_item = if cached_child\r
113       cached_child.hold()\r
114     else\r
115       Pettanr.cache.store(decoded_child).hold()\r
116     holden_item.attributes = attr\r
117     holden_item\r
118   \r
119   hold: () ->\r
120     super()\r
121   \r
122   fix: () ->\r
123     super()\r
124     @fix_elements()\r
125   \r
126   release: () ->\r
127     super()\r
128     @release_elements()\r
129   \r
130   release_elements: () ->\r
131     _.each @attributes, (elements, name) =>\r
132       a = @my_class().my_manifest().associations\r
133       if a.has_many[name]\r
134         # has many association\r
135         _.each elements, (element) =>\r
136           element.release()\r
137       else if a.has_one[name]\r
138         # has one association\r
139         elements.release()\r
140   \r
141   fix_elements: () ->\r
142     return \r
143     _.each @attributes, (elements, name) =>\r
144       a = @my_class().my_manifest().associations\r
145       if a.has_many[name]\r
146         # has many association\r
147         _.each elements, (element) =>\r
148           if @has('_destroy')\r
149             element.release()  # destroy element by editor\r
150           else\r
151             element.fix()\r
152       else if a.has_one[name]\r
153         # has one association\r
154         if @has('_destroy')\r
155           element.release()\r
156         else\r
157           element.fix()\r
158   \r