OSDN Git Service

fix:
[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.PickIcon extends Backbone.View\r
61   \r
62   initialize: (options) ->\r
63     @class_name = options.class_name\r
64     @name = options.name\r
65     @half = options.half\r
66     @button = if @is_button()\r
67       @linked_icon()\r
68     else\r
69       @icon()\r
70   \r
71   render: () ->\r
72     this.$el.html('')\r
73     @el.className = @class_name if @class_name\r
74     if @is_button()\r
75       @listenTo(@button, 'click', @click)\r
76     this.$el.append(@button.render().el)\r
77     this\r
78   \r
79   is_button: () ->\r
80     @name != 'none'\r
81   \r
82   icon: () ->\r
83     new Pettanr.Image.SymbolImg({attr: {src: '/images/' + @name + '.gif'}, half: @half})\r
84   \r
85   linked_icon: () ->\r
86     new Tag.A({\r
87       attr: {href: '#'},\r
88       content: @icon().render().el\r
89     })\r
90   \r
91   click: () ->\r
92     @trigger('click', this)\r
93   \r
94 class Pettanr.Views.Common.EmptyCaption extends Backbone.View\r
95   tagName: 'div'\r
96   className: 'empty'\r
97   \r
98   render: () ->\r
99     this.$el.html('empty')\r
100     this\r
101   \r
102 class Pettanr.Views.Common.Icon extends Backbone.View\r
103   tagName: 'span'\r
104   \r
105   initialize: (options) ->\r
106     @item = options.item\r
107     @half = options.half\r
108     @icon_url = Pettanr.url(@item.table_name(), 'show', {id: @item.get('id')})\r
109     @icon = new Pettanr.Image.Icon({item: @item, half: @half})\r
110     @icon_button = new Tag.A({\r
111       attr: {href: '/' + @icon_url}, \r
112       class_name: 'icon',\r
113       content: @icon.render().el\r
114     })\r
115     @listenTo(@icon_button, 'click', @click)\r
116   \r
117   render: () ->\r
118     this.$el.html(@icon_button.render().el)\r
119     this\r
120   \r
121   click: () ->\r
122     @trigger('click')\r
123   \r
124   url: () ->\r
125     @icon_url\r
126   \r
127 class Pettanr.Views.Common.Caption extends Backbone.View\r
128   tagName: 'span'\r
129   \r
130   initialize: (options) ->\r
131     @item = options.item\r
132     @column_name = options.column_name\r
133     @length = options.length\r
134     @name = Pettanr.truncate(@item.get(@column_name), @length)\r
135     @url = Pettanr.url(@item.table_name(), 'show', {id: @item.get('id')})\r
136     @linked_name = new Tag.A({\r
137       attr: {href: '/' + @url}, \r
138       content: _.escape(@name)\r
139     })\r
140     @listenTo(@linked_name, 'click', @click)\r
141   \r
142   render: () ->\r
143     this.$el.html(@linked_name.render().el)\r
144     this\r
145   \r
146   click: () ->\r
147     @trigger('click')\r
148   \r
149   url: () ->\r
150     @url\r
151   \r
152 class Pettanr.Views.Common.IconWithCaption extends Backbone.View\r
153   tagName: 'span'\r
154   \r
155   initialize: (options) ->\r
156     @item = options.item\r
157     @half = options.half\r
158     @column_name = options.column_name\r
159     @length = options.length\r
160     @icon = new Pettanr.Views.Common.Icon({item: @item, half: @half})\r
161     @caption = new Pettanr.Views.Common.Caption({item: @item, column_name: @column_name, length: @length})\r
162     @listenTo(@icon, 'click', @click)\r
163     @listenTo(@caption, 'click', @click)\r
164   \r
165   render: () ->\r
166     this.$el.html('')\r
167     this.$el.append(@icon.render().el)\r
168     this.$el.append(@caption.render().el)\r
169     this\r
170   \r
171   click: () ->\r
172     @trigger('click')\r
173   \r
174   url: () ->\r
175     @icon.url()\r
176   \r
177 class Pettanr.Views.Common.Summary extends Backbone.View\r
178   \r
179   initialize: (options) ->\r
180     @clear()\r
181     @listenTo(this, 'ready', @render)\r
182   \r
183   clear: () ->\r
184     icon = new Pettanr.Views.Common.LoadIcon()\r
185     this.$el.html(icon.render().el)\r
186     this\r
187   \r
188   render: () ->\r
189     this.$el.html('')\r
190     this\r
191   \r
192 class Pettanr.Views.Common.Binder\r
193 class Pettanr.Views.Common.Binder.Summary extends Pettanr.Views.Common.Summary\r
194   \r
195   initialize: (options) ->\r
196     super(options)\r
197     @item = options.item\r
198     @visible_t = options.visible_t\r
199     @visible_column_name = options.visible_column_name\r
200     @load()\r
201   \r
202   load: () ->\r
203     @visible = new Tag.Div({\r
204       content: Pettanr.AppHelper.t_selected_item(@visible_t, @item.get(@visible_column_name))\r
205     })\r
206     @empty_author = @item.author()\r
207     @author = Pettanr.cache.restore(@empty_author)\r
208     if @author\r
209       # got cached author\r
210       @render_author()\r
211     else\r
212       # built empty author\r
213       @author = @empty_author\r
214       @author.fetch({cache: true}).done =>\r
215         Pettanr.cache.store(@author)\r
216         @render_author()\r
217   \r
218   render: () ->\r
219     this.$el.html('')\r
220     this.$el.append(@visible.render().el)\r
221     this.$el.append(@author_icon_with_caption.render().el)\r
222     this\r
223   \r
224   render_author: () ->\r
225     @author_icon_with_caption = @author.icon_with_caption_view(true, 'name', 12)\r
226     @listenTo(@author_icon_with_caption, 'click', @author_click)\r
227     @trigger('ready')\r
228   \r
229   author_click: () ->\r
230     @trigger('http_get', @author_icon_with_caption.url())\r
231   \r
232 class Pettanr.Views.Common.Leaf\r
233 class Pettanr.Views.Common.Leaf.Summary extends Pettanr.Views.Common.Summary\r
234   \r
235   initialize: (options) ->\r
236     super(options)\r
237     @item = options.item\r
238     @binder_item_name = options.binder_item_name\r
239     @destination_item_name = options.destination_item_name\r
240     @binder_view = new Tag.Span({})\r
241     @destination_view = new Tag.Span({})\r
242     @load()\r
243   \r
244   load: () ->\r
245     @listenTo(this, 'load_binder', @load_destination)\r
246     @listenTo(this, 'load_destination', @ready)\r
247     @load_binder()\r
248   \r
249   ready: () ->\r
250     @trigger('ready')\r
251   \r
252   load_binder: () ->\r
253     @binder = @item[@binder_item_name]()\r
254     @binder.fetch({cache: true}).done =>\r
255       @binder_icon = @binder.icon_view(true)\r
256       @listenTo(@binder_icon, 'click', @binder_click)\r
257       @binder_author = @binder.author()\r
258       @binder_author.fetch({cache: true}).done =>\r
259         @binder_author_icon_with_caption = @binder_author.icon_with_caption_view(true, 'name', 12)\r
260         @listenTo(@binder_author_icon_with_caption, 'click', @binder_author_click)\r
261         @binder_view.$el.append(@binder_icon.render().el)\r
262         @binder_view.$el.append(@binder_author_icon_with_caption.render().el)\r
263         @trigger('load_binder')\r
264   \r
265   load_destination: () ->\r
266     @destination = @item[@destination_item_name]()\r
267     @destination.fetch({cache: true}).done =>\r
268       @destination_icon = @destination.icon_view(true)\r
269       @listenTo(@destination_icon, 'click', @destination_click)\r
270       @destination_author = @destination.author()\r
271       @destination_author.fetch({cache: true}).done =>\r
272         @destination_author_icon_with_caption = @destination_author.icon_with_caption_view(true, 'name', 12)\r
273         @listenTo(@destination_author_icon_with_caption, 'click', @destination_author_click)\r
274         @destination_view.$el.append(@destination_icon.render().el)\r
275         @destination_view.$el.append(@destination_author_icon_with_caption.render().el)\r
276         @trigger('load_destination')\r
277   \r
278   render: () ->\r
279     this.$el.html('')\r
280     this.$el.append(@binder_view.render().el)\r
281     this.$el.append(@destination_view.render().el)\r
282     this\r
283   \r
284   binder_click: () ->\r
285     @trigger('http_get', @binder_icon.url())\r
286   \r
287   destination_click: () ->\r
288     @trigger('http_get', @destination_icon.url())\r
289   \r
290   binder_author_click: () ->\r
291     @trigger('http_get', @binder_author_icon_with_caption.url())\r
292   \r
293   destination_author_click: () ->\r
294     @trigger('http_get', @destination_author_icon_with_caption.url())\r
295   \r