From 5a215e1b00e4fd026a83e81baa7a45c28435f73c Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 12 Jun 2014 14:52:34 -0400 Subject: routes for starting a new project --- .../assets/javascripts/ui/editor/EditorSettings.js | 124 +++++++++++++++++++++ .../assets/javascripts/ui/editor/EditorToolbar.js | 22 ++++ public/assets/javascripts/ui/editor/EditorView.js | 28 ++++- 3 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 public/assets/javascripts/ui/editor/EditorSettings.js create mode 100644 public/assets/javascripts/ui/editor/EditorToolbar.js (limited to 'public/assets/javascripts/ui/editor') diff --git a/public/assets/javascripts/ui/editor/EditorSettings.js b/public/assets/javascripts/ui/editor/EditorSettings.js new file mode 100644 index 0000000..ad578d2 --- /dev/null +++ b/public/assets/javascripts/ui/editor/EditorSettings.js @@ -0,0 +1,124 @@ + +var EditorSettings = FormView.extend({ + el: "#editorSettings", + + createAction: "/api/projects/new", + updateAction: "/api/projects/edit", + destroyAction: "/api/projects/destroy", + + events: { + "keydown [name=name]": 'enterSubmit', + "click [data-role='save-project']": 'save', + "click [data-role='clone-project']": 'clone', + "click [data-role='clear-project']": 'clear', + "click [data-role='destroy-project']": 'destroy', + }, + + initialize: function(opt){ + this.parent = opt.parent + this.__super__.initialize.call(this) + + this.$id = this.$("[name=_id]") + this.$csrf = this.$("[name=_csrf]") + this.$name = this.$("[name=name]") + this.$privacy = this.$("[name=privacy]") + }, + + load: function(data){ + this.$id.val(data._id) + this.$name.val(data.name) + + data.rooms && Rooms.deserialize(data.rooms) + data.startPosition && scene.camera.move(data.startPosition) + data.privacy && this.$privacy.find("[value=" + data.privacy + "]").prop('checked', "checked") + + this.action = data.isNew ? this.createAction : this.updateAction + }, + + clone: function(){ + var names = this.$name.val().split(" ") + if ( ! isNaN(Number( names[names.length-1] )) ) { + names[names.length-1] = Number( names[names.length-1] ) + 1 + } + else { + names.push("2") + } + + this.$id.val('new') + this.$name.val( names.join(" ") ) + this.action = this.createAction + + window.history.pushState(null, document.title, "/builder/new") + }, + + clear: function(){ + Rooms.removeAll() + }, + + destroy: function(){ + var msg = "Are you sure you want to delete the project " + sanitize(this.$name.val()) + "?" + ConfirmModal.confirm(msg, $.proxy(function(){ + $.ajax({ + url: this.destroyAction, + type: "delete", + data: { _id: this.$id.val(), _csrf: this.$csrf.val() }, + success: function(data){ + window.location.href = "/project" + } + }) + }, this)) + }, + + toggle: function(){ + this.$el.toggleClass("active") + }, + + enterSubmit: function (e) { + e.stopPropagation() + var base = this + if (e.keyCode == 13) { + setTimeout(function(){ base.save(e) }, 100) + } + }, + + validate: function(){ + var errors = [] + var name = this.$name.val() + if (! name || ! name.length) { + errors.push("Please name your project.") + } + return errors + }, + + showErrors: function(errors){ + var $errors = $("") + errors.forEach(function(err){ + var $row = $("
") + $row.html(err) + $errors.append( $row ) + }) + ErrorModal.alert($errors) + }, + + serialize: function(){ + map.draw.render() + var fd = new FormData() + fd.append( "_csrf", this.$csrf.val() ) + fd.append( "_id", this.$id.val() ) + fd.append( "name", this.$name.val() ) + fd.append( "privacy", this.$privacy.filter(":checked").val() == "private" ) + fd.append( "rooms", JSON.stringify( Rooms.serialize() ) ) + fd.append( "startPosition", JSON.stringify( app.position(scene.camera) ) ) + fd.append( "thumbnail", dataUriToBlob(map.canvas.toDataURL()) ) + return fd + }, + + success: function(data){ + this.$id.val(data._id) + this.$name.val(data.name) + this.action = this.updateAction + + window.history.pushState(null, document.title, "/project/" + data.slug) + }, + +}) diff --git a/public/assets/javascripts/ui/editor/EditorToolbar.js b/public/assets/javascripts/ui/editor/EditorToolbar.js new file mode 100644 index 0000000..1942fe8 --- /dev/null +++ b/public/assets/javascripts/ui/editor/EditorToolbar.js @@ -0,0 +1,22 @@ + +var EditorToolbar = View.extend({ + el: "#builderToolbar", + + events: { + "click [data-role='toggle-map-view']": 'toggleMap', + "click [data-role='toggle-layout-settings']": 'toggleSettings', + }, + + initialize: function(opt){ + this.parent = opt.parent + }, + + toggleMap: function(){ + map.toggle() + }, + + toggleSettings: function(){ + this.parent.settings.toggle() + }, + +}) diff --git a/public/assets/javascripts/ui/editor/EditorView.js b/public/assets/javascripts/ui/editor/EditorView.js index 91329de..7cabeb7 100644 --- a/public/assets/javascripts/ui/editor/EditorView.js +++ b/public/assets/javascripts/ui/editor/EditorView.js @@ -1,13 +1,35 @@ var EditorView = View.extend({ -// el: "#editorControls", + el: "#editorView", + + action: "/api/layouts/", events: { }, + + initialize: function(){ + this.toolbar = new EditorToolbar ({ parent: this }) + this.settings = new EditorSettings ({ parent: this }) + }, - load: function(){ + load: function(name){ + }, + + loadLayout: function(name){ + if (! name || name == "new") { + this.ready({ isNew: true, _id: "new", name: "" }) + return + } + + name = sanitize(name) + + $.get(this.action + name, $.proxy(this.ready, this)) + }, + + ready: function(data){ $("#map").hide() - } + this.settings.load(data) + }, }) -- cgit v1.2.3-70-g09d2