OSDN Git Service

fix:asoc system
[pettanr/pettanr.git] / app / assets / javascripts / views / speech_balloons / new_element.js.coffee
1 class Pettanr.Views.SpeechBalloon.NewElementLauncher extends Editor.EditorModule.DockModule.NewElementLauncher\r
2   \r
3   initialize: (options) ->\r
4     super(options)\r
5     @links_conf = {}\r
6     _.extend(@links_conf, {\r
7       template: {\r
8         caption: 'Template',\r
9         params: {\r
10           controller: @model.pick_model().path_name(), action: 'index',\r
11           page_size: 6, format: 'html'\r
12         }\r
13       }\r
14     })\r
15     _.extend(@links_conf, {\r
16       inspire_recent: {\r
17         caption: "Inspire recent item",\r
18         params: {\r
19           controller: @model.path_name(), action: 'index',\r
20           page_size: 6, format: 'html'\r
21         }\r
22       }\r
23     })\r
24     if Pettanr.cache.operators.author\r
25       _.extend(@links_conf, {\r
26         inspire_my_recent: {\r
27           caption: 'Inspire my recent item',\r
28           params: {\r
29             controller: @model.path_name(), action: 'by_author', id: Pettanr.cache.operators.author.get('id'), \r
30             page_size: 6, format: 'html'\r
31           }\r
32         }\r
33       })\r
34   \r
35   render: () ->\r
36     super()\r
37     @add_links()\r
38     this\r
39   \r
40   click_template: () ->\r
41     @click_default(@links_conf.template.params)\r
42   \r
43   click_inspire_recent: () ->\r
44     @click_default(@links_conf.inspire_recent.params)\r
45   \r
46   click_inspire_my_recent: () ->\r
47     @click_default(@links_conf.inspire_my_recent.params)\r
48   \r
49 class Pettanr.Views.SpeechBalloon.NewElement extends Pettanr.View\r
50   \r
51   initialize: (options) ->\r
52     @parent = options.parent\r
53     @target_model = options.target_model\r
54     @pick_model = @target_model.pick_model()\r
55   \r
56   start: (@dialog, params) ->\r
57     @dialog.start(@target_model, params, null)\r
58     @listenTo(@dialog, 'pick', @pick)\r
59     @trigger('dialog:open', @dialog)\r
60   \r
61   pick: (item) ->\r
62     if item.item_name() == @target_model.item_name()\r
63       # Inspire\r
64       @listenTo(this, 'ready', @ready)\r
65       item.get_child('balloon', this, {\r
66         success: (balloon) =>\r
67           item.get_child('speech', this, {\r
68             success: (speech) =>\r
69               new_item = new @target_model(item.attributes)\r
70               new_item.unset('id', {silent: true})\r
71               new_item.set({panel_id: @editor().item.get('id')}, {silent: true})\r
72               balloon.unset('id', {silent: true})\r
73               balloon.unset('speech_balloon_id', {silent: true})\r
74               speech.unset('id', {silent: true})\r
75               speech.unset('speech_balloon_id', {silent: true})\r
76               new_item.boosts 'post'\r
77               balloon.boosts 'post'\r
78               speech.boosts 'post'\r
79               new_item.set({balloon: balloon, speech: speech}, {silent: true})\r
80               @trigger('ready', new_item)\r
81           })\r
82       })\r
83     else if @target_model.is_traceable(item.item_name())\r
84       # Trace\r
85       item.trace_to(item.my_class().pick_item_name(), this, {\r
86         success: (pick_item) => \r
87           @dialog.http_get(pick_item.show_url())\r
88       })\r
89     else if item.item_name() == @target_model.pick_item_name()\r
90       # Pick\r
91       @listenTo(this, 'ready', @ready)\r
92       speech_balloon_template = item\r
93       writing_format_list = Locmare.ListGroup.list(\r
94         'writing_formats', 'index', {}\r
95       )\r
96       writing_format_list.open(this, {\r
97         success: (writing_formats) => \r
98           writing_format = writing_formats[0]\r
99           templates = {\r
100             speech_balloon_template: speech_balloon_template,\r
101             writing_format: writing_format\r
102           }\r
103           new_item = new @target_model()\r
104           new_item.templates = templates\r
105           new_item.set(new_item.pick(), {silent: true})\r
106           new_item.boosts 'post'\r
107           new_item.supply_default()\r
108           # prepare balloon\r
109           balloon = new Pettanr.Balloon()\r
110           balloon.templates = templates\r
111           balloon.set(balloon.pick(), {silent: true})\r
112           balloon.boosts 'post'\r
113           balloon.supply_default()\r
114           # prepare speech\r
115           speech = new Pettanr.Speech()\r
116           speech.templates = templates\r
117           speech.set(speech.pick(), {silent: true})\r
118           speech.boosts 'post'\r
119           speech.supply_default()\r
120           # join elements\r
121           new_item.set({balloon: balloon, speech: speech}, {silent: true})\r
122           @trigger('ready', new_item)\r
123       })\r
124   \r
125   ready: (new_item) ->\r
126     @dialog.stop()\r
127     @trigger('pick', new_item)\r
128   \r
129   dock: () ->\r
130     @parent.dock()\r
131   \r
132   editor: () ->\r
133     @dock().editor()\r
134   \r