var EditorSettings = FormView.extend({ el: "#editorSettings", createAction: "/api/project/new", updateAction: "/api/project/edit", destroyAction: "/api/project/destroy", events: { "keydown": 'stopPropagation', "keydown [name=name]": 'enterSubmit', "click [data-role='show-collaborators']": 'showCollaborators', "click [data-role='save-project']": 'clickSave', "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.$description = this.$("[name=description]") this.$privacy = this.$("[name=privacy]") }, load: function(data){ this.action = data.isNew ? this.createAction : this.updateAction this.parent.data = data data.rooms && Rooms.deserialize(data.rooms) data.walls && Walls.deserialize(data.walls) data.startPosition && scene.camera.move(data.startPosition) if (data.colors && data.colors.wall) { this.parent.lightControl.load(data.colors) } else { this.parent.lightControl.loadDefaults() } if (data.walls) { data.walls.some(function(wall){ if (wall.background !== "none") { this.parent.wallpaperPicker.$remove.show() return true } return false }.bind(this)) } if (data.isNew) { this.$name.val( "Untitled Room" ) } else { this.thumbnailIsStale() this.$id.val( data._id ) this.$name.val( data.name ) this.$description.val( data.description ) data.privacy && this.$privacy.find("[value=" + data.privacy + "]").prop("checked", "checked") data.media && Scenery.deserialize(data.media) } }, showCollaborators: function(e){ e && e.preventDefault() this.parent.collaborators.show() }, 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, function(){ $.ajax({ url: this.destroyAction, type: "delete", data: { _id: this.$id.val(), _csrf: this.$csrf.val() }, success: function(data){ window.location.href = "/project" } }) }.bind(this)) }, toggle: function(state){ var state = typeof state == 'boolean' ? state : ! this.$el.hasClass("active") this.$el.toggleClass("active", state) $("[data-role='toggle-project-settings']").toggleClass("inuse", state) }, 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( "description", this.$description.val() ) fd.append( "privacy", this.$privacy.filter(":checked").val() == "private" ) fd.append( "rooms", JSON.stringify( Rooms.serialize() ) ) fd.append( "walls", JSON.stringify( Walls.serialize() ) ) fd.append( "colors", JSON.stringify( Walls.colors ) ) fd.append( "media", JSON.stringify( Scenery.serialize() ) ) fd.append( "startPosition", JSON.stringify( app.position(scene.camera) ) ) if (this.thumbnailIsStale()) { fd.append( "thumbnail", dataUriToBlob(map.canvas.toDataURL()) ) } return fd }, thumbnailState: null, thumbnailIsStale: function(){ var newState = JSON.stringify( Rooms.serialize() ) if (newState !== this.thumbnailState) { this.thumbnailState = newState return true } return false }, clickSave: function(){ this.toggle(false) this.save() }, success: function(data){ this.$id.val(data._id) this.$name.val(data.name) this.action = this.updateAction Minotaur.unwatch(this) Minotaur.hide() window.history.pushState(null, document.title, "/project/" + data.slug + "/edit") this.parent.data = data }, })