Move user to local storage part one, should work already

This commit is contained in:
2012-12-07 14:46:34 +01:00
parent fa68893510
commit 69dff38fd7
5 changed files with 47 additions and 58 deletions
+10 -17
View File
@@ -113,11 +113,12 @@ var $translations = {
} }
function redirect_to(mapping, variables){ function redirect_to(mapping, variables){
variables || (variables = {}); variables || (variables = {});
var vars = [] var vars = [];
for(var name in variables){ 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){ function direct_to_site(mapping, variables){
variables || (variables = {}); variables || (variables = {});
@@ -156,29 +157,21 @@ $.ajaxSetup({
} }
} }
}); });
function getUrlVars() { var Qstorage = sessionStorage;
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;
}
$(function(){ $(function(){
$locale = QMobile.locale(); $locale = QMobile.locale();
if(getUrlVars().message){ if(Qstorage.message){
var container = $('.alert-success'); var container = $('.alert-success');
// Prepend messages. if there is no path/period specified container.find('div').text($translations[$locale]['messages'][Qstorage.message]);
var message = t((getUrlVars().message.indexOf('.') > -1 ? '' : 'messages.' ) + getUrlVars().message )
container.find('div').text(message);
container.show(); container.show();
Qstorage.removeItem('message');
} }
if(getUrlVars().list_closed){ if(Qstorage.list_closed){
var container = $('.alert-error'); var container = $('.alert-error');
container.find('div').text($translations[$locale]['messages']['the_list_has_been_closed']); container.find('div').text($translations[$locale]['messages']['the_list_has_been_closed']);
container.show(); container.show();
Qstorage.removeItem('list_closed');
} }
$('[data-t]').each(function(){$(this).text(t($(this).attr('data-t')))})
setTranslations(); setTranslations();
}); });
function setLocale(locale){ function setLocale(locale){
+22 -29
View File
@@ -173,11 +173,7 @@ class Quser
@build_list_table(body, foot, res) @build_list_table(body, foot, res)
) )
load_history_list: () -> load_history_list: () ->
match = window.document.URL.toString().match('list_id=([0-9a-zA-Z]+)') return unless list_id = Qstorage.list_id
if match
list_id = match[1]
else
return
$.getJSON(data_host + '/user/history_list.json?list_id='+list_id+'&'+ authentication_string, (res) => $.getJSON(data_host + '/user/history_list.json?list_id='+list_id+'&'+ authentication_string, (res) =>
body = $('#history-list-table tbody') body = $('#history-list-table tbody')
foot = $('#history-list-table tfoot') foot = $('#history-list-table tfoot')
@@ -187,37 +183,38 @@ class Quser
$('.supplier-name').text(res.supplier_name) $('.supplier-name').text(res.supplier_name)
) )
load_list_history: -> load_list_history: ->
match = window.document.URL.toString().match('page=([0-9]+)') page = Qstorage.page || 1
if match page = parseInt(page)
page = match[1]
else
page = 1
$.getJSON(data_host + '/user/list_history.json?'+authentication_string+'&page='+page, (res) => $.getJSON(data_host + '/user/list_history.json?'+authentication_string+'&page='+page, (res) =>
@paginate(res, '/user/list_history.html') @paginate(res, @load_list_history)
container = $('#list-history-container') container = $('#list-history-container').html('')
for list in res.lists for list in res.lists
li = $('<li></li>').appendTo(container) li = $('<li></li>').appendTo(container)
link = $('<a></a>').appendTo(li) link = $('<a href="#"></a>').appendTo(li)
link.attr('href', root_url + '/user/history_list.html?list_id='+list._id) link.click(-> redirect_to 'history_list', list_id: list._id)
txt = list.supplier_name txt = list.supplier_name
txt += ' - ' txt += ' - '
txt += @format_date(list.created_at) txt += @format_date(list.created_at)
link.text(txt) link.text(txt)
) )
paginate: (wrapper, src) -> paginate: (wrapper, callback) ->
container = $('nav.pagination') container = $('nav.pagination')
container.html('') container.html('')
list = $('<ul></ul>').appendTo(container) list = $('<ul></ul>').appendTo(container)
if wrapper.num_pages && wrapper.num_pages > 1 if wrapper.num_pages && wrapper.num_pages > 1
for i in [1..wrapper.num_pages] for i in [1..wrapper.num_pages]
li = $('<li class="page"></li>') li = $('<li class="page"></li>')
link = $('<a></a>') link = $('<a href="#"></a>')
link.text(i) link.text(i)
if wrapper.current_page && wrapper.current_page == i if wrapper.current_page && wrapper.current_page == i
li.addClass('active') li.addClass('active')
link.attr('href', 'javascript:void(0)')
else 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) li.append(link)
list.append(li) list.append(li)
mustache: (selector, locals)-> mustache: (selector, locals)->
@@ -239,9 +236,9 @@ class Quser
order_selected_products: ()-> order_selected_products: ()->
return if $.isEmptyObject(window.active_products_list) return if $.isEmptyObject(window.active_products_list)
return unless Qstorage.table_id
h = {} h = {}
match = window.document.URL.toString().match('table_id=([0-9a-zA-Z]+)') h['table_id'] = Qstorage.table_id
h['table_id'] = match[1] if match
for product_id, number of window.active_products_list for product_id, number of window.active_products_list
h['products['+product_id+']'] = number h['products['+product_id+']'] = number
$.post(data_host + '/user/order_selected_products', $.extend(h, authentication_object), ((res) => @handle_response(res)), 'json') $.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: -> load_active_list_products: ->
@populate_products_table('/user/list_products.json?'+authentication_string) @populate_products_table('/user/list_products.json?'+authentication_string)
load_table_products: -> load_table_products: ->
match = window.document.URL.toString().match('table_id=([0-9a-zA-Z]+)') return redirect_to 'user_root', {message: 'cannot_identify_table'} unless Qstorage.table_id
if match @populate_products_table('/user/list_products_for_table.json?'+authentication_string+'&table_id='+Qstorage.table_id)
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)
populate_products_table: (src)-> populate_products_table: (src)->
$.getJSON(data_host + src, (res) => $.getJSON(data_host + src, (res) =>
include_order_buttons = res.my_list || !res.table_occupied include_order_buttons = res.my_list || !res.table_occupied
@@ -325,6 +317,7 @@ class Quser
false false
actions_for_table: (table)-> actions_for_table: (table)->
table = JSON.parse(table) if typeof(table) == 'string' 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)=> $.getJSON(data_host + '/user/table_info.json?'+authentication_string+'&table_id='+table.table_id, (res)=>
if res.current_table_id if res.current_table_id
if res.other_supplier if res.other_supplier
@@ -366,12 +359,12 @@ class Quser
redirect_to 'list_products_for_table', {table_id: table.table_id} redirect_to 'list_products_for_table', {table_id: table.table_id}
, 'json') , 'json')
show_table_products: -> show_table_products: ->
unless table_id = getUrlVars().table_id unless table_id = Qstorage.table_id
redirect_to 'user_root', {message: 'cannot_identify_table'} redirect_to 'user_root', {message: 'cannot_identify_table'}
return return
redirect_to 'list_products_for_table', table_id: table_id redirect_to 'list_products_for_table', table_id: table_id
join_occupied_table: () -> join_occupied_table: () ->
unless table_id = getUrlVars().table_id unless table_id = Qstorage.table_id
redirect_to 'user_root', {message: 'cannot_identify_table'} redirect_to 'user_root', {message: 'cannot_identify_table'}
return return
$('.form-actions').remove() $('.form-actions').remove()
+12 -11
View File
@@ -101,11 +101,11 @@ class UserController < ApplicationController
end end
def list_products_for_table def list_products_for_table
@table = Table.find(params[:table_id])
respond_to do |format| respond_to do |format|
format.html do format.html do
end end
format.json do format.json do
@table = Table.find(params[:table_id])
h = ProductCategory.for_user(current_user, table: @table) h = ProductCategory.for_user(current_user, table: @table)
render json: h render json: h
end end
@@ -114,8 +114,8 @@ class UserController < ApplicationController
# GET /user/join_occupied_table # GET /user/join_occupied_table
def join_occupied_table def join_occupied_table
redirect_to user_root_path(message: 'table_not_found') and return unless params[:table_id].present? #redirect_to user_root_path(message: 'table_not_found') and return unless params[:table_id].present?
@table = Table.find(params[:table_id]) #@table = Table.find(params[:table_id])
end end
# POST /user/join_occupied_table # POST /user/join_occupied_table
@@ -219,11 +219,11 @@ class UserController < ApplicationController
# Displays the closed lists of the user # Displays the closed lists of the user
# GET /user/list_history # GET /user/list_history
def 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| respond_to do |format|
format.html {} format.html {}
format.json do 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} 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 end
@@ -233,22 +233,23 @@ class UserController < ApplicationController
# Displays a closed list of the user # Displays a closed list of the user
# GET /user/list_history/:list_id # GET /user/list_history/:list_id
def history_list 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| respond_to do |format|
format.html do format.html do
end end
format.json do 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) render json: @list.with_orders_as_json.merge(supplier_name: @list.supplier.name)
end end
end end
end end
# POST /user/order_selected_products.json
def order_selected_products def order_selected_products
if list.present? if list.present?
@list = list @list = list
+2
View File
@@ -82,6 +82,8 @@ class List
end end
def self.for_user(user, options = {}) 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| 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))) database.view(for_user_view({startkey: ["#{user.id}\u9999"], endkey: [user.id], include_docs: true, reduce: false, descending: true}.merge(options)))
end end
+1 -1
View File
@@ -2,4 +2,4 @@
h1 data-t="list_history.title" = t('user.list_history.title') h1 data-t="list_history.title" = t('user.list_history.title')
nav.pagination nav.pagination
ul#list-history-container ul#list-history-container
- onload_javascript "Quser.load_list_history()" - onload_javascript "Qstorage.page = 1; Quser.load_list_history()"