OSDN Git Service

fix:balloon r
[pettanr/pettanr.git] / app / assets / javascripts / views / speech_balloons / new_element.js.coffee
index 03bce08..d10c67c 100644 (file)
@@ -4,17 +4,95 @@ class Pettanr.Views.SpeechBalloon.NewElement extends Backbone.View
   \r
   initialize: (options) ->\r
     @parent = options.parent\r
-    @model = options.model\r
+    @target_model = options.target_model\r
+    @pick_model = @target_model.pick_model()\r
+  \r
+  clear: () ->\r
+    this.$el.html('')\r
     params = {\r
-      controller: @model.path_name(), action: 'index',\r
-      page_size: 6, format: 'pick'\r
+      controller: @pick_model.path_name(), action: 'index',\r
+      page_size: 6, format: 'html'\r
     }\r
-    @dialog().get(params, null)\r
+    @dialog().start(@target_model, params, null)\r
+    @listenTo(@dialog(), 'pick', @pick)\r
+    this\r
   \r
   render: () ->\r
-    this.$el.html(@img.clear().el)\r
+    this.$el.html('')\r
     this\r
   \r
+  pick: (item) ->\r
+    if item.item_name() == @target_model.item_name()\r
+      # Inspire\r
+      @listenTo(this, 'ready', @ready)\r
+      balloon_list = item.has_one('balloon')\r
+      speech_list = item.has_one('speech')\r
+      balloon_list.open(() =>\r
+        balloon = balloon_list.items()[0]\r
+        speech_list.open(() =>\r
+          speech = speech_list.items()[0]\r
+          new_item = new @target_model(item.attributes)\r
+          new_item.unset('id', {silent: true})\r
+          new_item.set({panel_id: @editor().item.get('id')}, {silent: true})\r
+          balloon.unset('id', {silent: true})\r
+          balloon.unset('speech_balloon_id', {silent: true})\r
+          speech.unset('id', {silent: true})\r
+          speech.unset('speech_balloon_id', {silent: true})\r
+          new_item.boosts 'post'\r
+          balloon.boosts 'post'\r
+          speech.boosts 'post'\r
+          new_item.set({balloon: balloon, speech: speech}, {silent: true})\r
+          @trigger('ready', new_item)\r
+        )\r
+      )\r
+    else if @target_model.is_traceable(item.item_name())\r
+      # Trace\r
+      tracer_class = @target_model['TraceFrom' + item.singular()]\r
+      tracer = new tracer_class()\r
+      @listenTo(tracer, 'trace', @trace)\r
+      tracer.trace(item)\r
+    else if item.item_name() == @target_model.pick_item_name()\r
+      # Pick\r
+      @listenTo(this, 'ready', @ready)\r
+      speech_balloon_template = item\r
+      writing_format_list = Locmare.ListGroup.list(\r
+        'writing_formats', 'index', {}\r
+      )\r
+      writing_format_list.open(() =>\r
+        writing_format = writing_format_list.items()[0]\r
+        templates = {\r
+          speech_balloon_template: speech_balloon_template,\r
+          writing_format: writing_format\r
+        }\r
+        new_item = new @target_model()\r
+        new_item.templates = templates\r
+        new_item.set(new_item.pick(), {silent: true})\r
+        new_item.boosts 'post'\r
+        new_item.supply_default()\r
+        # prepare balloon\r
+        balloon = new Pettanr.Balloon()\r
+        balloon.templates = templates\r
+        balloon.set(balloon.pick(), {silent: true})\r
+        balloon.boosts 'post'\r
+        balloon.supply_default()\r
+        # prepare speech\r
+        speech = new Pettanr.Speech()\r
+        speech.templates = templates\r
+        speech.set(speech.pick(), {silent: true})\r
+        speech.boosts 'post'\r
+        speech.supply_default()\r
+        # join elements\r
+        new_item.set({balloon: balloon, speech: speech}, {silent: true})\r
+        @trigger('ready', new_item)\r
+      )\r
+  \r
+  ready: (new_item) ->\r
+    @dialog().stop()\r
+    @trigger('pick', new_item)\r
+  \r
+  trace: (speech_balloon) ->\r
+    @dialog().http_get(speech_balloon.show_url())\r
+  \r
   dock: () ->\r
     @parent.dock()\r
   \r