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){
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){
+22 -29
View File
@@ -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 = $('<li></li>').appendTo(container)
link = $('<a></a>').appendTo(li)
link.attr('href', root_url + '/user/history_list.html?list_id='+list._id)
link = $('<a href="#"></a>').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 = $('<ul></ul>').appendTo(container)
if wrapper.num_pages && wrapper.num_pages > 1
for i in [1..wrapper.num_pages]
li = $('<li class="page"></li>')
link = $('<a></a>')
link = $('<a href="#"></a>')
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()