OSDN Git Service

operator move to cache
[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   is_visible: (operators = Pettanr.cache.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     retriever = @retriever()\r
43     @listenTo(retriever, 'retrieve', @retrieve_symbol)\r
44     retriever.retrieve()\r
45   \r
46   retrieve_symbol: (item) -> \r
47     @trigger('ready:symbol', item.tmb_opt_img_tag())\r
48   \r
49   filer_caption: () ->\r
50     history = @history()\r
51     history.fetch({cache: true}).done =>\r
52       caption = if _.isEmpty(history.models)\r
53         'unpublished'\r
54       else\r
55         head = _.first(history.models)\r
56         Pettanr.to_s(head.get('revision'))\r
57       @trigger('ready:caption', caption)\r
58   \r
59   revision: () ->\r
60     new Pettanr.OriginalPicture.Head({original_picture: this})\r
61   \r
62   history: () ->\r
63     new Pettanr.OriginalPicture.History({original_picture: this})\r
64   \r
65   is_unpublished: () ->\r
66     @get('published_at') == null and @get('stopped_at') == null\r
67   \r
68   is_stopped: () ->\r
69     @get('stopped_at') != null\r
70   \r
71   is_unlicensed: () ->\r
72     dt = @get('published_at') || @get('stopped_at')\r
73     return false if not dt\r
74     @get('uploaded_at') > dt\r
75   \r
76   is_published: () ->\r
77     @get('published_at') != null\r
78   \r
79   state: () ->\r
80     switch true\r
81       when @is_unpublished()\r
82         'unpublished'\r
83       when @is_unlicensed()\r
84         'unlicensed'\r
85       when @is_stopped()\r
86         'stopped'\r
87       when @is_published()\r
88         'published'\r
89   \r
90   initialize: (attr = {}, options = {}) ->\r
91     super(attr, options)\r
92     if @id\r
93       @url = @url + @id\r
94   \r
95 class Pettanr.OriginalPicture.History extends Backbone.Collection\r
96   url: '/original_pictures/'\r
97   \r
98   initialize: (options) ->\r
99     @original_picture = options.original_picture\r
100     @model = Pettanr.Picture\r
101     @url = @url + @original_picture.get('id') + '/history'\r
102   \r
103 class Pettanr.OriginalPicture.Head extends Backbone.View\r
104   tagName: 'span'\r
105   initialize: (options) ->\r
106     @original_picture = options.original_picture\r
107     @pictures = new Pettanr.OriginalPicture.History({id: @original_picture.get('id')})\r
108   \r
109   render: () ->\r
110     @pictures.fetch({cache: true}).done =>\r
111       head = @pictures.models[0]\r
112       value = if head\r
113         head.get('revision')\r
114       else\r
115         'unpublished'\r
116       this.$el.html(value)\r
117     this\r
118   \r