From 2b2b81c23b6d2b84e2c12a822474decccfb0d599 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Mon, 17 Sep 2012 18:35:16 +0200 Subject: [PATCH] javascript translations and default format html --- app/assets/javascripts/user/application.js | 14 ++++-- app/assets/javascripts/user/quser.js.coffee | 56 ++++++++++++++++++--- app/controllers/user_controller.rb | 6 +++ app/views/user/list_history.html.slim | 11 ++-- config/application.rb | 1 + config/routes.rb | 1 + script/build_mobile_app.rb | 1 + 7 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 script/build_mobile_app.rb diff --git a/app/assets/javascripts/user/application.js b/app/assets/javascripts/user/application.js index 4278916b..f6f7df16 100644 --- a/app/assets/javascripts/user/application.js +++ b/app/assets/javascripts/user/application.js @@ -53,6 +53,12 @@ var translations = { selected_products: { order: 'Order', clear: 'Clear' + }, + join_request: { + title: 'Join request', + body: '%{email} wants to join the table', + reject: 'Reject', + approve: 'Approve' } } function redirect_to(mapping, variables){ @@ -61,7 +67,7 @@ function redirect_to(mapping, variables){ for(var name in variables){ vars.push(name + '=' +variables[name]) } - window.location = path_mapping[mapping] + '?' + vars.join('&') + window.location = path_mapping[mapping] + '.html?' + vars.join('&') } function currency(num) { return Qwaiter.currency(num); @@ -69,8 +75,9 @@ function currency(num) { String.prototype.capitalize = function() { return this.charAt(0).toUpperCase() + this.slice(1); } -function t(path){ - var parts = path.split('.') +function t(path, vars){ + vars || (vars = {}); + var parts = path.split('.'); var accessor = 'translations["' + parts.join('"]["')+ '"]'; var result; try{ @@ -78,5 +85,6 @@ function t(path){ } catch(err){ result = parts[parts.length - 1].capitalize(); } + $.each(vars, function(v, value){ result = result.replace('%{'+v+'}', value)}); return result; } diff --git a/app/assets/javascripts/user/quser.js.coffee b/app/assets/javascripts/user/quser.js.coffee index 07a7844d..0976af0a 100644 --- a/app/assets/javascripts/user/quser.js.coffee +++ b/app/assets/javascripts/user/quser.js.coffee @@ -3,7 +3,7 @@ window.Quser= handle_active_list: (callback) -> $.get(data_host + '/user/list_info.json', (res) -> if !res.list_active - window.location = '/user?list_closed=true' + redirect_to 'user_root', {list_closed: 'true'} return window.active_list = res callback.call() if callback @@ -33,16 +33,17 @@ window.Quser= )(join_request) header = $('') .append('') - .append('

Join request

').appendTo(wrapper) + .append($('

').text(t('join_request.title'))).appendTo(wrapper) body = $('') - body.append(join_request.user_email + ' wants to join the table') + #body.append(join_request.user_email + ' wants to join the table') + body.text(t('join_request.body', {email: join_request.user_email})) body.appendTo(wrapper) footer = $('') - .append($('Reject').click(reject_callback)) - .append($('Approve').click(join_callback)) + .append($('').text(t('join_request.reject')).click(reject_callback)) + .append($('').text(t('join_request.approve')).click(join_callback)) .appendTo(wrapper) wrapper.modal() @@ -100,7 +101,7 @@ window.Quser= $.get(data_host + '/user/active_list.json', (res) -> window.active_list = res unless res.list_active - window.location = '/user/list_history/'+res._id + '?list_closed=true' + window.location = '/user/list_history/'+res._id + '.html?list_closed=true' return Quser.handle_active_list_default_actions(res) body = $('#active-list-table tbody') @@ -118,6 +119,42 @@ window.Quser= foot = $('#history-list-table tfoot') Quser.build_list_table(body, foot, res) ) + load_list_history: -> + match = window.document.URL.toString().match('page=([0-9]+)') + if match + page = match[1] + else + page = 1 + $.get(data_host + '/user/list_history.json?page='+page, (res) -> + Quser.paginate(res, '/user/list_history.html') + container = $('#list-history-container') + for list in res.lists + li = $('
  • ').appendTo(container) + link = $('').appendTo(li) + link.attr('href', '/user/list_history/'+list._id + '.html') + txt = list.supplier_name + txt += ' - ' + txt += list.created_at.substr(0,10) + txt += ' ' + txt += list.created_at.substr(11, 5) + link.text(txt) + ) + paginate: (wrapper, src) -> + container = $('nav.pagination') + container.html('') + list = $('').appendTo(container) + if wrapper.num_pages && wrapper.num_pages > 1 + for i in [1..wrapper.num_pages] + li = $('
  • ') + link = $('') + link.text(i) + if wrapper.current_page && wrapper.current_page == i + li.addClass('active') + link.attr('href', 'javascript:void(0)') + else + link.attr('href', src + '?page='+i) + li.append(link) + list.append(li) build_list_table: (body, foot, res) -> body.find('tr').remove() foot.find('tr').remove() @@ -152,8 +189,11 @@ window.Quser= else eval(res) return - window.location = '/user' if res['message'] && !res['ok'] - window.location = res.location || '/user/list_products' if res['ok'] + + if res['message'] && !res['ok'] + redirect_to 'user_root', {message: res['message']} + else + redirect_to 'list_products' if res['ok'] build_product_list: -> table = $('#active-order-table') tbody = table.find('tbody') diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 72bd8a91..eaac3324 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -201,6 +201,12 @@ class UserController < ApplicationController def list_history @lists = List.for_user(current_user, page: params[:page], per_page: params[:per_page].presence || 14) @lists.include_relation(:supplier) + respond_to do |format| + format.html {} + format.json do + render json: @lists.inject(lists: [], current_page: @lists.current_page, num_pages: @lists.num_pages, total_count: @lists.total_count){|h, l| h[:lists] << l.as_json.merge(supplier_name: l.supplier.name); h} + end + end end ## diff --git a/app/views/user/list_history.html.slim b/app/views/user/list_history.html.slim index 049ac106..5c02408d 100644 --- a/app/views/user/list_history.html.slim +++ b/app/views/user/list_history.html.slim @@ -1,5 +1,8 @@ .page-header= title 'User list history' -= paginate @lists -ul - - for list in @lists - li= link_to "#{l(list.created_at, format: :short)} - #{list.supplier.name}", user_history_list_path(list_id: list.id) +nav.pagination +ul#list-history-container +- content_for :footer do + javascript: + $(function(){ + Quser.load_list_history(); + }); diff --git a/config/application.rb b/config/application.rb index ea2a2b73..fd1d1330 100644 --- a/config/application.rb +++ b/config/application.rb @@ -73,6 +73,7 @@ module Qrammer # Enable the asset pipeline config.assets.enabled = true config.assets.precompile += ['supplier/application.css', 'user/application.css', 'qr_sheet/application.css'] + config.default_url_options = {format: 'html'} # Version of your assets, change this if you want to expire all your assets config.assets.version = '1.0' diff --git a/config/routes.rb b/config/routes.rb index 168ce8ce..0c45a2a5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -37,6 +37,7 @@ Qrammer::Application.routes.draw do match '/supplier/settings' => 'supplier#update', as: :supplier_update_settings, via: [:put, :post] # USER + default_url_options format: 'html' match '/user' => 'user#home', as: :user_root get '/user/active_list(.:format)' => 'user#active_list', as: :user_active_list get '/user/list_info' => 'user#list_info', as: :user_list_info diff --git a/script/build_mobile_app.rb b/script/build_mobile_app.rb new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/script/build_mobile_app.rb @@ -0,0 +1 @@ +