/* global odinAdminParams */ (function ( $ ) { 'use strict'; /** * Theme Options and Metaboxes. */ $( function () { /** * Image field. */ $( '.odin-upload-image .button' ).on( 'click', function ( e ) { e.preventDefault(); var uploadFrame, uploadInput = $(this).siblings( '.image' ), uploadPreview = $(this).siblings( '.preview' ); // If the media frame already exists, reopen it. if ( uploadFrame ) { uploadFrame.open(); return; } // Create the media frame. uploadFrame = wp.media.frames.downloadable_file = wp.media({ title: odinAdminParams.uploadTitle, button: { text: odinAdminParams.uploadButton }, multiple: false, library: { type: 'image' } }); uploadFrame.on( 'select', function () { var attachment = uploadFrame.state().get( 'selection' ).first().toJSON(); uploadPreview.attr( 'src', attachment.url ); uploadInput.val( attachment.id ); }); // Finally, open the modal. uploadFrame.open(); }); $( '.odin-upload-image .delete' ).click( function ( e ) { e.preventDefault(); var wrapper = $( this ).parents( '.odin-upload-image' ), defaultImage = $( '.default-image', wrapper ).text(); $( '.image', wrapper ).val( '' ); $( '.preview', wrapper ).attr( 'src', defaultImage ); }); /** * Upload. */ $( '.odin-upload-button' ).on( 'click', function ( e ) { e.preventDefault(); var uploadFrame, uploadInput = $( this ).prev( 'input' ); // If the media frame already exists, reopen it. if ( uploadFrame ) { uploadFrame.open(); return; } // Create the media frame. uploadFrame = wp.media.frames.downloadable_file = wp.media({ title: odinAdminParams.uploadTitle, button: { text: odinAdminParams.uploadButton }, multiple: false }); uploadFrame.on( 'select', function () { var attachment = uploadFrame.state().get( 'selection').first().toJSON(); uploadInput.val( attachment.url ); }); // Finally, open the modal. uploadFrame.open(); }); /** * Color Picker. */ $( '.odin-color-field' ).wpColorPicker(); /** * Image plupload adds. */ $( '.odin-gallery-container' ).on( 'click', '.odin-gallery-add', function ( e ) { e.preventDefault(); var galleryFrame, galleryWrap = $( this ).parent( '.odin-gallery-container' ), imageGalleryIds = $( '.odin-gallery-field', galleryWrap ), images = $( 'ul.odin-gallery-images', galleryWrap ), attachmentIds = imageGalleryIds.val(); // If the media frame already exists, reopen it. if ( galleryFrame ) { galleryFrame.open(); return; } // Create the media frame. galleryFrame = wp.media.frames.downloadable_file = wp.media({ title: odinAdminParams.galleryTitle, button: { text: odinAdminParams.galleryButton }, multiple: true, library: { type: 'image' } }); // When an image is selected, run a callback. galleryFrame.on( 'select', function () { var selection = galleryFrame.state().get( 'selection' ); selection.map( function ( attachment ) { attachment = attachment.toJSON(); if ( attachment.id ) { attachmentIds = attachmentIds ? attachmentIds + ',' + attachment.id : attachment.id; images.append( '