OSDN Git Service

c34e9082a90eb91de3089b4f2a24f82adc4aa963
[pettanr/pettanr.git] / app / assets / javascripts / views / common.js.coffee
1 class Pettanr.Views.Common\r
2   @replace_empty: (caption) ->\r
3     if Pettanr.is_blank(caption)\r
4       empty = new Pettanr.Views.Common.EmptyCaption()\r
5       empty.render().el\r
6     else\r
7       caption\r
8   \r
9 class Pettanr.Views.Common.Logo extends Tag.Span\r
10   id: 'logo'\r
11   \r
12   initialize: () ->\r
13     title = Manifest.manifest().magic_numbers['profile']['users']['caption']\r
14     @logo_button = new Tag.A({\r
15       attr: {href: '/'}, \r
16       class_name: 'logo',\r
17       content: _.escape(title)\r
18     })\r
19     @listenTo(@logo_button, 'click', @click)\r
20   \r
21   render: () ->\r
22     this.$el.html(@logo_button.render().el)\r
23     this\r
24   \r
25   click: () ->\r
26     @trigger('click')\r
27   \r
28 \r
29 class Pettanr.Views.Common.EmptyIcon extends Tag.Img\r
30   \r
31   initialize: (options) ->\r
32     @attr = {}\r
33     @width = Manifest.manifest().magic_numbers['thumbnail_width']\r
34     @height = Manifest.manifest().magic_numbers['thumbnail_height']\r
35     @width = Pettanr.to_i(@width/2)\r
36     @height = Pettanr.to_i(@height/2)\r
37     @attr['src'] = @file_name()\r
38     @attr['width'] = @width\r
39     @attr['height'] = @height\r
40     @attr['alt'] = 'Loading'\r
41     @attr['title'] = 'Loading'\r
42     opt = {\r
43       attr: @attr,\r
44     }\r
45     super(opt)\r
46     \r
47   file_name: () ->\r
48     '/images/empty.gif'\r
49 \r
50 class Pettanr.Views.Common.LoadIcon extends Pettanr.Views.Common.EmptyIcon\r
51   \r
52   file_name: () ->\r
53     '/images/loading.gif'\r
54   \r
55 class Pettanr.Views.Common.RootIcon extends Pettanr.Views.Common.EmptyIcon\r
56   \r
57   file_name: () ->\r
58     '/images/root.png'\r
59   \r
60 class Pettanr.Views.Common.EmptyCaption extends Backbone.View\r
61   tagName: 'div'\r
62   className: 'empty'\r
63   \r
64   render: () ->\r
65     this.$el.html('empty')\r
66     this\r
67   \r
68 class Pettanr.Views.Common.Icon extends Backbone.View\r
69   tagName: 'span'\r
70   \r
71   initialize: (options) ->\r
72     @item = options.item\r
73     @half = options.half\r
74     @icon_url = Pettanr.url(@item.table_name(), 'show', {id: @item.get('id')})\r
75     @icon = new Pettanr.Image.Icon({item: @item, half: @half})\r
76     @icon_button = new Tag.A({\r
77       attr: {href: '/' + @icon_url}, \r
78       class_name: 'icon',\r
79       content: @icon.render().el\r
80     })\r
81     @listenTo(@icon_button, 'click', @click)\r
82   \r
83   render: () ->\r
84     this.$el.html(@icon_button.render().el)\r
85     this\r
86   \r
87   click: () ->\r
88     @trigger('click')\r
89   \r
90   url: () ->\r
91     @icon_url\r
92   \r
93 class Pettanr.Views.Common.Caption extends Backbone.View\r
94   tagName: 'span'\r
95   \r
96   initialize: (options) ->\r
97     @item = options.item\r
98     @column_name = options.column_name\r
99     @length = options.length\r
100     @name = Pettanr.truncate(@item.get(@column_name), @length)\r
101     @url = Pettanr.url(@item.table_name(), 'show', {id: @item.get('id')})\r
102     @linked_name = new Tag.A({\r
103       attr: {href: '/' + @url}, \r
104       content: _.escape(@name)\r
105     })\r
106     @listenTo(@linked_name, 'click', @click)\r
107   \r
108   render: () ->\r
109     this.$el.html(@linked_name.render().el)\r
110     this\r
111   \r
112   click: () ->\r
113     @trigger('click')\r
114   \r
115   url: () ->\r
116     @url\r
117   \r
118 class Pettanr.Views.Common.IconWithCaption extends Backbone.View\r
119   tagName: 'span'\r
120   \r
121   initialize: (options) ->\r
122     @item = options.item\r
123     @half = options.half\r
124     @column_name = options.column_name\r
125     @length = options.length\r
126     @icon = new Pettanr.Views.Common.Icon({item: @item, half: @half})\r
127     @caption = new Pettanr.Views.Common.Caption({item: @item, column_name: @column_name, length: @length})\r
128     @listenTo(@icon, 'click', @click)\r
129     @listenTo(@caption, 'click', @click)\r
130   \r
131   render: () ->\r
132     this.$el.html('')\r
133     this.$el.append(@icon.render().el)\r
134     this.$el.append(@caption.render().el)\r
135     this\r
136   \r
137   click: () ->\r
138     @trigger('click')\r
139   \r
140   url: () ->\r
141     @icon.url()\r
142   \r
143 class Pettanr.Views.Common.Summary extends Backbone.View\r
144   \r
145   initialize: (options) ->\r
146     @clear()\r
147     @listenTo(this, 'ready', @render)\r
148   \r
149   clear: () ->\r
150     icon = new Pettanr.Views.Common.LoadIcon()\r
151     this.$el.html(icon.render().el)\r
152     this\r
153   \r
154   render: () ->\r
155     this.$el.html('')\r
156     this\r
157   \r
158 class Pettanr.Views.Common.Binder\r
159 class Pettanr.Views.Common.Binder.Summary extends Pettanr.Views.Common.Summary\r
160   \r
161   initialize: (options) ->\r
162     super(options)\r
163     @item = options.item\r
164     @visible_t = options.visible_t\r
165     @visible_column_name = options.visible_column_name\r
166     @load()\r
167   \r
168   load: () ->\r
169     @visible = new Tag.Div({\r
170       content: Pettanr.AppHelper.t_selected_item(@visible_t, @item.get(@visible_column_name))\r
171     })\r
172     @empty_author = @item.author()\r
173     @author = Pettanr.cache.restore(@empty_author)\r
174     if @author\r
175       # got cached author\r
176       @render_author()\r
177     else\r
178       # built empty author\r
179       @author = @empty_author\r
180       @author.fetch({cache: true}).done =>\r
181         Pettanr.cache.store(@author)\r
182         @render_author()\r
183   \r
184   render: () ->\r
185     this.$el.html('')\r
186     this.$el.append(@visible.render().el)\r
187     this.$el.append(@author_icon_with_caption.render().el)\r
188     this\r
189   \r
190   render_author: () ->\r
191     @author_icon_with_caption = @author.icon_with_caption_view(true, 'name', 12)\r
192     @listenTo(@author_icon_with_caption, 'click', @author_click)\r
193     @trigger('ready')\r
194   \r
195   author_click: () ->\r
196     @trigger('http_get', @author_icon_with_caption.url())\r
197   \r
198 class Pettanr.Views.Common.Leaf\r
199 class Pettanr.Views.Common.Leaf.Summary extends Pettanr.Views.Common.Summary\r
200   \r
201   initialize: (options) ->\r
202     super(options)\r
203     @item = options.item\r
204     @binder_item_name = options.binder_item_name\r
205     @destination_item_name = options.destination_item_name\r
206     @binder_view = new Tag.Span({})\r
207     @destination_view = new Tag.Span({})\r
208     @load()\r
209   \r
210   load: () ->\r
211     @listenTo(this, 'load_binder', @load_destination)\r
212     @listenTo(this, 'load_destination', @ready)\r
213     @load_binder()\r
214   \r
215   ready: () ->\r
216     @trigger('ready')\r
217   \r
218   load_binder: () ->\r
219     @binder = @item[@binder_item_name]()\r
220     @binder.fetch({cache: true}).done =>\r
221       @binder_icon = @binder.icon_view(true)\r
222       @listenTo(@binder_icon, 'click', @binder_click)\r
223       @binder_author = @binder.author()\r
224       @binder_author.fetch({cache: true}).done =>\r
225         @binder_author_icon_with_caption = @binder_author.icon_with_caption_view(true, 'name', 12)\r
226         @listenTo(@binder_author_icon_with_caption, 'click', @binder_author_click)\r
227         @binder_view.$el.append(@binder_icon.render().el)\r
228         @binder_view.$el.append(@binder_author_icon_with_caption.render().el)\r
229         @trigger('load_binder')\r
230   \r
231   load_destination: () ->\r
232     @destination = @item[@destination_item_name]()\r
233     @destination.fetch({cache: true}).done =>\r
234       @destination_icon = @destination.icon_view(true)\r
235       @listenTo(@destination_icon, 'click', @destination_click)\r
236       @destination_author = @destination.author()\r
237       @destination_author.fetch({cache: true}).done =>\r
238         @destination_author_icon_with_caption = @destination_author.icon_with_caption_view(true, 'name', 12)\r
239         @listenTo(@destination_author_icon_with_caption, 'click', @destination_author_click)\r
240         @destination_view.$el.append(@destination_icon.render().el)\r
241         @destination_view.$el.append(@destination_author_icon_with_caption.render().el)\r
242         @trigger('load_destination')\r
243   \r
244   render: () ->\r
245     this.$el.html('')\r
246     this.$el.append(@binder_view.render().el)\r
247     this.$el.append(@destination_view.render().el)\r
248     this\r
249   \r
250   binder_click: () ->\r
251     @trigger('http_get', @binder_icon.url())\r
252   \r
253   destination_click: () ->\r
254     @trigger('http_get', @destination_icon.url())\r
255   \r
256   binder_author_click: () ->\r
257     @trigger('http_get', @binder_author_icon_with_caption.url())\r
258   \r
259   destination_author_click: () ->\r
260     @trigger('http_get', @destination_author_icon_with_caption.url())\r
261   \r