OSDN Git Service

fix bucket
[pettanr/pettanr.git] / app / assets / javascripts / routes.js.coffee
index 40e91e6..acfaa44 100644 (file)
@@ -1,28 +1,85 @@
 class Pettanr.Router extends Backbone.Router\r
   \r
-  _index: () ->\r
-    list_result = new Pettanr.Scroll.Collection({})\r
-    new Pettanr.FilerRender('scroll', list_result, 'default', window.operators)\r
+  parse_query_string: (query_string) ->\r
+    params = {}\r
+    if query_string\r
+      ary = _.map decodeURI(query_string).split(/&/g), (el, i) ->\r
+        aux = el.split('=')\r
+        o = {}\r
+        if aux.length >= 1\r
+          val = undefined\r
+          if aux.length == 2\r
+            val = aux[1]\r
+          o[aux[0]] = val\r
+          o\r
+      _.each ary, (o) -> \r
+        _.extend(params, o)\r
+    params\r
   \r
-  show_html_format: (format) -> \r
+  fire: (params) ->\r
+    c = Pettanr[Pettanr.camelize(params['controller']) + 'Controller']\r
+    controller = new c\r
+    controller.params = params\r
+    controller[params['action']]()\r
   \r
-  folders_show: (id) ->\r
-    console.log(controller_name)\r
-  \r
-  count: () ->\r
-  \r
-  new: () ->\r
-  \r
-  edit: () ->\r
-  \r
-  create: () ->\r
-  \r
-  update: () ->\r
+  initialize: () ->\r
+    this['c_i_a'] = (controller, id, action, query_string) ->\r
+      params = this.parse_query_string(query_string)\r
+      params['controller'] = controller\r
+      params['format'] = 'html'\r
+      t = id.split('.')\r
+      if t.length > 1\r
+        params['format'] = t[1]\r
+        id = t[0]\r
+      t = action.split('.')\r
+      if t.length > 1\r
+        params['format'] = t[1]\r
+        action = t[0]\r
+      if Pettanr.is_number(action)\r
+        params['action'] = id\r
+        params['id'] = action\r
+      else\r
+        params['action'] = action\r
+        params['id'] = id\r
+      this.fire(params)\r
+    this['c_i'] = (controller, id, query_string) ->\r
+      params = this.parse_query_string(query_string)\r
+      params['controller'] = controller\r
+      params['format'] = 'html'\r
+      t = id.split('.')\r
+      if t.length > 1\r
+        params['format'] = t[1]\r
+        id = t[0]\r
+      if Pettanr.is_number(id)\r
+        params['action'] = 'show'\r
+        params['id'] = id\r
+      else\r
+        if id\r
+          params['action'] = id\r
+        else\r
+          params['action'] = 'index'\r
+      this.fire(params)\r
+    this['c'] = (controller, query_string) ->\r
+      params = this.parse_query_string(query_string)\r
+      params['controller'] = controller\r
+      params['format'] = 'html'\r
+      params['action'] = 'index'\r
+      this.fire(params)\r
+    this.route('', 'home')\r
+    this.route(':controller/:id/:action?*query_string', 'c_i_a')\r
+    this.route(':controller/:id/:action', 'c_i_a')\r
+    this.route(':controller/:id?*query_string', 'c_i')\r
+    this.route(':controller/:id', 'c_i')\r
+    this.route(':controller?*query_string', 'c')\r
+    this.route(':controller', 'c')\r
   \r
-  destroy: () ->\r
-    \r
+  home: () ->\r
+    params = {}\r
+    params['controller'] = 'folders'\r
+    params['action'] = 'root'\r
+    @fire(params)\r
   \r
-  initialize: () ->\r
+  hoge: () ->\r
     names = _.map Manifest.manifest().controllers, (controller_manifest, controller_name) ->\r
       c = Pettanr[Pettanr.camelize(controller_name) + 'Controller']\r
       controller = new c\r
@@ -37,21 +94,56 @@ class Pettanr.Router extends Backbone.Router
     _this = this\r
     _.each _.flatten(names), (status) ->\r
       _status = status\r
-      f = switch status.action_manifest.type\r
+      switch status.action_manifest.type\r
         when 'list'\r
-          _this[status.event_name] = () ->\r
-            _status.controller[_status.action_name]()\r
-          r = _status.controller_name + '/' + _status.action_name\r
+          e = status.event_name\r
+          e_f = e + '_format'\r
+          _this[e] = (queryString) ->\r
+            params = _this.parse_query_string(query_string)\r
+            console.log(params)\r
+            _status.controller[_status.action_name](params)\r
+          c = _status.controller_name\r
+          a = _status.action_name\r
+          c_a_url = c + '/' + a\r
+          _this.route(c_a_url, e)\r
+          r = r + '?*queryString'\r
           _this.route(r, _status.event_name)\r
           if _status.action_name == 'index'\r
             r = _status.controller_name + '/'\r
             _this.route(r, _status.event_name)\r
         when 'show'\r
-          _this[status.event_name] = (id) ->\r
-            _status.controller[_status.action_name](id)\r
-          r = _status.controller_name + '/:id/' + _status.action_name\r
-          _this.route(r, _status.event_name)\r
-          if _status.action_name == 'show'\r
-            r = _status.controller_name + '/:id'\r
-            _this.route(r, _status.event_name)\r
+          e = status.event_name\r
+          e_f = e + '_format'\r
+          _this[e] = (id, query_string) ->\r
+            params = _this.parse_query_string(query_string)\r
+            t = id.split('.')\r
+            if t.length > 1\r
+              params['format'] = t[1]\r
+              id = t[0]\r
+            console.log(id)\r
+            console.log(params)\r
+            _status.controller[_status.action_name](id, params)\r
+          _this[e_f] = (id, format, query_string) ->\r
+            params = _this.parse_query_string(query_string)\r
+            params['format'] = format\r
+            console.log(id)\r
+            console.log(params)\r
+            _status.controller[_status.action_name](id, params)\r
+          c = _status.controller_name\r
+          a = _status.action_name\r
+          q = '?*query_string'\r
+          i = '\/([0-9]+)'\r
+          c_id_a_url = c + '/:id/' + a\r
+          c_id_a_f_url = c + '/:id/' + a + '.:format'\r
+          c_id_a_q_url = c_id_a_url + q\r
+          c_id_a_q_url = c_id_a_url + '.:format' + q\r
+          _this.route(c_id_a_q_url, e_f)\r
+          _this.route(c_id_a_q_url, e)\r
+          _this.route(c_id_a_f_url, e_f)\r
+          _this.route(c_id_a_url, e)\r
+          if a == 'show'\r
+            c_id_url = c + '/:id'\r
+            c_id_q_url = c_id_url + q\r
+            _this.route(c_id_q_url, e)\r
+            _this.route(c_id_url, e)\r
 \r