|
20 | 20 | } |
21 | 21 | } ); |
22 | 22 |
|
| 23 | + /** |
| 24 | + * Parse the class name for an .hentry element, that is for an element that uses post_class(). |
| 25 | + * |
| 26 | + * @param {string} className Class name. |
| 27 | + * @returns {object|null} Object with postType and postId props, or null if no matches. |
| 28 | + */ |
| 29 | + api.previewPosts.parsePostClassName = function parsePostClassName( className ) { |
| 30 | + var matches, postId, postType; |
| 31 | + matches = className.match( /(\s|^)post-(\d+)(\s|$)/ ); |
| 32 | + if ( matches ) { |
| 33 | + postId = parseInt( matches[2], 10 ); |
| 34 | + } else { |
| 35 | + return null; |
| 36 | + } |
| 37 | + matches = className.match( /(\s|^)type-(\S+)(\s|$)/ ); |
| 38 | + if ( matches ) { |
| 39 | + postType = matches[2]; |
| 40 | + } else { |
| 41 | + return null; |
| 42 | + } |
| 43 | + return { |
| 44 | + postId: postId, |
| 45 | + postType: postType |
| 46 | + }; |
| 47 | + }; |
| 48 | + |
| 49 | + // Add partials when the document is modified and new post hentry elements are added. |
| 50 | + if ( 'undefined' !== typeof MutationObserver ) { |
| 51 | + api.previewPosts.mutationObserver = new MutationObserver( function( mutations ) { |
| 52 | + _.each( mutations, function( mutation ) { |
| 53 | + var hentryElements, mutationTarget; |
| 54 | + mutationTarget = $( mutation.target ); |
| 55 | + hentryElements = mutationTarget.find( '.hentry' ); |
| 56 | + if ( mutationTarget.is( '.hentry' ) ) { |
| 57 | + hentryElements = hentryElements.add( mutationTarget ); |
| 58 | + } |
| 59 | + |
| 60 | + hentryElements.each( function() { |
| 61 | + var postInfo, settingId; |
| 62 | + postInfo = api.previewPosts.parsePostClassName( $( this ).prop( 'className' ) ); |
| 63 | + if ( ! postInfo ) { |
| 64 | + return; |
| 65 | + } |
| 66 | + settingId = 'post[' + postInfo.postType + '][' + String( postInfo.postId ) + ']'; |
| 67 | + api.previewPosts.ensurePartialsForPostSetting( settingId ); |
| 68 | + |
| 69 | + // Ensure edit shortcuts are added for all placements inside the mutation target. |
| 70 | + _.each( api.previewPosts.partialSchema( settingId ), function( schema ) { |
| 71 | + var partial; |
| 72 | + partial = api.selectiveRefresh.partial( schema.id ); |
| 73 | + if ( ! partial ) { |
| 74 | + return; |
| 75 | + } |
| 76 | + _.each( partial.placements(), function( placement ) { // eslint-disable-line max-nested-callbacks |
| 77 | + if ( mutationTarget.is( placement.container ) || $.contains( mutation.target, placement.container[0] ) ) { |
| 78 | + $( placement.container ).attr( 'title', api.selectiveRefresh.data.l10n.shiftClickToEdit ); |
| 79 | + partial.createEditShortcutForPlacement( placement ); |
| 80 | + } |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + }); |
| 85 | + }); |
| 86 | + api.previewPosts.mutationObserver.observe( document.documentElement, { |
| 87 | + childList: true, |
| 88 | + subtree: true |
| 89 | + }); |
| 90 | + } |
| 91 | + |
23 | 92 | /** |
24 | 93 | * Ensure that each post setting is added and has corresponding partials. |
25 | 94 | * |
|
0 commit comments