use mustache to abstract out html logic in javascript
This commit is contained in:
@@ -175,31 +175,35 @@ window.Quser=
|
||||
link.attr('href', root_url + src + '?'+authentication_string+'&page='+i)
|
||||
li.append(link)
|
||||
list.append(li)
|
||||
mustache: (selector, locals)->
|
||||
locs = $.extend(locals,
|
||||
currency: ->
|
||||
(val)->
|
||||
currency(Mustache.render(val, this))
|
||||
)
|
||||
Mustache.to_html($(selector).html(), locs)
|
||||
build_list_table: (body, foot, res) ->
|
||||
body.find('tr').remove()
|
||||
foot.find('tr').remove()
|
||||
if !res.orders && !res.orders.length
|
||||
alert('No orders in list')
|
||||
return
|
||||
for order in res.orders
|
||||
m_obj = res
|
||||
for order in m_obj.orders
|
||||
order_txts = []
|
||||
row = $('<tr></tr>').appendTo(body)
|
||||
row.addClass(order.state)
|
||||
#if(order.state == 'placed') row.addClass('info');
|
||||
#if(order.state == 'delivered') row.addClass('success');
|
||||
row.addClass('error') if order.state == 'cancelled'
|
||||
for product in order.products
|
||||
order_txts.push(product.name + ' (' + product['number'] + ')')
|
||||
row.append($('<td></td>').text(order_txts.join(', ')))
|
||||
row.append($('<td class="currency"></td>').html(currency(order.total_amount)))
|
||||
foot.append('<tr><td></td><td class="currency"><strong>'+currency(res.total_amount)+'</strong></td></tr>')
|
||||
order.products_display = order_txts.join(', ')
|
||||
body.append @mustache('#active-list-orders-template', m_obj)
|
||||
foot.append @mustache('#active-list-orders-footer-template', m_obj)
|
||||
|
||||
order_selected_products: ()->
|
||||
return if $.isEmptyObject(window.active_products_list)
|
||||
h = {}
|
||||
match = window.document.URL.toString().match('table_id=([0-9a-zA-Z]+)')
|
||||
h['table_id'] = match[1] if match
|
||||
for product_id, info of window.active_products_list
|
||||
h['products['+product_id+']'] = info.number
|
||||
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) -> Quser.handle_response(res)), 'json')
|
||||
handle_response: (res) ->
|
||||
if(typeof(res) == 'string')
|
||||
@@ -215,21 +219,17 @@ window.Quser=
|
||||
else
|
||||
redirect_to res.location || 'list_products' if res['ok']
|
||||
build_product_list: ->
|
||||
table = $('#active-order-table')
|
||||
tbody = table.find('tbody')
|
||||
tbody = $('<tbody></tbody>').appendTo(table) unless tbody.length
|
||||
tbody.find('tr').remove()
|
||||
total = 0.0
|
||||
for product_id, info of window.active_products_list
|
||||
total += info.product.price * info.number
|
||||
row = $('<tr></tr>').attr('id', 'active-order-row-'+product_id).appendTo(tbody)
|
||||
row.append('<td>'+info.product.name+'</td>')
|
||||
row.append('<td>'+info.number+'</td>')
|
||||
row.append('<td class="currency">'+currency(info.product.price * info.number)+'</td>')
|
||||
x_btn = $('<button class="btn btn-warning btn-mini">x</button>').click(-> delete(window.active_products_list[product_id]) && Quser.build_product_list() )
|
||||
row.append($('<td></td>').append(x_btn))
|
||||
$('#active-order-total').html(currency(total))
|
||||
table.show()
|
||||
h = {products: []}
|
||||
for product_id, number of window.active_products_list
|
||||
product = window.products[product_id]
|
||||
product.number = number
|
||||
product.product_total = product.price * number
|
||||
h.products.push product
|
||||
total += product.price * number
|
||||
h.total = total
|
||||
$('#active-order-container').html @mustache('#active-order-template', h)
|
||||
Qupdate('#active-order-container')
|
||||
|
||||
load_active_list_products: ->
|
||||
Quser.populate_products_table('/user/list_products.json?'+authentication_string)
|
||||
@@ -256,40 +256,39 @@ window.Quser=
|
||||
if res.supplier_name
|
||||
$('.supplier-name').text(res.supplier_name)
|
||||
delete(res['supplier_name'])
|
||||
window.products = {}
|
||||
body.find('tr').remove()
|
||||
script_id = if include_order_buttons then '#products-category-for-order-template' else '#products-category-template'
|
||||
for category, products of res
|
||||
body.append('<tr><td colspan="4"><h4>'+category+'<h4></td></tr>')
|
||||
body.append Quser.mustache(script_id,
|
||||
category: category,
|
||||
products: products,
|
||||
include_order_buttons: include_order_buttons
|
||||
)
|
||||
for product in products
|
||||
row = $('<tr></tr>')
|
||||
row.append('<td>'+product.name+'</td>')
|
||||
if include_order_buttons
|
||||
button = $('<button class="btn btn-mini btn-primary">Add</button>')
|
||||
callback = ((prod) ->
|
||||
->
|
||||
product_count_holder = $('#order-product-count-'+prod._id)
|
||||
count = parseInt(product_count_holder.text())
|
||||
product_count_holder.text(1)
|
||||
Quser.add_product(prod, count)
|
||||
)(product)
|
||||
button.click(callback)
|
||||
order_product_count = $('<span id="order-product-count-'+product._id+'" class="order-product-count">1</span>')
|
||||
order_count_minus = $('<span class="btn btn-info btn-mini">-</span>')
|
||||
order_count_minus.click(->
|
||||
val_holder = $(this).siblings('.order-product-count')
|
||||
val = parseInt(val_holder.text())
|
||||
val_holder.text(val - 1) if val > 1
|
||||
)
|
||||
order_count_plus = $('<span class="btn btn-info btn-mini">+</span>')
|
||||
order_count_plus.click(->
|
||||
val_holder = $(this).siblings('.order-product-count')
|
||||
val = parseInt(val_holder.text())
|
||||
val_holder.text(val + 1)
|
||||
)
|
||||
row.append($('<td class="order-count-cell"></td>').append(order_count_minus).append(' ').append(order_product_count).append(' ').append(order_count_plus))
|
||||
row.append('<td>'+currency(product.price)+'</td>')
|
||||
row.append($('<td></td>').append(button)) if include_order_buttons
|
||||
body.append(row)
|
||||
window.products[product._id] = product
|
||||
)
|
||||
increment_products_counter: (product_id)->
|
||||
product_count_holder = $('#order-product-count-'+product_id)
|
||||
count = parseInt(product_count_holder.text())
|
||||
product_count_holder.text(count + 1)
|
||||
false
|
||||
lower_products_counter: (product_id)->
|
||||
product_count_holder = $('#order-product-count-'+product_id)
|
||||
count = parseInt(product_count_holder.text())
|
||||
product_count_holder.text(count - 1)
|
||||
false
|
||||
add_product_to_order: (product_id, context) ->
|
||||
product_count_holder = $('#order-product-count-'+product_id)
|
||||
count = parseInt(product_count_holder.text())
|
||||
count ||= 1
|
||||
product_count_holder.text(1)
|
||||
window.active_products_list ||= {}
|
||||
window.active_products_list[product_id] ||= 0
|
||||
window.active_products_list[product_id] += count
|
||||
delete(window.active_products_list[product_id]) if window.active_products_list[product_id] < 1
|
||||
Quser.build_product_list()
|
||||
false
|
||||
actions_for_table: (table)->
|
||||
table = JSON.parse(table) if typeof(table) == 'string'
|
||||
$.getJSON(data_host + '/user/table_info.json?'+authentication_string+'&table_id='+table.table_id, (res)->
|
||||
@@ -357,11 +356,11 @@ window.Quser=
|
||||
else
|
||||
redirect_to 'user_root', {message: 'join_request_rejected'}
|
||||
)
|
||||
add_product: (product, count) ->
|
||||
add_product: (product_id, count) ->
|
||||
count ||= 1
|
||||
window.active_products_list = {} unless window.active_products_list
|
||||
window.active_products_list[product._id] = {product: product, number: 0} unless window.active_products_list[product._id]
|
||||
window.active_products_list[product._id].number += count
|
||||
window.active_products_list ||= {}
|
||||
window.active_products_list[product_id] ||= 0
|
||||
window.active_products_list[product_id] += count
|
||||
Quser.build_product_list()
|
||||
clear_selected_products: ->
|
||||
window.active_products_list = {}
|
||||
|
||||
Reference in New Issue
Block a user