summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/ui/editor/EditorSettings.js
blob: b319404f15d7bc46711957c48a7d9bfefd8f7cd4 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
var EditorSettings = FormView.extend({
	el: "#editorSettings",
	
	createAction: "/api/project/new",
	updateAction: "/api/project/edit",
	destroyAction: "/api/project/destroy",
	
	useMinotaur: true,
  
	events: {
	  "mousedown": "stopPropagation",
		"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',
		"click [data-role='toggle-map']": 'toggleMap',
		"click [data-role='view-project']": 'viewProject',
    "click #startText": "setStartPosition",
    "click #moveText": "confirmStartPosition",
    "click #confirmText": "setStartPosition",
    "click #goText": "goToStartPosition",
	},
	
	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]")
		this.$startPoint = this.$("#startpoint")
	},
	
	load: function(data){
		this.action = data.isNew ? this.createAction : this.updateAction
    this.parent.data = data
    
		data.rooms && Rooms.deserialize(data.rooms, data.walls)
		if (data.startPosition) {
		  scene.camera.move(data.startPosition)
		  this.startPosition = data.startPosition
		  this.$startPoint.addClass("confirmed")
		}

    if (data.colors && data.colors.wall) {
      this.parent.colorControl.load(data.colors)
    }
    else {
		  this.parent.colorControl.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" )
		}
		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()
	},
	
	toggleMap: function(e){
	  e.preventDefault()
	  app.controller.toolbar.toggleMap()
	},
	
	clone: function(e){
		e.preventDefault()

		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
		this.thumbnailState = null
		
		window.history.pushState(null, document.title, "/project/new")
	},
	
	clear: function(e){
		e.preventDefault()
		Scenery.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 = "/profile"
				}
			})
		}.bind(this))
	},

	toggle: function(state){
	  var state = typeof state == 'boolean' ? state : ! this.$el.hasClass("active")
		this.$el.toggleClass("active", state)
		
 	  $(".inuse").removeClass("inuse")
		$("[data-role='toggle-project-settings']").toggleClass("inuse", state)
    if (state) {
      this.parent.cursor.message("settings")
    }
	},

	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 = $("<span>")
		errors.forEach(function(err){
			var $row = $("<div>")
			$row.html(err)
			$errors.append( $row )
		})
		ErrorModal.alert($errors)
	},
	
	startPosition: null,
	setStartPosition: function(){
	  this.$startPoint.addClass("active").removeClass("confirmed")
	},
	confirmStartPosition: function(){
	  this.$startPoint.removeClass("active").addClass("confirmed")
	  this.startPosition = app.position(scene.camera)
	},
	goToStartPosition: function(){
	  if (! this.startPosition) return
		scene.camera.move(this.startPosition)
	},
	
	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( "description", this.$description.val() )
		fd.append( "privacy", this.$privacy.filter(":checked").val() == "private" )
		fd.append( "viewHeight", window.viewHeight )
		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( this.startPosition || false ) )
		fd.append( "lastPosition", JSON.stringify( app.position(scene.camera) ) )
		
		if (this.thumbnailIsStale()) {
      var thumbnail = map.draw.render()
      fd.append( "thumbnail", dataUriToBlob(thumbnail.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()
    this.isVisible = true
	},
	
	viewAfterSave: false,
	viewProject: function(e){
	  e.preventDefault()
		Minotaur.unwatch(this)
		Minotaur.hide()
	  this.viewAfterSave = true
	  this.save()
	},
	
	success: function(data){
		this.$id.val(data._id)
		this.$name.val(data.name)
		this.action = this.updateAction
		
		if (this.viewAfterSave) {
		  window.location.pathname = "/project/" + data.slug
		  return
		}
		
		Minotaur.unwatch(this)
		Minotaur.hide()
		
		window.history.pushState(null, document.title, "/project/" + data.slug + "/edit")
		
		this.parent.share.setLink( "http://vvalls.com/project/" + data.slug )
		if (this.isVisible) {
		  this.isVisible = false
      this.parent.share.show()
    }
    
		this.parent.data = data
	},

})