diff options
Diffstat (limited to 'public/assets/javascripts/ui/blueprint/BlueprintSettings.js')
| -rw-r--r-- | public/assets/javascripts/ui/blueprint/BlueprintSettings.js | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/public/assets/javascripts/ui/blueprint/BlueprintSettings.js b/public/assets/javascripts/ui/blueprint/BlueprintSettings.js new file mode 100644 index 0000000..1ff3d6e --- /dev/null +++ b/public/assets/javascripts/ui/blueprint/BlueprintSettings.js @@ -0,0 +1,139 @@ + +var BlueprintSettings = FormView.extend({ + el: "#blueprintSettings", + + createAction: "/api/layout/new", + updateAction: "/api/layout/edit", + destroyAction: "/api/layout/destroy", + + events: { + "mousedown": "stopPropagation", + "keydown": 'stopPropagation', + "keydown [name=name]": 'enterSubmit', + "click [data-role='save-layout']": 'clickSave', + "click [data-role='clone-layout']": 'clone', + "click [data-role='clear-layout']": 'clear', + "click [data-role='destroy-layout']": '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) + + this.parent.colorControl.loadDefaults() + + 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 blueprint " + 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 = "/layout" + } + }) + }.bind(this)) + }, + + toggle: function(state){ + this.$el.toggleClass("active", 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("Layout needs a name.") + } + if (Rooms.count() == 0) { + errors.push("Please add some rooms by drawing boxes.") + } + return errors + }, + + showErrors: function(errors){ + var $errors = $("<span>") + errors.forEach(function(err){ + var $row = $("<div>") + $row.html(err) + $errors.append( $row ) + }) + ErrorModal.alert($errors) + }, + + serialize: function(){ + var thumbnail = 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(thumbnail.toDataURL()) ) + return fd + }, + + 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, "/layout/" + data.slug) + }, + +}) |
