OSDN Git Service

b28fdcd1b67a1402a61259b3a2846bbaf4455131
[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   replaced_attributes: (options = {}) ->\r
17     attributes = {}\r
18     _.each @attributes, (attribute, name) =>\r
19       replaced = if _.isArray(attribute)\r
20         # has many association\r
21         model = @my_class().my_manifest().associations.has_many[name].model()\r
22         _.map attribute, (attr) =>\r
23           child = if options.hold\r
24             @replace_and_hold_element(model, attr)\r
25           else\r
26             @replace_element(model, attr)\r
27           child.attributes = child.replaced_attributes(options)\r
28           child\r
29       else if _.isObject(attribute)\r
30         # has one association\r
31         model = @my_class().my_manifest().associations.has_one[name].model()\r
32         child = if options.hold\r
33           @replace_and_hold_element(model, attribute)\r
34         else\r
35           @replace_element(model, attribute)\r
36         child.attributes = child.replaced_attributes(options)\r
37         child\r
38       else\r
39         attribute\r
40       attributes[name] = replaced\r
41     attributes\r
42   \r
43   replace_element: (model, attr) ->\r
44     empty_child = new model(attr)\r
45     cached_child = Pettanr.cache.restore(empty_child)\r
46     item = if cached_child\r
47       cached_child\r
48     else\r
49       Pettanr.cache.store(empty_child)\r
50     item.attributes = attr\r
51     item\r
52   \r
53   replace_and_hold_element: (model, attr) ->\r
54     empty_child = new model(attr)\r
55     cached_child = Pettanr.cache.restore(empty_child)\r
56     item = if cached_child\r
57       cached_child\r
58     else\r
59       Pettanr.cache.store(empty_child) \r
60     holden = item.hold()\r
61     holden.attributes = attr\r
62     holden\r
63   \r
64   hold: () ->\r
65     super()\r
66   \r
67   fix: () ->\r
68     super()\r
69     @fix_elements()\r
70   \r
71   release: () ->\r
72     super()\r
73     @release_elements()\r
74   \r
75   release_elements: () ->\r
76     _.each @attributes, (elements, name) =>\r
77       if _.isArray(elements)\r
78         # has many association\r
79         model = @my_class().my_manifest().associations.has_many[name].model()\r
80         _.each elements, (element) =>\r
81           element.release()\r
82       else if _.isObject(elements)\r
83         # has one association\r
84         elements.release()\r
85   \r
86   fix_elements: () ->\r
87     _.each @attributes, (elements, name) =>\r
88       if _.isArray(elements)\r
89         # has many association\r
90         model = @my_class().my_manifest().associations.has_many[name].model()\r
91         _.each elements, (element) =>\r
92           element.fix()\r
93       else if _.isObject(elements)\r
94         # has one association\r
95         elements.fix()\r
96   \r