2013-04-26 14:22:55 +02:00
|
|
|
/*globals window, $, jQuery, document, _, postFormats, tinymce, ajaxurl, wp, getUserSetting */
|
|
|
|
|
2013-03-29 04:35:41 +01:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
|
2013-04-26 14:22:55 +02:00
|
|
|
(function ($) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var mediaFrame, insertMediaButton, container, icon, formatField,
|
|
|
|
lastMimeType,
|
|
|
|
classRegex = /\s?\bwp-format-[^ ]+/g,
|
|
|
|
shortHeight = 120,
|
|
|
|
lastHeight = 360,
|
2013-04-25 06:06:06 +02:00
|
|
|
initialFormat = 'standard',
|
2013-04-25 04:07:05 +02:00
|
|
|
shortClass = 'short-format',
|
2013-04-26 14:22:55 +02:00
|
|
|
noTitleFormats = ['status'],
|
|
|
|
noMediaFormats = ['status', 'aside', 'image', 'audio', 'video'],
|
2013-04-25 04:07:05 +02:00
|
|
|
shortContentFormats = ['status', 'aside'],
|
2013-04-26 14:22:55 +02:00
|
|
|
noUIFormats = ['standard', 'chat', 'status', 'aside', 'gallery'];
|
2013-04-25 04:07:05 +02:00
|
|
|
|
2013-03-29 08:46:24 +01:00
|
|
|
function switchFormatClass( format ) {
|
2013-04-26 14:22:55 +02:00
|
|
|
formatField.val( format );
|
|
|
|
|
|
|
|
container
|
|
|
|
.prop( 'className', container.prop( 'className' ).replace( classRegex, '' ) )
|
|
|
|
.addClass( 'wp-format-' + format );
|
|
|
|
|
|
|
|
icon
|
|
|
|
.prop( 'className', icon.prop( 'className' ).replace( classRegex, '' ) )
|
|
|
|
.addClass( 'wp-format-' + format );
|
2013-03-29 08:46:24 +01:00
|
|
|
}
|
|
|
|
|
2013-04-25 06:06:06 +02:00
|
|
|
function resizeContent( format, noAnimate ) {
|
2013-04-26 14:22:55 +02:00
|
|
|
var height, content = $( '#content, #content_ifr' );
|
2013-04-25 06:06:06 +02:00
|
|
|
|
2013-04-26 14:22:55 +02:00
|
|
|
height = content.outerHeight();
|
|
|
|
if ( shortHeight < height ) {
|
2013-04-25 06:06:06 +02:00
|
|
|
lastHeight = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( -1 < $.inArray( format, shortContentFormats ) ) {
|
2013-04-26 14:22:55 +02:00
|
|
|
if ( ! content.hasClass( shortClass ) ) {
|
|
|
|
content.addClass( shortClass );
|
|
|
|
_(content).each(function (elem) {
|
|
|
|
$(elem)[noAnimate ? 'css' : 'animate']( { height : shortHeight } );
|
|
|
|
});
|
2013-04-25 04:07:05 +02:00
|
|
|
}
|
2013-04-25 06:06:06 +02:00
|
|
|
} else {
|
2013-04-26 14:22:55 +02:00
|
|
|
content.removeClass( shortClass ).animate( { height : lastHeight } );
|
2013-04-25 04:07:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-26 14:22:55 +02: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 07:42:54 +01:00
|
|
|
|
2013-04-26 14:22:55 +02:00
|
|
|
elem.addClass( 'active' ).siblings().removeClass( 'active' );
|
2013-03-29 08:46:24 +01:00
|
|
|
|
2013-04-26 14:22:55 +02: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 06:08:46 +02:00
|
|
|
|
2013-04-26 14:22:55 +02: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 06:08:46 +02:00
|
|
|
switchFormatClass( format );
|
2013-04-26 14:22:55 +02:00
|
|
|
fields.slideDown( 400 );
|
2013-04-17 06:08:46 +02:00
|
|
|
});
|
|
|
|
}
|
2013-03-29 08:46:24 +01:00
|
|
|
|
2013-04-25 06:06:06 +02:00
|
|
|
resizeContent( format );
|
2013-04-17 06:08:46 +02:00
|
|
|
postTitle.focus();
|
2013-03-29 08:06:21 +01:00
|
|
|
|
2013-04-26 14:22:55 +02:00
|
|
|
if ( '' === postTitle.val() ) {
|
|
|
|
titlePrompt.removeClass( 'screen-reader-text' );
|
2013-03-29 07:42:54 +01:00
|
|
|
|
2013-04-26 14:22:55 +02:00
|
|
|
postTitle.keydown( function (e) {
|
|
|
|
titlePrompt.addClass( 'screen-reader-text' );
|
|
|
|
$( e.currentTarget ).unbind( e );
|
|
|
|
} );
|
|
|
|
}
|
2013-03-29 07:42:54 +01:00
|
|
|
|
2013-04-26 14:22:55 +02: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 07:42:54 +01:00
|
|
|
|
2013-04-29 22:44:15 +02:00
|
|
|
if ( typeof tinymce !== 'undefined' ) {
|
2013-04-26 14:22:55 +02:00
|
|
|
editor = tinymce.get( 'content' );
|
2013-03-29 07:42:54 +01:00
|
|
|
|
2013-04-17 06:08:46 +02:00
|
|
|
if ( editor ) {
|
|
|
|
body = editor.getBody();
|
|
|
|
body.className = body.className.replace( /\bpost-format-[^ ]+/, '' );
|
|
|
|
editor.dom.addClass( body, 'post-format-' + format );
|
2013-03-29 07:42:54 +01:00
|
|
|
}
|
2013-04-17 06:08:46 +02:00
|
|
|
}
|
|
|
|
|
2013-04-25 07:43:14 +02:00
|
|
|
// If gallery, force it to open to gallery state
|
2013-04-25 07:58:55 +02:00
|
|
|
insertMediaButton.toggleClass( 'gallery', 'gallery' === format );
|
|
|
|
postFormats.currentPostFormat = format;
|
|
|
|
}
|
2013-04-25 07:43:14 +02:00
|
|
|
|
2013-04-26 14:22:55 +02:00
|
|
|
$(function () {
|
|
|
|
container = $( '#post-body-content' );
|
|
|
|
icon = $( '.icon32' );
|
|
|
|
formatField = $( '#post_format' );
|
2013-04-25 07:58:55 +02:00
|
|
|
insertMediaButton = $( '#insert-media-button' ).toggleClass( 'gallery', 'gallery' === postFormats.currentPostFormat );
|
2013-04-25 06:06:06 +02:00
|
|
|
initialFormat = $( '.post-format-options .active' ).data( 'wp-format' );
|
2013-04-26 14:22:55 +02:00
|
|
|
|
2013-04-25 06:06:06 +02:00
|
|
|
if ( -1 < $.inArray( initialFormat, shortContentFormats ) ) {
|
|
|
|
resizeContent( initialFormat, true );
|
|
|
|
}
|
2013-04-17 06:08:46 +02:00
|
|
|
|
2013-04-26 14:22:55 +02:00
|
|
|
$( '#show_post_format_ui' ).on( 'change', function () {
|
|
|
|
$( '.wp-post-format-ui' ).toggleClass( 'no-ui', ! this.checked );
|
2013-04-25 09:28:33 +02:00
|
|
|
$.post( ajaxurl, {
|
|
|
|
action: 'show-post-format-ui',
|
2013-04-26 14:22:55 +02:00
|
|
|
post_type: $( '#post_type' ).val(),
|
2013-04-25 09:28:33 +02:00
|
|
|
show: this.checked ? 1 : 0,
|
2013-04-26 14:22:55 +02:00
|
|
|
nonce: $( '#show_post_format_ui_nonce' ).val()
|
|
|
|
} );
|
|
|
|
} );
|
2013-04-17 06:08:46 +02:00
|
|
|
|
|
|
|
// Post formats selection
|
2013-04-26 14:22:55 +02:00
|
|
|
$( '.post-format-options' ).on( 'click', 'a', function (e) {
|
2013-04-17 06:08:46 +02:00
|
|
|
e.preventDefault();
|
2013-04-26 14:22:55 +02:00
|
|
|
switchFormat( $( e.currentTarget ) );
|
|
|
|
} );
|
2013-03-16 06:57:54 +01:00
|
|
|
|
2013-03-29 07:42:54 +01:00
|
|
|
// Media selection
|
2013-04-26 14:22:55 +02:00
|
|
|
$( '.wp-format-media-select' ).click( function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var $el = $(e.currentTarget), mediaPreview, mime = 'image', $holder, $field;
|
2013-03-29 07:42:54 +01:00
|
|
|
|
2013-04-26 14:22:55 +02:00
|
|
|
$holder = $el.closest( '.wp-format-media-holder' );
|
|
|
|
$field = $( '#wp_format_' + $holder.data( 'format' ) );
|
|
|
|
mime = $holder.data( 'format' );
|
2013-03-16 06:57:54 +01:00
|
|
|
|
2013-03-29 07:42:54 +01:00
|
|
|
// If the media frame already exists, reopen it.
|
2013-04-17 06:08:46 +02:00
|
|
|
if ( mediaFrame && lastMimeType === mime ) {
|
2013-03-29 07:42:54 +01:00
|
|
|
mediaFrame.open();
|
|
|
|
return;
|
|
|
|
}
|
2013-03-29 04:35:41 +01:00
|
|
|
|
2013-03-29 07:42:54 +01:00
|
|
|
lastMimeType = mime;
|
|
|
|
|
2013-04-23 00:11:42 +02:00
|
|
|
mediaFrame = wp.media.frames.formatMedia = wp.media( {
|
2013-03-29 07:42:54 +01:00
|
|
|
button: {
|
2013-04-26 14:22:55 +02:00
|
|
|
text: $el.data( 'update' )
|
2013-04-23 00:11:42 +02:00
|
|
|
},
|
|
|
|
states: [
|
|
|
|
new wp.media.controller.Library({
|
|
|
|
library: wp.media.query( { type: mime } ),
|
2013-04-26 14:22:55 +02:00
|
|
|
title: $el.data( 'choose' ),
|
2013-04-23 00:11:42 +02:00
|
|
|
displaySettings: 'image' === mime
|
|
|
|
})
|
|
|
|
]
|
|
|
|
} );
|
2013-03-29 07:42:54 +01:00
|
|
|
|
2013-04-26 14:22:55 +02:00
|
|
|
mediaPreview = function (attachment) {
|
2013-04-17 07:22:14 +02:00
|
|
|
var w, h, dimensions = '', url = attachment.url, mime = attachment.mime, format = attachment.type;
|
2013-04-12 00:34:05 +02:00
|
|
|
|
|
|
|
if ( 'video' === format ) {
|
2013-04-14 18:43:26 +02:00
|
|
|
if ( attachment.width ) {
|
|
|
|
w = attachment.width;
|
2013-04-26 14:22:55 +02:00
|
|
|
if ( w > 600 ) {
|
2013-04-14 18:43:26 +02:00
|
|
|
w = 600;
|
2013-04-26 14:22:55 +02:00
|
|
|
}
|
2013-04-14 18:43:26 +02:00
|
|
|
dimensions += ' width="' + w + '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( attachment.height ) {
|
|
|
|
h = attachment.height;
|
2013-04-26 14:22:55 +02:00
|
|
|
if ( attachment.width && w < attachment.width ) {
|
2013-04-14 18:43:26 +02:00
|
|
|
h = Math.round( ( h * w ) / attachment.width );
|
2013-04-26 14:22:55 +02:00
|
|
|
}
|
2013-04-14 18:43:26 +02:00
|
|
|
dimensions += ' height="' + h + '"';
|
|
|
|
}
|
2013-04-12 00:34:05 +02:00
|
|
|
}
|
|
|
|
|
2013-04-26 14:22:55 +02:00
|
|
|
$( '#' + format + '-preview' ).remove();
|
2013-03-29 07:42:54 +01:00
|
|
|
$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>' );
|
2013-04-26 14:22:55 +02:00
|
|
|
$( '.wp-' + format + '-shortcode' ).mediaelementplayer();
|
2013-03-29 07:42:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// When an image is selected, run a callback.
|
2013-04-26 14:22:55 +02:00
|
|
|
mediaFrame.on( 'select', function () {
|
2013-03-29 07:42:54 +01:00
|
|
|
// Grab the selected attachment.
|
2013-04-26 14:22:55 +02:00
|
|
|
var w = 0, h = 0, html, attachment = mediaFrame.state().get( 'selection' ).first().toJSON();
|
2013-03-29 07:42:54 +01:00
|
|
|
|
2013-04-26 14:22:55 +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-26 14:22:55 +02:00
|
|
|
mediaPreview( attachment );
|
|
|
|
} else if ( 0 === attachment.mime.indexOf( 'video' ) ) {
|
2013-04-12 00:34:05 +02:00
|
|
|
attachment.src = attachment.url;
|
2013-04-26 14:22:55 +02:00
|
|
|
$field.val( wp.shortcode.string( {
|
2013-04-12 00:34:05 +02:00
|
|
|
tag: 'video',
|
|
|
|
attrs: _.pick( attachment, 'src', 'width', 'height' )
|
2013-04-26 14:22:55 +02:00
|
|
|
} ) );
|
2013-03-29 07:42:54 +01:00
|
|
|
// show one preview at a time
|
2013-04-26 14:22:55 +02:00
|
|
|
mediaPreview( attachment );
|
2013-03-29 07:42:54 +01:00
|
|
|
} else {
|
2013-04-23 00:11:42 +02:00
|
|
|
html = wp.media.string.image({
|
2013-04-26 14:22:55 +02:00
|
|
|
align : getUserSetting( 'align' ),
|
|
|
|
size : getUserSetting( 'imgsize' ),
|
|
|
|
link : getUserSetting( 'urlbutton' )
|
2013-04-23 00:11:42 +02:00
|
|
|
}, attachment);
|
2013-04-26 14:22:55 +02:00
|
|
|
|
2013-03-29 07:42:54 +01:00
|
|
|
// set the hidden input's value
|
2013-04-26 14:22:55 +02:00
|
|
|
$field.val( html );
|
|
|
|
|
|
|
|
$( '#image-preview' ).remove();
|
|
|
|
|
|
|
|
if ( attachment.width ) {
|
2013-04-17 06:08:46 +02:00
|
|
|
w = attachment.width > 600 ? 600 : attachment.width;
|
2013-04-26 14:22:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( attachment.height ) {
|
2013-04-17 06:08:46 +02:00
|
|
|
h = attachment.height;
|
2013-04-26 14:22:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( w < attachment.width ) {
|
2013-04-17 06:08:46 +02:00
|
|
|
h = Math.round( ( h * w ) / attachment.width );
|
2013-04-26 14:22:55 +02:00
|
|
|
}
|
|
|
|
|
2013-04-17 06:08:46 +02: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 14:22:55 +02:00
|
|
|
'</div>'].join( '' ) );
|
2013-03-29 07:42:54 +01:00
|
|
|
}
|
|
|
|
});
|
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
|
|
|
});
|
2013-04-26 14:22:55 +02:00
|
|
|
}( jQuery ) );
|