wp/wp-includes/js/media-editor.js
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
    28 			attrs[ key ] = false;
    28 			attrs[ key ] = false;
    29 		}
    29 		}
    30 		return attrs[ key ];
    30 		return attrs[ key ];
    31 	};
    31 	};
    32 
    32 
    33 	/**
    33 	/** @namespace wp.media.string */
    34 	 * wp.media.string
       
    35 	 * @namespace
       
    36 	 */
       
    37 	wp.media.string = {
    34 	wp.media.string = {
    38 		/**
    35 		/**
    39 		 * Joins the `props` and `attachment` objects,
    36 		 * Joins the `props` and `attachment` objects,
    40 		 * outputting the proper object format based on the
    37 		 * outputting the proper object format based on the
    41 		 * attachment's type.
    38 		 * attachment's type.
    42 		 *
    39 		 *
    43 		 * @global wp.media.view.settings
       
    44 		 * @global getUserSetting()
       
    45 		 *
       
    46 		 * @param {Object} [props={}] Attachment details (align, link, size, etc).
    40 		 * @param {Object} [props={}] Attachment details (align, link, size, etc).
    47 		 * @param {Object} attachment The attachment object, media version of Post.
    41 		 * @param {Object} attachment The attachment object, media version of Post.
    48 		 * @returns {Object} Joined props
    42 		 * @returns {Object} Joined props
    49 		 */
    43 		 */
    50 		props: function( props, attachment ) {
    44 		props: function( props, attachment ) {
    51 			var link, linkUrl, size, sizes, fallbacks,
    45 			var link, linkUrl, size, sizes,
    52 				defaultProps = wp.media.view.settings.defaultProps;
    46 				defaultProps = wp.media.view.settings.defaultProps;
    53 
       
    54 			// Final fallbacks run after all processing has been completed.
       
    55 			fallbacks = function( props ) {
       
    56 				// Generate alt fallbacks and strip tags.
       
    57 				if ( 'image' === props.type && ! props.alt ) {
       
    58 					props.alt = props.caption || props.title || '';
       
    59 					props.alt = props.alt.replace( /<\/?[^>]+>/g, '' );
       
    60 					props.alt = props.alt.replace( /[\r\n]+/g, ' ' );
       
    61 				}
       
    62 
       
    63 				return props;
       
    64 			};
       
    65 
    47 
    66 			props = props ? _.clone( props ) : {};
    48 			props = props ? _.clone( props ) : {};
    67 
    49 
    68 			if ( attachment && attachment.type ) {
    50 			if ( attachment && attachment.type ) {
    69 				props.type = attachment.type;
    51 				props.type = attachment.type;
    78 				});
    60 				});
    79 			}
    61 			}
    80 
    62 
    81 			// All attachment-specific settings follow.
    63 			// All attachment-specific settings follow.
    82 			if ( ! attachment ) {
    64 			if ( ! attachment ) {
    83 				return fallbacks( props );
    65 				return props;
    84 			}
    66 			}
    85 
    67 
    86 			props.title = props.title || attachment.title;
    68 			props.title = props.title || attachment.title;
    87 
    69 
    88 			link = props.link || defaultProps.link || getUserSetting( 'urlbutton', 'file' );
    70 			link = props.link || defaultProps.link || getUserSetting( 'urlbutton', 'file' );
   114 			} else {
    96 			} else {
   115 				props.title = props.title || attachment.filename;
    97 				props.title = props.title || attachment.filename;
   116 				props.rel = props.rel || 'attachment wp-att-' + attachment.id;
    98 				props.rel = props.rel || 'attachment wp-att-' + attachment.id;
   117 			}
    99 			}
   118 
   100 
   119 			return fallbacks( props );
   101 			return props;
   120 		},
   102 		},
   121 		/**
   103 		/**
   122 		 * Create link markup that is suitable for passing to the editor
   104 		 * Create link markup that is suitable for passing to the editor
   123 		 *
       
   124 		 * @global wp.html.string
       
   125 		 *
   105 		 *
   126 		 * @param {Object} props Attachment details (align, link, size, etc).
   106 		 * @param {Object} props Attachment details (align, link, size, etc).
   127 		 * @param {Object} attachment The attachment object, media version of Post.
   107 		 * @param {Object} attachment The attachment object, media version of Post.
   128 		 * @returns {string} The link markup
   108 		 * @returns {string} The link markup
   129 		 */
   109 		 */
   169 		/**
   149 		/**
   170 		 * Helper function to create a media shortcode string
   150 		 * Helper function to create a media shortcode string
   171 		 *
   151 		 *
   172 		 * @access private
   152 		 * @access private
   173 		 *
   153 		 *
   174 		 * @global wp.shortcode
       
   175 		 * @global wp.media.view.settings
       
   176 		 *
       
   177 		 * @param {string} type The shortcode tag name: 'audio' or 'video'.
   154 		 * @param {string} type The shortcode tag name: 'audio' or 'video'.
   178 		 * @param {Object} props Attachment details (align, link, size, etc).
   155 		 * @param {Object} props Attachment details (align, link, size, etc).
   179 		 * @param {Object} attachment The attachment object, media version of Post.
   156 		 * @param {Object} attachment The attachment object, media version of Post.
   180 		 * @returns {string} The media shortcode
   157 		 * @returns {string} The media shortcode
   181 		 */
   158 		 */
   220 		},
   197 		},
   221 		/**
   198 		/**
   222 		 * Create image markup, optionally with a link and/or wrapped in a caption shortcode,
   199 		 * Create image markup, optionally with a link and/or wrapped in a caption shortcode,
   223 		 *  that is suitable for passing to the editor
   200 		 *  that is suitable for passing to the editor
   224 		 *
   201 		 *
   225 		 * @global wp.html
       
   226 		 * @global wp.shortcode
       
   227 		 *
       
   228 		 * @param {Object} props Attachment details (align, link, size, etc).
   202 		 * @param {Object} props Attachment details (align, link, size, etc).
   229 		 * @param {Object} attachment The attachment object, media version of Post.
   203 		 * @param {Object} attachment The attachment object, media version of Post.
   230 		 * @returns {string}
   204 		 * @returns {string}
   231 		 */
   205 		 */
   232 		image: function( props, attachment ) {
   206 		image: function( props, attachment ) {
   233 			var img = {},
   207 			var img = {},
   234 				options, classes, shortcode, html;
   208 				options, classes, shortcode, html;
   235 
   209 
       
   210 			props.type = 'image';
   236 			props = wp.media.string.props( props, attachment );
   211 			props = wp.media.string.props( props, attachment );
   237 			classes = props.classes || [];
   212 			classes = props.classes || [];
   238 
   213 
   239 			img.src = ! _.isUndefined( attachment ) ? attachment.url : props.url;
   214 			img.src = ! _.isUndefined( attachment ) ? attachment.url : props.url;
   240 			_.extend( img, _.pick( props, 'width', 'height', 'alt' ) );
   215 			_.extend( img, _.pick( props, 'width', 'height', 'alt' ) );
   350 				content: content
   325 				content: content
   351 			});
   326 			});
   352 		}
   327 		}
   353 	};
   328 	};
   354 
   329 
       
   330 	/**
       
   331 	 * @class wp.media.collection
       
   332 	 *
       
   333 	 * @param {Object} attributes
       
   334 	 */
   355 	wp.media.collection = function(attributes) {
   335 	wp.media.collection = function(attributes) {
   356 		var collections = {};
   336 		var collections = {};
   357 
   337 
   358 		return _.extend( {
   338 		return _.extend(/** @lends wp.media.collection.prototype */{
   359 			coerce : wp.media.coerce,
   339 			coerce : wp.media.coerce,
   360 			/**
   340 			/**
   361 			 * Retrieve attachments based on the properties of the passed shortcode
   341 			 * Retrieve attachments based on the properties of the passed shortcode
   362 			 *
       
   363 			 * @global wp.media.query
       
   364 			 *
   342 			 *
   365 			 * @param {wp.shortcode} shortcode An instance of wp.shortcode().
   343 			 * @param {wp.shortcode} shortcode An instance of wp.shortcode().
   366 			 * @returns {wp.media.model.Attachments} A Backbone.Collection containing
   344 			 * @returns {wp.media.model.Attachments} A Backbone.Collection containing
   367 			 *      the media items belonging to a collection.
   345 			 *      the media items belonging to a collection.
   368 			 *      The query[ this.tag ] property is a Backbone.Model
   346 			 *      The query[ this.tag ] property is a Backbone.Model
   426 				return query;
   404 				return query;
   427 			},
   405 			},
   428 			/**
   406 			/**
   429 			 * Triggered when clicking 'Insert {label}' or 'Update {label}'
   407 			 * Triggered when clicking 'Insert {label}' or 'Update {label}'
   430 			 *
   408 			 *
   431 			 * @global wp.shortcode
       
   432 			 * @global wp.media.model.Attachments
       
   433 			 *
       
   434 			 * @param {wp.media.model.Attachments} attachments A Backbone.Collection containing
   409 			 * @param {wp.media.model.Attachments} attachments A Backbone.Collection containing
   435 			 *      the media items belonging to a collection.
   410 			 *      the media items belonging to a collection.
   436 			 *      The query[ this.tag ] property is a Backbone.Model
   411 			 *      The query[ this.tag ] property is a Backbone.Model
   437 			 *          containing the 'props' for the collection.
   412 			 *          containing the 'props' for the collection.
   438 			 * @returns {wp.shortcode}
   413 			 * @returns {wp.shortcode}
   496 				return shortcode;
   471 				return shortcode;
   497 			},
   472 			},
   498 			/**
   473 			/**
   499 			 * Triggered when double-clicking a collection shortcode placeholder
   474 			 * Triggered when double-clicking a collection shortcode placeholder
   500 			 *   in the editor
   475 			 *   in the editor
   501 			 *
       
   502 			 * @global wp.shortcode
       
   503 			 * @global wp.media.model.Selection
       
   504 			 * @global wp.media.view.l10n
       
   505 			 *
   476 			 *
   506 			 * @param {string} content Content that is searched for possible
   477 			 * @param {string} content Content that is searched for possible
   507 			 *    shortcode markup matching the passed tag name,
   478 			 *    shortcode markup matching the passed tag name,
   508 			 *
   479 			 *
   509 			 * @this wp.media.{prop}
   480 			 * @this wp.media.{prop}
   619 			return attrs;
   590 			return attrs;
   620 		}
   591 		}
   621 	});
   592 	});
   622 
   593 
   623 	/**
   594 	/**
   624 	 * wp.media.featuredImage
   595 	 * @namespace wp.media.featuredImage
   625 	 * @namespace
   596 	 * @memberOf wp.media
   626 	 */
   597 	 */
   627 	wp.media.featuredImage = {
   598 	wp.media.featuredImage = {
   628 		/**
   599 		/**
   629 		 * Get the featured image post ID
   600 		 * Get the featured image post ID
   630 		 *
   601 		 *
   631 		 * @global wp.media.view.settings
       
   632 		 *
       
   633 		 * @returns {wp.media.view.settings.post.featuredImageId|number}
   602 		 * @returns {wp.media.view.settings.post.featuredImageId|number}
   634 		 */
   603 		 */
   635 		get: function() {
   604 		get: function() {
   636 			return wp.media.view.settings.post.featuredImageId;
   605 			return wp.media.view.settings.post.featuredImageId;
   637 		},
   606 		},
   638 		/**
   607 		/**
   639 		 * Set the featured image id, save the post thumbnail data and
   608 		 * Set the featured image id, save the post thumbnail data and
   640 		 * set the HTML in the post meta box to the new featured image.
   609 		 * set the HTML in the post meta box to the new featured image.
   641 		 *
   610 		 *
   642 		 * @global wp.media.view.settings
       
   643 		 * @global wp.media.post
       
   644 		 *
       
   645 		 * @param {number} id The post ID of the featured image, or -1 to unset it.
   611 		 * @param {number} id The post ID of the featured image, or -1 to unset it.
   646 		 */
   612 		 */
   647 		set: function( id ) {
   613 		set: function( id ) {
   648 			var settings = wp.media.view.settings;
   614 			var settings = wp.media.view.settings;
   649 
   615 
   650 			settings.post.featuredImageId = id;
   616 			settings.post.featuredImageId = id;
   651 
   617 
   652 			wp.media.post( 'set-post-thumbnail', {
   618 			wp.media.post( 'get-post-thumbnail-html', {
   653 				json:         true,
       
   654 				post_id:      settings.post.id,
   619 				post_id:      settings.post.id,
   655 				thumbnail_id: settings.post.featuredImageId,
   620 				thumbnail_id: settings.post.featuredImageId,
   656 				_wpnonce:     settings.post.nonce
   621 				_wpnonce:     settings.post.nonce
   657 			}).done( function( html ) {
   622 			}).done( function( html ) {
       
   623 				if ( html == '0' ) {
       
   624 					window.alert( window.setPostThumbnailL10n.error );
       
   625 					return;
       
   626 				}
   658 				$( '.inside', '#postimagediv' ).html( html );
   627 				$( '.inside', '#postimagediv' ).html( html );
   659 			});
   628 			});
   660 		},
   629 		},
   661 		/**
   630 		/**
       
   631 		 * Remove the featured image id, save the post thumbnail data and
       
   632 		 * set the HTML in the post meta box to no featured image.
       
   633 		 */
       
   634 		remove: function() {
       
   635 			wp.media.featuredImage.set( -1 );
       
   636 		},
       
   637 		/**
   662 		 * The Featured Image workflow
   638 		 * The Featured Image workflow
   663 		 *
       
   664 		 * @global wp.media.controller.FeaturedImage
       
   665 		 * @global wp.media.view.l10n
       
   666 		 *
   639 		 *
   667 		 * @this wp.media.featuredImage
   640 		 * @this wp.media.featuredImage
   668 		 *
   641 		 *
   669 		 * @returns {wp.media.view.MediaFrame.Select} A media workflow.
   642 		 * @returns {wp.media.view.MediaFrame.Select} A media workflow.
   670 		 */
   643 		 */
   671 		frame: function() {
   644 		frame: function() {
   672 			if ( this._frame ) {
   645 			if ( this._frame ) {
       
   646 				wp.media.frame = this._frame;
   673 				return this._frame;
   647 				return this._frame;
   674 			}
   648 			}
   675 
   649 
   676 			this._frame = wp.media({
   650 			this._frame = wp.media({
   677 				state: 'featured-image',
   651 				state: 'featured-image',
   703 		},
   677 		},
   704 		/**
   678 		/**
   705 		 * 'select' callback for Featured Image workflow, triggered when
   679 		 * 'select' callback for Featured Image workflow, triggered when
   706 		 *  the 'Set Featured Image' button is clicked in the media modal.
   680 		 *  the 'Set Featured Image' button is clicked in the media modal.
   707 		 *
   681 		 *
   708 		 * @global wp.media.view.settings
       
   709 		 *
       
   710 		 * @this wp.media.controller.FeaturedImage
   682 		 * @this wp.media.controller.FeaturedImage
   711 		 */
   683 		 */
   712 		select: function() {
   684 		select: function() {
   713 			var selection = this.get('selection').single();
   685 			var selection = this.get('selection').single();
   714 
   686 
   721 		/**
   693 		/**
   722 		 * Open the content media manager to the 'featured image' tab when
   694 		 * Open the content media manager to the 'featured image' tab when
   723 		 * the post thumbnail is clicked.
   695 		 * the post thumbnail is clicked.
   724 		 *
   696 		 *
   725 		 * Update the featured image id when the 'remove' link is clicked.
   697 		 * Update the featured image id when the 'remove' link is clicked.
   726 		 *
       
   727 		 * @global wp.media.view.settings
       
   728 		 */
   698 		 */
   729 		init: function() {
   699 		init: function() {
   730 			$('#postimagediv').on( 'click', '#set-post-thumbnail', function( event ) {
   700 			$('#postimagediv').on( 'click', '#set-post-thumbnail', function( event ) {
   731 				event.preventDefault();
   701 				event.preventDefault();
   732 				// Stop propagation to prevent thickbox from activating.
   702 				// Stop propagation to prevent thickbox from activating.
   733 				event.stopPropagation();
   703 				event.stopPropagation();
   734 
   704 
   735 				wp.media.featuredImage.frame().open();
   705 				wp.media.featuredImage.frame().open();
   736 			}).on( 'click', '#remove-post-thumbnail', function() {
   706 			}).on( 'click', '#remove-post-thumbnail', function() {
   737 				wp.media.view.settings.post.featuredImageId = -1;
   707 				wp.media.featuredImage.remove();
       
   708 				return false;
   738 			});
   709 			});
   739 		}
   710 		}
   740 	};
   711 	};
   741 
   712 
   742 	$( wp.media.featuredImage.init );
   713 	$( wp.media.featuredImage.init );
   743 
   714 
   744 	/**
   715 	/** @namespace wp.media.editor */
   745 	 * wp.media.editor
       
   746 	 * @namespace
       
   747 	 */
       
   748 	wp.media.editor = {
   716 	wp.media.editor = {
   749 		/**
   717 		/**
   750 		 * Send content to the editor
   718 		 * Send content to the editor
   751 		 *
       
   752 		 * @global tinymce
       
   753 		 * @global QTags
       
   754 		 * @global wpActiveEditor
       
   755 		 * @global tb_remove() - Possibly overloaded by legacy plugins
       
   756 		 *
   719 		 *
   757 		 * @param {string} html Content to send to the editor
   720 		 * @param {string} html Content to send to the editor
   758 		 */
   721 		 */
   759 		insert: function( html ) {
   722 		insert: function( html ) {
   760 			var editor, wpActiveEditor,
   723 			var editor, wpActiveEditor,
   801 		},
   764 		},
   802 
   765 
   803 		/**
   766 		/**
   804 		 * Setup 'workflow' and add to the 'workflows' cache. 'open' can
   767 		 * Setup 'workflow' and add to the 'workflows' cache. 'open' can
   805 		 *  subsequently be called upon it.
   768 		 *  subsequently be called upon it.
   806 		 *
       
   807 		 * @global wp.media.view.l10n
       
   808 		 *
   769 		 *
   809 		 * @param {string} id A slug used to identify the workflow.
   770 		 * @param {string} id A slug used to identify the workflow.
   810 		 * @param {Object} [options={}]
   771 		 * @param {Object} [options={}]
   811 		 *
   772 		 *
   812 		 * @this wp.media.editor
   773 		 * @this wp.media.editor
   911 			return workflow;
   872 			return workflow;
   912 		},
   873 		},
   913 		/**
   874 		/**
   914 		 * Determines the proper current workflow id
   875 		 * Determines the proper current workflow id
   915 		 *
   876 		 *
   916 		 * @global wpActiveEditor
       
   917 		 * @global tinymce
       
   918 		 *
       
   919 		 * @param {string} [id=''] A slug used to identify the workflow.
   877 		 * @param {string} [id=''] A slug used to identify the workflow.
   920 		 *
   878 		 *
   921 		 * @returns {wpActiveEditor|string|tinymce.activeEditor.id}
   879 		 * @returns {wpActiveEditor|string|tinymce.activeEditor.id}
   922 		 */
   880 		 */
   923 		id: function( id ) {
   881 		id: function( id ) {
   959 		 */
   917 		 */
   960 		remove: function( id ) {
   918 		remove: function( id ) {
   961 			id = this.id( id );
   919 			id = this.id( id );
   962 			delete workflows[ id ];
   920 			delete workflows[ id ];
   963 		},
   921 		},
   964 		/**
   922 		/** @namespace wp.media.editor.send */
   965 		 * @namespace
       
   966 		 */
       
   967 		send: {
   923 		send: {
   968 			/**
   924 			/**
   969 			 * Called when sending an attachment to the editor
   925 			 * Called when sending an attachment to the editor
   970 			 *   from the medial modal.
   926 			 *   from the medial modal.
   971 			 *
       
   972 			 * @global wp.media.view.settings
       
   973 			 * @global wp.media.post
       
   974 			 *
   927 			 *
   975 			 * @param {Object} props Attachment details (align, link, size, etc).
   928 			 * @param {Object} props Attachment details (align, link, size, etc).
   976 			 * @param {Object} attachment The attachment object, media version of Post.
   929 			 * @param {Object} attachment The attachment object, media version of Post.
   977 			 * @returns {Promise}
   930 			 * @returns {Promise}
   978 			 */
   931 			 */
  1025 				});
   978 				});
  1026 			},
   979 			},
  1027 			/**
   980 			/**
  1028 			 * Called when 'Insert From URL' source is not an image. Example: YouTube url.
   981 			 * Called when 'Insert From URL' source is not an image. Example: YouTube url.
  1029 			 *
   982 			 *
  1030 			 * @global wp.media.view.settings
       
  1031 			 *
       
  1032 			 * @param {Object} embed
   983 			 * @param {Object} embed
  1033 			 * @returns {Promise}
   984 			 * @returns {Promise}
  1034 			 */
   985 			 */
  1035 			link: function( embed ) {
   986 			link: function( embed ) {
  1036 				return wp.media.post( 'send-link-to-editor', {
   987 				return wp.media.post( 'send-link-to-editor', {
  1065 			// Redo workflow if state has changed
  1016 			// Redo workflow if state has changed
  1066 			if ( ! workflow || ( workflow.options && options.state !== workflow.options.state ) ) {
  1017 			if ( ! workflow || ( workflow.options && options.state !== workflow.options.state ) ) {
  1067 				workflow = this.add( id, options );
  1018 				workflow = this.add( id, options );
  1068 			}
  1019 			}
  1069 
  1020 
       
  1021 			wp.media.frame = workflow;
       
  1022 
  1070 			return workflow.open();
  1023 			return workflow.open();
  1071 		},
  1024 		},
  1072 
  1025 
  1073 		/**
  1026 		/**
  1074 		 * Bind click event for .insert-media using event delegation
  1027 		 * Bind click event for .insert-media using event delegation
  1075 		 *
       
  1076 		 * @global wp.media.view.l10n
       
  1077 		 */
  1028 		 */
  1078 		init: function() {
  1029 		init: function() {
  1079 			$(document.body)
  1030 			$(document.body)
  1080 				.on( 'click.add-media-button', '.insert-media', function( event ) {
  1031 				.on( 'click.add-media-button', '.insert-media', function( event ) {
  1081 					var elem = $( event.currentTarget ),
  1032 					var elem = $( event.currentTarget ),
  1087 							multiple: true
  1038 							multiple: true
  1088 						};
  1039 						};
  1089 
  1040 
  1090 					event.preventDefault();
  1041 					event.preventDefault();
  1091 
  1042 
  1092 					// Remove focus from the `.insert-media` button.
       
  1093 					// Prevents Opera from showing the outline of the button
       
  1094 					// above the modal.
       
  1095 					//
       
  1096 					// See: https://core.trac.wordpress.org/ticket/22445
       
  1097 					elem.blur();
       
  1098 
       
  1099 					if ( elem.hasClass( 'gallery' ) ) {
  1043 					if ( elem.hasClass( 'gallery' ) ) {
  1100 						options.state = 'gallery';
  1044 						options.state = 'gallery';
  1101 						options.title = wp.media.view.l10n.createGalleryTitle;
  1045 						options.title = wp.media.view.l10n.createGalleryTitle;
  1102 					}
  1046 					}
  1103 
  1047