OSDN Git Service

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