OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[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['action']](params)\r
41   \r
42   initialize: () ->\r
43     this['c_i_a'] = (controller, id, action, query_string) ->\r
44       params = this.parse_query_string(query_string)\r
45       params['controller'] = controller\r
46       params['format'] = 'html'\r
47       t = id.split('.')\r
48       if t.length > 1\r
49         params['format'] = t[1]\r
50         id = t[0]\r
51       t = action.split('.')\r
52       if t.length > 1\r
53         params['format'] = t[1]\r
54         action = t[0]\r
55       if Pettanr.is_number(action)\r
56         params['action'] = id\r
57         params['id'] = action\r
58       else\r
59         params['action'] = action\r
60         params['id'] = id\r
61       this.fire(params)\r
62     this['c_i'] = (controller, id, query_string) ->\r
63       params = this.parse_query_string(query_string)\r
64       params['controller'] = controller\r
65       params['format'] = 'html'\r
66       t = id.split('.')\r
67       if t.length > 1\r
68         params['format'] = t[1]\r
69         id = t[0]\r
70       if Pettanr.is_number(id)\r
71         params['action'] = 'show'\r
72         params['id'] = id\r
73       else\r
74         params['action'] = 'index'\r
75       this.fire(params)\r
76     this.route(':controller/:id/:action?*query_string', 'c_i_a')\r
77     this.route(':controller/:id/:action', 'c_i_a')\r
78     this.route(':controller/:id?*query_string', 'c_i')\r
79     this.route(':controller/:id', 'c_i')\r
80   \r
81   hoge: () ->\r
82     names = _.map Manifest.manifest().controllers, (controller_manifest, controller_name) ->\r
83       c = Pettanr[Pettanr.camelize(controller_name) + 'Controller']\r
84       controller = new c\r
85       _.map controller_manifest.actions, (action_manifest, action_name) ->\r
86         {\r
87           event_name: controller_name + '_' + action_name, \r
88           controller: controller,\r
89           controller_name: controller_name,\r
90           action_manifest: action_manifest,\r
91           action_name: action_name\r
92         }\r
93     _this = this\r
94     _.each _.flatten(names), (status) ->\r
95       _status = status\r
96       switch status.action_manifest.type\r
97         when 'list'\r
98           e = status.event_name\r
99           e_f = e + '_format'\r
100           _this[e] = (queryString) ->\r
101             params = _this.parse_query_string(query_string)\r
102             console.log(params)\r
103             _status.controller[_status.action_name](params)\r
104           c = _status.controller_name\r
105           a = _status.action_name\r
106           c_a_url = c + '/' + a\r
107           _this.route(c_a_url, e)\r
108           r = r + '?*queryString'\r
109           _this.route(r, _status.event_name)\r
110           if _status.action_name == 'index'\r
111             r = _status.controller_name + '/'\r
112             _this.route(r, _status.event_name)\r
113         when 'show'\r
114           e = status.event_name\r
115           e_f = e + '_format'\r
116           _this[e] = (id, query_string) ->\r
117             params = _this.parse_query_string(query_string)\r
118             t = id.split('.')\r
119             if t.length > 1\r
120               params['format'] = t[1]\r
121               id = t[0]\r
122             console.log(id)\r
123             console.log(params)\r
124             _status.controller[_status.action_name](id, params)\r
125           _this[e_f] = (id, format, query_string) ->\r
126             params = _this.parse_query_string(query_string)\r
127             params['format'] = format\r
128             console.log(id)\r
129             console.log(params)\r
130             _status.controller[_status.action_name](id, params)\r
131           c = _status.controller_name\r
132           a = _status.action_name\r
133           q = '?*query_string'\r
134           i = '\/([0-9]+)'\r
135           c_id_a_url = c + '/:id/' + a\r
136           c_id_a_f_url = c + '/:id/' + a + '.:format'\r
137           c_id_a_q_url = c_id_a_url + q\r
138           c_id_a_q_url = c_id_a_url + '.:format' + q\r
139           _this.route(c_id_a_q_url, e_f)\r
140           _this.route(c_id_a_q_url, e)\r
141           _this.route(c_id_a_f_url, e_f)\r
142           _this.route(c_id_a_url, e)\r
143           if a == 'show'\r
144             c_id_url = c + '/:id'\r
145             c_id_q_url = c_id_url + q\r
146             _this.route(c_id_q_url, e)\r
147             _this.route(c_id_url, e)\r
148 \r