From 69dff38fd730101ab837d9bc40328947438927a1 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Fri, 7 Dec 2012 14:46:34 +0100 Subject: [PATCH] Move user to local storage part one, should work already --- .../javascripts/user/application.js.erb | 27 ++++------ app/assets/javascripts/user/quser.js.coffee | 51 ++++++++----------- app/controllers/user_controller.rb | 23 +++++---- app/models/list.rb | 2 + app/views/user/list_history.html.slim | 2 +- 5 files changed, 47 insertions(+), 58 deletions(-) diff --git a/app/assets/javascripts/user/application.js.erb b/app/assets/javascripts/user/application.js.erb index 385b934e..55ed9706 100644 --- a/app/assets/javascripts/user/application.js.erb +++ b/app/assets/javascripts/user/application.js.erb @@ -113,11 +113,12 @@ var $translations = { } function redirect_to(mapping, variables){ variables || (variables = {}); - var vars = [] + var vars = []; for(var name in variables){ - vars.push(name + '=' +variables[name]) + vars.push(name + '=' +variables[name]); + Qstorage[name] = variables[name]; } - window.location = QMobile.root_url() + path_mapping[mapping] + '.html?' + vars.join('&') + window.location = QMobile.root_url() + path_mapping[mapping] + '.html' } function direct_to_site(mapping, variables){ variables || (variables = {}); @@ -156,29 +157,21 @@ $.ajaxSetup({ } } }); -function getUrlVars() { - if($url_vars) return $url_vars; - $url_vars = {}; - var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { - $url_vars[key] = value; - }); - return $url_vars; -} +var Qstorage = sessionStorage; $(function(){ $locale = QMobile.locale(); - if(getUrlVars().message){ + if(Qstorage.message){ var container = $('.alert-success'); - // Prepend messages. if there is no path/period specified - var message = t((getUrlVars().message.indexOf('.') > -1 ? '' : 'messages.' ) + getUrlVars().message ) - container.find('div').text(message); + container.find('div').text($translations[$locale]['messages'][Qstorage.message]); container.show(); + Qstorage.removeItem('message'); } - if(getUrlVars().list_closed){ + if(Qstorage.list_closed){ var container = $('.alert-error'); container.find('div').text($translations[$locale]['messages']['the_list_has_been_closed']); container.show(); + Qstorage.removeItem('list_closed'); } - $('[data-t]').each(function(){$(this).text(t($(this).attr('data-t')))}) setTranslations(); }); function setLocale(locale){ diff --git a/app/assets/javascripts/user/quser.js.coffee b/app/assets/javascripts/user/quser.js.coffee index 86e3518d..3481f67b 100644 --- a/app/assets/javascripts/user/quser.js.coffee +++ b/app/assets/javascripts/user/quser.js.coffee @@ -173,11 +173,7 @@ class Quser @build_list_table(body, foot, res) ) load_history_list: () -> - match = window.document.URL.toString().match('list_id=([0-9a-zA-Z]+)') - if match - list_id = match[1] - else - return + return unless list_id = Qstorage.list_id $.getJSON(data_host + '/user/history_list.json?list_id='+list_id+'&'+ authentication_string, (res) => body = $('#history-list-table tbody') foot = $('#history-list-table tfoot') @@ -187,37 +183,38 @@ class Quser $('.supplier-name').text(res.supplier_name) ) load_list_history: -> - match = window.document.URL.toString().match('page=([0-9]+)') - if match - page = match[1] - else - page = 1 + page = Qstorage.page || 1 + page = parseInt(page) $.getJSON(data_host + '/user/list_history.json?'+authentication_string+'&page='+page, (res) => - @paginate(res, '/user/list_history.html') - container = $('#list-history-container') + @paginate(res, @load_list_history) + container = $('#list-history-container').html('') for list in res.lists li = $('
  • ').appendTo(container) - link = $('').appendTo(li) - link.attr('href', root_url + '/user/history_list.html?list_id='+list._id) + link = $('').appendTo(li) + link.click(-> redirect_to 'history_list', list_id: list._id) txt = list.supplier_name txt += ' - ' txt += @format_date(list.created_at) link.text(txt) ) - paginate: (wrapper, src) -> + paginate: (wrapper, callback) -> 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 = $('') link.text(i) if wrapper.current_page && wrapper.current_page == i li.addClass('active') - link.attr('href', 'javascript:void(0)') else - link.attr('href', root_url + src + '?'+authentication_string+'&page='+i) + callback = ((i)-> + -> + Qstorage.page = i + window.Quser.load_list_history() + )(i) + link.click(callback) li.append(link) list.append(li) mustache: (selector, locals)-> @@ -239,9 +236,9 @@ class Quser order_selected_products: ()-> return if $.isEmptyObject(window.active_products_list) + return unless Qstorage.table_id h = {} - match = window.document.URL.toString().match('table_id=([0-9a-zA-Z]+)') - h['table_id'] = match[1] if match + h['table_id'] = Qstorage.table_id for product_id, number of window.active_products_list h['products['+product_id+']'] = number $.post(data_host + '/user/order_selected_products', $.extend(h, authentication_object), ((res) => @handle_response(res)), 'json') @@ -274,13 +271,8 @@ class Quser load_active_list_products: -> @populate_products_table('/user/list_products.json?'+authentication_string) load_table_products: -> - match = window.document.URL.toString().match('table_id=([0-9a-zA-Z]+)') - if match - table_id = match[1] - else - redirect_to 'user_root', {message: 'cannot_identify_table'} - return - @populate_products_table('/user/list_products_for_table.json?'+authentication_string+'&table_id='+table_id) + return redirect_to 'user_root', {message: 'cannot_identify_table'} unless Qstorage.table_id + @populate_products_table('/user/list_products_for_table.json?'+authentication_string+'&table_id='+Qstorage.table_id) populate_products_table: (src)-> $.getJSON(data_host + src, (res) => include_order_buttons = res.my_list || !res.table_occupied @@ -325,6 +317,7 @@ class Quser false actions_for_table: (table)-> table = JSON.parse(table) if typeof(table) == 'string' + Qstorage.table_id = table.table_id $.getJSON(data_host + '/user/table_info.json?'+authentication_string+'&table_id='+table.table_id, (res)=> if res.current_table_id if res.other_supplier @@ -366,12 +359,12 @@ class Quser redirect_to 'list_products_for_table', {table_id: table.table_id} , 'json') show_table_products: -> - unless table_id = getUrlVars().table_id + unless table_id = Qstorage.table_id redirect_to 'user_root', {message: 'cannot_identify_table'} return redirect_to 'list_products_for_table', table_id: table_id join_occupied_table: () -> - unless table_id = getUrlVars().table_id + unless table_id = Qstorage.table_id redirect_to 'user_root', {message: 'cannot_identify_table'} return $('.form-actions').remove() diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index b787fcd1..367b514a 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -101,11 +101,11 @@ class UserController < ApplicationController end def list_products_for_table - @table = Table.find(params[:table_id]) respond_to do |format| format.html do end format.json do + @table = Table.find(params[:table_id]) h = ProductCategory.for_user(current_user, table: @table) render json: h end @@ -114,8 +114,8 @@ class UserController < ApplicationController # GET /user/join_occupied_table def join_occupied_table - redirect_to user_root_path(message: 'table_not_found') and return unless params[:table_id].present? - @table = Table.find(params[:table_id]) + #redirect_to user_root_path(message: 'table_not_found') and return unless params[:table_id].present? + #@table = Table.find(params[:table_id]) end # POST /user/join_occupied_table @@ -219,11 +219,11 @@ class UserController < ApplicationController # Displays the closed lists of the user # GET /user/list_history 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 + @lists = List.for_user(current_user, page: params[:page], per_page: params[:per_page].presence || 14) + @lists.include_relation(:supplier) 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 @@ -233,22 +233,23 @@ class UserController < ApplicationController # Displays a closed list of the user # GET /user/list_history/:list_id def history_list - @list = List.find(params[:list_id]) - if params[:list_closed].present? && current_user.active_list_id == @list.id - current_user.list_is_closed! - flash.now[:notice] = t('messages.the_list_has_been_closed', list: List.model_name.human) - end - redirect_to user_root_path, alert: t('messages.illegal_history_list_attempt') and return unless @list.user_ids.include?(current_user.id) respond_to do |format| format.html do end format.json do + @list = List.find(params[:list_id]) + if params[:list_closed].present? && current_user.active_list_id == @list.id + current_user.list_is_closed! + flash.now[:notice] = t('messages.the_list_has_been_closed', list: List.model_name.human) + end + redirect_to user_root_path, alert: t('messages.illegal_history_list_attempt') and return unless @list.user_ids.include?(current_user.id) render json: @list.with_orders_as_json.merge(supplier_name: @list.supplier.name) end end end + # POST /user/order_selected_products.json def order_selected_products if list.present? @list = list diff --git a/app/models/list.rb b/app/models/list.rb index 3e576890..2fa43d5f 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -82,6 +82,8 @@ class List end def self.for_user(user, options = {}) + total_entries = database.view(for_user_view({startkey: ["#{user.id}\u9999"], endkey: [user.id], include_docs: false, reduce: true, descending: true})) + options[:total_entries] = total_entries with_pagination_options(options) do |options| database.view(for_user_view({startkey: ["#{user.id}\u9999"], endkey: [user.id], include_docs: true, reduce: false, descending: true}.merge(options))) end diff --git a/app/views/user/list_history.html.slim b/app/views/user/list_history.html.slim index 58bd467a..156ead0a 100644 --- a/app/views/user/list_history.html.slim +++ b/app/views/user/list_history.html.slim @@ -2,4 +2,4 @@ h1 data-t="list_history.title" = t('user.list_history.title') nav.pagination ul#list-history-container -- onload_javascript "Quser.load_list_history()" +- onload_javascript "Qstorage.page = 1; Quser.load_list_history()"