Permalink
Browse files

Version 2.0

  • Loading branch information...
toantran committed Mar 30, 2012
1 parent 5191a80 commit 4f54957ea932fd348df29359b9e60f617d36382c
View
4 app.js
@@ -71,6 +71,10 @@
}
};
+ app.del('/customerremove', restrict, routes.customerdelete);
+
+ app.post('/addcolumn', restrict, routes.addcolumn);
+
app.get('/defloc', restrict, routes.defaultLocation);
app.get('/accountadd', routes.accountadd);
View
@@ -55,6 +55,8 @@ restrict = (req, res, next) ->
# Routes
+app.del '/customerremove', restrict, routes.customerdelete
+app.post '/addcolumn', restrict, routes.addcolumn
app.get '/defloc', restrict, routes.defaultLocation
app.get '/accountadd', routes.accountadd
app.post '/accountadd', routes.createaccount
No changes.
No changes.
No changes.
@@ -4,7 +4,7 @@ jQuery ($) ->
global = makethis.call()
global.setActiveMenu 1
- formsubmit = ->
+ customerformsubmit = ->
formdata = {}
form = $(@)
@@ -15,12 +15,10 @@ jQuery ($) ->
$.post('customeradd', formdata)
.success (data) ->
if data?.success
- el = $.el('tr', {}, [
+ cols = ( $.el( 'td', {}, [data?.customer[key]] ) for key, value of window.schema )
+ el = $.el('tr', {'data-customerid': data?.customer?._id}, [
$.el('td', {}, [ $.el('div.label.label-success', {}, ['new']) ]),
- $.el('td', {}, [ data?.customer?.name]),
- $.el('td', {}, [ data?.customer?.contact]),
- $.el('td', {}, [ data?.customer?.address]),
- $.el('td', {}, ['']),
+ cols,
$.el('td', {}, [''])
])
@@ -30,7 +28,7 @@ jQuery ($) ->
return false
- $('form#customer-form').on 'submit', formsubmit
+ $('form#customer-form').on 'submit', customerformsubmit
$(':file').change ->
file = @files?[0]
@@ -64,6 +62,45 @@ jQuery ($) ->
false
+ $('form#newcolumn-form').on 'submit', ->
+ formdata = {}
+ form = $(@)
+ for item in $(@).serializeArray()
+ do (item) ->
+ formdata[item.name] = item.value
+
+ $.post('/addcolumn', formdata)
+ .success (data) ->
+ if data?.success
+ window.location.href = window.location.href
+ else
+ $(form).find('.error-msg').val data?.error
+ $(form).find('.error-msg').alert()
+ .error ->
+
+ false
+
+
+ removeCustomer = (id, callback = ->) ->
+ $.ajax '/customerremove',
+ type: 'DELETE'
+ data:
+ id: id
+ success: (data) ->
+ callback null
+ error: ->
+ callback 'error'
+
+
+
+ $('#customer-list').on 'click', (e)->
+ target = $(e.target).closest('.customer-remove')
+ if target
+ e.stopPropagation()
+ id =$(target).closest('tr').attr 'data-customerid'
+ removeCustomer id, (err) ->
+ if not err
+ $(target).closest('tr').remove()
@@ -111,7 +111,8 @@ jQuery ($) ->
@map = new google.maps.Map( document.getElementById("map_canvas"), mapOptions )
@geo = new google.maps.Geocoder()
@icon = new google.maps.MarkerImage('/images/mapslt.png', new google.maps.Size(9,9), new google.maps.Point(138, 227))
- @hlicon = new google.maps.MarkerImage('/images/m3.png')
+ @hlicon = new google.maps.MarkerImage('/images/red_markers_A_J2.png', new google.maps.Size(20,34), new google.maps.Point(0, 340))
+ @hlshadow = new google.maps.MarkerImage('/images/shadow50.png', new google.maps.Size(37,34), new google.maps.Point(0, 0))
@infoWindow = new google.maps.InfoWindow()
loadDefaultLocation()
loadCustomers()
@@ -147,7 +148,29 @@ jQuery ($) ->
getCustomerList = ->
@customers
+
+ resetMarkers = ->
+ if @customers?.length
+ for customer in @customers
+ do (customer) =>
+ marker = customer?.marker
+ if marker?
+ marker.setIcon @icon
+
+
+ highlightCustomers = (customers) ->
+ resetMarkers()
+ clearGrid()
+ if customers?.length
+ for customer in customers
+ do (customer) =>
+ marker = customer?.marker
+ if marker?
+ marker.setIcon @hlicon
+ showInGrid customer
+
+
onsearchclick = (e) ->
val = $('input#search_input').val()
@@ -156,9 +179,9 @@ jQuery ($) ->
currCustomers = getCustomerList()
matchCustomers = (customer for customer in currCustomers when patt.test(customer.name) or patt.test(customer.address) or patt.test(customer.contact))
if matchCustomers?.length
- displayCustomers matchCustomers
+ highlightCustomers matchCustomers
else
- displayCustomers []
+ highlightCustomers []
else
displayCustomers getCustomerList()
View
@@ -1,3 +1,73 @@
+
+
+exports.addcolumn = (req, res, next) ->
+ fieldname = req.param 'fieldname', ''
+ fielddef = req.param 'fielddef', ''
+
+ if fieldname
+ schema = req.user?.dataschema ? {name: 1, contact: 1, address: 1}
+ schema[fieldname] = 1
+ user = req.user
+ user.dataschema = schema
+
+ userSvc = require '../services/user'
+ try
+ userSvc.setSchema req.user._id, schema, (err, saveduser) ->
+ req.session?.regenerate ->
+ req.session.user = user
+
+ if fielddef
+ customersvc = require '../services/customer'
+ obj = {}
+ obj[fieldname] = fielddef
+ customersvc.setDefVal user._id, obj, ->
+ res.send
+ success: !!!(err)
+ error: err
+ dataschema: schema
+ else
+ res.send
+ success: !!!(err)
+ error: err
+ dataschema: schema
+ catch e
+ console.trace e
+ res.send
+ success: false
+ error: e
+ else
+ res.send
+ success: false
+ error: 'field name cannot be empty'
+
+
+exports.dataschema = (req, res, next) ->
+ if req.user?.dataschema?
+ res.send
+ success: true
+ dataschema: req.user.dataschema
+ else
+ userSvc = require '../services/user'
+ try
+ userSvc.getById req.user._id, (err, user) ->
+ if user?.dataschema?
+ req.session?.regenerate ->
+ req.session.user = user
+ res.send
+ success: true
+ dataschema: req.user.dataschema
+ else
+ user?.dataschema =
+ name: 1
+ contact: 1
+ address: 1
+ catch e
+ console.trace e
+ res.send
+ success: false
+ error: e
+
+
exports.defaultLocation = (req, res, next) ->
userSvc = require '../services/user'
@@ -51,15 +121,35 @@ exports.createaccount = (req, res, next) ->
res.redirect 'back'
+exports.customerdelete = (req, res, next) ->
+ customersvc = require '../services/customer'
+ customerid = req.param 'id', ''
+
+ if customerid
+ customersvc.remove customerid, (err) ->
+ res.send
+ success: !!!(err)
+ error: err
+ else
+ res.send
+ success: false
+ error: 'customerid is empty'
+
+
exports.customeradd = (req, res, next) ->
customersvc = require '../services/customer'
- customername = req.param 'customername', ''
- customercontact = req.param 'customercontact', ''
- customeraddress = req.param 'customeraddress', ''
+ data = {}
+ data[key] = value for own key,value of req.user?.dataschema
+
+ data[key] = req.body[key] for key, val of data
+
+ data.userid = req.user._id
+
+ console.log data
try
- customersvc.add {name:customername, contact: customercontact, address:customeraddress, userid: req.user._id}, (err, customer) ->
+ customersvc.add data, (err, customer) ->
res.send
success: !!! (err)
customer: customer
@@ -70,12 +160,15 @@ exports.customeradd = (req, res, next) ->
exports.data = (req, res, next) ->
customersvc = require '../services/customer'
+ usersvc = require '../services/user'
+ utils = require '../services/utils'
try
customersvc.getAll req.user._id, '', (err, customers) ->
res.render 'data'
title: 'Where Art Thou - Data'
customers: customers
+ user: req.user
layout: true
catch e
console.trace e
@@ -1,6 +1,38 @@
repo = require '../repository/customers'
+exports.remove = (id, callback = ->) ->
+ console.assert id, 'customerid cannot be null or 0'
+ throw 'customerid is null or empty' unless id?
+
+ id = new repo.ObjectId(id) if typeof id is 'string'
+
+ try
+ repo.remove {_id: id}, callback
+ catch e
+ console.trace e
+ callback e
+
+
+exports.setDefVal = (userid, setdata, callback = ->) ->
+ console.assert userid, 'userid cannot be null or 0'
+ throw 'userid is null or empty' unless userid?
+
+ setdata ?= {}
+ setdata.updatedat = new Date()
+
+ userid = new repo.ObjectId(userid) if typeof userid is 'string'
+ findObj =
+ userid: userid
+ updateObj =
+ $set: setdata
+ try
+ repo.update findObj, updateObj, {multi: true}, callback
+ catch e
+ console.trace e
+ callback e
+
+
exports.add = (customer, callback = ->) ->
console.assert customer, 'customer cannot be null or 0'
throw 'customer is null or empty' unless customer?
@@ -50,7 +82,6 @@ exports.getAll = (userid, filter, callback = ->) ->
customers?.sort (c1, c2) ->
if c1?.name < c2?.name then -1 else 1
- db = cursor.db
callback.apply null, [toarrayerr, customers]
cursor.close()
else
@@ -137,8 +137,8 @@ exports.insert = (user, callback = ->) ->
callback 'You Chose an Email Address That is Already Registered, You Hacker!'
else
user.createdat = new Date()
- user.pictureurl ?= '/images/player.jpg'
user.password = hash user.password, 'a little dog'
+ user.dataschema ?= name: 1, contact: 1, address: 1
try
newUserRepo.create user, cb
catch e
@@ -243,6 +243,27 @@ exports.setPassword = (userid, password, callback = ->) ->
console.trace e
callback e
+
+
+exports.setSchema = (userid, schema, callback = ->) ->
+ console.assert userid, 'userid cannot be null'
+ throw 'userid cannot be null' unless userid? and userid
+
+ userid = new newUserRepo.ObjectId( userid ) if typeof userid is 'string'
+
+ findObj = _id : userid
+ updateObj =
+ $set:
+ dataschema: schema
+ updatedat: new Date()
+
+ try
+ newUserRepo.update findObj, updateObj, {}, callback
+ catch e
+ console.trace e
+ callback e
+
+
exports.assignTeam = (userid, team, callback = ->) ->
console.assert userid, 'userid cannot be null'
Binary file not shown.
View
Binary file not shown.
Oops, something went wrong.

0 comments on commit 4f54957

Please sign in to comment.