diff options
| author | Jules Laplace <jules@okfoc.us> | 2015-08-18 15:56:15 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2015-08-18 15:56:15 -0400 |
| commit | 1807ea009f23ac446cb103005045942b733ffc61 (patch) | |
| tree | 861791e2abbaf893694555871c551957803e3e09 | |
| parent | bb973c2a2ceef0008325129bcde0ff9ddc3fdb6a (diff) | |
edit scale
| -rw-r--r-- | public/assets/javascripts/ui/blueprint/BlueprintEditor.js | 2 | ||||
| -rw-r--r-- | public/assets/javascripts/ui/blueprint/BlueprintScaler.js | 32 | ||||
| -rw-r--r-- | public/assets/javascripts/ui/blueprint/BlueprintToolbar.js | 8 | ||||
| -rw-r--r-- | public/assets/javascripts/ui/blueprint/BlueprintUploader.js | 12 | ||||
| -rw-r--r-- | public/assets/javascripts/ui/blueprint/BlueprintView.js | 2 | ||||
| -rw-r--r-- | server/index.js | 7 | ||||
| -rw-r--r-- | server/lib/api/blueprint.js | 105 | ||||
| -rw-r--r-- | server/lib/api/index.js | 1 | ||||
| -rw-r--r-- | server/lib/api/media.js | 16 | ||||
| -rw-r--r-- | server/lib/schemas/Blueprint.js | 53 | ||||
| -rw-r--r-- | views/controls/blueprint/toolbar.ejs | 6 |
11 files changed, 210 insertions, 34 deletions
diff --git a/public/assets/javascripts/ui/blueprint/BlueprintEditor.js b/public/assets/javascripts/ui/blueprint/BlueprintEditor.js index 545de7b..cc469b6 100644 --- a/public/assets/javascripts/ui/blueprint/BlueprintEditor.js +++ b/public/assets/javascripts/ui/blueprint/BlueprintEditor.js @@ -64,7 +64,7 @@ var BlueprintEditor = View.extend(AnimatedView.prototype).extend({ }, loadFloorplan: function(media){ - console.log(media) + // console.log(media) this.floorplan.load({ media: media, keepImage: true, diff --git a/public/assets/javascripts/ui/blueprint/BlueprintScaler.js b/public/assets/javascripts/ui/blueprint/BlueprintScaler.js index ff26c8e..0f2fdcd 100644 --- a/public/assets/javascripts/ui/blueprint/BlueprintScaler.js +++ b/public/assets/javascripts/ui/blueprint/BlueprintScaler.js @@ -2,7 +2,7 @@ var BlueprintScaler = ModalFormView.extend(AnimatedView.prototype).extend({ el: ".blueprintScaler", - action: "/api/media/scale", + action: "/api/blueprint/scale", events: { "change [name=blueprint-dimensions]": "changeDimensions", @@ -26,8 +26,8 @@ var BlueprintScaler = ModalFormView.extend(AnimatedView.prototype).extend({ width: window.innerWidth, height: window.innerHeight, zoom: -2, - zoom_min: -6.2, - zoom_max: 1, + zoom_min: -7.0, + zoom_max: 2, }) this.lineTool = new LineTool this.map.ui.add_tool("line", this.lineTool) @@ -42,17 +42,27 @@ var BlueprintScaler = ModalFormView.extend(AnimatedView.prototype).extend({ this.parent.uploader.show() }, - pick: function(media){ + pick: function(media, shouldEdit){ this.media = media - - if (!! media.units) { + + this.floorplan.load({ media: media, scale: 1, keepImage: true }) + + if (!! media.units && ! shouldEdit) { this.parent.useFloorplan(media) this.hide() this.stopAnimating() return } - this.floorplan.load({ media: media, keepImage: true }) + if (media.units && media.line && media.scale) { + var points = media.line.split(",") + this.lineTool.line[0] = new vec2( +points[0], +points[1] ) + this.lineTool.line[1] = new vec2( +points[2], +points[3] ) + + app.units = media.units + this.$units.val( media.units ) + this.$dimensions.unitVal( media.scale * this.lineLength() ) + } this.startAnimating() }, @@ -65,8 +75,9 @@ var BlueprintScaler = ModalFormView.extend(AnimatedView.prototype).extend({ this.floorplan.draw(this.map.draw.ctx, true) + this.map.draw.ctx.save() this.map.draw.ctx.strokeStyle = "#f00" - this.map.draw.ctx.lineWidth = 2/map.zoom + this.map.draw.ctx.lineWidth = 1/map.zoom switch (this.lineTool.line.length) { case 1: this.map.draw.line( @@ -85,6 +96,7 @@ var BlueprintScaler = ModalFormView.extend(AnimatedView.prototype).extend({ ) break } + this.map.draw.ctx.restore() this.map.draw.coords() @@ -125,15 +137,17 @@ var BlueprintScaler = ModalFormView.extend(AnimatedView.prototype).extend({ showErrors: function(){}, serialize: function(){ - var fd = new FormData() + var fd = new FormData(), line = this.lineTool.line fd.append( "_id", this.media._id) fd.append( "units", this.$units.val() ) fd.append( "scale", this.$dimensions.unitVal() / this.lineLength() ) + fd.append( "line", [line[0].a,line[0].b,line[1].a,line[1].b].join(",") ) fd.append( "_csrf", $("[name=_csrf]").val()) return fd }, success: function(){ + this.media.scale = this.$dimensions.unitVal() / this.lineLength() this.stopAnimating() this.parent.useFloorplan(this.media) this.hide() diff --git a/public/assets/javascripts/ui/blueprint/BlueprintToolbar.js b/public/assets/javascripts/ui/blueprint/BlueprintToolbar.js index 69288f6..a21a0ef 100644 --- a/public/assets/javascripts/ui/blueprint/BlueprintToolbar.js +++ b/public/assets/javascripts/ui/blueprint/BlueprintToolbar.js @@ -3,6 +3,7 @@ var BlueprintToolbar = View.extend({ el: "#blueprintToolbar", events: { + "click [data-role=upload-floorplan]": 'showUploader', "click [data-role=toggle-orbit-mode]": 'toggleOrbitMode', "click [data-role=arrow-mode]": 'arrowMode', "click [data-role=polyline-mode]": 'polylineMode', @@ -14,6 +15,7 @@ var BlueprintToolbar = View.extend({ this.parent = opt.parent this.$modes = this.$('.mode') + this.$toggleOrbitMode = this.$('[data-role=toggle-orbit-mode]') this.$arrowMode = this.$('[data-role=arrow-mode]') this.$polylineMode = this.$('[data-role=polyline-mode]') this.$orthoPolylineMode = this.$('[data-role=ortho-polyline-mode]') @@ -22,9 +24,15 @@ var BlueprintToolbar = View.extend({ this.orthoPolylineMode() }, + showUploader: function(){ + this.parent.scaler.show() + this.parent.uploader.show() + }, + orbiting: true, toggleOrbitMode: function(){ this.orbiting = ! this.orbiting + this.$toggleOrbitMode.toggleClass("inuse", ! this.orbiting) if (this.orbiting) { controls.toggle(true) movements.lock() diff --git a/public/assets/javascripts/ui/blueprint/BlueprintUploader.js b/public/assets/javascripts/ui/blueprint/BlueprintUploader.js index aeb7d9c..676976f 100644 --- a/public/assets/javascripts/ui/blueprint/BlueprintUploader.js +++ b/public/assets/javascripts/ui/blueprint/BlueprintUploader.js @@ -3,10 +3,10 @@ var BlueprintUploader = UploadView.extend({ el: ".blueprintUploader", mediaTag: "blueprint", - createAction: "/api/media/new", - uploadAction: "/api/media/upload", - listAction: "/api/media/user", - destroyAction: "/api/media/destroy", + createAction: "/api/blueprint/new", + uploadAction: "/api/blueprint/upload", + listAction: "/api/blueprint/user", + destroyAction: "/api/blueprint/destroy", events: { "mousedown": 'stopPropagation', @@ -47,7 +47,7 @@ var BlueprintUploader = UploadView.extend({ var $el = $(e.currentTarget) var media = $el.data("media") this.hide() - this.parent.scaler.pick(media) + this.parent.scaler.pick(media, true) }, destroy: function(e){ @@ -104,7 +104,7 @@ var BlueprintUploader = UploadView.extend({ this.$blueprints.show() this.append(media) this.hide() - this.parent.scaler.pick(media) + this.parent.scaler.pick(media, true) }, append: function(media){ diff --git a/public/assets/javascripts/ui/blueprint/BlueprintView.js b/public/assets/javascripts/ui/blueprint/BlueprintView.js index cf627d9..4cb9138 100644 --- a/public/assets/javascripts/ui/blueprint/BlueprintView.js +++ b/public/assets/javascripts/ui/blueprint/BlueprintView.js @@ -2,7 +2,7 @@ var BlueprintView = View.extend({ el: "#blueprintView", - action: "/api/layout/", + action: "/api/blueprint/", events: { }, diff --git a/server/index.js b/server/index.js index 078db8e..421a6a7 100644 --- a/server/index.js +++ b/server/index.js @@ -159,9 +159,14 @@ site.route = function () { app.get('/api/media/user', middleware.ensureAuthenticated, api.media.user) app.post('/api/media/new', middleware.ensureAuthenticated, api.media.create) app.post('/api/media/upload', middleware.ensureAuthenticated, api.media.upload) - app.post('/api/media/scale', middleware.ensureAuthenticated, api.media.scale) app.delete('/api/media/destroy', middleware.ensureAuthenticated, api.media.destroy) + app.post('/api/blueprint/new', middleware.ensureAuthenticated, api.blueprint.create) + app.post('/api/blueprint/upload', middleware.ensureAuthenticated, api.blueprint.upload) + app.get('/api/blueprint/user', middleware.ensureAuthenticated, api.blueprint.user) + app.post('/api/blueprint/scale', middleware.ensureAuthenticated, api.blueprint.scale) + app.delete('/api/blueprint/destroy', middleware.ensureAuthenticated, api.blueprint.destroy) + app.get('/api/subscription', middleware.ensureAuthenticated, api.subscription.middleware.ensurePlans, api.subscription.middleware.ensureSubscription, api.subscription.show) app.put('/api/subscription', middleware.ensureAuthenticated, api.subscription.middleware.ensureSubscription, api.subscription.update) app.put('/api/subscription/sync', middleware.ensureAuthenticated, api.subscription.middleware.ensurePlans, api.subscription.middleware.ensureSubscription, api.subscription.sync) diff --git a/server/lib/api/blueprint.js b/server/lib/api/blueprint.js new file mode 100644 index 0000000..a932383 --- /dev/null +++ b/server/lib/api/blueprint.js @@ -0,0 +1,105 @@ +/* jshint node: true */ + +var _ = require('lodash'), + crypto = require('crypto'), + util = require('../util'), + upload = require('../upload'), + config = require('../../../config.json'), + Blueprint = require('../schemas/Blueprint'); + +var blueprint = { + + user: function(req, res){ + var offset = Number(req.query.offset) || 0 + var limit = Math.min( Number(req.query.limit), 50 ) || 20 + var query = { user_id: req.user._id } + if (req.query.tag) { + query.tag = req.query.tag + } + Blueprint.find(query) + .sort({'created_at': -1}) + .skip(offset) + .limit(limit) + .exec(function(err, media){ + res.json(media || []) + }) + }, + + create: function(req, res){ + var data = util.cleanQuery(req.body) + data.user_id = req.user._id + data.created_at = new Date () + + if (data.tag) { + data.tag = util.sanitize(data.tag) + } + + new Blueprint(data).save(function(err, rec){ + if (err || ! rec) { return res.json({ error: err }) } + return res.json(rec) + }) + }, + + upload: function(req, res){ + var data = util.cleanQuery(req.body) + data.user_id = req.user._id + data.created_at = new Date () + data.type = "image" + + upload.put("media", req.files.image, { + username: req.user.username, + unacceptable: function(err){ + res.json({ error: { errors: { media: { message: "Problem saving image: " + err } } } }) + }, + success: function(url){ + data.url = url + done() + } + }) + + function done () { + new Blueprint(data).save(function(err, rec) { + if (err || ! rec) { return res.json({ error: err }) } + res.json(rec) + }) + } + }, + + scale: function(req, res){ + var _id = req.body._id + var data = util.cleanQuery(req.body) + if (! _id) { return res.json({ error: 404 }) } + Blueprint.findOne({ _id: _id }, function(err, doc){ + if (! doc) { return res.json({ error: 404 }) } + if (String(doc.user_id) !== String(req.user._id)) { return res.json({ error: 404 }) } + doc.scale = data.scale + doc.units = data.units + doc.line = data.line + console.log(doc) + doc.save(function(err, rec){ + if (err || ! rec) { return res.json({ error: err }) } + res.json(rec) + }) + }) + }, + + destroy: function(req, res){ + var _id = util.sanitize(req.body._id) + if (! _id || ! _id.length) { + res.json({ error: 404 }) + return + } + Blueprint.findOne({ _id: _id }, function(err, doc){ + if (! doc) { return res.json({ error: 404 }) } + if (String(doc.user_id) !== String(req.user._id)) { + return res.json({ error: "access denied" }) + } + Blueprint.remove({ _id: _id }, function(err){ + res.json({ status: "OK" }) + }) + }) + } + +} + +module.exports = blueprint diff --git a/server/lib/api/index.js b/server/lib/api/index.js index 9478d9b..8254232 100644 --- a/server/lib/api/index.js +++ b/server/lib/api/index.js @@ -1,6 +1,7 @@ /* jshint node: true */ var api = { + blueprint: require('./blueprint'), docs: require('./docs'), layouts: require('./layouts'), media: require('./media'), diff --git a/server/lib/api/media.js b/server/lib/api/media.js index 68e012c..85cbdd6 100644 --- a/server/lib/api/media.js +++ b/server/lib/api/media.js @@ -65,22 +65,6 @@ var media = { } }, - scale: function(req, res){ - var _id = req.body._id - var data = util.cleanQuery(req.body) - if (! _id) { return res.json({ error: 404 }) } - Media.findOne({ _id: _id }, function(err, doc){ - if (! doc) { return res.json({ error: 404 }) } - if (String(doc.user_id) !== String(req.user._id)) { return res.json({ error: 404 }) } - doc.scale = data.scale - doc.units = data.units - doc.save(function(err, rec){ - if (err || ! rec) { return res.json({ error: err }) } - res.json(rec) - }) - }) - }, - destroy: function(req, res){ var _id = util.sanitize(req.body._id) if (! _id || ! _id.length) { diff --git a/server/lib/schemas/Blueprint.js b/server/lib/schemas/Blueprint.js new file mode 100644 index 0000000..40e67d8 --- /dev/null +++ b/server/lib/schemas/Blueprint.js @@ -0,0 +1,53 @@ +/* jshint node: true */ + +var mongoose = require('mongoose'), + _ = require('lodash'), + util = require('../util'); + +var BlueprintSchema = new mongoose.Schema({ + type: { + type: String, + required: true + }, + url: { + type: String, + required: true, + }, + token: { + type: String, + default: "" + }, + thumbnail: { + type: String, + default: "" + }, + width: { + type: Number, + default: 0 + }, + height: { + type: Number, + default: 0 + }, + title: { + type: String, + default: "" + }, + description: { + type: String, + default: "" + }, + tag: { type: String, default: "" }, + scale: { type: Number, default: 1.0 }, + + widthDimension: { type: Number }, + heightDimension: { type: Number }, + units: { type: String }, + line: { type: String }, + + user_id: { type: mongoose.Schema.ObjectId, index: true }, + created_at: { type: Date }, +}); + +module.exports = exports = mongoose.model('blueprint', BlueprintSchema) +exports.schema = BlueprintSchema; diff --git a/views/controls/blueprint/toolbar.ejs b/views/controls/blueprint/toolbar.ejs index 13e99ec..82cbcba 100644 --- a/views/controls/blueprint/toolbar.ejs +++ b/views/controls/blueprint/toolbar.ejs @@ -1,4 +1,10 @@ <div class="edit menu vvbox" id="blueprintToolbar"> + + <span + data-role='upload-floorplan' + data-info="upload a new floorplan" + class="ion-ios-upload-outline"></span> + <span data-role='toggle-orbit-mode' data-info="toggle 3D mode" |
