var WallpaperPicker = UploadView.extend({ el: ".wallpaper", mediaTag: "wallpaper", uploadAction: "/api/media/upload", events: { "mousedown": 'stopPropagation', "click .swatch": 'pick', "click .wallpaperRemove": 'remove', "input [data-role='wallpaper-scale']": 'updateScale', }, initialize: function(opt){ this.parent = opt.parent this.__super__.initialize.call(this) this.$swatches = this.$(".swatches") this.$remove = this.$(".wallpaperRemove") this.$remove.hide() this.$position = this.$("[data-role='wallpaper-position']") this.$scale = this.$("[data-role='wallpaper-scale']") this.initializePositionCursor() }, loaded: false, show: function(){ if (! this.loaded) { this.parent.cursor.message("wallpaper") this.load() } else { this.toggle(true) } }, hide: function(){ this.__super__.hide.call(this) }, 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) }, add: function (media) { if (media.type !== "image") { 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() }, toggle: function (state) { if (state && ! this.loaded) { this.show() } else { this.$el.toggleClass("active", state) } // toggle the class that makes the cursor a paintbucket // $("body").removeClass("pastePaper") }, hide: function(){ this.toggle(false) }, 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") { return; } this.wall = wall this.$scale.val( this.wall.background.scale ) }, updateScale: function(){ if (! this.wall) return; s = parseFloat(this.$scale.val()) this.wall.wallpaperPosition({ scale: s }) }, 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 }, }) }, })