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
|
var WallpaperPicker = UploadView.extend({
el: ".wallpaper",
mediaTag: "wallpaper",
createAction: "/api/media/new",
uploadAction: "/api/media/upload",
events: {
"mousedown": 'stopPropagation',
"click .swatch": 'pick',
"click .wallpaperRemove": 'remove',
"input [data-role='wallpaper-scale']": 'updateScale',
"change .url": "tileWalls",
"keydown .url": "enterSetUrl",
},
initialize: function(opt){
this.parent = opt.parent
this.__super__.initialize.call(this)
this.$swatches = this.$(".swatches")
this.$remove = this.$(".wallpaperRemove")
this.$remove.hide()
this.$url = this.$(".url")
this.$position = this.$("[data-role='wallpaper-position']")
this.$scale = this.$("[data-role='wallpaper-scale']")
this.$wallpaperResizeControls = this.$(".wallpaperResizeControls")
this.$wallpaperResizeControls.addClass('disabled')
this.initializePositionCursor()
},
loaded: false,
show: function(){
this.toggle(true)
},
hide: function(){
this.toggle(false)
},
toggle: function (state) {
Scenery.nextWallpaper = null
app.tube('cancel-wallpaper')
this.$el.toggleClass("active", state)
if (state) {
this.parent.cursor.message("wallpaper")
if (! this.loaded) {
this.load()
}
}
// toggle the class that makes the cursor a paintbucket
// $("body").removeClass("pastePaper")
},
load: function(){
$.get("/api/media/user", { tag: this.mediaTag }, this.populate.bind(this))
},
populate: function(data){
this.loaded = true
if (data && data.length) {
data.forEach(this.add.bind(this))
this.$(".txt").hide()
}
else {
this.$(".txt").show()
}
this.toggle(true)
},
seenWallpapers: {},
add: function (media) {
if (media.type !== "image") { return }
if (this.seenWallpapers[ media.url ]) { return }
var swatch = document.createElement("div")
swatch.className = "swatch"
swatch.style.backgroundImage = "url(" + media.url + ")"
this.$swatches.append(swatch)
this.$swatches.show()
this.$(".txt").hide()
this.seenWallpapers[ media.url ] = true
},
addUrl: function (url){
Parser.loadImage(url, function(media){
if (! media) return
media._csrf = $("[name=_csrf]").val()
media.tag = this.mediaTag
var request = $.ajax({
type: "post",
url: this.createAction,
data: media,
})
request.done(this.add.bind(this))
}.bind(this))
},
beforeUpload: function(){
},
pick: function(e){
app.tube('cancel-wallpaper')
var $swatch = $(e.currentTarget)
this.follow( e, $swatch.css('background-image') )
this.$remove.show()
},
remove: function(e){
if (Scenery.nextWallpaper) {
Scenery.nextWallpaper = null
app.tube('cancel-wallpaper')
}
else {
this.follow( e, "none" )
$(".floatingSwatch").addClass("scissors")
}
},
follow: function(e, wallpaper, icon){
icon = icon || wallpaper
var $floatingSwatch = $(".floatingSwatch")
$floatingSwatch.css('background-image', wallpaper)
Scenery.nextWallpaper = wallpaper
setTimeout(function(){
function _followCursor(e) {
$floatingSwatch.css({
top: (e.pageY + 10) + 'px',
left: (e.pageX + 10) + 'px',
});
}
function _hideCursor (e) {
$(window).off('mousemove', _followCursor)
// $(window).off('click', _hideCursor)
app.off('cancel-wallpaper', _hideCursor)
$floatingSwatch.removeClass("scissors").hide()
}
$(window).on('mousemove', _followCursor)
// $(window).one('click', _hideCursor);
app.on('cancel-wallpaper', _hideCursor)
$floatingSwatch.show()
_followCursor(e);
})
},
wall: null,
pickWall: function(wall){
if (! wall.background || wall.background.src == "none") {
this.$wallpaperResizeControls.addClass('disabled')
this.$scale.val( 0.0 )
return;
}
this.$wallpaperResizeControls.removeClass('disabled')
this.wall = wall
this.$scale.val( Math.log( this.wall.background.scale ) )
},
updateScale: function(){
if (! this.wall) return;
var scale = Math.exp( parseFloat(this.$scale.val()) )
this.wall.wallpaperPosition({ scale: scale })
},
tileWalls: function(){
var url = this.$url.sanitize()
if (url.length && url.indexOf("http") == 0) {
Walls.setWallpaper.wall({ src: url })
Walls.setWallpaper.floor({ src: url })
Walls.setWallpaper.ceiling({ src: url })
}
this.addUrl(url)
this.$url.val("")
},
enterSetUrl: function (e) {
e.stopPropagation()
if (e.keyCode == 13) {
setTimeout(this.tileWalls.bind(this), 100)
}
},
initializePositionCursor: function(){
var base = this
var dx = 0, dy = 0, dragging = false, delta
var x = 0, y = 0, s = 1
var mymouse = new mouse({
el: this.$position[0],
down: function(e, cursor){
if (! base.wall) return
dragging = true
// s = parseFloat( base.$scale.val() )
x = base.wall.background.x
y = base.wall.background.y
},
drag: function(e, cursor){
if (! dragging) return
delta = cursor.delta()
delta.a = - delta.a
dx = delta.a*s
dy = delta.b*s
base.wall.wallpaperPosition({
// scale: s,
x: x+dx,
y: y+dy,
})
},
up: function(e, cursor, new_cursor){
dragging = false
},
})
},
})
|