OSDN Git Service

v07
[pettanr/pettanr.git] / app / assets / javascripts / models / panel.js.coffee
1 class Pettanr.Panel extends Peta.Root\r
2   \r
3   @singular: () ->\r
4     'Panel'\r
5   \r
6   @plural: () ->\r
7     'Panels'\r
8   \r
9   defaults: {\r
10     id: null,\r
11     width: 500,\r
12     height: 300,\r
13     border: 2,\r
14     publish: 0\r
15   } \r
16   \r
17   with_elements: () ->\r
18     new Pettanr.Panel.WithElements({id: @get('id')})\r
19   \r
20   overwrite: (options) ->\r
21     operators = Pettanr.cache.operators\r
22     return false if not operators.author\r
23     @set('author_id', operators.author.id)\r
24   \r
25   is_visible: (operators = Pettanr.cache.operators) ->\r
26     switch super(operators)\r
27       when null # super return\r
28         return true\r
29       when false\r
30         return false\r
31       else\r
32         return true if @isNew()\r
33         @is_publish()\r
34   \r
35   overwrite: (options) ->\r
36     @set('author_id', Pettanr.cache.operators.author.id)\r
37   \r
38   is_usable: (operators = Pettanr.cache.operators) ->\r
39     @is_visible(operators)\r
40   \r
41   is_publish: () ->\r
42     @get('publish') > 0\r
43   \r
44   @licensed_pictures: (panels) ->\r
45     r = {}\r
46     _.each panels, (panel) ->\r
47       _.extend r, panel.licensed_pictures()\r
48     r\r
49   \r
50   licensed_pictures: () ->\r
51     r = {}\r
52     _.each @scenario_elements(), (elm) ->\r
53       return if not elm.my_class().has_picture()\r
54       pid = elm.get('picture_id')\r
55       r[pid] = true if not r[pid]\r
56     r\r
57   \r
58   label: (shorten_length) ->\r
59     @_label('caption', shorten_length)\r
60   \r
61   play: () ->\r
62     new Pettanr.Panel.Play({id: @get('id')})\r
63   \r
64   edit: (options) ->\r
65     i = new Pettanr.Panel.Edit({id: @get('id')})\r
66     i.load(options)\r
67   \r
68   initialize: (attr = {}, options = {}) ->\r
69     super(attr, options)\r
70     @set(attr)\r
71   \r
72 class Pettanr.Panel.Play extends Pettanr.Panel\r
73   \r
74   fetch: (options) ->\r
75     @url = @default_url() + '?with_elements=true'\r
76     super(options)\r
77   \r
78 class Pettanr.Panel.Edit extends Pettanr.Panel.Play\r
79   \r
80   load: (options) ->\r
81     @fetch({\r
82       success: (model, response, opt) => \r
83         @attributes = @decoded_attributes()\r
84         @load_elements(options)\r
85       error: (item, response, opt) =>\r
86         options.fail.call(options.context, response, opt)\r
87     })\r
88   \r
89   save: (attrs) ->\r
90     @url = @default_url()\r
91     method = if @isNew()\r
92       'create'\r
93     else\r
94       'update'\r
95     _this = this\r
96     xhr = @sync(method, this, {\r
97       attrs: {panel: attrs},\r
98       success: (model, response, options) =>\r
99         _this.set(model, {silent: true})\r
100         _this.trigger('save:success', _this, response)\r
101       error: (model, response, options) =>\r
102         _this.trigger('save:fail', _this, response)\r
103     })\r
104     xhr\r
105   \r
106   destroy: (options) ->\r
107     @url = @my_url()\r
108     super(options)\r
109   \r