OSDN Git Service

fix js
[pettanr/pettanr.git] / app / assets / javascripts / routes.js.coffee
index 60033f0..1b45072 100644 (file)
 class Pettanr.Router extends Backbone.Router\r
-  routes: {\r
-    'folders/:id': 'folders_show'\r
-    'scrolls/': 'scrolls_index'\r
-    'aa': 'folders_show'\r
-  }\r
   \r
-  index: (r, a) ->\r
-  \r
-  show_html_format: (format) -> \r
-  \r
-  show: (id) ->\r
-  \r
-  count: () ->\r
-  \r
-  new: () ->\r
-  \r
-  edit: () ->\r
-  \r
-  create: () ->\r
-  \r
-  update: () ->\r
-  \r
-  destroy: () ->\r
-    \r
-  folders_index: () ->\r
-  \r
-  folders_root: () ->\r
-  \r
-  folders_show_html_format: (format) -> \r
-  \r
-  folders_show: (id) ->\r
-    @item = new Pettanr.FolderWatch({id: id})\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
+  fire: (params) ->\r
+    c = Pettanr[Pettanr.camelize(params['controller']) + 'Controller']\r
+    controller = new c\r
+    controller.params = params\r
+    controller[params['action']]()\r
+  \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.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
+  \r
+  home: () ->\r
+    params = {}\r
+    params['controller'] = 'folders'\r
+    params['action'] = 'root'\r
+    @fire(params)\r
+  \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
+      _.map controller_manifest.actions, (action_manifest, action_name) ->\r
+        {\r
+          event_name: controller_name + '_' + action_name, \r
+          controller: controller,\r
+          controller_name: controller_name,\r
+          action_manifest: action_manifest,\r
+          action_name: action_name\r
+        }\r
     _this = this\r
-    @item.fetch({}).done () ->\r
-      _this.render()\r
-    \r
-  render: () ->\r
-    if @item.is_remote()\r
-      window.router.navigate(@item.remote_name(), true)\r
-    else\r
-      list_result = new Pettanr.Folder.Children({id: @item.id})\r
-      new Pettanr.FilerRender('folder', list_result, 'default', window.operators)\r
-  \r
-  folders_count: () ->\r
-  \r
-  folders_new: () ->\r
-  \r
-  folders_edit: () ->\r
-  \r
-  scrolls_index: () ->\r
-    list_result = new Pettanr.Scroll.Collection({})\r
-    new Pettanr.FilerRender('scroll', list_result, 'default', window.operators)\r
-  \r
+    _.each _.flatten(names), (status) ->\r
+      _status = status\r
+      switch status.action_manifest.type\r
+        when 'list'\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
+          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