OSDN Git Service

add: resource_picture_pictures
[pettanr/pettanr.git] / app / assets / javascripts / models / original_picture.js.coffee
1 class Pettanr.OriginalPicture extends Peta.Content\r
2   url: '/original_pictures/'\r
3   \r
4   @singular: () ->\r
5     'OriginalPicture'\r
6   \r
7   @plural: () ->\r
8     'OriginalPictures'\r
9   \r
10   defaults: {\r
11     id: null,\r
12     artist_id: null\r
13   } \r
14   \r
15   picture: () ->\r
16     new Pettanr.Picture({id: @get('picture_id')})\r
17   \r
18   is_visible: (operators) ->\r
19     @is_own(operators)\r
20   \r
21   filename: () ->\r
22     @get('id') + '.' + @get('ext')\r
23   \r
24   mime_type: () ->\r
25     'image/' + @get('ext')\r
26   \r
27   r_url: () ->\r
28     '/original_pictures/' + @filename()\r
29   \r
30   opt_img_tag: () ->\r
31     {src: @r_url(), width: @get('width'), height: @get('height')}\r
32   \r
33   tmb_opt_img_tag: () ->\r
34     new Pettanr.Image.SymbolPicture({\r
35       attr: {\r
36         src: @r_url()\r
37       },\r
38       picture: this\r
39     })\r
40   \r
41   symbol_option: () ->\r
42     @fetch({cache: true}).done =>\r
43       @trigger('ready:symbol', @tmb_opt_img_tag())\r
44   \r
45   filer_caption: () ->\r
46     history = @history()\r
47     history.fetch({cache: true}).done =>\r
48       caption = if _.isEmpty(history.models)\r
49         'unpublished'\r
50       else\r
51         head = _.first(history.models)\r
52         Pettanr.to_s(head.get('revision'))\r
53       @trigger('ready:caption', caption)\r
54   \r
55   revision: () ->\r
56     new Pettanr.OriginalPicture.Head({original_picture: this})\r
57   \r
58   history: () ->\r
59     new Pettanr.OriginalPicture.History({original_picture: this})\r
60   \r
61   is_unpublished: () ->\r
62     @get('published_at') == null and @get('stopped_at') == null\r
63   \r
64   is_stopped: () ->\r
65     @get('stopped_at') != null\r
66   \r
67   is_unlicensed: () ->\r
68     dt = @get('published_at') || @get('stopped_at')\r
69     return false if not dt\r
70     @get('uploaded_at') > dt\r
71   \r
72   is_published: () ->\r
73     @get('published_at') != null\r
74   \r
75   state: () ->\r
76     switch true\r
77       when @is_unpublished()\r
78         'unpublished'\r
79       when @is_unlicensed()\r
80         'unlicensed'\r
81       when @is_stopped()\r
82         'stopped'\r
83       when @is_published()\r
84         'published'\r
85   \r
86   initialize: (attr = {}, options = {}) ->\r
87     super(attr, options)\r
88     if @id\r
89       @url = @url + @id\r
90   \r
91 class Pettanr.OriginalPicture.History extends Backbone.Collection\r
92   url: '/original_pictures/'\r
93   \r
94   initialize: (options) ->\r
95     @original_picture = options.original_picture\r
96     @model = Pettanr.Picture\r
97     @url = @url + @original_picture.get('id') + '/history'\r
98   \r
99 class Pettanr.OriginalPicture.Head extends Backbone.View\r
100   tagName: 'span'\r
101   initialize: (options) ->\r
102     @original_picture = options.original_picture\r
103     @pictures = new Pettanr.OriginalPicture.History({id: @original_picture.get('id')})\r
104   \r
105   render: () ->\r
106     @pictures.fetch({cache: true}).done =>\r
107       head = @pictures.models[0]\r
108       value = if head\r
109         head.get('revision')\r
110       else\r
111         'unpublished'\r
112       this.$el.html(value)\r
113     this\r
114   \r