diff options
Diffstat (limited to 'public/assets/javascripts/ui/editor')
| -rw-r--r-- | public/assets/javascripts/ui/editor/MediaUpload.js | 46 | ||||
| -rw-r--r-- | public/assets/javascripts/ui/editor/MediaViewer.js | 34 |
2 files changed, 50 insertions, 30 deletions
diff --git a/public/assets/javascripts/ui/editor/MediaUpload.js b/public/assets/javascripts/ui/editor/MediaUpload.js index 76bf6f0..a66f2e8 100644 --- a/public/assets/javascripts/ui/editor/MediaUpload.js +++ b/public/assets/javascripts/ui/editor/MediaUpload.js @@ -2,16 +2,18 @@ var MediaUpload = View.extend({ el: ".fileUpload", - createAction: "/api/media/url", + createAction: "/api/media/new", uploadAction: "/api/media/upload", events: { "keydown .url": "enterSubmit", "change .file": "handleFileSelect", + "submit form": "preventDefault", }, initialize: function(opt){ this.parent = opt.parent + this.$csrf = this.$("[name=_csrf]") this.$url = this.$(".url") this.$file = this.$(".file") this.$upload = this.$(".upload-icon") @@ -43,14 +45,15 @@ var MediaUpload = View.extend({ return } + media._csrf = this.$csrf.val() console.log(media) - $.ajax({ + var request = $.ajax({ type: "post", url: this.createAction, - data: rec, - success: $.proxy(this.add, this) + data: media, }) + request.done($.proxy(this.add, this)) }, this)) }, @@ -67,28 +70,29 @@ var MediaUpload = View.extend({ this.$upload.addClass('uploading') - var xhr = new XMLHttpRequest(), - fd = new FormData(); + var fd = new FormData(); + fd.append( 'image', f ) + fd.append( '_csrf', this.$csrf.val() ) - fd.append( 'file', f ); - - xhr.addEventListener("error", $.proxy(function(){ - console.log("error uploading file..") - this.$upload.removeClass('uploading') - }, this), false); - - xhr.onreadystatechange = $.proxy(function() { - if (xhr.readyState == 4 && xhr.status == 200) { - this.$upload.removeClass('uploading') - } - }, this) - - xhr.open("POST", '/api/media/upload', true); - xhr.send( fd ); + var request = $.ajax({ + url: this.uploadAction, + type: "post", + data: fd, + dataType: "json", + processData: false, + contentType: false, + }) + request.done($.proxy(this.add, this)) } }, add: function(media){ + console.log(media) + if (media.error) { + return + } + this.$upload.removeClass('uploading') + this.parent.mediaViewer.add(media) } }) diff --git a/public/assets/javascripts/ui/editor/MediaViewer.js b/public/assets/javascripts/ui/editor/MediaViewer.js index cd58231..96d7cf7 100644 --- a/public/assets/javascripts/ui/editor/MediaViewer.js +++ b/public/assets/javascripts/ui/editor/MediaViewer.js @@ -10,7 +10,7 @@ var MediaViewer = ModalView.extend({ this.load() } else { - this.__super__.show() + this.__super__.show.call(this) } }, @@ -19,14 +19,30 @@ var MediaViewer = ModalView.extend({ }, populate: function(data){ - data.forEach($.proxy(function(room){ - var $span = $("<span>") - $span.data("slug", room.slug) - $span.css("background-image", "url(" + room.photo + ")") - - this.$templates.append($span) - }, this)) - this.__super__.show() + data.forEach($.proxy(this.add, this)) + + this.__super__.show.call(this) + }, + + add: function(media){ + var image = new Image () + var $span = $("<span>") + $span.addClass("mediaContainer") + switch (media.type) { + case 'image': + image.src = media.url + break + + case 'youtube': + case 'vimeo': + image.src = media.thumbnail + break + } + + $span.data("media", media) + $span.append(image) + + this.$(".myMedia").prepend($span) }, destroy: function(name, cb){ |
