alphalisting-widget-admin.js
146 lines
| 1 | jQuery( function( $ ) { |
| 2 | const wireup_alphalisting = function() { |
| 3 | jQuery( '.alphalisting-widget' ).each( function( idx, el ) { |
| 4 | el = $( el ); |
| 5 | |
| 6 | const target_post = el.find( '.alphalisting-target-post' ); |
| 7 | const target_post_title = el.find( |
| 8 | '.alphalisting-target-post-title' |
| 9 | ); |
| 10 | const display_type = el.find( '.alphalisting-display-type' ); |
| 11 | const listing_post_type = el.find( '.alphalisting-post-type' ); |
| 12 | const listing_post_type_wrapper = el.find( |
| 13 | '.alphalisting-post-type-wrapper' |
| 14 | ); |
| 15 | const listing_parent_post = el.find( '.alphalisting-parent-post' ); |
| 16 | const listing_parent_post_title = el.find( |
| 17 | '.alphalisting-parent-post-title' |
| 18 | ); |
| 19 | const listing_parent_post_wrapper = el.find( |
| 20 | '.alphalisting-parent-post-wrapper' |
| 21 | ); |
| 22 | const listing_taxonomy = el.find( '.alphalisting-taxonomy' ); |
| 23 | const listing_taxonomy_wrapper = el.find( |
| 24 | '.alphalisting-taxonomy-wrapper' |
| 25 | ); |
| 26 | const listing_parent_term = el.find( '.alphalisting-parent-term' ); |
| 27 | const listing_parent_term_wrapper = el.find( |
| 28 | '.alphalisting-parent-term-wrapper' |
| 29 | ); |
| 30 | const listing_hide_empty_terms = el.find( |
| 31 | '.alphalisting-hide-empty-terms' |
| 32 | ); |
| 33 | const listing_hide_empty_terms_wrapper = el.find( |
| 34 | '.alphalisting-hide-empty-terms-wrapper' |
| 35 | ); |
| 36 | const listing_exclude_terms = el.find( |
| 37 | '.alphalisting-exclude-terms' |
| 38 | ); |
| 39 | const listing_exclude_terms_wrapper = el.find( |
| 40 | '.alphalisting-exclude-terms-wrapper' |
| 41 | ); |
| 42 | const listing_wpnonce = el.find( |
| 43 | '#_posts_by_title_wpnonce' |
| 44 | ); |
| 45 | |
| 46 | const switch_taxonomy_or_posts = function() { |
| 47 | if ( 'terms' === display_type.val() ) { |
| 48 | listing_post_type.attr( 'disabled', 'disabled' ); |
| 49 | listing_post_type_wrapper.hide(); |
| 50 | listing_parent_post_title.attr( 'disabled', 'disabled' ); |
| 51 | listing_parent_post_wrapper.hide(); |
| 52 | listing_taxonomy.removeAttr( 'disabled' ); |
| 53 | listing_taxonomy_wrapper.show(); |
| 54 | listing_parent_term.removeAttr( 'disabled' ); |
| 55 | listing_parent_term_wrapper.show(); |
| 56 | listing_hide_empty_terms.removeAttr( 'disabled' ); |
| 57 | listing_hide_empty_terms_wrapper.show(); |
| 58 | listing_exclude_terms.removeAttr( 'disabled' ); |
| 59 | listing_exclude_terms_wrapper.show(); |
| 60 | } else { |
| 61 | listing_post_type.removeAttr( 'disabled' ); |
| 62 | listing_post_type_wrapper.show(); |
| 63 | listing_parent_post_title.removeAttr( 'disabled' ); |
| 64 | listing_parent_post_wrapper.show(); |
| 65 | listing_taxonomy.attr( 'disabled', 'disabled' ); |
| 66 | listing_taxonomy_wrapper.hide(); |
| 67 | listing_parent_term.attr( 'disabled', 'disabled' ); |
| 68 | listing_parent_term_wrapper.hide(); |
| 69 | listing_hide_empty_terms.attr( 'disabled', 'disabled' ); |
| 70 | listing_hide_empty_terms_wrapper.hide(); |
| 71 | listing_exclude_terms.attr( 'disabled', 'disabled' ); |
| 72 | listing_exclude_terms_wrapper.hide(); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | switch_taxonomy_or_posts(); |
| 77 | display_type.on( 'change', switch_taxonomy_or_posts ); |
| 78 | |
| 79 | $( target_post_title ).autocomplete( { |
| 80 | source( post_title, response ) { |
| 81 | |
| 82 | jQuery.ajax( { |
| 83 | url: alphalisting_widget_admin.ajax_url, |
| 84 | type: 'POST', |
| 85 | dataType: 'json', |
| 86 | data: { |
| 87 | action: 'alphalisting_get_autocomplete_post_titles', |
| 88 | _posts_by_title_wpnonce: listing_wpnonce.val(), |
| 89 | post_type: '', |
| 90 | post_title, |
| 91 | }, |
| 92 | success( data ) { |
| 93 | response( data ); |
| 94 | }, |
| 95 | error() { |
| 96 | response(); |
| 97 | }, |
| 98 | } ); |
| 99 | }, |
| 100 | select( event, ui ) { |
| 101 | event.preventDefault(); |
| 102 | target_post_title |
| 103 | .find( '~ input[type="hidden"]' ) |
| 104 | .val( ui.item.value ); |
| 105 | target_post_title.val( ui.item.label ); |
| 106 | }, |
| 107 | } ); |
| 108 | |
| 109 | $( listing_parent_post_title ).autocomplete( { |
| 110 | source( post_title, response ) { |
| 111 | jQuery.ajax( { |
| 112 | url: |
| 113 | alphalisting_widget_admin.ajax_url, |
| 114 | type: 'POST', |
| 115 | dataType: 'json', |
| 116 | data: { |
| 117 | action: 'alphalisting_get_autocomplete_post_titles', |
| 118 | _posts_by_title_wpnonce: listing_wpnonce.val(), |
| 119 | post_type: listing_post_type.val(), |
| 120 | post_title, |
| 121 | }, |
| 122 | success( data ) { |
| 123 | response( data ); |
| 124 | }, |
| 125 | error() { |
| 126 | response(); |
| 127 | }, |
| 128 | } ); |
| 129 | }, |
| 130 | select( event, ui ) { |
| 131 | event.preventDefault(); |
| 132 | listing_parent_post_title |
| 133 | .find( '~ input[type="hidden"]' ) |
| 134 | .val( ui.item.value ); |
| 135 | listing_parent_post_title.val( ui.item.label ); |
| 136 | }, |
| 137 | } ); |
| 138 | } ); |
| 139 | }; |
| 140 | |
| 141 | wireup_alphalisting(); |
| 142 | $( document ).on( 'widget-updated widget-added', function() { |
| 143 | wireup_alphalisting(); |
| 144 | } ); |
| 145 | } ); |
| 146 |