X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fassets%2Fjavascripts%2Froutes.js.coffee;h=8058b417e0f9dce6d48319fe638741bc0884045f;hb=bc33449931cc6705c3cfa5a79ee1450ae2d046fb;hp=e4019e01c4331d24c2033b47368e03bf9e1ff8f1;hpb=a35e88aec60a6165ba60285dd293fbb69c2e3f61;p=pettanr%2Fpettanr.git diff --git a/app/assets/javascripts/routes.js.coffee b/app/assets/javascripts/routes.js.coffee index e4019e01..8058b417 100644 --- a/app/assets/javascripts/routes.js.coffee +++ b/app/assets/javascripts/routes.js.coffee @@ -1,5 +1,38 @@ +class Pettanr.BeforeUnload + + constructor: (options) -> + + test: () -> + if @is_lock() + if confirm('leave? edit data unsaved') + return 1 + else + return -1 + 0 + + is_lock: () -> + if @params + true + else + false + + lock: (params, form) -> + @form = form + @params = params + + unlock: () -> + @form.quit() + @params = null + + url: () -> + Pettanr.params_to_url(@params) + class Pettanr.Router extends Backbone.Router + initialize: (options) -> + super() + _.extend(this, Backbone.Events) + parse_query_string: (query_string) -> params = {} if query_string @@ -16,139 +49,172 @@ class Pettanr.Router extends Backbone.Router _.extend(params, o) params - fire: (params) -> - c = Pettanr[Pettanr.camelize(params['controller']) + 'Controller'] - controller = new c - controller.params = params - controller[params['action']]() - - initialize: () -> - this['c_i_a'] = (controller, id, action, query_string) -> - params = this.parse_query_string(query_string) - params['controller'] = controller - params['format'] = 'html' - t = id.split('.') - if t.length > 1 - params['format'] = t[1] - id = t[0] - t = action.split('.') - if t.length > 1 - params['format'] = t[1] - action = t[0] - if Pettanr.is_number(action) - params['action'] = id - params['id'] = action - else - params['action'] = action - params['id'] = id - this.fire(params) - this['c_i'] = (controller, id, query_string) -> - params = this.parse_query_string(query_string) - params['controller'] = controller - params['format'] = 'html' - t = id.split('.') - if t.length > 1 - params['format'] = t[1] - id = t[0] - if Pettanr.is_number(id) - params['action'] = 'show' - params['id'] = id - else - if id - params['action'] = id - else - params['action'] = 'index' - this.fire(params) - this['c'] = (controller, query_string) -> - params = this.parse_query_string(query_string) - params['controller'] = controller - params['format'] = 'html' - params['action'] = 'index' - this.fire(params) - this.route('', 'home') - this.route(':controller/:id/:action?*query_string', 'c_i_a') - this.route(':controller/:id/:action', 'c_i_a') - this.route(':controller/:id?*query_string', 'c_i') - this.route(':controller/:id', 'c_i') - this.route(':controller?*query_string', 'c') - this.route(':controller', 'c') - this.route('users/sign_in', 'user_sign_in') + peta_routes: () -> + [ + {route: '', callback: 'home'}, + {route: 'top/:action', callback: 'top_a'}, + {route: 'home/:action', callback: 'home_a'}, + {route: 'users/:action', callback: 'users_a'}, # no manifest route + {route: 'user_sessions/:action', callback: 'user_sessions_a'}, # no manifest route + {route: ':controller/:id/:action?*query_string', callback: 'c_i_a'}, + {route: ':controller/:id/:action', callback: 'c_i_a'}, + {route: ':controller/:id?*query_string', callback: 'c_i'}, + {route: ':controller/:id', callback: 'c_i'}, + {route: ':controller?*query_string', callback: 'c'}, + {route: ':controller', callback: 'c'}, + ] + + find_route: (url) -> + r = null + _.any @peta_routes(), (route) => + route.regex = @_routeToRegExp(route.route) + if route.regex.test(url) + r = route + r + + url_to_params: (url) -> + if route = @find_route(url) + args = @_extractParameters(route.regex, url) + n = route.callback # callback function name by string + this[n + '_params'].apply(this, args) # call to c_i_a_params, c_i_params, c_params + else + {} + # render App(render:all) home: () -> + params = @home_params() + @trigger('go', params) + + home_params: () -> params = {} params['controller'] = 'folders' params['action'] = 'root' - @fire(params) - - user_sign_in: () -> - r = new Pettanr.UsersController() - r.index() - - hoge: () -> - names = _.map Manifest.manifest().controllers, (controller_manifest, controller_name) -> - c = Pettanr[Pettanr.camelize(controller_name) + 'Controller'] - controller = new c - _.map controller_manifest.actions, (action_manifest, action_name) -> - { - event_name: controller_name + '_' + action_name, - controller: controller, - controller_name: controller_name, - action_manifest: action_manifest, - action_name: action_name - } - _this = this - _.each _.flatten(names), (status) -> - _status = status - switch status.action_manifest.type - when 'list' - e = status.event_name - e_f = e + '_format' - _this[e] = (queryString) -> - params = _this.parse_query_string(query_string) - console.log(params) - _status.controller[_status.action_name](params) - c = _status.controller_name - a = _status.action_name - c_a_url = c + '/' + a - _this.route(c_a_url, e) - r = r + '?*queryString' - _this.route(r, _status.event_name) - if _status.action_name == 'index' - r = _status.controller_name + '/' - _this.route(r, _status.event_name) - when 'show' - e = status.event_name - e_f = e + '_format' - _this[e] = (id, query_string) -> - params = _this.parse_query_string(query_string) - t = id.split('.') - if t.length > 1 - params['format'] = t[1] - id = t[0] - console.log(id) - console.log(params) - _status.controller[_status.action_name](id, params) - _this[e_f] = (id, format, query_string) -> - params = _this.parse_query_string(query_string) - params['format'] = format - console.log(id) - console.log(params) - _status.controller[_status.action_name](id, params) - c = _status.controller_name - a = _status.action_name - q = '?*query_string' - i = '\/([0-9]+)' - c_id_a_url = c + '/:id/' + a - c_id_a_f_url = c + '/:id/' + a + '.:format' - c_id_a_q_url = c_id_a_url + q - c_id_a_q_url = c_id_a_url + '.:format' + q - _this.route(c_id_a_q_url, e_f) - _this.route(c_id_a_q_url, e) - _this.route(c_id_a_f_url, e_f) - _this.route(c_id_a_url, e) - if a == 'show' - c_id_url = c + '/:id' - c_id_q_url = c_id_url + q - _this.route(c_id_q_url, e) - _this.route(c_id_url, e) + params + + # controller/id/action case + # ex) scrolls/3/edit + c_i_a: (controller, id, action, query_string, form) -> + params = @c_i_a_params(controller, id, action, query_string) + @trigger('go', params, form) + + c_i_a_params: (controller, id, action, query_string) -> + params = @parse_query_string(query_string) + params['controller'] = controller + params['format'] = 'html' + t = id.split('.') + if t.length > 1 + params['format'] = t[1] + id = t[0] + t = action.split('.') + if t.length > 1 + params['format'] = t[1] + action = t[0] + if Pettanr.is_number(action) + params['action'] = id + params['id'] = action + else + params['action'] = action + params['id'] = id + params + + # controller and id case + # ex) scrolls/3 + c_i: (controller, id, query_string, form) -> + params = @c_i_params(controller, id, query_string) + @trigger('go', params, form) + + c_i_params: (controller, id, query_string) -> + params = @parse_query_string(query_string) + params['controller'] = controller + params['format'] = 'html' + t = id.split('.') + if t.length > 1 + params['format'] = t[1] + id = t[0] + if Pettanr.is_number(id) + params['action'] = 'show' + params['id'] = id + else + if id + params['action'] = id + else + params['action'] = 'index' + params + + # controller only case + # ex) scrolls + c: (controller, query_string, form) -> + params = @c_params(controller, query_string) + @trigger('go', params, form) + + c_params: (controller, query_string) -> + params = @parse_query_string(query_string) + params['controller'] = controller + params['format'] = 'html' + params['action'] = 'index' + params + + top_a: (action, query_string, form) -> + params = @users_a_params(action, query_string) + @trigger('go', params, form) + + top_a_params: (action, query_string) -> + params = @parse_query_string(query_string) + params['controller'] = 'top' + params['format'] = 'html' + params['action'] = action + params + + home_a: (action, query_string, form) -> + params = @home_a_params(action, query_string) + @trigger('go', params, form) + + home_a_params: (action, query_string) -> + params = @parse_query_string(query_string) + params['controller'] = 'home' + params['format'] = 'html' + params['action'] = action + params + + user_sessions_a: (action, query_string, form) -> + params = @user_sessions_a_params(action, query_string) + @trigger('go', params, form) + + user_sessions_a_params: (action, query_string) -> + params = @parse_query_string(query_string) + params['controller'] = 'user_sessions' + params['format'] = 'html' + params['action'] = action + params + + users_a: (action, query_string, form) -> + params = @users_a_params(action, query_string) + @trigger('go', params, form) + + users_a_params: (action, query_string) -> + params = @parse_query_string(query_string) + params['controller'] = 'users' + params['format'] = 'html' + params['action'] = action + params +class Pettanr.GlobalRouter extends Pettanr.Router + + initialize: (options) -> + super(options) + _.each @peta_routes(), (r) => + @route(r.route, r.callback) + +class Pettanr.LocalRouter extends Pettanr.Router + + initialize: (options) -> + super(options) + + # selfish navigate + navigate: (url, form) -> + if route = @find_route(url) + args = @_extractParameters(route.regex, url) + args.push(form) + n = route.callback # callback function name by string + this[n].apply(this, args) # call to c_i_a, c_i, c +