JSONApi part1

This commit is contained in:
2015-09-02 15:52:48 +02:00
parent 5d1ecd81c8
commit f47a8a9ed0
51 changed files with 386 additions and 141 deletions
@@ -1,4 +1,4 @@
App.ListsIndexEntryComponent = Ember.Component.extend
App.ListIndexEntryComponent = Ember.Component.extend
layoutName: 'lists/index-entry'
classNames: ['lists-overview-entry']
classNameBindings: ['content.active:active']
@@ -4,7 +4,7 @@ App.MenuItemProductOrdersComponent = Ember.Component.extend
layoutName: 'components/menu/product_orders'
classNames: ['menu-list-item', 'menu-list-item-product-orders']
classNameBindings: ['product_orders.length:show:hide', 'currentRoute:active']
currentRoute: (-> @get('targetObject.currentRouteName') is 'product_orders' ).property('targetObject.currentRouteName')
currentRoute: Ember.computed.equal 'targetObject.currentPath', 'product-orders'
orderTotals: Ember.computed.mapBy 'product_orders', 'total'
orderTotal: Ember.computed.sum 'orderTotals'
#orderTotal: Ember.computed 'product_orders.@each.total', ->
@@ -2,9 +2,9 @@ App.MenuItemListNeedsHelpComponent = Ember.Component.extend Ember.ViewTargetActi
action: 'listNeedsHelp'
layoutName: "components/menu/list_needs_help"
classNames: 'menu-list-item callout'
classNameBindings: ['globals.list.needs_help:active']
classNameBindings: ['globals.active_list.needs_help:active']
click: ->
if @get('globals.list.needs_help')
if @get('globals.active_list.needs_help')
@set 'globals.notice', t('list_needs_help.help_is_on_its_way')
else
@triggerAction()
@@ -2,9 +2,9 @@ App.MenuItemListNeedsPaymentComponent = Ember.Component.extend Ember.ViewTargetA
action: 'listNeedsPayment'
layoutName: "components/menu/list_needs_payment"
classNames: 'menu-list-item callout'
classNameBindings: ['controller.list.needs_payment:active']
classNameBindings: ['globals.active_list.needs_payment:active']
click: ->
if @get('controller.list.needs_payment')
if @get('globals.active_list.needs_payment')
@set 'globals.notice', t('list_needs_payment.payment_already_requested')
else
@triggerAction() #action: 'listNeedsPayment'
@@ -14,14 +14,14 @@ App.NewProductOrdersListComponent = Ember.Component.extend
# @get('product_orders').invoke 'eraseRecord'
orderProducts: ->
# table = @get('controllers.table.model')
# list = @get('globals.list')
# list = @get('globals.active_list')
# order = @store.createRecord('order', list: list, table: table)
# new_product_orders = @store.all('product_order').filterProperty('order', null)
# order.get('product_orders').pushObjects(new_product_orders)
#
# order.save().then (response)=>
# new_product_orders.invoke 'eraseRecord'
# if @get('globals.list')
# if @get('globals.active_list')
# @transitionToRoute 'active_list'
# else
# # Get list info from the server
@@ -2,4 +2,4 @@ App.ActiveListController = Ember.Controller.extend
#orders: (->
#@get('list.orders')
#).property('list.orders')
list: (-> @get('globals.list') ).property('globals.list')
list: Ember.computed.alias 'globals.active_list'
@@ -1,5 +1,4 @@
App.ApplicationController = Ember.Controller.extend
#list: Ember.computed.alias 'globals.list'
#notice: ''
actions:
confirmCancel: ->
@@ -10,7 +9,7 @@ App.ApplicationController = Ember.Controller.extend
@set 'globals.notice', ''
showSupplierStatusInfo: ->
@modal 'supplier_status_info',
model: @get('globals.list.supplier')
model: @get('globals.active_list.supplier')
title_path: 'supplier_status_info.title'
openDebugger: ->
@@ -21,14 +20,14 @@ App.ApplicationController = Ember.Controller.extend
).observes('currentPath')
events:
notify: (notification) -> @set 'globals.notice', notification.message
list_helped: -> @set 'globals.list.needs_help', false
list_needs_help: -> @set 'globals.list.needs_help', true # incoming from other users
list_is_paid: -> @set 'globals.list.needs_payment', false
list_needs_payment: -> @set 'globals.list.needs_payment', true # incoming from other users
list_helped: -> @set 'globals.active_list.needs_help', false
list_needs_help: -> @set 'globals.active_list.needs_help', true # incoming from other users
list_is_paid: -> @set 'globals.active_list.needs_payment', false
list_needs_payment: -> @set 'globals.active_list.needs_payment', true # incoming from other users
list_closed: (data)->
@transitionToRoute('list', data.id).then =>
@set 'globals.list.state', 'closed'
@set 'globals.list', null
@set 'globals.active_list.state', 'closed'
@set 'globals.active_list', null
order_being_processed: (data)->
order = @store.all('order').findBy 'id', data.id
order.set('state', 'active') if order
@@ -65,7 +64,7 @@ App.ApplicationController = Ember.Controller.extend
join_request_approved: (data)->
return @transitionToRoute 'active_list' if @get('globals.list.id') # Not interested when there is an active list
return @transitionToRoute 'active_list' if @get('globals.active_list.id') # Not interested when there is an active list
# The rest of the code is for the new user
@setCurrentList ->
@transitionToRoute('active_list').then =>
@@ -73,16 +72,16 @@ App.ApplicationController = Ember.Controller.extend
@set 'globals.join_request_sent', false
list_changed_table: (data)->
@store.findRecord('table', data.to_table_id).then (table)=>@set('globals.list.table', table)
@store.findRecord('table', data.to_table_id).then (table)=>@set('globals.active_list.table', table)
orders_in_process_count: (data)->
@set 'globals.list.supplier.orders_in_process_count', data.count
@set 'globals.active_list.supplier.orders_in_process_count', data.count
orders_placed_count: (data)->
@set 'globals.list.supplier.orders_placed_count', data.count
@set 'globals.active_list.supplier.orders_placed_count', data.count
new_order: (data)->
# return if @store.all('order').findProperty('id', data.order.id)
@store.pushPayload data
@store.findById('list', data.list.id).then (list)=> @set 'globals.list', list
# @store.findById('order', data.order.id).then (order)->
@store.peekRecord('list', data.list.id).then (list)=> @set 'globals.active_list', list
# @store.peekRecord('order', data.order.id).then (order)->
# list = order.get('list')
# list.get('orders').addObject(order)
@@ -92,15 +91,16 @@ App.ApplicationController = Ember.Controller.extend
list.set 'needs_payment', false
setCurrentList: (callback)->
success = (list)=>
#@store.find('list', 'current').deleteRecord() # gets not replaced, buty stays as dummy
#@store.findRecord('list', 'current').deleteRecord() # gets not replaced, buty stays as dummy
# A list record with id current and with the content of the returned list is created
# at the moment remove the dummy list, this should be resolved by Ember eventually
if error_list = @store.peekRecord('list', 'current')
# TODO: keek checking if peekAll can be replaced by peekRecord
if error_list = @store.peekAll('list', 'current').findBy('id', 'current') # peekRecord('list', 'current') not working in Ember 2.0.0
error_list.eraseRecord()
#TODO: depricate list on application controller
#@set 'list', list
@set 'globals.list', list
@set 'globals.active_list', list
if list.get('join_requests.length')
@transitionToRoute 'join_requests'
else if @currentRouteName is 'index'
@@ -112,7 +112,7 @@ App.ApplicationController = Ember.Controller.extend
#console.log "Error: #{emberError.message}" if emberError.message
if error_list = @store.peekRecord('list', 'current')
error_list.eraseRecord()
@set 'globals.list', null
@set 'globals.active_list', null
switch @currentRouteName
when 'table' then # nothing
else @redirect_to 'user_root'
@@ -1,5 +1,5 @@
App.JoinRequestsController = Ember.Controller.extend
join_requests: (-> @get('globals.list.join_requests') ).property('globals.list.join_requests')
join_requests: Ember.computed.alias 'globals.active_list.join_requests'
actions:
rejectRequest: (join_request)->
Ember.$.post("#{$data_host}/user/reject_join_request", user_id: join_request.get('user.id')).then (response)->
@@ -1,21 +1,19 @@
App.TableController = Ember.Controller.extend
tableCanTakeOrders: (->
return false unless @get('supplier.can_take_orders')
list = @get('globals.list')
if list
if list = @get('globals.active_list')
return false unless list.get('supplier.id') == @get('supplier.id')
return false unless list.get('table.id') == @get('model.id')
true
else
# no list and open supplier
if @get('model.occupied') then false else true
).property('globals.list.id', 'supplier.can_take_orders', 'model.occupied', 'model.id', 'globals.list.table.id')
supplier: Ember.computed 'model.supplier', -> @get('model.supplier')
showJoinButton: (->
).property('globals.active_list.id', 'supplier.can_take_orders', 'model.occupied', 'model.id', 'globals.active_list.table.id')
supplier: Ember.computed.alias 'model.supplier'
showJoinButton: Ember.computed 'globals.active_list.id', 'supplier.can_take_orders', 'model.occupied', 'model.id', 'globals.active_list.table.id', ->
return false unless @get('supplier.can_take_orders')
return false if @get('globals.list') # if you already have an active list, do not join another
return false if @get('globals.active_list') # if you already have an active list, do not join another
if @get('model.occupied') then true else false # no point in joining tables that are not occupied
).property('globals.list.id', 'supplier.can_take_orders', 'model.occupied', 'model.id', 'globals.list.table.id')
actions:
joinOccupiedTable: ->
Ember.$.post("#{$data_host}/user/join_occupied_table.json", table_id: @get('model.id'))
@@ -1,4 +1,4 @@
attr = DS.attr
App.JoinRequest= DS.Model.extend
list: DS.belongsTo('list')
user: DS.belongsTo('user')
list: DS.belongsTo('list', async: false)
user: DS.belongsTo('user', async: false)
@@ -1,31 +1,31 @@
attr = DS.attr
App.List = DS.Model.extend
orders: DS.hasMany('order')
needs_help: attr('boolean')
needs_payment: attr('boolean')
user_requests_closing: attr('boolean')
needs_help: attr('boolean', defaultValue: false)
needs_payment: attr('boolean', defaultValue: false)
user_requests_closing: attr('boolean', defaultValue: false)
state: attr('string')
price: attr('number')
closed_at: DS.attr('date')
closed_at: attr('date')
extended_version: attr('boolean')
supplier_orders_in_process_count: attr('number')
supplier_orders_placed_count: attr('number')
cached_supplier_name: attr('string')
supplier: DS.belongsTo('supplier')
table: DS.belongsTo('table')
join_requests: DS.hasMany('join_request')
users: DS.hasMany('user')
orders: DS.hasMany('order', async: true)
supplier: DS.belongsTo('supplier', async: false)
table: DS.belongsTo('table', async: true)
join_requests: DS.hasMany('join_request', async: false)
users: DS.hasMany('user', async: false)
is_extended_version: ->
@get('extended_version')
relevant_orders: (-> @get('orders').filter((o)->o.get('state') isnt 'cancelled')).property('orders.@each.state')
relevant_orders: Ember.computed 'orders.@each.state', -> @get('orders').rejectBy('state', 'cancelled')
sorted_orders: (-> @get('relevant_orders').sortBy('created_at').reverseObjects()).property('relevant_orders.@each.isLoaded')
total: (->
@get('relevant_orders').getEach('total').reduce(((sum, total) -> sum + total), 0)
).property('relevant_orders.@each.total')
active: (-> @get('state') is 'active' ).property('state')
active: Ember.computed.equal 'state', 'active'
showTotal: (->
if @get('orders.length') && @get('orders.length') > 1 then true else false
).property('orders.length')
#showTotal: (->
# if @get('orders.length') && @get('orders.length') > 1 then true else false
#).property('orders.length')
showTotal: Ember.computed.gt 'relevant_orders.length', 1
@@ -1,9 +1,9 @@
attr = DS.attr
App.Order = DS.Model.extend
state: attr 'string'
list: DS.belongsTo('list')
list: DS.belongsTo('list', async: true)
table: DS.belongsTo('table')
product_orders: DS.hasMany('product_order', embedded: 'always')
product_orders: DS.hasMany('product_order', async: false)
total: (->
@get('product_orders').getEach('total').reduce(((sum, total) -> sum + total), 0)
).property('product_orders.@each.quantity')
@@ -2,16 +2,16 @@ attr = DS.attr
App.ProductOrder = DS.Model.extend
quantity: attr 'number', defaultValue: 1
price: attr 'number'
product_name: attr('string')
product: DS.belongsTo('product')
product_name: attr 'string'
product_variant: attr('string')
order: DS.belongsTo('order')
placed: attr('boolean', defaultValue: false) # virtual attribute for new orders to be placed, should be more elegant
product: DS.belongsTo('product', async: true)
order: DS.belongsTo('order', async: false)
increment: ->
@set('quantity', @get('quantity') + 1)
total: (-> @get('quantity') * @get('price')).property('quantity', 'price')
display: Ember.computed 'quantity', 'product_variant', 'product.name', ->
disp = "#{@get('quantity')} x #{@get('product.name')}"
display: Ember.computed 'quantity', 'product_variant', 'product_name', ->
disp = "#{@get('quantity')} x #{@get('product_name')}"
if variant = @get('product_variant')
disp = "#{disp} (#{variant})"
disp.htmlSafe()
@@ -7,8 +7,8 @@ App.Supplier= DS.Model.extend
orders_placed_count: attr('number')
orders: DS.hasMany('order')
product_categories: DS.hasMany('product_category')
lists: DS.hasMany('list')
product_categories: DS.hasMany('product_category', async: true)
lists: DS.hasMany('list', async: true)
is_extended_version: ->
@get('extended_version')
@@ -4,4 +4,4 @@ App.Table= DS.Model.extend
needs_help: attr('boolean')
occupied: attr('boolean')
supplier: DS.belongsTo('supplier')
supplier: DS.belongsTo('supplier', async: true)
@@ -1,6 +1,6 @@
DS.Model.reopen
created_at: DS.attr('date')
updated_at: DS.attr('date')
#created_at: DS.attr('date')
#updated_at: DS.attr('date')
eraseRecord: ->
#@clearRelationships()
@transitionTo('deleted.saved')
@@ -1,6 +1,6 @@
App.ActiveListRoute = Ember.Route.extend App.ResetScroll
#model: ->
#@get('globals.list')
#@get('globals.active_list')
#afterModel: ->
#controller = @controllerFor('application')
#table_id = controller.get('list.table.id')
@@ -13,7 +13,7 @@ App.ApplicationRoute = Ember.Route.extend
unauthorized: ->
Qstorage.removeItem('auth_token')
Qstorage.removeItem('user_id')
@set 'globals.list', null
@set 'globals.active_list', null
#@send 'obtain_token'
@controllerFor('application').redirect_to 'sign_in', message: 'unauthorized'
@@ -76,13 +76,13 @@ App.ApplicationRoute = Ember.Route.extend
listNeedsPayment: ->
@get('controller').secured ->
@set 'globals.list.needs_payment', true
@set 'globals.active_list.needs_payment', true
Ember.$.post "#{$data_host}/user/list_needs_payment.json"
#Ember.$.post("#{$data_host}/user/list_needs_payment.json").then (res) =>
#@set('list.needs_payment', true) # also done by faye
listNeedsHelp: ->
@get('controller').secured ->
@set 'globals.list.needs_help', true
@set 'globals.active_list.needs_help', true
Ember.$.post "#{$data_host}/user/needs_help.json"
#Ember.$.post("#{$data_host}/user/needs_help.json").then (res) =>
#@set('list.needs_help', true) # also done by faye
@@ -1,5 +1,5 @@
App.ListRoute = Ember.Route.extend App.ResetScroll,
model: (options)->
@store.find 'list', options.list_id
@store.findRecord 'list', options.list_id
afterModel: (model)->
model.reload() unless model.is_extended_version()
@@ -1,5 +1,5 @@
App.ListsRoute = Ember.Route.extend App.ResetScroll,
model: ->
@store.find 'list'
@store.findAll 'list'
setupController: (controller, model)->
controller.set('alreadyLoaded', true)
@@ -1,7 +1,10 @@
App.ApplicationSerializer = DS.ActiveModelSerializer.extend()
App.ApplicationAdapter = DS.ActiveModelAdapter.extend
#App.ApplicationSerializer = DS.ActiveModelSerializer.extend()
App.ApplicationSerializer = DS.JSONAPISerializer.extend
keyForAttribute: (attr, method)-> attr
#App.ApplicationAdapter = DS.ActiveModelAdapter.extend
App.ApplicationAdapter = DS.JSONAPIAdapter.extend
host: $data_host
namespace: 'user'
headers:
"Accept": "application/json, text/javascript; q=0.01"
#headers:
# "Accept": "application/json, text/javascript; q=0.01"
@@ -13,7 +13,7 @@ if list.closed_at
.display-row
.display-label=t 'models.supplier'
.display-field= list.supplier.name
if list.orders.isLoaded
if list.isLoaded
if list.sorted_orders
.list-orders-container
each list.sorted_orders as |order|
@@ -30,7 +30,7 @@ if list.orders.isLoaded
span=t 'active_list.no_orders_explanation'
br
if list.table
link-to 'table' list.table class="button"
= link-to 'table' list.table class="button"
span=t 'list_products.title'
else
span.loading.large
@@ -1,13 +1,13 @@
.row
h2=t 'active_list.title'
if list.orders.isLoaded
if list.isLoaded
if list
partial "list_content"
else
p
span=t 'active_list.not_active.message'
br
link-to 'index' class="button"
= link-to 'index' class="button"
span= t 'active_list.not_active.home_button_text'
else
span.loading.large
@@ -9,9 +9,9 @@
a{action "scanQr" bubbles=false}
span.scan-qr-icon
span Scan QR
if globals.list.id
if globals.active_list.id
li
=link-to 'table' globals.list.table.id class="side-menu-list-products"
=link-to 'table' globals.active_list.table.id class="side-menu-list-products"
span.fa.fa-cutlery
span.fa.fa-glass
= t 'list_products.title'
@@ -19,7 +19,7 @@
=link-to 'active_list' class="side-menu-active-list"
span.active-list-icon
span= t 'active_list.title'
if globals.list.join_requests.length
if globals.active_list.join_requests.length
li
=link-to 'join_requests'
span= t 'models.plural.join_request'
@@ -2,11 +2,11 @@
.top-menu-bar
.menu-content
section.main-buttons
if globals.list.id
if globals.active_list.id
link-to 'index' class="top-menu-logo with-list"
= image-tag 'user/logo-small.png'
= menu-item route="active_list"
= menu-item route="table" route_param=globals.list.table.id
= menu-item route="table" route_param=globals.active_list.table.id
= menu-item-list-needs-help
= menu-item-list-needs-payment
else
@@ -14,18 +14,18 @@
= image-tag 'user/logo-small.png'
= menu-item-scan-qr
= menu-item-product-orders
if globals.list
if globals.active_list
.extra-info{action "showSupplierStatusInfo"}
.supplier-info-row
/ .supplier-name= list.supplier.name
.table-number
|# 
= globals.list.table.number
= globals.active_list.table.number
.supplier-info-row
.counter.supplier-orders-placed-count
= globals.list.supplier.orders_placed_count
= globals.active_list.supplier.orders_placed_count
span.orders-placed-count-icon
.supplier-info-row
.counter.supplier-orders-in-process-count
= globals.list.supplier.orders_in_process_count
= globals.active_list.supplier.orders_in_process_count
span.orders-in-process-count-icon
@@ -1,3 +1,3 @@
span.created_at=time list.created_at
span.price.currency= currency list.price
span.name= list.cached_supplier_name
span.name= list.supplier.name
@@ -24,6 +24,8 @@
.currency
float: right
margin-right: 0.5em
min-width: 60px
text-align: right
.list-order-container
background-position: left center
background-repeat: no-repeat