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