OSDN Git Service

fix event
[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.LoadIcon extends Tag.Img\r
10   \r
11   initialize: () ->\r
12     @attr = {}\r
13     @width = Manifest.manifest().magic_numbers['thumbnail_width']\r
14     @height = Manifest.manifest().magic_numbers['thumbnail_height']\r
15     @width = Pettanr.to_i(@width/2)\r
16     @height = Pettanr.to_i(@height/2)\r
17     @attr['src'] = '/images/loading.gif'\r
18     @attr['width'] = @width\r
19     @attr['height'] = @height\r
20     @attr['alt'] = 'Loading'\r
21     @attr['title'] = 'Loading'\r
22     opt = {\r
23       attr: @attr,\r
24     }\r
25     super(opt)\r
26 \r
27 class Pettanr.Views.Common.EmptyCaption extends Backbone.View\r
28   tagName: 'div'\r
29   className: 'empty'\r
30   \r
31   render: () ->\r
32     this.$el.html('empty')\r
33     this\r
34   \r
35 class Pettanr.Views.Common.Icon extends Backbone.View\r
36   tagName: 'span'\r
37   \r
38   initialize: (options) ->\r
39     @item = options.item\r
40     @half = options.half\r
41     @icon_url = Pettanr.url(@item.table_name(), 'show', {id: @item.get('id')})\r
42     @icon = new Pettanr.Image.Icon({item: @item, half: @half})\r
43     @icon_button = new Tag.A({\r
44       attr: {href: '/' + @icon_url}, \r
45       class_name: 'icon',\r
46       content: @icon.render().el\r
47     })\r
48     @listenTo(@icon_button, 'click', @click)\r
49   \r
50   render: () ->\r
51     this.$el.html(@icon_button.render().el)\r
52     this\r
53   \r
54   click: () ->\r
55     @trigger('click')\r
56   \r
57   url: () ->\r
58     @icon_url\r
59   \r
60 class Pettanr.Views.Common.Caption extends Backbone.View\r
61   tagName: 'span'\r
62   \r
63   initialize: (options) ->\r
64     @item = options.item\r
65     @column_name = options.column_name\r
66     @length = options.length\r
67     @name = Pettanr.truncate(@item.get(@column_name), @length)\r
68     @url = Pettanr.url(@item.table_name(), 'show', {id: @item.get('id')})\r
69     @linked_name = new Tag.A({\r
70       attr: {href: '/' + @url}, \r
71       content: _.escape(@name)\r
72     })\r
73     @listenTo(@linked_name, 'click', @click)\r
74   \r
75   render: () ->\r
76     this.$el.html(@linked_name.render().el)\r
77     this\r
78   \r
79   click: () ->\r
80     @trigger('click')\r
81   \r
82   url: () ->\r
83     @url\r
84   \r
85 class Pettanr.Views.Common.IconWithCaption extends Backbone.View\r
86   tagName: 'span'\r
87   \r
88   initialize: (options) ->\r
89     @item = options.item\r
90     @half = options.half\r
91     @column_name = options.column_name\r
92     @length = options.length\r
93     @icon = new Pettanr.Views.Common.Icon({item: @item, half: @half})\r
94     @caption = new Pettanr.Views.Common.Caption({item: @item, column_name: @column_name, length: @length})\r
95     @listenTo(@icon, 'click', @click)\r
96     @listenTo(@caption, 'click', @click)\r
97   \r
98   render: () ->\r
99     this.$el.html('')\r
100     this.$el.append(@icon.render().el)\r
101     this.$el.append(@caption.render().el)\r
102     this\r
103   \r
104   click: () ->\r
105     @trigger('click')\r
106   \r
107   url: () ->\r
108     @icon.url()\r
109   \r
110 class Pettanr.Views.Common.Summary extends Backbone.View\r
111   \r
112   initialize: (options) ->\r
113     @clear()\r
114     @listenTo(this, 'ready', @render)\r
115   \r
116   clear: () ->\r
117     icon = new Pettanr.Views.Common.LoadIcon()\r
118     this.$el.html(icon.render().el)\r
119     this\r
120   \r
121   render: () ->\r
122     this.$el.html('')\r
123     this\r
124   \r
125 class Pettanr.Views.Common.Binder\r
126 class Pettanr.Views.Common.Binder.Summary extends Pettanr.Views.Common.Summary\r
127   \r
128   initialize: (options) ->\r
129     super(options)\r
130     @item = options.item\r
131     @visible_t = options.visible_t\r
132     @visible_column_name = options.visible_column_name\r
133     @load()\r
134   \r
135   load: () ->\r
136     @visible = new Tag.Div({\r
137       content: Pettanr.AppHelper.t_selected_item(@visible_t, @item.get(@visible_column_name))\r
138     })\r
139     @author = @item.author()\r
140     @author.fetch({cache: true}).done =>\r
141       @author_icon_with_caption = @author.icon_with_caption_view(true, 'name', 12)\r
142       @listenTo(@author_icon_with_caption, 'click', @author_click)\r
143       @trigger('ready')\r
144   \r
145   render: () ->\r
146     this.$el.html('')\r
147     this.$el.append(@visible.render().el)\r
148     this.$el.append(@author_icon_with_caption.render().el)\r
149     this\r
150   \r
151   author_click: () ->\r
152     @trigger('navigate', @author_icon_with_caption.url())\r
153   \r
154 class Pettanr.Views.Common.Leaf\r
155 class Pettanr.Views.Common.Leaf.Summary extends Pettanr.Views.Common.Summary\r
156   \r
157   initialize: (options) ->\r
158     super(options)\r
159     @item = options.item\r
160     @binder_item_name = options.binder_item_name\r
161     @destination_item_name = options.destination_item_name\r
162     @binder_view = new Tag.Span({})\r
163     @destination_view = new Tag.Span({})\r
164     @load()\r
165   \r
166   load: () ->\r
167     @listenTo(this, 'load_binder', @load_destination)\r
168     @listenTo(this, 'load_destination', @ready)\r
169     @load_binder()\r
170   \r
171   ready: () ->\r
172     @trigger('ready')\r
173   \r
174   load_binder: () ->\r
175     @binder = @item[@binder_item_name]()\r
176     @binder.fetch({cache: true}).done =>\r
177       @binder_icon = @binder.icon_view(true)\r
178       @listenTo(@binder_icon, 'click', @binder_click)\r
179       @binder_author = @binder.author()\r
180       @binder_author.fetch({cache: true}).done =>\r
181         @binder_author_icon_with_caption = @binder_author.icon_with_caption_view(true, 'name', 12)\r
182         @listenTo(@binder_author_icon_with_caption, 'click', @binder_author_click)\r
183         @binder_view.$el.append(@binder_icon.render().el)\r
184         @binder_view.$el.append(@binder_author_icon_with_caption.render().el)\r
185         @trigger('load_binder')\r
186   \r
187   load_destination: () ->\r
188     @destination = @item[@destination_item_name]()\r
189     @destination.fetch({cache: true}).done =>\r
190       @destination_icon = @destination.icon_view(true)\r
191       @listenTo(@destination_icon, 'click', @destination_click)\r
192       @destination_author = @destination.author()\r
193       @destination_author.fetch({cache: true}).done =>\r
194         @destination_author_icon_with_caption = @destination_author.icon_with_caption_view(true, 'name', 12)\r
195         @listenTo(@destination_author_icon_with_caption, 'click', @destination_author_click)\r
196         @destination_view.$el.append(@destination_icon.render().el)\r
197         @destination_view.$el.append(@destination_author_icon_with_caption.render().el)\r
198         @trigger('load_destination')\r
199   \r
200   render: () ->\r
201     this.$el.html('')\r
202     this.$el.append(@binder_view.render().el)\r
203     this.$el.append(@destination_view.render().el)\r
204     this\r
205   \r
206   binder_click: () ->\r
207     @trigger('navigate', @binder_icon.url())\r
208   \r
209   destination_click: () ->\r
210     @trigger('navigate', @destination_icon.url())\r
211   \r
212   binder_author_click: () ->\r
213     @trigger('navigate', @binder_author_icon_with_caption.url())\r
214   \r
215   destination_author_click: () ->\r
216     @trigger('navigate', @destination_author_icon_with_caption.url())\r
217   \r