2013-04-26 08:22:55 -04:00
|
|
|
/*globals window, $, jQuery, document, _, postFormats, tinymce, ajaxurl, wp, getUserSetting */
|
|
|
|
|
2013-03-28 23:35:41 -04:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
(function ($) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var mediaFrame, insertMediaButton, container, icon, formatField,
|
2013-05-09 18:45:14 -04:00
|
|
|
body,
|
2013-04-26 08:22:55 -04:00
|
|
|
lastMimeType,
|
|
|
|
classRegex = /\s?\bwp-format-[^ ]+/g,
|
|
|
|
shortHeight = 120,
|
|
|
|
lastHeight = 360,
|
2013-04-25 00:06:06 -04:00
|
|
|
initialFormat = 'standard',
|
2013-04-24 22:07:05 -04:00
|
|
|
shortClass = 'short-format',
|
2013-04-26 08:22:55 -04:00
|
|
|
noTitleFormats = ['status'],
|
|
|
|
noMediaFormats = ['status', 'aside', 'image', 'audio', 'video'],
|
2013-04-24 22:07:05 -04:00
|
|
|
shortContentFormats = ['status', 'aside'],
|
2013-04-26 08:22:55 -04:00
|
|
|
noUIFormats = ['standard', 'chat', 'status', 'aside', 'gallery'];
|
2013-04-24 22:07:05 -04:00
|
|
|
|
2013-05-09 18:45:14 -04:00
|
|
|
function imageFormatUploadProgress( uploader, file ) {
|
|
|
|
var $bar = $( '#' + uploader.settings.drop_element + ' .media-progress-bar div' );
|
|
|
|
$bar.width( file.percent + '%' );
|
|
|
|
}
|
|
|
|
|
|
|
|
function imageFormatUploadStart( uploader ) {
|
|
|
|
$( '#' + uploader.settings.drop_element + ' .wp-format-media-select' ).append('<div class="media-progress-bar"><div></div></div>');
|
|
|
|
}
|
|
|
|
|
|
|
|
function imageFormatUploadError() {
|
|
|
|
$( '.media-progress-bar', $('.wp-format-media-holder[data-format=image]') ).remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
function imageFormatUploadSuccess( attachment ) {
|
|
|
|
var $holder, $field, html = wp.media.string.image({
|
|
|
|
size : 'full',
|
|
|
|
align : false,
|
|
|
|
link : getUserSetting( 'urlbutton' )
|
|
|
|
}, attachment.attributes );
|
|
|
|
|
|
|
|
$holder = $('.wp-format-media-holder[data-format=image]');
|
|
|
|
$( '.media-progress-bar', $holder ).remove();
|
|
|
|
|
|
|
|
if ( 'image' !== attachment.attributes.type )
|
|
|
|
return;
|
|
|
|
|
|
|
|
$field = $( '#wp_format_' + $holder.data( 'format' ) );
|
|
|
|
|
|
|
|
// set the hidden input's value
|
|
|
|
$field.val( html );
|
|
|
|
|
|
|
|
$( '#image-preview' ).remove();
|
|
|
|
|
|
|
|
$holder.parent().prepend( ['<div id="image-preview" class="wp-format-media-preview">',
|
|
|
|
'<img src="', attachment.get('url'), '"',
|
|
|
|
attachment.get('width') ? ' width="' + attachment.get('width') + '"' : '',
|
|
|
|
attachment.get('height') ? ' height="' + attachment.get('height') + '"' : '',
|
|
|
|
' />',
|
|
|
|
'</div>'].join( '' ) );
|
|
|
|
}
|
|
|
|
|
2013-05-10 14:49:05 -04:00
|
|
|
function imageFormatUploadFilesAdded( uploader, files ) {
|
|
|
|
$.each( files, function( i, file ) {
|
|
|
|
if ( i > 0 )
|
|
|
|
uploader.removeFile(file);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-05-09 18:45:14 -04:00
|
|
|
var uploader = {
|
|
|
|
dropzone: $('.wp-format-media-holder[data-format=image]'),
|
|
|
|
success: imageFormatUploadSuccess,
|
2013-05-10 14:49:05 -04:00
|
|
|
error: imageFormatUploadError,
|
|
|
|
plupload: {
|
|
|
|
runtimes: 'html5',
|
|
|
|
filters: [ {title: 'Image', extensions: 'jpg,jpeg,gif,png'} ]
|
|
|
|
},
|
2013-05-09 18:45:14 -04:00
|
|
|
params: {}
|
|
|
|
};
|
|
|
|
uploader = new wp.Uploader( uploader );
|
|
|
|
uploader.uploader.bind( 'BeforeUpload', imageFormatUploadStart );
|
|
|
|
uploader.uploader.bind( 'UploadProgress', imageFormatUploadProgress );
|
2013-05-10 14:49:05 -04:00
|
|
|
uploader.uploader.bind( 'FilesAdded', imageFormatUploadFilesAdded );
|
2013-05-09 18:45:14 -04:00
|
|
|
|
2013-03-29 03:46:24 -04:00
|
|
|
function switchFormatClass( format ) {
|
2013-04-26 08:22:55 -04:00
|
|
|
formatField.val( format );
|
|
|
|
|
2013-05-09 18:45:14 -04:00
|
|
|
$.each( [ container, icon, body ], function(i, thing) {
|
|
|
|
thing.prop( 'className', thing.prop( 'className' ).replace( classRegex, '' ) )
|
2013-04-26 08:22:55 -04:00
|
|
|
.addClass( 'wp-format-' + format );
|
2013-05-09 18:45:14 -04:00
|
|
|
});
|
2013-03-29 03:46:24 -04:00
|
|
|
}
|
|
|
|
|
2013-04-25 00:06:06 -04:00
|
|
|
function resizeContent( format, noAnimate ) {
|
2013-04-26 08:22:55 -04:00
|
|
|
var height, content = $( '#content, #content_ifr' );
|
2013-04-25 00:06:06 -04:00
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
height = content.outerHeight();
|
|
|
|
if ( shortHeight < height ) {
|
2013-04-25 00:06:06 -04:00
|
|
|
lastHeight = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( -1 < $.inArray( format, shortContentFormats ) ) {
|
2013-04-26 08:22:55 -04:00
|
|
|
if ( ! content.hasClass( shortClass ) ) {
|
|
|
|
content.addClass( shortClass );
|
|
|
|
_(content).each(function (elem) {
|
|
|
|
$(elem)[noAnimate ? 'css' : 'animate']( { height : shortHeight } );
|
|
|
|
});
|
2013-04-24 22:07:05 -04:00
|
|
|
}
|
2013-04-25 00:06:06 -04:00
|
|
|
} else {
|
2013-04-26 08:22:55 -04:00
|
|
|
content.removeClass( shortClass ).animate( { height : lastHeight } );
|
2013-04-24 22:07:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
function switchFormat(elem) {
|
|
|
|
var editor, body, formatTo, formatFrom,
|
|
|
|
format = elem.data( 'wp-format' ),
|
|
|
|
titlePrompt = $( '#title-prompt-text' ),
|
|
|
|
description = $( '.post-format-description' ),
|
|
|
|
postTitle = $( '#title'),
|
|
|
|
fields = $( '.post-formats-fields' ),
|
|
|
|
tinyIcon = $( '.post-format-change span.icon' );
|
|
|
|
|
|
|
|
if ( format === postFormats.currentPostFormat ) {
|
|
|
|
return;
|
|
|
|
}
|
2013-03-29 02:42:54 -04:00
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
elem.addClass( 'active' ).siblings().removeClass( 'active' );
|
2013-03-29 03:46:24 -04:00
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
// Animate the media button going away or coming back
|
|
|
|
formatTo = -1 < $.inArray( format, noMediaFormats );
|
|
|
|
formatFrom = -1 < $.inArray( postFormats.currentPostFormat, noMediaFormats );
|
|
|
|
if ( formatFrom ? !formatTo : formatTo ) { // XOR
|
|
|
|
insertMediaButton.fadeToggle( 200 ).css( 'display', 'inline-block' );
|
|
|
|
}
|
|
|
|
// Animate the title going away or coming back
|
|
|
|
formatTo = -1 < $.inArray( format, noTitleFormats );
|
|
|
|
formatFrom = -1 < $.inArray( postFormats.currentPostFormat, noTitleFormats );
|
|
|
|
if ( formatFrom ? !formatTo : formatTo ) { // XOR
|
|
|
|
$( '#titlewrap' ).fadeToggle( 200 );
|
|
|
|
}
|
2013-04-17 00:08:46 -04:00
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
// Animate the fields moving going away or coming in
|
|
|
|
formatTo = -1 < $.inArray( format, noUIFormats );
|
|
|
|
formatFrom = -1 < $.inArray( postFormats.currentPostFormat, noUIFormats );
|
|
|
|
if ( formatTo && formatFrom ) { // To/from have no UI. No slide.
|
|
|
|
switchFormatClass( format );
|
|
|
|
fields.hide();
|
|
|
|
} else if ( formatFrom ) { // Only destination has UI. Slide down.
|
|
|
|
fields.hide();
|
|
|
|
switchFormatClass( format );
|
|
|
|
fields.slideDown( 400 );
|
|
|
|
} else if ( formatTo ) { // Only source has UI. Slide up.
|
|
|
|
fields.slideUp( 200, function(){
|
|
|
|
switchFormatClass( format );
|
|
|
|
});
|
|
|
|
} else { // Both have UI. Slide both ways.
|
|
|
|
fields.slideUp( 200, function(){
|
2013-04-17 00:08:46 -04:00
|
|
|
switchFormatClass( format );
|
2013-04-26 08:22:55 -04:00
|
|
|
fields.slideDown( 400 );
|
2013-04-17 00:08:46 -04:00
|
|
|
});
|
|
|
|
}
|
2013-03-29 03:46:24 -04:00
|
|
|
|
2013-04-25 00:06:06 -04:00
|
|
|
resizeContent( format );
|
2013-04-17 00:08:46 -04:00
|
|
|
postTitle.focus();
|
2013-03-29 03:06:21 -04:00
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
if ( '' === postTitle.val() ) {
|
|
|
|
titlePrompt.removeClass( 'screen-reader-text' );
|
2013-03-29 02:42:54 -04:00
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
postTitle.keydown( function (e) {
|
|
|
|
titlePrompt.addClass( 'screen-reader-text' );
|
|
|
|
$( e.currentTarget ).unbind( e );
|
|
|
|
} );
|
|
|
|
}
|
2013-03-29 02:42:54 -04:00
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
// Update description line
|
|
|
|
description.html( elem.data( 'description' ) );
|
|
|
|
tinyIcon
|
|
|
|
.show()
|
|
|
|
.prop( 'className', tinyIcon.prop( 'className' ).replace( classRegex, '' ) )
|
|
|
|
.addClass( 'wp-format-' + format );
|
|
|
|
|
|
|
|
if ( description.not( ':visible' ) ) {
|
|
|
|
description.slideDown( 'fast' );
|
|
|
|
}
|
2013-03-29 02:42:54 -04:00
|
|
|
|
2013-04-29 16:44:15 -04:00
|
|
|
if ( typeof tinymce !== 'undefined' ) {
|
2013-04-26 08:22:55 -04:00
|
|
|
editor = tinymce.get( 'content' );
|
2013-03-29 02:42:54 -04:00
|
|
|
|
2013-04-17 00:08:46 -04:00
|
|
|
if ( editor ) {
|
|
|
|
body = editor.getBody();
|
|
|
|
body.className = body.className.replace( /\bpost-format-[^ ]+/, '' );
|
|
|
|
editor.dom.addClass( body, 'post-format-' + format );
|
2013-03-29 02:42:54 -04:00
|
|
|
}
|
2013-04-17 00:08:46 -04:00
|
|
|
}
|
|
|
|
|
2013-04-25 01:43:14 -04:00
|
|
|
// If gallery, force it to open to gallery state
|
2013-04-25 01:58:55 -04:00
|
|
|
insertMediaButton.toggleClass( 'gallery', 'gallery' === format );
|
|
|
|
postFormats.currentPostFormat = format;
|
|
|
|
}
|
2013-04-25 01:43:14 -04:00
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
$(function () {
|
2013-05-09 18:45:14 -04:00
|
|
|
body = $( 'body' );
|
2013-04-26 08:22:55 -04:00
|
|
|
container = $( '#post-body-content' );
|
|
|
|
icon = $( '.icon32' );
|
|
|
|
formatField = $( '#post_format' );
|
2013-04-25 01:58:55 -04:00
|
|
|
insertMediaButton = $( '#insert-media-button' ).toggleClass( 'gallery', 'gallery' === postFormats.currentPostFormat );
|
2013-04-25 00:06:06 -04:00
|
|
|
initialFormat = $( '.post-format-options .active' ).data( 'wp-format' );
|
2013-04-26 08:22:55 -04:00
|
|
|
|
2013-04-25 00:06:06 -04:00
|
|
|
if ( -1 < $.inArray( initialFormat, shortContentFormats ) ) {
|
|
|
|
resizeContent( initialFormat, true );
|
|
|
|
}
|
2013-04-17 00:08:46 -04:00
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
$( '#show_post_format_ui' ).on( 'change', function () {
|
2013-05-15 16:43:09 -04:00
|
|
|
body.toggleClass( 'wp-post-format-show-ui', this.checked );
|
|
|
|
|
|
|
|
// Reset the display properties of possibly hidden items.
|
|
|
|
insertMediaButton.css( 'display', '' );
|
|
|
|
$( '#titlewrap' ).css( 'display', '' );
|
|
|
|
|
2013-04-25 03:28:33 -04:00
|
|
|
$.post( ajaxurl, {
|
|
|
|
action: 'show-post-format-ui',
|
2013-04-26 08:22:55 -04:00
|
|
|
post_type: $( '#post_type' ).val(),
|
2013-04-25 03:28:33 -04:00
|
|
|
show: this.checked ? 1 : 0,
|
2013-04-26 08:22:55 -04:00
|
|
|
nonce: $( '#show_post_format_ui_nonce' ).val()
|
|
|
|
} );
|
|
|
|
} );
|
2013-04-17 00:08:46 -04:00
|
|
|
|
|
|
|
// Post formats selection
|
2013-04-26 08:22:55 -04:00
|
|
|
$( '.post-format-options' ).on( 'click', 'a', function (e) {
|
2013-04-17 00:08:46 -04:00
|
|
|
e.preventDefault();
|
2013-04-26 08:22:55 -04:00
|
|
|
switchFormat( $( e.currentTarget ) );
|
|
|
|
} );
|
2013-03-16 01:57:54 -04:00
|
|
|
|
2013-05-09 18:45:14 -04:00
|
|
|
// Toggle select/upload and URL/HTML for images
|
|
|
|
$( '.use-url-or-html' ).on( 'click', 'a', function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
$( '.wp-format-media-holder, .wp-format-image-textarea' ).toggle();
|
|
|
|
$(this).closest( 'p' ).find( 'span' ).toggle();
|
|
|
|
});
|
|
|
|
|
2013-03-29 02:42:54 -04:00
|
|
|
// Media selection
|
2013-04-26 08:22:55 -04:00
|
|
|
$( '.wp-format-media-select' ).click( function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var $el = $(e.currentTarget), mediaPreview, mime = 'image', $holder, $field;
|
2013-03-29 02:42:54 -04:00
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
$holder = $el.closest( '.wp-format-media-holder' );
|
|
|
|
$field = $( '#wp_format_' + $holder.data( 'format' ) );
|
|
|
|
mime = $holder.data( 'format' );
|
2013-03-16 01:57:54 -04:00
|
|
|
|
2013-03-29 02:42:54 -04:00
|
|
|
// If the media frame already exists, reopen it.
|
2013-04-17 00:08:46 -04:00
|
|
|
if ( mediaFrame && lastMimeType === mime ) {
|
2013-03-29 02:42:54 -04:00
|
|
|
mediaFrame.open();
|
|
|
|
return;
|
|
|
|
}
|
2013-03-28 23:35:41 -04:00
|
|
|
|
2013-03-29 02:42:54 -04:00
|
|
|
lastMimeType = mime;
|
|
|
|
|
2013-04-22 18:11:42 -04:00
|
|
|
mediaFrame = wp.media.frames.formatMedia = wp.media( {
|
2013-03-29 02:42:54 -04:00
|
|
|
button: {
|
2013-04-26 08:22:55 -04:00
|
|
|
text: $el.data( 'update' )
|
2013-04-22 18:11:42 -04:00
|
|
|
},
|
|
|
|
states: [
|
|
|
|
new wp.media.controller.Library({
|
|
|
|
library: wp.media.query( { type: mime } ),
|
2013-04-26 08:22:55 -04:00
|
|
|
title: $el.data( 'choose' ),
|
2013-04-22 18:11:42 -04:00
|
|
|
displaySettings: 'image' === mime
|
|
|
|
})
|
|
|
|
]
|
|
|
|
} );
|
2013-03-29 02:42:54 -04:00
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
mediaPreview = function (attachment) {
|
2013-04-17 01:22:14 -04:00
|
|
|
var w, h, dimensions = '', url = attachment.url, mime = attachment.mime, format = attachment.type;
|
2013-04-11 18:34:05 -04:00
|
|
|
|
|
|
|
if ( 'video' === format ) {
|
2013-04-14 12:43:26 -04:00
|
|
|
if ( attachment.width ) {
|
|
|
|
w = attachment.width;
|
2013-04-26 08:22:55 -04:00
|
|
|
if ( w > 600 ) {
|
2013-04-14 12:43:26 -04:00
|
|
|
w = 600;
|
2013-04-26 08:22:55 -04:00
|
|
|
}
|
2013-04-14 12:43:26 -04:00
|
|
|
dimensions += ' width="' + w + '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( attachment.height ) {
|
|
|
|
h = attachment.height;
|
2013-04-26 08:22:55 -04:00
|
|
|
if ( attachment.width && w < attachment.width ) {
|
2013-04-14 12:43:26 -04:00
|
|
|
h = Math.round( ( h * w ) / attachment.width );
|
2013-04-26 08:22:55 -04:00
|
|
|
}
|
2013-04-14 12:43:26 -04:00
|
|
|
dimensions += ' height="' + h + '"';
|
|
|
|
}
|
2013-04-11 18:34:05 -04:00
|
|
|
}
|
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
$( '#' + format + '-preview' ).remove();
|
2013-03-29 02:42:54 -04:00
|
|
|
$holder.parent().prepend( '<div id="' + format + '-preview" class="wp-format-media-preview">' +
|
2013-04-11 18:34:05 -04:00
|
|
|
'<' + format + dimensions + ' class="wp-' + format + '-shortcode" controls="controls" preload="none">' +
|
2013-03-29 02:42:54 -04:00
|
|
|
'<source type="' + mime + '" src="' + url + '" />' +
|
|
|
|
'</' + format + '></div>' );
|
2013-04-26 08:22:55 -04:00
|
|
|
$( '.wp-' + format + '-shortcode' ).mediaelementplayer();
|
2013-03-29 02:42:54 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// When an image is selected, run a callback.
|
2013-04-26 08:22:55 -04:00
|
|
|
mediaFrame.on( 'select', function () {
|
2013-03-29 02:42:54 -04:00
|
|
|
// Grab the selected attachment.
|
2013-04-26 08:22:55 -04:00
|
|
|
var w = 0, h = 0, html, attachment = mediaFrame.state().get( 'selection' ).first().toJSON();
|
2013-03-29 02:42:54 -04:00
|
|
|
|
2013-04-26 08:22:55 -04:00
|
|
|
if ( 0 === attachment.mime.indexOf( 'audio' ) ) {
|
|
|
|
$field.val( attachment.url );
|
2013-03-29 02:42:54 -04:00
|
|
|
// show one preview at a time
|
2013-04-26 08:22:55 -04:00
|
|
|
mediaPreview( attachment );
|
|
|
|
} else if ( 0 === attachment.mime.indexOf( 'video' ) ) {
|
2013-04-11 18:34:05 -04:00
|
|
|
attachment.src = attachment.url;
|
2013-04-26 08:22:55 -04:00
|
|
|
$field.val( wp.shortcode.string( {
|
2013-04-11 18:34:05 -04:00
|
|
|
tag: 'video',
|
|
|
|
attrs: _.pick( attachment, 'src', 'width', 'height' )
|
2013-04-26 08:22:55 -04:00
|
|
|
} ) );
|
2013-03-29 02:42:54 -04:00
|
|
|
// show one preview at a time
|
2013-04-26 08:22:55 -04:00
|
|
|
mediaPreview( attachment );
|
2013-03-29 02:42:54 -04:00
|
|
|
} else {
|
2013-04-22 18:11:42 -04:00
|
|
|
html = wp.media.string.image({
|
2013-05-09 18:45:14 -04:00
|
|
|
size: 'full',
|
|
|
|
align : false,
|
2013-04-26 08:22:55 -04:00
|
|
|
link : getUserSetting( 'urlbutton' )
|
2013-04-22 18:11:42 -04:00
|
|
|
}, attachment);
|
2013-04-26 08:22:55 -04:00
|
|
|
|
2013-03-29 02:42:54 -04:00
|
|
|
// set the hidden input's value
|
2013-04-26 08:22:55 -04:00
|
|
|
$field.val( html );
|
|
|
|
|
|
|
|
$( '#image-preview' ).remove();
|
|
|
|
|
|
|
|
if ( attachment.width ) {
|
2013-04-17 00:08:46 -04:00
|
|
|
w = attachment.width > 600 ? 600 : attachment.width;
|
2013-04-26 08:22:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( attachment.height ) {
|
2013-04-17 00:08:46 -04:00
|
|
|
h = attachment.height;
|
2013-04-26 08:22:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( w < attachment.width ) {
|
2013-04-17 00:08:46 -04:00
|
|
|
h = Math.round( ( h * w ) / attachment.width );
|
2013-04-26 08:22:55 -04:00
|
|
|
}
|
|
|
|
|
2013-04-17 00:08:46 -04:00
|
|
|
$holder.parent().prepend( ['<div id="image-preview" class="wp-format-media-preview">',
|
|
|
|
'<img src="', attachment.url, '"',
|
|
|
|
w ? ' width="' + w + '"' : '',
|
|
|
|
h ? ' height="' + h + '"' : '',
|
|
|
|
' />',
|
2013-04-26 08:22:55 -04:00
|
|
|
'</div>'].join( '' ) );
|
2013-03-29 02:42:54 -04:00
|
|
|
}
|
|
|
|
});
|
2013-02-18 14:11:24 -05:00
|
|
|
|
2013-03-28 23:35:41 -04:00
|
|
|
mediaFrame.open();
|
|
|
|
});
|
2013-03-29 02:33:55 -04:00
|
|
|
});
|
2013-04-26 08:22:55 -04:00
|
|
|
}( jQuery ) );
|