OSDN Git Service

fix: views
[pettanr/pettanr.git] / app / assets / javascripts / peta / item.js.coffee
1 class Peta.Item extends Backbone.Model\r
2   \r
3   initialize: (attr = {}, options = {}) ->\r
4     @url = @default_url()\r
5     super(attr, options)\r
6     @expire_time = options.expire_time\r
7     @boosters = {}\r
8     @templates = {}\r
9   \r
10   @child_models: () ->\r
11     @my_manifest().child_models()\r
12   \r
13   @child_element_names: () ->\r
14     @my_manifest().child_element_names()\r
15   \r
16   # ClassMethods\r
17   \r
18   @default_url: () ->\r
19     '/' + @table_name() + '/'\r
20   \r
21   @my_class: () ->\r
22     this\r
23   \r
24   my_class: () ->\r
25     @constructor\r
26   \r
27   @my_peta: () ->\r
28     return null if not Manifest.manifest().items\r
29     Manifest.manifest().items[@item_name()]\r
30   \r
31   @my_manifest: () ->\r
32     return null if not Manifest.manifest().models\r
33     Manifest.manifest().models[@item_name()]\r
34   \r
35   @singular: () ->\r
36     'Name'\r
37   \r
38   @plural: () ->\r
39     table_name = Manifest.manifest().pluralize @item_name()\r
40     Pettanr.camelize(table_name)\r
41   \r
42   @class_name: () ->\r
43     @singular()\r
44   \r
45   @model_name: () ->\r
46     @class_name()\r
47   \r
48   @item_name: () ->\r
49     Pettanr.underscore(@singular())\r
50   \r
51   @table_name: () ->\r
52     Pettanr.underscore(@plural())\r
53   \r
54   @is_item: () ->\r
55     true\r
56   \r
57   @is_content: () ->\r
58     false\r
59   \r
60   @is_element: () ->\r
61     false\r
62   \r
63   @is_root: () ->\r
64     false\r
65  \r
66   @parent_model: () ->\r
67     null\r
68   \r
69   @has_picture: () ->\r
70     false\r
71   \r
72   @path_name: (with_engine = false) ->\r
73     @table_name()\r
74   \r
75   @find_boost_name: (column_name) ->\r
76     @my_peta().find_boost_name column_name\r
77   \r
78   @is_extend_column: (column_name) -> \r
79     @my_peta().is_extend_column column_name\r
80   \r
81   @fold_extend_settings: (attr) ->\r
82     _.each my_peta().boost, (name, manifest) ->\r
83       my_settings = attr[manifest.settings_column_name]\r
84       if my_settings\r
85         attr[manifest.settings_column_name] = my_settings.to_json\r
86   \r
87   @retrieve: (id, context, options = {}) ->\r
88     item = new this({id: id})\r
89     item.retrieve(context, options)\r
90   \r
91   @trace_routes: () ->\r
92     {}\r
93   \r
94   @default_label_shorten_length = 13\r
95   \r
96   #InstanceMethods\r
97   \r
98   default_url: () ->\r
99     r = @my_class().default_url()\r
100     r = r.concat( @id ) if @id\r
101     r\r
102   \r
103   singular: () ->\r
104     @my_class().singular()\r
105   \r
106   plural: () ->\r
107     @my_class().plural()\r
108   \r
109   item_name: () ->\r
110     @my_class().item_name()\r
111   \r
112   model_name: () ->\r
113     @item_name()\r
114   \r
115   table_name: () ->\r
116     @my_class().table_name()\r
117   \r
118   path_name: (with_engine = false) ->\r
119     @my_class().path_name(with_engine)\r
120   \r
121   form_template: (with_engine = false) ->\r
122     @path_name(with_engine) + '/form'\r
123   \r
124   form_name: () ->\r
125     if @extend_column()\r
126       @get(@extend_column())\r
127     else\r
128       @item_name()\r
129   \r
130   _label: (label_column_name, shorten_length = null) ->\r
131     shorten_length ||= @constructor.default_label_shorten_length\r
132     Pettanr.truncate(@get(label_column_name), shorten_length)\r
133   \r
134   label: (shorten_length = null) ->\r
135     ''\r
136   \r
137   get_association: (routes, context, options) ->\r
138     routes = [routes] if _.isString(routes)\r
139     route = routes.shift()\r
140     if _.isEmpty(routes)\r
141       # fetching terminate association. callback\r
142       cxt = options.context || context\r
143       @fetch_association(route, cxt, {\r
144         success: (association_item, options) =>\r
145           options.success.call(context, association_item)\r
146         context: context,\r
147         options: options\r
148       })\r
149     else\r
150       # fetching through associations\r
151       @fetch_association(route, this, {\r
152         success: (association_item, options) =>\r
153           association_item.get_association(routes, this, options)\r
154         context: context,\r
155         options: options\r
156       })\r
157   \r
158   fetch_association: (name, context, options) =>\r
159     a = @my_class().my_manifest().associations\r
160     fetch_options = {\r
161       success: (association_item) =>\r
162         options.success.call(context, association_item, options.options)\r
163     }\r
164     if a.belongs_to[name]\r
165       @get_parent(name, context, fetch_options)\r
166     else if a.has_many[name]\r
167       @get_children(name, context, fetch_options)\r
168     else if a.has_one[name]\r
169       @get_child(name, context, fetch_options)\r
170     else\r
171       console.error('association does not exist in model manifest')\r
172   \r
173   get_parent: (belongs_to_name, context, options = null) ->\r
174     m = Manifest.item_name_to_model(belongs_to_name)\r
175     retriever = new Pettanr.Cache.Retriever(m, @get(belongs_to_name + '_id'))\r
176     return retriever if !options\r
177     @listenTo(retriever, 'retrieve', (item) =>\r
178       options.success.call(context, item)\r
179     )\r
180     retriever.retrieve()\r
181   \r
182   get_child: (has_one_name, context, options = null) ->\r
183     list = @has_one(has_one_name)\r
184     list.open(context, {\r
185       success: (items) =>\r
186         callback = options.success\r
187         item = items[0]\r
188         callback.call(context, item)\r
189     })\r
190   \r
191   get_children: (has_many_name, context, options = null) ->\r
192     list = @has_many(has_many_name)\r
193     list.open(context, {\r
194       success: (items) =>\r
195         callback = options.success\r
196         callback.call(context, items)\r
197     })\r
198   \r
199   has_many: (has_many_name) ->\r
200     has_many_manifest = @my_class().my_manifest().associations.has_many[has_many_name]\r
201     action_name = has_many_manifest.list_action_name\r
202     Locmare.ListGroup.list(\r
203       has_many_name, action_name, {id: @get('id')}\r
204     )\r
205   \r
206   has_one: (has_one_name) ->\r
207     has_one_manifest = @my_class().my_manifest().associations.has_one[has_one_name]\r
208     controller_name = has_one_manifest.model().table_name()\r
209     action_name = has_one_manifest.list_action_name\r
210     Locmare.ListGroup.list(\r
211       controller_name, action_name, {id: @get('id')}\r
212     )\r
213   \r
214   trace_to: (trace_name, context, options) ->\r
215     routes = @my_class().trace_routes()[trace_name]\r
216     if !routes\r
217       console.error('no trace route')\r
218       return\r
219     @get_association(routes, this, {\r
220       success: (association) => \r
221         options.success.call(context, association)\r
222     })\r
223   \r
224   boosts: (level) ->\r
225     c = @my_class().my_peta().boost\r
226     _.each c, (boost_manifest, boost_name) =>\r
227       return if level == 'read' and boost_manifest.level == 'post'\r
228       @boost boost_manifest\r
229   \r
230   boost: (boost_manifest) ->\r
231     @boosters[boost_manifest.name] = new Locmare.Booster(boost_manifest, this)\r
232     # fetched item is not cleared .  force write booster\r
233     #@boosters[boost_manifest.name] ||= new Locmare.Booster(boost_manifest, this)\r
234   \r
235   is_extend_column: (column_name) ->\r
236     @is_extend_column column_name\r
237   \r
238   is_visible: (operators = Pettanr.cache.operators) ->\r
239     if Manifest.manifest().magic_numbers['run_mode'] == 0\r
240       return false if not operators.is_guest()\r
241     else\r
242       return false if not operators.is_resource_reader()\r
243     true\r
244   \r
245   retriever: () ->\r
246     new Pettanr.Cache.Retriever(@my_class(), @get('id'))\r
247   \r
248   retrieve: (context, options = {}) ->\r
249     retriever = new Pettanr.Cache.Retriever(@my_class(), @get('id'))\r
250     @listenTo(retriever, 'retrieve', (item) =>\r
251       callback = options.success\r
252       callback.call(context, item)\r
253     )\r
254     retriever.retrieve(options.force)\r
255   \r
256   @pick_item_name: () ->\r
257     null\r
258   \r
259   @pick_model: () ->\r
260     Manifest.item_name_to_model(@pick_item_name())\r
261   \r
262   @traceable_item_names: () ->\r
263     []\r
264   \r
265   @is_traceable: (item_name) ->\r
266     _.contains(@traceable_item_names(), item_name)\r
267   \r
268   @is_picker_inspire: (item_name) ->\r
269     item_name == @item_name()\r
270   \r
271   @is_picker_trace: (item_name) ->\r
272     @is_traceable(item_name)\r
273   \r
274   @is_picker_pick: (item_name) ->\r
275     item_name == @pick_item_name()\r
276   \r
277   @pick_type: (item_name) ->\r
278     if @is_picker_inspire(item_name)\r
279       'inspire'\r
280     else if @is_picker_trace(item_name)\r
281       'trace'\r
282     else if @is_picker_pick(item_name)\r
283       'pick'\r
284     else\r
285       'none'\r
286   \r
287   symbol_option: (context, options) ->\r
288     @trace_to('symbol', this, {\r
289       success: (symbol_item) => \r
290         options.success.call(context, symbol_item.symbol_file())\r
291     })\r
292   \r
293   @face_file: () ->\r
294     new Pettanr.ImageFile('/images/' + @item_name() + '.gif')\r
295   \r
296   face_file: () ->\r
297     @my_class().face_file()\r
298   \r
299   # thumbnail size picture\r
300   symbol_file: (subdir) ->\r
301     new Pettanr.PictureFile(this, subdir)\r
302   \r
303   # real size picture\r
304   picture_file: (subdir = null) ->\r
305     new Pettanr.PictureFile(this, subdir)\r
306   \r
307   real_picture: (subdir = null) ->\r
308     new Pettanr.View.RealIcon(@picture_file(subdir))\r
309   \r
310   # item.face_button({\r
311   #   context: this, \r
312   #   click: () ->\r
313   #     # ...\r
314   # })\r
315   face_button: (options) ->\r
316     Pettanr.View.face_button(this, @face_file(), options)\r
317   \r
318   mini_face_button: (options) ->\r
319     Pettanr.View.mini_face_button(this, @face_file(), options)\r
320   \r
321   prof_button: (options) ->\r
322     Pettanr.View.prof_button(@prof_url(), options)\r
323   \r
324   mini_prof_button: (options) ->\r
325     Pettanr.View.mini_prof_button(@prof_url(), options)\r
326   \r
327   symbol_button: (options) ->\r
328     Pettanr.View.face_button(this, @symbol_file(), options)\r
329   \r
330   mini_symbol_button: (options) ->\r
331     Pettanr.View.mini_face_button(this, @symbol_file(), options)\r
332   \r
333   label_button: (options) ->\r
334     url = options.url || @show_url()\r
335     new Pettanr.View.Button(url, _.escape(@label()), options)\r
336   \r
337   #  faced_label({\r
338   #      url: @show_url(),\r
339   #      context: this,\r
340   #      click: () =>\r
341   #        # ...\r
342   #    })\r
343   faced_label: (options) ->\r
344     new Pettanr.View.FacedLabel(this, options)\r
345   \r
346   mini_faced_label: (options) ->\r
347     new Pettanr.View.MiniFacedLabel(this, options)\r
348   \r
349   summary: (context, options) ->\r
350     klass = Pettanr.Views[@singular()].Summary\r
351     new klass(this, context, options)\r
352   \r
353   @index_url: () ->\r
354     Pettanr.url(@table_name(), 'index', {id: null})\r
355   \r
356   index_url: () ->\r
357     @my_class().index_url()\r
358   \r
359   list_url: (action_name = 'index') ->\r
360     Pettanr.url(@table_name(), action_name, {id: @get('id')})\r
361   \r
362   show_url: () ->\r
363     Pettanr.url(@table_name(), 'show', {id: @get('id')})\r
364   \r
365   prof_url: () ->\r
366     Pettanr.url(@table_name(), 'show', {id: @get('id'), format: 'prof'})\r
367   \r
368   new_url: () ->\r
369     Pettanr.url(@table_name(), 'new', {})\r
370   \r
371   create_url: () ->\r
372     Pettanr.url(@table_name(), 'create', {})\r
373   \r
374   edit_url: () ->\r
375     Pettanr.url(@table_name(), 'edit', {id: @get('id')})\r
376   \r
377   update_url: () ->\r
378     Pettanr.url(@table_name(), 'update', {id: @get('id')})\r
379   \r
380   destroy_url: () ->\r
381     Pettanr.url(@table_name(), 'destroy', {id: @get('id')})\r
382   \r
383   hold: () ->\r
384     Pettanr.cache.hold(this)\r
385   \r
386   fix: () ->\r
387     Pettanr.cache.fix(this)\r
388   \r
389   release: () ->\r
390     Pettanr.cache.release(this)\r
391   \r
392   save: (model_attr) ->\r
393     super(model_attr || @attributes, {\r
394       success: (model, response, options) =>\r
395         @trigger('save:success', model, response)\r
396       error: (model, response, options) =>\r
397         @trigger('save:fail', model, response)\r
398     })\r
399   \r
400   destroy: () ->\r
401     super({\r
402       success: (model, response, options) =>\r
403         @trigger('destroy:success', model, response)\r
404       error: (model, response, options) =>\r
405         @trigger('destroy:fail', model, response)\r
406     })\r
407   \r
408   is_editize: () ->\r
409     @editor\r
410   \r
411   dom_id: () ->\r
412     (@get('id') || '').toString()\r
413   \r
414   cache_key: () ->\r
415     @table_name() + '-' + @dom_id()\r
416   \r
417   dom_pool_type: () ->\r
418     @new_record ? 'stored' : 'new'\r
419   \r
420   merge_dom_item_id: (attr, name = null) ->\r
421     if @is_editize()\r
422       _.extend(attr, {'id': @dom_item_id(name)})\r
423     else\r
424       attr\r
425   \r
426   tag_attributes: (name = null, opt = {}) ->\r
427     r = {\r
428       'data-pool_type': @dom_pool_type(), 'data-id': @dom_id(), \r
429       'data-item_name': @item_name()\r
430     }\r
431     r = @merge_dom_item_id(r, name)\r
432     _.extend(r, opt)\r
433     r\r
434   \r
435   field_tag_attributes: (column_name, opt = {}) ->\r
436     r = @tag_attributes(column_name, opt)\r
437     _.extend(r, {'data-column_name': column_name})\r
438     r\r
439   \r
440   post_attribute_key: () ->\r
441     @dom_id\r
442   \r
443   @test: () ->\r
444     console.log(@child_models())\r
445     console.log(@child_element_names())\r
446     console.log(@my_peta())\r
447     console.log(@my_manifest())\r
448     console.log()\r
449     console.log()\r
450   \r
451   test: () ->\r
452     console.log(@my_class())\r
453     console.log(@item_name())\r
454     console.log(@model_name())\r
455     console.log(@table_name())\r
456     console.log(@path_name())\r
457     console.log(@pickup_item_name())\r
458     console.log(@pickup_column_name())\r
459     console.log(@dom_id())\r
460     console.log(@dom_pool_type())\r
461     console.log(@tag_attributes())\r
462     console.log(@field_tag_attributes('id'))\r
463     console.log(@post_attribute_key())\r