OSDN Git Service

fix: fetch err
[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, options) ->\r
131     shorten = options.shorten\r
132     l = if _.isBoolean(shorten)\r
133       if shorten\r
134         # supply default shorten_length\r
135         @constructor.default_label_shorten_length\r
136       else\r
137         # False be no shorten\r
138         null\r
139     else\r
140       # numeric\r
141       shorten\r
142     Pettanr.truncate(@get(label_column_name), l)\r
143   \r
144   label: (options) ->\r
145     ''\r
146   \r
147   get_association: (routes, context, options) ->\r
148     routes = [routes] if _.isString(routes)\r
149     route = routes.shift()\r
150     if _.isEmpty(routes)\r
151       # fetching terminate association. callback\r
152       cxt = options.context || context\r
153       @fetch_association(route, cxt, {\r
154         success: (association_item, options) =>\r
155           options.success.call(context, association_item)\r
156         context: context,\r
157         options: options\r
158       })\r
159     else\r
160       # fetching through associations\r
161       @fetch_association(route, this, {\r
162         success: (association_item, options) =>\r
163           association_item.get_association(routes, this, options)\r
164         context: context,\r
165         options: options\r
166       })\r
167   \r
168   fetch_association: (name, context, options) =>\r
169     a = @my_class().my_manifest().associations\r
170     fetch_options = {\r
171       success: (association_item) =>\r
172         options.success.call(context, association_item, options.options)\r
173     }\r
174     if a.belongs_to[name]\r
175       @get_parent(name, context, fetch_options)\r
176     else if a.has_many[name]\r
177       @get_children(name, context, fetch_options)\r
178     else if a.has_one[name]\r
179       @get_child(name, context, fetch_options)\r
180     else\r
181       console.error('association does not exist in model manifest')\r
182   \r
183   get_parent: (belongs_to_name, context, options = null) ->\r
184     m = Manifest.item_name_to_model(belongs_to_name)\r
185     retriever = new Pettanr.Cache.Retriever(m, @get(belongs_to_name + '_id'))\r
186     return retriever if !options\r
187     @listenTo(retriever, 'retrieve', (item) =>\r
188       options.success.call(context, item)\r
189     )\r
190     @listenTo(retriever, 'fail', (response, opt) =>\r
191       options.fail.call(context, response, opt)\r
192     )\r
193     retriever.retrieve()\r
194   \r
195   get_child: (has_one_name, context, options = null) ->\r
196     list = @has_one(has_one_name)\r
197     list.open(context, {\r
198       success: (items) =>\r
199         callback = options.success\r
200         item = items[0]\r
201         callback.call(context, item)\r
202     })\r
203   \r
204   get_children: (has_many_name, context, options = null) ->\r
205     list = @has_many(has_many_name)\r
206     list.open(context, {\r
207       success: (items) =>\r
208         callback = options.success\r
209         callback.call(context, items)\r
210     })\r
211   \r
212   has_many: (has_many_name) ->\r
213     has_many_manifest = @my_class().my_manifest().associations.has_many[has_many_name]\r
214     action_name = has_many_manifest.list_action_name\r
215     Locmare.ListGroup.list(\r
216       has_many_name, action_name, {id: @get('id')}\r
217     )\r
218   \r
219   has_one: (has_one_name) ->\r
220     has_one_manifest = @my_class().my_manifest().associations.has_one[has_one_name]\r
221     controller_name = has_one_manifest.model().table_name()\r
222     action_name = has_one_manifest.list_action_name\r
223     Locmare.ListGroup.list(\r
224       controller_name, action_name, {id: @get('id')}\r
225     )\r
226   \r
227   trace_to: (trace_name, context, options) ->\r
228     routes = @my_class().trace_routes()[trace_name]\r
229     if !routes\r
230       console.error('no trace route')\r
231       return\r
232     @get_association(routes, this, {\r
233       success: (association) => \r
234         options.success.call(context, association)\r
235     })\r
236   \r
237   boosts: (level) ->\r
238     c = @my_class().my_peta().boost\r
239     _.each c, (boost_manifest, boost_name) =>\r
240       return if level == 'read' and boost_manifest.level == 'post'\r
241       @boost boost_manifest\r
242   \r
243   boost: (boost_manifest) ->\r
244     @boosters[boost_manifest.name] = new Locmare.Booster(boost_manifest, this)\r
245     # fetched item is not cleared .  force write booster\r
246     #@boosters[boost_manifest.name] ||= new Locmare.Booster(boost_manifest, this)\r
247   \r
248   is_extend_column: (column_name) ->\r
249     @is_extend_column column_name\r
250   \r
251   is_visible: (operators = Pettanr.cache.operators) ->\r
252     if Manifest.manifest().magic_numbers['run_mode'] == 0\r
253       return false if not operators.is_guest()\r
254     else\r
255       return false if not operators.is_resource_reader()\r
256     true\r
257   \r
258   retriever: () ->\r
259     new Pettanr.Cache.Retriever(@my_class(), @get('id'))\r
260   \r
261   retrieve: (context, options = {}) ->\r
262     retriever = new Pettanr.Cache.Retriever(@my_class(), @get('id'))\r
263     @listenTo(retriever, 'retrieve', (item) =>\r
264       callback = options.success\r
265       callback.call(context, item)\r
266     )\r
267     @listenTo(retriever, 'fail', (response, opt) =>\r
268       callback = options.fail\r
269       callback.call(context, response, opt)\r
270     )\r
271     retriever.retrieve(options.force)\r
272   \r
273   @pick_item_name: () ->\r
274     null\r
275   \r
276   @pick_model: () ->\r
277     Manifest.item_name_to_model(@pick_item_name())\r
278   \r
279   @traceable_item_names: () ->\r
280     []\r
281   \r
282   @is_traceable: (item_name) ->\r
283     _.contains(@traceable_item_names(), item_name)\r
284   \r
285   @is_picker_inspire: (item_name) ->\r
286     item_name == @item_name()\r
287   \r
288   @is_picker_trace: (item_name) ->\r
289     @is_traceable(item_name)\r
290   \r
291   @is_picker_pick: (item_name) ->\r
292     item_name == @pick_item_name()\r
293   \r
294   @pick_type: (item_name) ->\r
295     if @is_picker_inspire(item_name)\r
296       'inspire'\r
297     else if @is_picker_trace(item_name)\r
298       'trace'\r
299     else if @is_picker_pick(item_name)\r
300       'pick'\r
301     else\r
302       'none'\r
303   \r
304   symbol_option: (context, options) ->\r
305     @trace_to('symbol', this, {\r
306       success: (symbol_item) => \r
307         options.success.call(context, symbol_item.symbol_file())\r
308     })\r
309   \r
310   @face_file: () ->\r
311     new Pettanr.ImageFile('/images/' + @item_name() + '.gif')\r
312   \r
313   face_file: () ->\r
314     @my_class().face_file()\r
315   \r
316   # thumbnail size picture\r
317   symbol_file: (subdir) ->\r
318     new Pettanr.PictureFile(this, subdir)\r
319   \r
320   # real size picture\r
321   picture_file: (subdir = null) ->\r
322     new Pettanr.PictureFile(this, subdir)\r
323   \r
324   real_picture: (subdir = null) ->\r
325     new Pettanr.View.RealIcon(@picture_file(subdir))\r
326   \r
327   # item.face_button({\r
328   #   context: this, \r
329   #   click: () ->\r
330   #     # ...\r
331   # })\r
332   face_button: (options) ->\r
333     Pettanr.View.face_button(this, @face_file(), options)\r
334   \r
335   mini_face_button: (options) ->\r
336     Pettanr.View.mini_face_button(this, @face_file(), options)\r
337   \r
338   prof_button: (options) ->\r
339     Pettanr.View.prof_button(@prof_url(), options)\r
340   \r
341   mini_prof_button: (options) ->\r
342     Pettanr.View.mini_prof_button(@prof_url(), options)\r
343   \r
344   symbol_button: (options) ->\r
345     Pettanr.View.face_button(this, @symbol_file(), options)\r
346   \r
347   mini_symbol_button: (options) ->\r
348     Pettanr.View.mini_face_button(this, @symbol_file(), options)\r
349   \r
350   # pencil button\r
351   edit_button: (options) ->\r
352     icon = new Pettanr.View.Icon(Pettanr.View.Image.icon_edit_file())\r
353     Pettanr.View.any_button(this, @edit_url(), icon, options)\r
354   \r
355   mini_edit_button: (options) ->\r
356     icon = new Pettanr.View.Minicon(Pettanr.View.Image.icon_edit_file())\r
357     Pettanr.View.mini_any_button(this,  @edit_url(), icon, options)\r
358   \r
359   # x button\r
360   destroy_button: (options) ->\r
361     icon = new Pettanr.View.Icon(Pettanr.View.Image.icon_destroy_file())\r
362     Pettanr.View.any_button(this, @destroy_url(), icon, options)\r
363   \r
364   mini_destroy_button: (options) ->\r
365     icon = new Pettanr.View.Minicon(Pettanr.View.Image.icon_destroy_file())\r
366     Pettanr.View.mini_any_button(this,  @destroy_url(), icon, options)\r
367   \r
368   label_button: (label_options, button_options) ->\r
369     url = button_options.url || @show_url()\r
370     new Pettanr.View.Button(url, _.escape(@label(label_options)), button_options)\r
371   \r
372   #  faced_label_button({\r
373   #      shorten: true\r
374   #    }, {\r
375   #      url: @show_url(),\r
376   #      context: this,\r
377   #      click: () =>\r
378   #        # ...\r
379   #    })\r
380   faced_label_button: (label_options, button_options) ->\r
381     new Pettanr.View.FacedLabelButton(this, label_options, button_options)\r
382   \r
383   mini_faced_label_button: (label_options, button_options) ->\r
384     new Pettanr.View.MiniFacedLabelButton(this, label_options, button_options)\r
385   \r
386   summary: (context, options) ->\r
387     klass = Pettanr.Views[@singular()].Summary\r
388     new klass(this, context, options)\r
389   \r
390   @index_url: () ->\r
391     Pettanr.url(@table_name(), 'index', {id: null})\r
392   \r
393   index_url: () ->\r
394     @my_class().index_url()\r
395   \r
396   list_url: (action_name = 'index') ->\r
397     Pettanr.url(@table_name(), action_name, {id: @get('id')})\r
398   \r
399   show_url: () ->\r
400     Pettanr.url(@table_name(), 'show', {id: @get('id')})\r
401   \r
402   prof_url: () ->\r
403     Pettanr.url(@table_name(), 'show', {id: @get('id'), format: 'prof'})\r
404   \r
405   new_url: () ->\r
406     Pettanr.url(@table_name(), 'new', {})\r
407   \r
408   create_url: () ->\r
409     Pettanr.url(@table_name(), 'create', {})\r
410   \r
411   edit_url: () ->\r
412     Pettanr.url(@table_name(), 'edit', {id: @get('id')})\r
413   \r
414   update_url: () ->\r
415     Pettanr.url(@table_name(), 'update', {id: @get('id')})\r
416   \r
417   destroy_url: () ->\r
418     Pettanr.url(@table_name(), 'destroy', {id: @get('id')})\r
419   \r
420   hold: () ->\r
421     Pettanr.cache.hold(this)\r
422   \r
423   fix: () ->\r
424     Pettanr.cache.fix(this)\r
425   \r
426   release: () ->\r
427     Pettanr.cache.release(this)\r
428   \r
429   free: () ->\r
430     Pettanr.cache.free(this)\r
431   \r
432   save: (model_attr) ->\r
433     super(model_attr || @attributes, {\r
434       success: (model, response, options) =>\r
435         @trigger('save:success', model, response)\r
436       error: (model, response, options) =>\r
437         @trigger('save:fail', model, response)\r
438     })\r
439   \r
440   destroy: () ->\r
441     super({\r
442       success: (model, response, options) =>\r
443         @free()\r
444         @trigger('destroy:success', model, response)\r
445       error: (model, response, options) =>\r
446         @trigger('destroy:fail', model, response)\r
447     })\r
448   \r
449   is_editize: () ->\r
450     @editor\r
451   \r
452   dom_id: () ->\r
453     (@get('id') || '').toString()\r
454   \r
455   cache_key: () ->\r
456     @table_name() + '-' + @dom_id()\r
457   \r
458   dom_pool_type: () ->\r
459     @new_record ? 'stored' : 'new'\r
460   \r
461   merge_dom_item_id: (attr, name = null) ->\r
462     if @is_editize()\r
463       _.extend(attr, {'id': @dom_item_id(name)})\r
464     else\r
465       attr\r
466   \r
467   tag_attributes: (name = null, opt = {}) ->\r
468     r = {\r
469       'data-pool_type': @dom_pool_type(), 'data-id': @dom_id(), \r
470       'data-item_name': @item_name()\r
471     }\r
472     r = @merge_dom_item_id(r, name)\r
473     _.extend(r, opt)\r
474     r\r
475   \r
476   field_tag_attributes: (column_name, opt = {}) ->\r
477     r = @tag_attributes(column_name, opt)\r
478     _.extend(r, {'data-column_name': column_name})\r
479     r\r
480   \r
481   post_attribute_key: () ->\r
482     @dom_id\r
483   \r
484   @test: () ->\r
485     console.log(@child_models())\r
486     console.log(@child_element_names())\r
487     console.log(@my_peta())\r
488     console.log(@my_manifest())\r
489     console.log()\r
490     console.log()\r
491   \r
492   test: () ->\r
493     console.log(@my_class())\r
494     console.log(@item_name())\r
495     console.log(@model_name())\r
496     console.log(@table_name())\r
497     console.log(@path_name())\r
498     console.log(@pickup_item_name())\r
499     console.log(@pickup_column_name())\r
500     console.log(@dom_id())\r
501     console.log(@dom_pool_type())\r
502     console.log(@tag_attributes())\r
503     console.log(@field_tag_attributes('id'))\r
504     console.log(@post_attribute_key())\r