OSDN Git Service

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