Emberfy supplier progress

This commit is contained in:
2014-12-01 14:54:15 +01:00
parent c714a7a74d
commit 9ef3d4b725
15 changed files with 131 additions and 315 deletions
@@ -1,4 +1,5 @@
#= require_self
#= require handlebars
#= require ember
#= require ember-data
#= require_directory ./modifications
@@ -8,7 +8,7 @@ App.MenuController = Ember.ObjectController.extend
if filter_day = @get('filter.day')
list = list.filterBy "active_on_#{filter_day}"
if filter_minute = @get('filter.minute_of_day')
list = list.filter (r) -> r.get('full_day') or (filter_minute > r.get('start_from') and filter_minute < r.get('end_on'))
list = list.filter (r) -> r.get('full_day') or (filter_minute >= r.get('start_from') and filter_minute <= r.get('end_on'))
list.sortBy('position')
).property('product_categories.@each', 'product_categories.@each.position', 'filter.day', 'filter.minute_of_day')
@@ -16,6 +16,8 @@ App.Supplier = DS.Model.extend
lng: attr 'number'
week_starts_on_monday: attr 'boolean'
product_categories: DS.hasMany 'product_category'
orders_in_process_count: attr('number')
orders_placed_count: attr('number')
close: ->
$.post Routes.supplier_mark_as_closed_path(), =>
@@ -1,55 +0,0 @@
App.ApplicationRoute = Ember.Route.extend
beforeModel: ->
# Preload only active lists and orders
@supplier = @store.push 'supplier', supplier_object
@product_categories = @store.find 'product_category'
@sections = @store.find 'section'
Ember.RSVP.all([@product_categories, @sections]).then (results)=>
@store.find('list', state: 'active').then (lists) -> lists.invoke('get', 'table')
@lists = @store.all 'list'
# product_categories = controller.set 'product_categories', @store.all('product_category')
#@store.find 'order', state: 'active' included in list
setupController: (controller)->
controller.set 'supplier', @supplier
# @set 'supplier', @store.find('supplier', supplier_id)
controller.set 'sections', @sections
controller.set 'product_categories', @product_categories
actions:
# openModal: (modalName, model, options={})->
# controller_name = options.controller || modalName
# controller = @controllerFor(controller_name)
# controller.set 'model', model
# controller.set 'modal_options', options
# @render modalName,
# into: 'application'
# outlet: 'modal'
# controller: controller_name
openModal: (modalName, options={})->
controller_name = options.controller || modalName
try
controller = @controllerFor("modals/#{modalName}")
catch error
controller = @controllerFor("modals/base")
controller.set 'model', options.model
defaultModalOptions =
closeOnOverlay: true
closeOnModalClick: false
controller.set 'modal_options', $.extend(defaultModalOptions, options)
@render "modals/#{modalName}",
into: 'application'
outlet: 'modal'
view: 'modal'
controller: controller
closeModal: ->
@disconnectOutlet
outlet: 'modal'
parentView: 'application'
confirm: (options = {})->
@send 'openModal', 'confirm',
model: Ember.Object.create
title: options.title
body: options.body
cancel: options.cancel
ok: options.ok
@@ -0,0 +1,93 @@
App.ApplicationRoute = Ember.Route.extend
beforeModel: ->
# Preload only active lists and orders
@supplier = @store.push 'supplier', supplier_object
@product_categories = @store.find 'product_category'
@sections = @store.find 'section'
Ember.RSVP.all([@product_categories, @sections]).then (results)=>
@store.find('list', state: 'active').then (lists) -> lists.invoke('get', 'table')
@lists = @store.all 'list'
# product_categories = controller.set 'product_categories', @store.all('product_category')
#@store.find 'order', state: 'active' included in list
setupController: (controller)->
controller.set 'supplier', @supplier
# @set 'supplier', @store.find('supplier', supplier_id)
controller.set 'sections', @sections
controller.set 'product_categories', @product_categories
faye = new Faye.Client(event_host)
faye.subscribe "/supplier/#{supplier_object.id}", (e)=>
@set('supplier.orders_placed_count', e.data.supplier_orders_placed_count) if e.data.supplier_orders_placed_count?
@set('supplier.orders_in_process_count', e.data.supplier_orders_in_process_count) if e.data.supplier_orders_in_process_count?
@events[e.event].call(@, e.data) if @events[e.event]
<% if Rails.env.test? %>
window.faye_log ||= []
faye_message = {}
faye_message[e.event] = JSON.parse(JSON.stringify(e.data))
faye_log.push faye_message
<% elsif Rails.env.development? %>
console.log "Event: #{e.event}"
console.log e.data
<% end %>
actions:
# openModal: (modalName, model, options={})->
# controller_name = options.controller || modalName
# controller = @controllerFor(controller_name)
# controller.set 'model', model
# controller.set 'modal_options', options
# @render modalName,
# into: 'application'
# outlet: 'modal'
# controller: controller_name
openModal: (modalName, options={})->
controller_name = options.controller || modalName
try
controller = @controllerFor("modals/#{modalName}")
catch error
controller = @controllerFor("modals/base")
controller.set 'model', options.model
defaultModalOptions =
closeOnOverlay: true
closeOnModalClick: false
controller.set 'modal_options', $.extend(defaultModalOptions, options)
@render "modals/#{modalName}",
into: 'application'
outlet: 'modal'
view: 'modal'
controller: controller
closeModal: ->
@disconnectOutlet
outlet: 'modal'
parentView: 'application'
confirm: (options = {})->
@send 'openModal', 'confirm',
model: Ember.Object.create
title: options.title
body: options.body
cancel: options.cancel
ok: options.ok
events:
list_needs_help: (data) -> list.markNeedsHelp() if list = @store.getById('list', data.id)
list_needs_payment: (data) -> list.markNeedsPayment() if list = @store.getById('list', data.id)
list_is_paid: (data) -> list.markIsPaid() if list = @store.getById('list', data.id)
list_update: (data) ->
@store.pushPayload('list', data)
# Fix broken reference. TODO: remove this when single source of
# truth is working
@store.findById('list', e.data.list.id).then (list)->
list.get('table').then (table)->table.set('active_list', list)
list_changed_table: (data) -> @store.pushPayload('list', lists: [data.list])
list_closed: (data) -> list.markClosed() if list = @store.getById('list', data.id)
list_helped: (data) -> list.markHelped() if list = @store.getById('list', data.id)
remove_list_needs_payment: (data) -> list.set('needs_payment', false) if list = @store.getById('list', data.id)
order_closed: (data) -> order.markClosed() if order = @store.getById('order', data.id)
order_being_processed: (data) -> order.markActive() if order = @store.getById('order', data.id)
order_being_delivered: (data) -> order.markDelivered() if order = @store.getById('order', data.id)
order_cancelled: (data) -> order.markCancelled() if order = @store.getById('order', data.id)
orders_in_process_count: (data) -> @set('supplier.orders_in_process_count', data.count)
orders_placed_count: (data) -> @set('supplier.orders_placed_count', data.count)
#return new Ember.Handlebars.SafeString('<span class="highlight">' + escaped + '</span>');
#"<span class='highlight'>#{escaped}</span>".htmlSafe()
@@ -13,17 +13,15 @@ header.top-menu
= t 'models.plural.table'
= link-to "lists" class="top-menu-lists"
= t 'models.plural.list'
section.extra-info
.supplier-info-row
.supplier-name= supplier.name
.table-number
.extra-info
.supplier-info-row
.counter.supplier-orders-placed-count
span.supplier-orders-placed-count-number= supplier.orders_placed_count
span.supplier-orders-placed-count-icon
= supplier.orders_placed_count
span.orders-placed-count-icon
.supplier-info-row
.counter.supplier-orders-in-process-count
span.supplier-orders-in-process-count-number= supplier.orders_in_process_count
span.supplier-orders-in-process-count-icon
= supplier.orders_in_process_count
span.orders-in-process-count-icon
aside.side-menu
ul
li.title: h3 Menu
@@ -1,7 +1,6 @@
App.ApplicationView = Ember.View.extend
classNames: ['application-view']
didInsertElement: ->
Qsupplier.watch_events()
$('.toggle-side-menu,.side-menu').click ->
menu = $('aside.side-menu')
toggle = $('.toggle-side-menu')
@@ -8,8 +8,7 @@
//= require moment
//= require translations
// require qwaiter
//= require ./qsupplier
//= require handlebars
// require ./qsupplier
//= require faye
//= require ./base
// require qtip # was used for table actions in section view, now done by EMBER
@@ -48,7 +47,7 @@ function currency(num) {
}
return '&euro;&nbsp;' + parseFloat(num).toFixed(2);
}
/*
Handlebars.registerHelper('t', function(tlocation) {
return t(tlocation)
})
@@ -60,4 +59,5 @@ Handlebars.registerHelper('currency', function(price) {
price = price.call(this)
}
return new Handlebars.SafeString(currency(price))
})
})
*/
@@ -1,223 +0,0 @@
root = exports ? this
root.Qsupplier =
watch_events: ->
faye = new Faye.Client(event_host)
faye.subscribe "/supplier/#{supplier_object.id}", (e)=>
<% if Rails.env.test? %>
window.faye_log ||= []
faye_message = {}
faye_message[e.event] = JSON.parse(JSON.stringify(e.data))
faye_log.push faye_message
<% elsif Rails.env.development? %>
console.log "Event: #{e.event}"
console.log e.data
<% end %>
if e.data
if e.data.supplier_orders_placed_count == 0 or e.data.supplier_orders_placed_count
$('.supplier-orders-placed-count-number').text(e.data.supplier_orders_placed_count)
if e.data.supplier_orders_in_process_count == 0 or e.data.supplier_orders_in_process_count
$('.supplier-orders-in-process-count-number').text(e.data.supplier_orders_in_process_count)
# if(e.event == 'new_order')
# if App
# App.store().pushPayload(e.data)
# # Fix for ember pushPayload bug not creating the proper relations/triggers
# setTimeout ->
# if order = App.store().all('order').findProperty('id', e.data.order.id)
# # console.log "Ugly ember fix for adding order #{e.data.order.id} as relation to its list"
# list = order.get('list')
# list.get('orders').addRecord order
# if table = list.get('table')
# if table.get('active_list')
# table.get('active_list').then (table_list)->
# table.set 'active_list', list if table_list isnt list
# , 200
if(e.event == 'list_needs_help')
list.markNeedsHelp() if list = App.List.findCached(e.data.id)
else if(e.event == 'list_needs_payment')
list.markNeedsPayment() if list = App.List.findCached(e.data.id)
else if(e.event == 'list_is_paid')
list.markIsPaid() if list = App.List.findCached(e.data.id)
else if e.event == 'list_update'
App.store().pushPayload('list', e.data)
App.store().findById('list', e.data.list.id).then (list)->
list.get('table').then (table)->table.set('active_list', list)
else if e.event == 'list_closed'
list.markClosed() if list = App.List.findCached(e.data.id)
else if e.event == 'order_closed'
order.markClosed() if order = App.Order.findCached(e.data.id)
else if e.event == 'list_helped'
list.markHelped() if list = App.List.findCached(e.data.id)
else if e.event == 'order_being_processed'
order.markActive() if order = App.Order.findCached(e.data.id)
else if e.event == 'order_being_delivered'
order.markDelivered() if order = App.Order.findCached(e.data.id)
else if e.event == 'remove_list_needs_payment'
if list = App.List.findCached(e.data.id)
list.set 'needs_payment', false
else if e.event == 'orders_in_process_count'
$('.supplier-orders-in-process-count-number').text e.data.count
else if e.event == 'orders_placed_count'
$('.supplier-orders-placed-count-number').text e.data.count
else if e.event == 'list_changed_table'
# App && App.List.updateOrAdd(e.data.list)
# App.store().update 'list', e.data.list
#TODO: waiting for single source of truth
if list = App.List.findCached(e.data.list.id)
list.get('table').set('active_list', null)
App.store().findById('table', e.data.list.table_id).then (new_table)->
list.set('table', new_table)
new_table.set('active_list', list) # making reverse association
else if e.event == 'order_cancelled'
order.markCancelled() if order = App.Order.findCached(e.data.id)
false
# move_table_to_active_section: (table_id)->
# table_container = $('#section-table-'+table_id)
# section_container = $('.section-tables-active')
# section_container.append(table_container)
# Qsupplier.position_table_in_active_section(section_container, table_container, true)
#
# # Set the section id of the table in the database
# $.ajax(
# type: 'PUT',
# url: '/supplier/tables/'+table_container.data('table-id'),
# data: {table: {section_id: current_section_id}},
# dataType: 'json'
# )
# position_table_in_active_section: (section_container, table_container, make_draggable)->
# make_draggable ||= false
# button_container = table_container.find('.action-button-container')
# button_container.html('')
# button_container.append($('<button class="btn btn-warning btn-mini">x</button>').click( -> Qsupplier.move_table_to_inactive_section(table_container.data('table-id')) ))
# position_x = parseFloat(table_container.data('position-x'))
# position_y = parseFloat(table_container.data('position-y'))
# table_container.css('left', section_container.width()*position_x/current_section_width)
# table_container.css('top', section_container.height()*position_y/current_section_height)
# #TODO place element at 0.0 if it happens to be outside the region
# table_container.show()
# table_container.draggable(
# containment: section_container,
# stop: ->
# position_x = current_section_width * $(this).position().left / section_container.width()
# position_y = current_section_height * $(this).position().top / section_container.height()
# table_container.data('position-x', position_x)
# table_container.data('position-y', position_y)
# $.ajax(
# type: 'PUT',
# url: '/supplier/tables/'+table_container.data('table-id'),
# data: {table: {position_x: position_x, position_y: position_y}},
# dataType: 'json'
# )
# ) if make_draggable
# move_table_to_inactive_section: (table_id)->
# table_container = $('#section-table-'+table_id)
# table_container.css('left', 'auto')
# table_container.css('top', 'auto')
# section_container = $('.section-tables-inactive')
# section_container.prepend(table_container)
# button_container = table_container.find('.action-button-container')
# button_container.html('')
# button_container.append($('<button class="btn btn-primary btn-mini">+</button>').click( -> Qsupplier.move_table_to_active_section(table_container.data('table-id')) ))
# #TODO make ajax call safe
# $.ajax(
# type: 'PUT',
# url: '/supplier/tables/'+table_container.data('table-id'),
# data: {table: {section_id: ''}},
# dataType: 'json'
# )
#
# load_active_lists: () ->
# $.get('/supplier/active_lists.json?section_id='+($('.section_selector').val() || ''), (res) =>
# body = $('#active-lists-table tbody')
# body.find('tr').remove()
# foot = $('#active-lists-table tfoot')
# for list in res.lists
# body.append @mustache('#active-list-template', new List(list))
# )
# mark_list_as_helped: (list_id)->
# $.post('/supplier/mark_list_as_helped', {list_id: list_id}, (res)->
# )
# close_list: (list_id)->
# $.post('/supplier/close_list', {list_id: list_id}, (res)->
# )
#
# load_active_orders: () ->
# $.get('/supplier/active_orders.json?section_id='+($('.section_selector').val() || ''), (res) =>
# body = $('#active-orders-table tbody')
# body.html('')
# foot = $('#active-orders-table tfoot')
# return unless res.orders
# for order in res.orders
# ord = new Order(order)
# body.append @mustache('#active-order-template', ord)
# )
# mark_order_in_process: (order_id)->
# $.post('/supplier/mark_order_in_process', {order_id: order_id})
# mark_order_delivered: (order_id)->
# $.post('/supplier/order_is_delivered', {order_id: order_id})
#
# load_list: (list_id) ->
# $.get(data_host + '/supplier/lists/'+list_id+'.json?old_style=1', (res) ->
# body = $('#list-table tbody')
# foot = $('#list-table tfoot')
# Qsupplier.build_list_table(body, foot, res)
# )
# build_list_table: (body, foot, res) ->
# body.html('')
# return unless res.orders
# for order in res.orders
# body.append @mustache('#list-order-template', new Order(order))
# foot.find('.list-total').html(currency(res.total_amount))
# update_section_tables_view: (section_id)->
# $.get(data_host + '/supplier/sections/'+section_id+'/tables_view.json', (res)->
# for table in res.tables
# to = $('#section-table-'+table._id)
# if table.occupied then to.addClass('occupied') else to.removeClass('occupied')
# if table.needs_help then to.addClass('needs_help') else to.removeClass('needs_help')
# if table.needs_payment then to.addClass('needs_payment') else to.removeClass('needs_payment')
# if table.active_order then to.addClass('active_order') else to.removeClass('active_order')
#
# )
# add_tables_to_active_section: ->
# number_start = $('#add-tables-number-start').val()
# number_end = $('#add-tables-number-end').val()
# $.post('/supplier/sections/'+current_section_id+'/add_tables', {number_start: number_start, number_end: number_end}, -> window.location.reload())
# #$('#add-tables-modal').modal('hide')
# false
# arrange_tables_of_active_section: ->
# option = $('input[name=arrange-table-option]:checked').val()
# by_row_count = parseInt($('#arrange-tables-by-row-count').val())
# by_column_count = parseInt($('#arrange-tables-by-column-count').val())
# if(option == "by_row")
# return @alert(t('section.arrange_tables.by_row_no_row_count')) unless by_row_count && by_row_count > 0
# if(option == "by_column")
# return @alert(t('section.arrange_tables.by_column_no_column_count')) unless by_column_count && by_column_count > 0
# $.post '/supplier/sections/'+current_section_id+'/arrange_tables', {option: option, row_count: by_row_count, column_count: by_column_count}, (response)=>
# if response.ok then window.location.reload() else @alert(t(response.message))
# false
# mustache: (selector, locals)->
# html = $(selector).html()
# return '' unless html
# template = Handlebars.compile(html)
# container = $('<div></div>')
# container.html(template(locals))
# setTranslations(container)
# return container.html()
# locs = $.extend(locals,
# currency: ->
# (val)->
# currency(Mustache.render(val, this))
# )
# Mustache.to_html(html, locs)
# alert: Qwaiter.alert