OSDN Git Service

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