Changeset 38436 for trunk/src/wp-admin/js/customize-nav-menus.js
- Timestamp:
- 08/29/2016 10:58:32 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/js/customize-nav-menus.js
r38301 r38436 81 81 api.Menus.availableMenuItems = new api.Menus.AvailableItemCollection( api.Menus.data.availableMenuItems ); 82 82 83 api.Menus.insertedAutoDrafts = []; 84 85 /** 86 * Insert a new `auto-draft` post. 87 * 88 * @param {object} params - Parameters for the draft post to create. 89 * @param {string} params.post_type - Post type to add. 90 * @param {string} params.post_title - Post title to use. 91 * @return {jQuery.promise} Promise resolved with the added post. 92 */ 93 api.Menus.insertAutoDraftPost = function insertAutoDraftPost( params ) { 94 var request, deferred = $.Deferred(); 95 96 request = wp.ajax.post( 'customize-nav-menus-insert-auto-draft', { 97 'customize-menus-nonce': api.settings.nonce['customize-menus'], 98 'wp_customize': 'on', 99 'params': params 100 } ); 101 102 request.done( function( response ) { 103 if ( response.post_id ) { 104 deferred.resolve( response ); 105 api.Menus.insertedAutoDrafts.push( response.post_id ); 106 api( 'nav_menus_created_posts' ).set( _.clone( api.Menus.insertedAutoDrafts ) ); 107 } 108 } ); 109 110 request.fail( function( response ) { 111 var error = response || ''; 112 113 if ( 'undefined' !== typeof response.message ) { 114 error = response.message; 115 } 116 117 console.error( error ); 118 deferred.rejectWith( error ); 119 } ); 120 121 return deferred.promise(); 122 }; 123 83 124 /** 84 125 * wp.customize.Menus.AvailableMenuItemsPanelView … … 101 142 'click #custom-menu-item-submit': '_submitLink', 102 143 'keypress #custom-menu-item-name': '_submitLink', 144 'click .new-content-item .add-content': '_submitNew', 145 'keypress .create-item-input': '_submitNew', 103 146 'keydown': 'keyboardAccessible' 104 147 }, … … 116 159 sectionContent: '', 117 160 loading: false, 161 addingNew: false, 118 162 119 163 initialize: function() { … … 125 169 126 170 this.$search = $( '#menu-items-search' ); 127 this.sectionContent = this.$el.find( '.a ccordion-section-content' );171 this.sectionContent = this.$el.find( '.available-menu-items-list' ); 128 172 129 173 this.debounceSearch = _.debounce( self.search, 500 ); … … 161 205 // Load more items. 162 206 this.sectionContent.scroll( function() { 163 var totalHeight = self.$el.find( '.accordion-section.open .a ccordion-section-content' ).prop( 'scrollHeight' ),207 var totalHeight = self.$el.find( '.accordion-section.open .available-menu-items-list' ).prop( 'scrollHeight' ), 164 208 visibleHeight = self.$el.find( '.accordion-section.open' ).height(); 165 209 … … 338 382 items = new api.Menus.AvailableItemCollection( items ); // @todo Why is this collection created and then thrown away? 339 383 self.collection.add( items.models ); 340 typeInner = availableMenuItemContainer.find( '.a ccordion-section-content' );384 typeInner = availableMenuItemContainer.find( '.available-menu-items-list' ); 341 385 items.each(function( menuItem ) { 342 386 typeInner.append( itemTemplate( menuItem.attributes ) ); … … 357 401 // Adjust the height of each section of items to fit the screen. 358 402 itemSectionHeight: function() { 359 var sections, totalHeight, accordionHeight, diff;403 var sections, lists, totalHeight, accordionHeight, diff; 360 404 totalHeight = window.innerHeight; 361 405 sections = this.$el.find( '.accordion-section:not( #available-menu-items-search ) .accordion-section-content' ); 362 accordionHeight = 46 * ( 2 + sections.length ) - 13; // Magic numbers. 406 lists = this.$el.find( '.accordion-section:not( #available-menu-items-search ) .available-menu-items-list:not(":only-child")' ); 407 accordionHeight = 46 * ( 1 + sections.length ) + 14; // Magic numbers. 363 408 diff = totalHeight - accordionHeight; 364 409 if ( 120 < diff && 290 > diff ) { 365 410 sections.css( 'max-height', diff ); 411 lists.css( 'max-height', ( diff - 60 ) ); 366 412 } 367 413 }, … … 455 501 itemUrl.val( 'http://' ); 456 502 itemName.val( '' ); 503 }, 504 505 // Submit handler for keypress (enter) on field and click on button. 506 _submitNew: function( event ) { 507 var container; 508 509 // Only proceed with keypress if it is Enter. 510 if ( 'keypress' === event.type && 13 !== event.which ) { 511 return; 512 } 513 514 if ( this.addingNew ) { 515 return; 516 } 517 518 container = $( event.target ).closest( '.accordion-section' ); 519 520 this.submitNew( container ); 521 }, 522 523 // Creates a new object and adds an associated menu item to the menu. 524 submitNew: function( container ) { 525 var panel = this, 526 itemName = container.find( '.create-item-input' ), 527 title = itemName.val(), 528 dataContainer = container.find( '.available-menu-items-list' ), 529 itemType = dataContainer.data( 'type' ), 530 itemObject = dataContainer.data( 'object' ), 531 itemTypeLabel = dataContainer.data( 'type_label' ), 532 promise; 533 534 if ( ! this.currentMenuControl ) { 535 return; 536 } 537 538 // Only posts are supported currently. 539 if ( 'post_type' !== itemType ) { 540 return; 541 } 542 543 if ( '' === $.trim( itemName.val() ) ) { 544 itemName.addClass( 'invalid' ); 545 itemName.focus(); 546 return; 547 } else { 548 itemName.removeClass( 'invalid' ); 549 container.find( '.accordion-section-title' ).addClass( 'loading' ); 550 } 551 552 panel.addingNew = true; 553 itemName.attr( 'disabled', 'disabled' ); 554 promise = api.Menus.insertAutoDraftPost( { 555 post_title: title, 556 post_type: itemObject 557 } ); 558 promise.done( function( data ) { 559 var availableItem, $content, itemTemplate; 560 availableItem = new api.Menus.AvailableItemModel( { 561 'id': 'post-' + data.post_id, // Used for available menu item Backbone models. 562 'title': itemName.val(), 563 'type': itemType, 564 'type_label': itemTypeLabel, 565 'object': itemObject, 566 'object_id': data.post_id, 567 'url': data.url 568 } ); 569 570 // Add new item to menu. 571 panel.currentMenuControl.addItemToMenu( availableItem.attributes ); 572 573 // Add the new item to the list of available items. 574 api.Menus.availableMenuItemsPanel.collection.add( availableItem ); 575 $content = container.find( '.available-menu-items-list' ); 576 itemTemplate = wp.template( 'available-menu-item' ); 577 $content.prepend( itemTemplate( availableItem.attributes ) ); 578 $content.scrollTop(); 579 580 // Reset the create content form. 581 itemName.val( '' ).removeAttr( 'disabled' ); 582 panel.addingNew = false; 583 container.find( '.accordion-section-title' ).removeClass( 'loading' ); 584 } ); 457 585 }, 458 586 … … 2546 2674 api.Menus.applySavedData( data ); 2547 2675 } 2676 2677 // Reset list of inserted auto draft post IDs. 2678 api.Menus.insertedAutoDrafts = []; 2548 2679 } ); 2549 2680
Note: See TracChangeset
for help on using the changeset viewer.