From e300d0888a3c564569743202d889aa3cbd00c4dd Mon Sep 17 00:00:00 2001 From: Jay Wood Date: Fri, 30 Sep 2016 12:02:31 -0400 Subject: [PATCH] Finalize media upload javascripts --- js/admin.js | 43 +++++++++++++++++++++++++++++++++++-------- js/admin.min.js | 2 +- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/js/admin.js b/js/admin.js index 1b2803e..7f206f6 100644 --- a/js/admin.js +++ b/js/admin.js @@ -14,25 +14,52 @@ window.cwv2Admin = {}; app.cache = function() { app.$c = { window: $(window), - // fooSelector: $( '.foo' ), + imgUploadBtn: $( '.upload_image_button' ), }; }; // Combine all events app.bindEvents = function() { - // app.$c.window.on( 'load', app.doFoo ); + app.$c.imgUploadBtn.on( 'click', app.handleImageUploader ); }; // Do we meet the requirements? app.meetsRequirements = function() { - // Basically check - // return app.$c.fooSelector.length; + return app.$c.imgUploadBtn.length; }; - // Some function - // app.doFoo = function() { - // do stuff - // }; + /** + * Handles Media Uploads + * + * @returns {boolean} + */ + app.handleImageUploader = function() { + var btnObj = $( this ); + + window.uploadID = btnObj.data( 'target-id' ); + if ( window.file_frame ) { + window.file_frame.open(); + return true; + } + + window.file_frame = wp.media.frames.file_frame = wp.media( { + title: btnObj.data( 'uploader-title' ), + button: { + text: btnObj.data( 'uploader-btn-txt' ) + }, + multiple: false, + } ); + + window.file_frame.on( 'select', function() { + var attachment = window.file_frame.state().get( 'selection' ).first().toJSON(); + $( '#' + window.uploadID ).val( attachment.url ); + + window.console.log( attachment ); + + }); + + window.file_frame.open(); + }; // Engage $( app.init ); diff --git a/js/admin.min.js b/js/admin.min.js index fcc65e7..5d052b7 100644 --- a/js/admin.min.js +++ b/js/admin.min.js @@ -1 +1 @@ -window.cwv2Admin={},function(window,$,app){app.init=function(){app.cache(),app.meetsRequirements()&&app.bindEvents()},app.cache=function(){app.$c={window:$(window)}},app.bindEvents=function(){},app.meetsRequirements=function(){},$(app.init)}(window,jQuery,window.cwv2Admin); \ No newline at end of file +window.cwv2Admin={},function(window,$,app){app.init=function(){app.cache(),app.meetsRequirements()&&app.bindEvents()},app.cache=function(){app.$c={window:$(window),imgUploadBtn:$(".upload_image_button")}},app.bindEvents=function(){app.$c.imgUploadBtn.on("click",app.handleImageUploader)},app.meetsRequirements=function(){return app.$c.imgUploadBtn.length},app.handleImageUploader=function(){var btnObj=$(this);return window.uploadID=btnObj.data("target-id"),window.file_frame?(window.file_frame.open(),!0):(window.file_frame=wp.media.frames.file_frame=wp.media({title:btnObj.data("uploader-title"),button:{text:btnObj.data("uploader-btn-txt")},multiple:!1}),window.file_frame.on("select",function(){var attachment=window.file_frame.state().get("selection").first().toJSON();$("#"+window.uploadID).val(attachment.url),window.console.log(attachment)}),void window.file_frame.open())},$(app.init)}(window,jQuery,window.cwv2Admin); \ No newline at end of file