OSDN Git Service

fix filer index
[pettanr/pettanr.git] / app / assets / javascripts / routes.js.coffee
1 class Pettanr.Router extends Backbone.Router\r
2   \r
3   _index: () ->\r
4     list_result = new Pettanr.Scroll.Collection({})\r
5     new Pettanr.FilerRender('scroll', list_result, 'default', window.operators)\r
6   \r
7   show_html_format: (format) -> \r
8   \r
9   count: () ->\r
10   \r
11   new: () ->\r
12   \r
13   edit: () ->\r
14   \r
15   create: () ->\r
16   \r
17   update: () ->\r
18   \r
19   destroy: () ->\r
20     \r
21   parse_query_string: (query_string) ->\r
22     params = {}\r
23     if query_string\r
24       ary = _.map decodeURI(query_string).split(/&/g), (el, i) ->\r
25         aux = el.split('=')\r
26         o = {}\r
27         if aux.length >= 1\r
28           val = undefined\r
29           if aux.length == 2\r
30             val = aux[1]\r
31           o[aux[0]] = val\r
32           o\r
33       _.each ary, (o) -> \r
34         _.extend(params, o)\r
35     params\r
36   \r
37   fire: (params) ->\r
38     c = Pettanr[Pettanr.camelize(params['controller']) + 'Controller']\r
39     controller = new c\r
40     controller.params = params\r
41     controller[params['action']]()\r
42   \r
43   initialize: () ->\r
44     this['c_i_a'] = (controller, id, action, query_string) ->\r
45       params = this.parse_query_string(query_string)\r
46       params['controller'] = controller\r
47       params['format'] = 'html'\r
48       t = id.split('.')\r
49       if t.length > 1\r
50         params['format'] = t[1]\r
51         id = t[0]\r
52       t = action.split('.')\r
53       if t.length > 1\r
54         params['format'] = t[1]\r
55         action = t[0]\r
56       if Pettanr.is_number(action)\r
57         params['action'] = id\r
58         params['id'] = action\r
59       else\r
60         params['action'] = action\r
61         params['id'] = id\r
62       this.fire(params)\r
63     this['c_i'] = (controller, id, query_string) ->\r
64       params = this.parse_query_string(query_string)\r
65       params['controller'] = controller\r
66       params['format'] = 'html'\r
67       t = id.split('.')\r
68       if t.length > 1\r
69         params['format'] = t[1]\r
70         id = t[0]\r
71       if Pettanr.is_number(id)\r
72         params['action'] = 'show'\r
73         params['id'] = id\r
74       else\r
75         params['action'] = 'index'\r
76       this.fire(params)\r
77     this.route(':controller/:id/:action?*query_string', 'c_i_a')\r
78     this.route(':controller/:id/:action', 'c_i_a')\r
79     this.route(':controller/:id?*query_string', 'c_i')\r
80     this.route(':controller/:id', 'c_i')\r
81   \r
82   hoge: () ->\r
83     names = _.map Manifest.manifest().controllers, (controller_manifest, controller_name) ->\r
84       c = Pettanr[Pettanr.camelize(controller_name) + 'Controller']\r
85       controller = new c\r
86       _.map controller_manifest.actions, (action_manifest, action_name) ->\r
87         {\r
88           event_name: controller_name + '_' + action_name, \r
89           controller: controller,\r
90           controller_name: controller_name,\r
91           action_manifest: action_manifest,\r
92           action_name: action_name\r
93         }\r
94     _this = this\r
95     _.each _.flatten(names), (status) ->\r
96       _status = status\r
97       switch status.action_manifest.type\r
98         when 'list'\r
99           e = status.event_name\r
100           e_f = e + '_format'\r
101           _this[e] = (queryString) ->\r
102             params = _this.parse_query_string(query_string)\r
103             console.log(params)\r
104             _status.controller[_status.action_name](params)\r
105           c = _status.controller_name\r
106           a = _status.action_name\r
107           c_a_url = c + '/' + a\r
108           _this.route(c_a_url, e)\r
109           r = r + '?*queryString'\r
110           _this.route(r, _status.event_name)\r
111           if _status.action_name == 'index'\r
112             r = _status.controller_name + '/'\r
113             _this.route(r, _status.event_name)\r
114         when 'show'\r
115           e = status.event_name\r
116           e_f = e + '_format'\r
117           _this[e] = (id, query_string) ->\r
118             params = _this.parse_query_string(query_string)\r
119             t = id.split('.')\r
120             if t.length > 1\r
121               params['format'] = t[1]\r
122               id = t[0]\r
123             console.log(id)\r
124             console.log(params)\r
125             _status.controller[_status.action_name](id, params)\r
126           _this[e_f] = (id, format, query_string) ->\r
127             params = _this.parse_query_string(query_string)\r
128             params['format'] = format\r
129             console.log(id)\r
130             console.log(params)\r
131             _status.controller[_status.action_name](id, params)\r
132           c = _status.controller_name\r
133           a = _status.action_name\r
134           q = '?*query_string'\r
135           i = '\/([0-9]+)'\r
136           c_id_a_url = c + '/:id/' + a\r
137           c_id_a_f_url = c + '/:id/' + a + '.:format'\r
138           c_id_a_q_url = c_id_a_url + q\r
139           c_id_a_q_url = c_id_a_url + '.:format' + q\r
140           _this.route(c_id_a_q_url, e_f)\r
141           _this.route(c_id_a_q_url, e)\r
142           _this.route(c_id_a_f_url, e_f)\r
143           _this.route(c_id_a_url, e)\r
144           if a == 'show'\r
145             c_id_url = c + '/:id'\r
146             c_id_q_url = c_id_url + q\r
147             _this.route(c_id_q_url, e)\r
148             _this.route(c_id_url, e)\r
149 \r