OSDN Git Service

924c1b77358e49993ddbc68422d4030c4897298b
[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   author: () ->\r
18     new Pettanr.Cache.Retriever(Pettanr.Author, @get('author_id'))\r
19   \r
20   with_elements: () ->\r
21     new Pettanr.Panel.WithElements({id: @get('id')})\r
22   \r
23   overwrite: (options) ->\r
24     operators = Pettanr.cache.operators\r
25     return false if not operators.author\r
26     @set('author_id', operators.author.id)\r
27   \r
28   is_visible: (operators = Pettanr.cache.operators) ->\r
29     switch super(operators)\r
30       when null # super return\r
31         return true\r
32       when false\r
33         return false\r
34       else\r
35         return true if @isNew()\r
36         @is_publish()\r
37   \r
38   overwrite: (options) ->\r
39     @set('author_id', Pettanr.cache.operators.author.id)\r
40   \r
41   is_usable: (operators = Pettanr.cache.operators) ->\r
42     @is_visible(operators)\r
43   \r
44   is_publish: () ->\r
45     @get('publish') > 0\r
46   \r
47   @licensed_pictures: (panels) ->\r
48     r = {}\r
49     _.each panels, (panel) ->\r
50       _.extend r, panel.licensed_pictures()\r
51     r\r
52   \r
53   licensed_pictures: () ->\r
54     r = {}\r
55     _.each @scenario_elements(), (elm) ->\r
56       return if not elm.my_class().has_picture()\r
57       pid = elm.get('picture_id')\r
58       r[pid] = elm.picture() if not r[pid]\r
59     r\r
60   \r
61   initialize: (attr = {}, options = {}) ->\r
62     super(attr, options)\r
63     @set(attr)\r
64   \r
65 class Pettanr.Panel.WithElements extends Pettanr.Panel\r
66   \r
67   fetch: (options) ->\r
68     @url = @default_url() + '?with_elements=true'\r
69     super(options)\r
70   \r
71   save: (attrs) ->\r
72     @url = @default_url()\r
73     method = if @isNew()\r
74       'create'\r
75     else\r
76       'update'\r
77     _this = this\r
78     xhr = @sync(method, this, {\r
79       attrs: {panel: attrs},\r
80       success: (model, response, options) =>\r
81         _this.set(model, {silent: true})\r
82         _this.trigger('save:success', _this, response)\r
83       error: (model, response, options) =>\r
84         _this.trigger('save:fail', _this, response)\r
85     })\r
86     xhr\r
87   \r
88   destroy: (options) ->\r
89     @url = @my_url()\r
90     super(options)\r
91   \r