X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fassets%2Fjavascripts%2Froutes.js.coffee;h=be17bf30ac06cd20d9342bd6706aea36b3672c62;hb=b16d4899d5e7332f572a40ba1ca32250a7952413;hp=60033f00036379b7a3b124c535a3f540f1f8f2ca;hpb=34c7085d1693a103bc14da5727e5f04d24ac40a1;p=pettanr%2Fpettanr.git diff --git a/app/assets/javascripts/routes.js.coffee b/app/assets/javascripts/routes.js.coffee index 60033f00..be17bf30 100644 --- a/app/assets/javascripts/routes.js.coffee +++ b/app/assets/javascripts/routes.js.coffee @@ -1,54 +1,209 @@ -class Pettanr.Router extends Backbone.Router - routes: { - 'folders/:id': 'folders_show' - 'scrolls/': 'scrolls_index' - 'aa': 'folders_show' - } - - index: (r, a) -> - - show_html_format: (format) -> - - show: (id) -> +class Pettanr.BeforeUnload - count: () -> + constructor: (options) -> - new: () -> + test: () -> + if @is_lock() + if confirm('leave? edit data unsaved') + return 1 + else + return -1 + 0 - edit: () -> - - create: () -> + is_lock: () -> + if @params + true + else + false - update: () -> + lock: (params, form) -> + @form = form + @params = params - destroy: () -> - - folders_index: () -> + unlock: () -> + @form.quit() + @params = null - folders_root: () -> + url: () -> + Pettanr.params_to_url(@params) - folders_show_html_format: (format) -> +class Pettanr.Router extends Backbone.Router - folders_show: (id) -> - @item = new Pettanr.FolderWatch({id: id}) - _this = this - @item.fetch({}).done () -> - _this.render() - - render: () -> - if @item.is_remote() - window.router.navigate(@item.remote_name(), true) + 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 - list_result = new Pettanr.Folder.Children({id: @item.id}) - new Pettanr.FilerRender('folder', list_result, 'default', window.operators) - - folders_count: () -> - - folders_new: () -> - - folders_edit: () -> - - scrolls_index: () -> - list_result = new Pettanr.Scroll.Collection({}) - new Pettanr.FilerRender('scroll', list_result, 'default', window.operators) + {} + + # 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