1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
var BlueprintSettings = FormView.extend({
el: "#blueprintSettings",
action: "/api/blueprint/edit",
destroyAction: "/api/blueprint/destroy",
events: {
"mousedown": "stopPropagation",
"keydown": 'stopPropagation',
"keydown [name=name]": 'enterSubmit',
"click [data-role='save-layout']": 'clickSave',
"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]")
},
load: function(data){
this.$id.val(data._id)
this.$name.val(data.name)
if (data.shapes) {
shapes.destroy()
shapes.deserialize( data.shapes )
}
this.data = data
},
clear: function(){
shapes.destroy()
},
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("Blueprint needs a name.")
}
if (shapes.count() == 0) {
errors.push("Please add some walls.")
}
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 fd = new FormData()
fd.append( "_csrf", this.$csrf.val() )
fd.append( "_id", this.$id.val() )
fd.append( "name", this.$name.val() )
fd.append( "shapes", JSON.stringify( shapes.serialize() ) )
fd.append( "startPosition", JSON.stringify( app.position(scene.camera) ) )
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, "/blueprint/" + data.slug)
},
})
|