2013-03-29 04:35:41 +01:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
|
|
|
|
(function($) {
|
2013-03-29 13:17:52 +01:00
|
|
|
var container, mediaFrame, lastMimeType, lastMenu, mediaPreview, noUIFormats = ['standard', 'chat', 'status', 'aside', 'gallery'];
|
2013-03-29 08:46:24 +01:00
|
|
|
|
|
|
|
function switchFormatClass( format ) {
|
|
|
|
container.get(0).className = container.get(0).className.replace( /\bwp-format-[^ ]+/, '' );
|
|
|
|
container.addClass('wp-format-' + format);
|
|
|
|
}
|
|
|
|
|
2013-03-29 07:42:54 +01:00
|
|
|
$(function(){
|
2013-03-29 08:06:21 +01:00
|
|
|
var $container = $( '.post-formats-fields' );
|
|
|
|
|
2013-03-29 07:42:54 +01:00
|
|
|
// Post formats selection
|
2013-03-29 08:06:21 +01:00
|
|
|
$('.post-format-options').on( 'click', 'a', function(e){
|
2013-03-29 08:46:24 +01:00
|
|
|
e.preventDefault();
|
2013-03-29 07:42:54 +01:00
|
|
|
var $this = $(this), editor, body,
|
|
|
|
parent = $this.parent(),
|
|
|
|
format = $this.data('wp-format'),
|
|
|
|
description = $('.post-format-description');
|
|
|
|
|
2013-03-29 08:46:24 +01:00
|
|
|
if ( typeof container === 'undefined' )
|
|
|
|
container = $('#post-body-content');
|
|
|
|
|
|
|
|
// Already on this post format. Bail.
|
|
|
|
if ( format === postFormats.currentPostFormat )
|
|
|
|
return;
|
|
|
|
|
2013-03-29 07:42:54 +01:00
|
|
|
parent.find('a.active').removeClass('active');
|
|
|
|
$this.addClass('active');
|
|
|
|
$('#icon-edit').removeClass(postFormats.currentPostFormat).addClass(format);
|
|
|
|
$('#post_format').val(format);
|
|
|
|
|
2013-03-29 08:46:24 +01:00
|
|
|
if ( -1 < $.inArray( format, noUIFormats ) && -1 < $.inArray( postFormats.currentPostFormat, noUIFormats ) ) {
|
|
|
|
switchFormatClass( format ); // No slide
|
|
|
|
} else {
|
|
|
|
$container.slideUp( 200, function(){
|
|
|
|
switchFormatClass( format );
|
|
|
|
$container.slideDown( 400 );
|
|
|
|
});
|
|
|
|
}
|
2013-03-29 08:06:21 +01:00
|
|
|
|
2013-03-29 07:42:54 +01:00
|
|
|
$('#title').focus();
|
|
|
|
|
|
|
|
// Update description line
|
|
|
|
description.html($this.data('description'));
|
|
|
|
|
|
|
|
if (description.not(':visible'))
|
|
|
|
description.slideDown('fast');
|
|
|
|
|
|
|
|
if ( typeof tinymce != 'undefined' ) {
|
|
|
|
editor = tinymce.get('content');
|
|
|
|
|
|
|
|
if ( editor ) {
|
|
|
|
body = editor.getBody();
|
|
|
|
body.className = body.className.replace( /\bpost-format-[^ ]+/, '' );
|
|
|
|
editor.dom.addClass( body, 'post-format-' + format );
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 20:11:24 +01:00
|
|
|
|
2013-03-29 07:42:54 +01:00
|
|
|
postFormats.currentPostFormat = format;
|
2013-03-29 08:06:21 +01:00
|
|
|
}).on('mouseenter focusin', 'a', function () {
|
2013-03-29 07:42:54 +01:00
|
|
|
$('.post-format-tip').html( $(this).prop('title') );
|
2013-03-29 08:06:21 +01:00
|
|
|
}).on('mouseleave focusout', 'a', function () {
|
2013-03-29 07:42:54 +01:00
|
|
|
$('.post-format-tip').html( $('.post-format-options a.active').prop('title') );
|
|
|
|
});
|
2013-03-16 06:57:54 +01:00
|
|
|
|
2013-03-29 07:42:54 +01:00
|
|
|
// Media selection
|
|
|
|
$('.wp-format-media-select').click(function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
var $el = $(this), $holder, $field, mime = 'image', menu = '',
|
|
|
|
$holder = $el.closest('.wp-format-media-holder'),
|
|
|
|
$field = $( '#wp_format_' + $holder.data('format') );
|
|
|
|
|
|
|
|
switch ( $holder.data('format') ) {
|
|
|
|
case 'audio':
|
|
|
|
mime = 'audio';
|
|
|
|
break;
|
|
|
|
case 'video':
|
|
|
|
mime = 'video';
|
|
|
|
break;
|
2013-03-16 06:57:54 +01:00
|
|
|
}
|
|
|
|
|
2013-03-29 07:42:54 +01:00
|
|
|
// If the media frame already exists, reopen it.
|
|
|
|
if ( mediaFrame && lastMimeType === mime && lastMenu === menu ) {
|
|
|
|
mediaFrame.open();
|
|
|
|
return;
|
|
|
|
}
|
2013-03-29 04:35:41 +01:00
|
|
|
|
2013-03-29 07:42:54 +01:00
|
|
|
lastMimeType = mime;
|
|
|
|
lastMenu = menu;
|
|
|
|
|
|
|
|
// Create the media frame.
|
|
|
|
mediaFrame = wp.media.frames.formatMedia = wp.media({
|
|
|
|
// Set the title of the modal.
|
|
|
|
title: $el.data('choose'),
|
|
|
|
|
|
|
|
// Set the menu sidebar of the modal, if applicable
|
|
|
|
toolbar: menu,
|
|
|
|
|
|
|
|
// Tell the modal to show only items matching the current mime type.
|
|
|
|
library: {
|
|
|
|
type: mime
|
|
|
|
},
|
|
|
|
|
|
|
|
// Customize the submit button.
|
|
|
|
button: {
|
|
|
|
// Set the text of the button.
|
|
|
|
text: $el.data('update')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-04-12 00:34:05 +02:00
|
|
|
mediaPreview = function (attachment) {
|
2013-04-14 18:43:26 +02:00
|
|
|
var w, h, dimensions = '', url = attachment.url,
|
2013-04-12 00:34:05 +02:00
|
|
|
mime = attachment.mime,
|
|
|
|
format = attachment.type;
|
|
|
|
|
|
|
|
if ( 'video' === format ) {
|
2013-04-14 18:43:26 +02:00
|
|
|
if ( attachment.width ) {
|
|
|
|
w = attachment.width;
|
|
|
|
if ( w > 600 )
|
|
|
|
w = 600;
|
|
|
|
dimensions += ' width="' + w + '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( attachment.height ) {
|
|
|
|
h = attachment.height;
|
|
|
|
if ( attachment.width && w < attachment.width )
|
|
|
|
h = Math.round( ( h * w ) / attachment.width );
|
|
|
|
dimensions += ' height="' + h + '"';
|
|
|
|
}
|
2013-04-12 00:34:05 +02:00
|
|
|
}
|
|
|
|
|
2013-03-29 07:42:54 +01:00
|
|
|
$('#' + format + '-preview').remove();
|
|
|
|
$holder.parent().prepend( '<div id="' + format + '-preview" class="wp-format-media-preview">' +
|
2013-04-12 00:34:05 +02:00
|
|
|
'<' + format + dimensions + ' class="wp-' + format + '-shortcode" controls="controls" preload="none">' +
|
2013-03-29 07:42:54 +01:00
|
|
|
'<source type="' + mime + '" src="' + url + '" />' +
|
|
|
|
'</' + format + '></div>' );
|
|
|
|
$('.wp-' + format + '-shortcode').mediaelementplayer();
|
|
|
|
};
|
|
|
|
|
|
|
|
// When an image is selected, run a callback.
|
|
|
|
mediaFrame.on( 'select', function () {
|
|
|
|
// Grab the selected attachment.
|
2013-04-12 00:34:05 +02:00
|
|
|
var attachment = mediaFrame.state().get('selection').first().toJSON();
|
2013-03-29 07:42:54 +01:00
|
|
|
|
2013-04-12 00:34:05 +02:00
|
|
|
if ( 0 === attachment.mime.indexOf('audio') ) {
|
|
|
|
$field.val(attachment.url);
|
2013-03-29 07:42:54 +01:00
|
|
|
// show one preview at a time
|
2013-04-12 00:34:05 +02:00
|
|
|
mediaPreview(attachment);
|
|
|
|
} else if ( 0 === attachment.mime.indexOf('video') ) {
|
|
|
|
attachment.src = attachment.url;
|
|
|
|
$field.val(wp.shortcode.string({
|
|
|
|
tag: 'video',
|
|
|
|
attrs: _.pick( attachment, 'src', 'width', 'height' )
|
|
|
|
}));
|
2013-03-29 07:42:54 +01:00
|
|
|
// show one preview at a time
|
2013-04-12 00:34:05 +02:00
|
|
|
mediaPreview(attachment);
|
2013-03-29 07:42:54 +01:00
|
|
|
} else {
|
|
|
|
// set the hidden input's value
|
2013-04-12 00:34:05 +02:00
|
|
|
$field.val(attachment.id);
|
2013-03-29 07:42:54 +01:00
|
|
|
// Show the image in the placeholder
|
2013-04-12 00:34:05 +02:00
|
|
|
$el.html('<img src="' + attachment.url + '" />');
|
2013-03-29 07:42:54 +01:00
|
|
|
$holder.removeClass('empty').show();
|
|
|
|
}
|
|
|
|
});
|
2013-02-18 20:11:24 +01:00
|
|
|
|
2013-03-29 04:35:41 +01:00
|
|
|
mediaFrame.open();
|
|
|
|
});
|
2013-03-29 07:33:55 +01:00
|
|
|
});
|
|
|
|
})(jQuery);
|