X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fassets%2Fjavascripts%2Froutes.js.coffee;h=be17bf30ac06cd20d9342bd6706aea36b3672c62;hb=d7ac24c1094fa64a52148bf868ae2c4814380596;hp=40e91e60101de53ac9486943eca2c483efd47c8d;hpb=398480eed63a15043b85aeb143886a1d2b7d576b;p=pettanr%2Fpettanr.git diff --git a/app/assets/javascripts/routes.js.coffee b/app/assets/javascripts/routes.js.coffee index 40e91e60..be17bf30 100644 --- a/app/assets/javascripts/routes.js.coffee +++ b/app/assets/javascripts/routes.js.coffee @@ -1,57 +1,209 @@ +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 - _index: () -> - list_result = new Pettanr.Scroll.Collection({}) - new Pettanr.FilerRender('scroll', list_result, 'default', window.operators) - - show_html_format: (format) -> - - folders_show: (id) -> - console.log(controller_name) - - count: () -> - - new: () -> - - edit: () -> - - create: () -> - - update: () -> - - destroy: () -> - - - initialize: () -> - 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 - f = switch status.action_manifest.type - when 'list' - _this[status.event_name] = () -> - _status.controller[_status.action_name]() - r = _status.controller_name + '/' + _status.action_name - _this.route(r, _status.event_name) - if _status.action_name == 'index' - r = _status.controller_name + '/' - _this.route(r, _status.event_name) - when 'show' - _this[status.event_name] = (id) -> - _status.controller[_status.action_name](id) - r = _status.controller_name + '/:id/' + _status.action_name - _this.route(r, _status.event_name) - if _status.action_name == 'show' - r = _status.controller_name + '/:id' - _this.route(r, _status.event_name) + initialize: (options) -> + super() + _.extend(this, Backbone.Events) + @operators = options.operators + + parse_query_string: (query_string) -> + params = {} + if query_string + ary = _.map decodeURI(query_string).split(/&/g), (el, i) -> + aux = el.split('=') + o = {} + if aux.length >= 1 + val = undefined + if aux.length == 2 + val = aux[1] + o[aux[0]] = val + o + _.each ary, (o) -> + _.extend(params, o) + params + + peta_routes: () -> + [ + {route: '', callback: 'home'}, + {route: 'top/:action', callback: 'top_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' + 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 + + 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 +