OSDN Git Service

change: cache system
[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 = options.operators\r
26     return false if not operators.author\r
27     @set('author_id', operators.author.id)\r
28   \r
29   is_visible: (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', options.operators.author.id)\r
41   \r
42   is_usable: (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     '/' + @path_name() + '/' + @get('id')\r
64   \r
65   initialize: (attr = {}, options = {}) ->\r
66     @set(attr)\r
67     if @get('id')\r
68       @url = @my_url()\r
69     @bind('change:width', () ->\r
70       console.log('cng')\r
71     )\r
72   \r
73 class Pettanr.Panel.WithElements extends Pettanr.Panel\r
74   \r
75   fetch: (options) ->\r
76     @url = @my_url() + '?with_elements=true'\r
77     super(options)\r
78   \r
79   save: (attrs) ->\r
80     @url = @my_url()\r
81     method = if @isNew()\r
82       'create'\r
83     else\r
84       'update'\r
85     _this = this\r
86     xhr = @sync(method, this, {\r
87       attrs: {panel: attrs},\r
88       success: (model, response, options) =>\r
89         _this.trigger('save:success', _this, response)\r
90       error: (model, response, options) =>\r
91         _this.trigger('save:fail', _this, response)\r
92     })\r
93     xhr\r
94   \r
95   destroy: (options) ->\r
96     @url = @my_url()\r
97     super(options)\r
98   \r