User foundation setup

This commit is contained in:
2014-03-26 13:17:34 +01:00
parent 6c2427e082
commit 4e75c72097
87 changed files with 861 additions and 120 deletions
@@ -0,0 +1,20 @@
@App = Ember.Application.create
LOG_TRANSITIONS: true
rootElement: '#ember-app-container'
Ember.$.ajaxPrefilter (options) ->
if options.type.toUpperCase() == 'GET'
if auth_token = Qstorage.getItem('auth_token')
if options.data
options.data += "&auth_token=#{auth_token}"
else
options.data = "auth_token=#{auth_token}"
if options.type.toUpperCase() == 'POST'
if auth_token = Qstorage.getItem('auth_token')
options.data ||= {}
options.data.auth_token = auth_token
true
Ember.$.ajaxSetup
error: (jqXHR, textStatus, errorThrown)->
console.log "Error: #{textStatus}: #{errorThrown}"
if jqXHR.status == 401
App.__container__.lookup('route:application').unauthorized()
@@ -0,0 +1,10 @@
#= require_self
#= require handlebars
#= require ember
#= require ember-data
#= require shared-ember-helpers/all
#= require_directory ./modifications
#= require ./app
#= require user/quser
#= require_tree .
@EmberENV = {FEATURES: {'query-params-new': true}}
@@ -0,0 +1,4 @@
App.ModalDialogComponent = Ember.Component.extend
actions:
close: ->
@sendAction()
@@ -0,0 +1,30 @@
App.ApplicationController = Ember.Controller.extend
notice: ''
actions:
confirmCancel: ->
if cancel = @get('confirm.cancel')
cancel.call(@)
$('#confirm-modal').hide()
clearNotice: ->
@set 'notice', null
openDebugger: ->
debugger
currentPathDidChange: (->
@set 'notice', ''
).observes('currentPath')
events:
list_helped: -> @set 'list.needs_help', false
list_needs_help: -> @set 'list.needs_help', true # incoming from other users
list_is_paid: -> @set 'list.needs_payment', false
list_needs_payment: -> @set 'list.needs_payment', true # incoming from other users
getList: ->
Ember.$.get('/user/list_info.json').then (res)=>
if res.not_present
@set 'list', null
else
@set 'list', App.List.create(res)
@getProducts table_id: @get('list.table_id')
getProducts: (options = {})->
@store.find('product_category', options).then (product_categories)=>
@controllerFor('list_products').set 'model', product_categories
@@ -0,0 +1 @@
App.IndexController = Ember.Controller.extend {}
@@ -0,0 +1,7 @@
App.ListProductsController = Ember.ArrayController.extend
actions:
addProduct: (product)->
if existing = @store.all('product_order').find((po)-> po.get('product') == product)
existing.increment()
else
@store.createRecord 'product_order', product: product
@@ -0,0 +1 @@
App.ListProductsForTableController = Ember.ArrayController.extend {}
@@ -0,0 +1,8 @@
App.ModalConfirmController = Ember.ObjectController.extend
actions:
close: ->
@get('model.cancel').call(@) if @get('model.cancel')
@send 'closeModal'
confirm: ->
@get('model.ok').call(@)
@send 'closeModal'
@@ -0,0 +1,4 @@
App.ModalController = Ember.ObjectController.extend
actions:
close: ->
@send 'closeModal'
@@ -0,0 +1,21 @@
App.ProductOrdersController = Ember.ArrayController.extend
orderTotal: (->
#Math.round(Math.random()*100)
@get('model').getEach('total').reduce(((sum, total) -> sum + total), 0)
).property('model.@each.quantity')
actions:
clearProductOrders: ->
@store.all('product_order').toArray().invoke 'unloadRecord'
orderProducts: ->
orders = @store.all('product_order').toArray()
data = orders.map( (order)->order.serialize() )
dataObject = {order: {}}
for product_order in data
dataObject['order'][product_order.product_id] = product_order.quantity
$.ajax
url: Routes.user_order_selected_products_path()
type: "POST",
data: JSON.stringify(dataObject),
contentType: "application/json",
dataType: 'json'
orders.invoke 'unloadRecord'
@@ -0,0 +1,43 @@
App.SelectQrcodeController = Ember.Controller.extend
actions:
selectQr: (table)->
Qstorage.setItem 'table_id', table._id
@secured ->
$.getJSON(data_host + '/user/table_info.json?'+@authentication_string+'&table_id='+table._id).then (res)=>
if res.current_table_id
if res.other_supplier
@redirect_to 'user_root', message: 'table_is_from_other_supplier'
else if res.current_table_id == table.table_id
#nothing has changed, show product list
@redirect_to 'list_products'
else if res.current_table_id != table.table_id
if res.occupied
@redirect_to 'user_root', message: 'table_is_occupied'
else if res.reserved
@redirect_to 'user_root', message: 'table_is_reserved'
else if table.closed
@redirect_to 'user_root', message: 'table_is_closed'
else if res.supplier_closed
@redirect_to 'user_root', message: 'supplier_is_closed'
else
## Offer to move table
@send 'confirm',
title: t('move_table.confirmation_title')
body: t('move_table.confirmation_body')
ok: =>
$.post(data_host + '/user/move_table.json', $.extend({table_id: table._id}, @authentication_object), (res2)=>
if res2.occupied
@redirect_to 'user_root', {message: 'move_table.cannot_move_to_occupied_tabe'}
else
@redirect_to 'list_products', {message: 'move_table.moved_to_another_table'}
)
cancel: =>
@redirect_to 'list_products'
else
if res.occupied
@redirect_to 'join_occupied_table', {table_id: table.table_id}
else if res.supplier_closed
@redirect_to 'user_root', {message: 'supplier_is_closed'}
else
#$.post(data_host + '/user/create_list.json', {table_id: table.table_id}, (res)-> Quser.handle_response(res))
@redirect_to 'list_products_for_table', {table_id: table.table_id}
@@ -0,0 +1 @@
App.List = Ember.Object.extend {}
@@ -0,0 +1,6 @@
attr = DS.attr
App.Product = DS.Model.extend
name: attr 'string'
price: attr 'number'
product_category: DS.belongsTo('product_category')
product_orders: DS.hasMany('product_order')
@@ -0,0 +1,4 @@
attr = DS.attr
App.ProductCategory = DS.Model.extend
name: attr('string')
products: DS.hasMany('product')
@@ -0,0 +1,7 @@
attr = DS.attr
App.ProductOrder = DS.Model.extend
quantity: attr 'number', defaultValue: 1
product: DS.belongsTo('product')
increment: ->
@set('quantity', @get('quantity') + 1)
total: (-> @get('quantity') * @get('product.price')).property('quantity')
@@ -0,0 +1,50 @@
Ember.Controller.reopen
needs: ['application']
secured: (callback)->
unless Qstorage.getItem('auth_token') && typeof(Qstorage.getItem('auth_token')) == 'string' && Qstorage.getItem('auth_token').length > 0
return @transitionToRoute('obtain_token')
@authentication_string = 'auth_token='+Qstorage.getItem('auth_token')
@authentication_object = {auth_token: Qstorage.getItem('auth_token')}
callback.call(@) if callback
redirect_to: (route, options={})->
route = 'index' if route == 'user_root'
@transitionToRoute(route).then =>
if options.message
tkey = if options.message.indexOf('.') > -1 then options.message else "messages.#{options.message}"
@set 'controllers.application.notice', t(tkey)
#confirm: (options = {})->
##@showModal options
##$(document).foundation('reflow') # needed (stupid!!!)
#r = @container.lookup('route:application')
#r.send('confirm', options)
##window.confirm_cancel = options.cancel
##window.confirm_ok = options.ok
##$('#confirm-modal .confirm-title').html options.title if options.title
##$('#confirm-modal .confirm-content').html options.content if options.content
##@set 'controllers.application.confirm.content', options.content if options.content
##$('#confirm-modal').foundation('reveal', 'open') # this kills the ember actions
##$('#confirm-modal').css('visibility', 'visible').show()
showModal: (options={})->
#this.container.lookup('view:modal', {title:'Test title'})
debugger
$(document).foundation('reflow') # needed (stupid!!!)
@confirm_cancel = options.cancel
@set 'controllers.application.modal.title', options.title if options.title
@set 'controllers.application.modal.content', options.content if options.content
#$('#confirm-modal').foundation('reveal', 'open') #this kills the ember actions
#$('#confirm-modal').css('visibility', 'visible').show()
Ember.ArrayController.reopen
needs: ['application']
secured: (callback)->
unless Qstorage.getItem('auth_token') && typeof(Qstorage.getItem('auth_token')) == 'string' && Qstorage.getItem('auth_token').length > 0
return @transitionToRoute('obtain_token')
@authentication_string = 'auth_token='+Qstorage.getItem('auth_token')
@authentication_object = {auth_token: Qstorage.getItem('auth_token')}
callback.call(@) if callback
redirect_to: (route, options={})->
route = 'index' if route == 'user_root'
@transitionToRoute(route).then =>
if options.message
tkey = if options.message.indexOf('.') > -1 then options.message else "messages.#{options.message}"
@set 'controllers.application.notice', t(tkey)
@@ -0,0 +1,14 @@
# For more information see: http://emberjs.com/guides/routing/
# and for queryParams: https://github.com/alexspeller/website/blob/a96d9afe4506454b155cc64299e86e558ce3c9f1/source/guides/routing/query-params.md
App.Router.reopen
#location: 'history'
rootURL: '/user'
activate: (a,b,c)->
debugger
App.Router.map ->
@route 'select_qrcode'
@route 'obtain_token'
@route 'active_list'
@route 'list_products'
@route 'list_products_for_table'
@@ -0,0 +1,45 @@
App.ApplicationRoute = Ember.Route.extend
setupController: (controller)->
@controllerFor('product_orders').set 'model', @store.all('product_order')
controller.secured ->
faye = new Faye.Client(event_host)
user_id = Qstorage.getItem('user_id')
faye.subscribe "/user/"+user_id, (e)=>
console.log e
@events[e.event].call(@) if @events[e.event]
@getList()
unauthorized: ->
Qstorage.setItem('auth_token', '')
@controllerFor('application').set 'list', null
@transitionTo('obtain_token').then =>
@controllerFor('application').set('notice', t('messages.unauthorized'))
actions:
openModal: (modalName, model)->
@controllerFor(modalName).set('model', model)
@render modalName,
into: 'application'
outlet: 'modal'
closeModal: ->
@disconnectOutlet
outlet: 'modal'
parentView: 'application'
confirm: (options = {})->
@send 'openModal', 'modal_confirm', Ember.Object.create
title: options.title
body: options.body
cancel: options.cancel
ok: options.ok
listNeedsPayment: ->
@get('controller').secured ->
$.post(data_host + '/user/list_needs_payment.json', @authentication_object).then (res) =>
@set('list.needs_payment', true)
listNeedsHelp: ->
@get('controller').secured ->
$.post(data_host + '/user/needs_help.json', @authentication_object).then (res) =>
@set('list.needs_help', true)
scanQr: ->
@transitionTo 'select_qrcode'
events: ->
error: (error)->
debugger
@@ -0,0 +1,18 @@
App.ListProductsForTableRoute = Ember.Route.extend {}
#setupController: (controller)->
#controller.secured ->
#src = '/user/list_products_for_table.json'
#data = {}
#$.getJSON(data_host + src, $.extend(@authentication_object, data)).then (res) =>
#if res.not_present
#@redirect_to 'index', message: 'the_list_has_been_closed'
#return
#for pc in res.categories
#product_category = @store.createRecord 'product_category',
#name: pc.name
#for pp in pc.products
#pp.id = pp._id
#pp.product_category = product_category
#product = @store.createRecord 'product', pp
#controller.set 'model', @store.all('product_category')
#debugger
@@ -0,0 +1,17 @@
App.ListProductsRoute = Ember.Route.extend {}
#setupController: (controller)->
#controller.secured ->
#src = '/user/list_products.json'
#data = {}
#$.getJSON(data_host + src, $.extend(@authentication_object, data)).then (res) =>
#if res.not_present
#@redirect_to 'index', message: 'the_list_has_been_closed'
#return
#for pc in res.categories
#product_category = @store.createRecord 'product_category',
#name: pc.name
#for pp in pc.products
#pp.id = pp._id
#pp.product_category = product_category
#product = @store.createRecord 'product', pp
#controller.set 'model', @store.all('product_category')
@@ -0,0 +1,9 @@
App.SelectQrcodeRoute = Ember.Route.extend
setupController: (controller)->
$.ajax
url: Routes.select_qrcode_path()
type: "GET"
dataType: 'json'
async: false
success: (res)->
controller.set 'tables', res
@@ -0,0 +1,13 @@
# http://emberjs.com/guides/models/defining-a-store/
DS.RESTAdapter.reopen
namespace: 'user'
App.ApplicationSerializer = DS.ActiveModelSerializer
App.CustomAdapter = DS.RESTAdapter.extend
# user underscored paths
pathForType: (type)->
decamelized = Ember.String.decamelize(type)
Ember.String.pluralize(decamelized)
App.Store = DS.Store.extend
adapter: App.CustomAdapter
@@ -0,0 +1 @@
h2 Active list
@@ -0,0 +1,42 @@
.top-menu.off-canvas-wrap
.inner-wrap
nav.tab-bar
section.left-small
a.left-off-canvas-toggle.menu-icon
span
section.right.tab-bar-section
= link-to 'index'
= image_tag 'icons/logo-small.png'
a{action openDebugger} alt=""
span.fa.fa-wrench.fa-lg
.right
if list.id
App.MenuItemView route="active_list"
App.MenuItemView route='list_products'
App.MenuItemListNeedsHelpView
App.MenuItemListNeedsPaymentView
aside.left-off-canvas-menu
ul.off-canvas-list
li
label Menu
li
= link-to 'index'
span Home
li
a{action scanQr bubbles=false}
span Scan QR
li
=link-to 'list_products'
span= t 'list_products.title'
li
=link-to 'active_list'
span= t 'active_list.title'
section.main-section
if notice
#notice.alert-box{action clearNotice} data-alert=true
a.right href="#"
span.fa.fa-times.fa-lg
span= notice
= outlet
a.exit-off-canvas
=outlet modal
@@ -0,0 +1,2 @@
.overlay{action "close"}
.modal{action bubbles=false preventDefault=false}= yield
@@ -0,0 +1,6 @@
.home-panel
.home-header = image_tag 'logo.png'
.home-center
a{action scanQr} href="#"= image_tag 'scan-logo.png'
.home-footer
.home-footer-content
@@ -0,0 +1,9 @@
.row
.large-6.columns
each product_category in controller
hr
h4= product_category.name
hr
each product in product_category.products
a{action addProduct product}= product.name
.large-6.columns= render 'product_orders'
@@ -0,0 +1,8 @@
.row
.large-6.columns
each product_category in controller
hr
h4= product_category.name
hr
each product in product_category.products
a{action addProduct product}= product.name
@@ -0,0 +1 @@
span.fa.fa-list.fa-lg
@@ -0,0 +1 @@
span.needs-help.fa.fa-hand-o-up.fa-lg
@@ -0,0 +1 @@
span.needs-payment.fa.fa-money.fa-lg
@@ -0,0 +1,2 @@
span.fa.fa-cutlery
span.fa.fa-glass
@@ -0,0 +1,4 @@
modal-dialog action="close"
h3.flush--top Alert
p= body
button{action "close"} Done
@@ -0,0 +1,6 @@
modal-dialog action="close"
h3.flush--top= title
p=body
hr
button{action "close"}= t 'confirm.cancel'
button.right{action 'confirm'}= t 'confirm.confirm'
@@ -0,0 +1 @@
h3 Obtain token...
@@ -0,0 +1,19 @@
hr.hide-for-medium-up
if model
a.tiny.button.right{action clearProductOrders} href="#" x
.clearfix
.panel
ul.product-orders
each product_order in controller
li
= product_order.quantity
| x
= product_order.product.name
span.currency=currency product_order.total
else
li= t 'product_orders.no_orders'
li.total
= t 'product_orders.total'
span.currency=currency orderTotal
if model
a.tiny.button.right{action orderProducts} href="#"= t 'product_orders.order_button'
@@ -0,0 +1,2 @@
each table in tables
img{action selectQr table} src="/table_qr_image.svg?table_id=#{unbound table._id}"
@@ -0,0 +1,10 @@
App.MenuItemListNeedsHelpView = Ember.View.extend Ember.ViewTargetActionSupport,
action: 'listNeedsHelp'
templateName: "menu_item_list_needs_help"
classNames: 'menu-list-item'
classNameBindings: ['controller.list.needs_help:active']
click: ->
if @get('controller.list.needs_help')
@set 'controller.notice', t('list_needs_help.help_is_on_its_way')
else
@triggerAction()
@@ -0,0 +1,11 @@
App.MenuItemListNeedsPaymentView = Ember.View.extend Ember.ViewTargetActionSupport,
action: 'listNeedsPayment'
templateName: "menu_item_list_needs_payment"
classNames: 'menu-list-item'
classNameBindings: ['controller.list.needs_payment:active']
click: ->
if @get('controller.list.needs_payment')
@set 'controller.notice', t('list_needs_payment.payment_already_requested')
else
@triggerAction() #action: 'listNeedsPayment'
@@ -0,0 +1,12 @@
App.MenuItemView = Ember.View.extend
classNames: 'menu-list-item'
classNameBindings: ['active']
click: ->
@get('controller').transitionToRoute(@route)
active: (->
if @get('controller.currentPath') == @route then 'active' else ''
).property('controller.currentPath')
init: ->
@templateName = "menu_item_#{@route}"
@_super()
@@ -0,0 +1,10 @@
#App.ModalView = Ember.View.extend
#templateName: "modal"
#title: ""
#classNames: ["reveal-modal"],
#didInsertElement: ->
##this.$().foundation('reveal', 'open')
#actions:
#close: ->
#console.log('close action fired')
#this.destroy()
@@ -48,11 +48,13 @@ var $translations = {
messages: <%= I18n.t('messages', locale: :en).to_json %>,
confirmations: {
},
// Moved to yml
list_needs_help: {
help_is_on_its_way: 'Help is already on its way',
title: 'Request a waiter',
content: 'Request a waiter to your table'
},
// Moved to yml
list_needs_payment: {
payment_already_requested: 'You already asked for the bill',
title: 'Ask for the check',
@@ -75,6 +77,7 @@ var $translations = {
waiting_for_confirmation: 'Waiting for approval of the person on this table...'
}
},
// Moved to yml
move_table: {
cannot_move_to_occupied_table: 'You cannot move to an occupied table',
moved_to_another_table: 'The table is changed.',
@@ -89,11 +92,13 @@ var $translations = {
messages: <%= I18n.t('messages', locale: :nl).to_json %>,
confirmations: {
},
// Moved to yml
list_needs_help: {
help_is_on_its_way: 'Er wordt al iemand naar je tafel gestuurd',
title: 'Ik heb een vraag',
content: 'Wil je een vraag stellen?'
},
// Moved to yml
list_needs_payment: {
payment_already_requested: 'De rekening is reeds gevraagd',
title: 'Vraag om de rekening',
@@ -0,0 +1,21 @@
#= require jquery
#= require jquery_ujs
#= require ../app/application
#= require faye
#= require foundation/foundation
#= require foundation/foundation.offcanvas
#= require moment
#= require jquery.ui.datepicker
#= require translations
#= require js-routes
#= require_directory .
#= require_self
@Qstorage = localStorage
$.extend($translations.en, <%= I18n.t('user', locale: :en).to_json %>);
$.extend($translations.nl, <%= I18n.t('user', locale: :nl).to_json %>);
$(document).foundation()
setLocale('en')
$ ->
$('.main-section').css 'min-height', ($(window).height() - $('.tab-bar:first').outerHeight())