OSDN Git Service

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