OSDN Git Service

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