jquery.mjs.nestedSortable.js
4 years ago
jquery.ui.touch-punch.min.js
11 years ago
nestedpages-factory.js
3 years ago
nestedpages.bulk-actions.js
4 years ago
nestedpages.check-all.js
9 years ago
nestedpages.clone.js
3 years ago
nestedpages.confirm-delete.js
8 years ago
nestedpages.dropdowns.js
8 years ago
nestedpages.formatter.js
3 years ago
nestedpages.hidden-item-count.js
9 years ago
nestedpages.manual-sync.js
9 years ago
nestedpages.menu-links.js
3 years ago
nestedpages.menu-search.js
2 weeks ago
nestedpages.menu-toggle.js
8 years ago
nestedpages.modals.js
8 years ago
nestedpages.move-post.js
8 years ago
nestedpages.nesting.js
3 years ago
nestedpages.new-post.js
6 years ago
nestedpages.page-toggle.js
4 years ago
nestedpages.post-search.js
9 years ago
nestedpages.quickedit-link.js
8 years ago
nestedpages.quickedit-post.js
3 years ago
nestedpages.settings-admin-customization.js
7 years ago
nestedpages.settings-reset.js
7 years ago
nestedpages.settings.js
3 years ago
nestedpages.sync-menu-setting.js
8 years ago
nestedpages.tabs.js
6 years ago
nestedpages.trash-with-children.js
6 years ago
nestedpages.trash.js
8 years ago
nestedpages.userprefs-reset.js
7 years ago
nestedpages.wpml.js
8 years ago
nestedpages.post-search.js
138 lines
| 1 | var NestedPages = NestedPages || {}; |
| 2 | |
| 3 | /** |
| 4 | * Perform an AJAX search for posts by type |
| 5 | * @package Nested Pages |
| 6 | * @author Kyle Phillips - https://github.com/kylephillips/wp-nested-pages |
| 7 | */ |
| 8 | NestedPages.PostSearch = function() |
| 9 | { |
| 10 | var plugin = this; |
| 11 | var $ = jQuery; |
| 12 | |
| 13 | plugin.selectors = { |
| 14 | input : 'data-nestedpages-post-search', |
| 15 | form : 'data-nestedpages-post-search-form', |
| 16 | loadingIndicator : 'data-nestedpages-loading', |
| 17 | noResults : 'data-nestedpages-no-results', |
| 18 | results: 'data-nestedpages-search-results' |
| 19 | } |
| 20 | |
| 21 | plugin.changed = false; |
| 22 | plugin.activeInput = ''; // The active input |
| 23 | plugin.results = ''; // Search results |
| 24 | plugin.defaultResults = ''; // The default, loaded results |
| 25 | plugin.postType = ''; // The type of search (post_type, taxonomy) |
| 26 | plugin.activeForm = ''; |
| 27 | |
| 28 | plugin.bindEvents = function() |
| 29 | { |
| 30 | $('['+ plugin.selectors.input + ']').on('input', function(){ |
| 31 | plugin.activeInput = $(this); |
| 32 | plugin.setOptions(); |
| 33 | if ( !plugin.changed ) plugin.setDefaultResults(); |
| 34 | if ( $(this).val() === '' ) { |
| 35 | $(plugin.activeForm).find('[' + plugin.selectors.noResults + ']').hide(); |
| 36 | plugin.showDefaultResults(); |
| 37 | return; |
| 38 | } |
| 39 | plugin.query(); |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Set the default results |
| 45 | */ |
| 46 | plugin.setDefaultResults = function() |
| 47 | { |
| 48 | plugin.defaultResults = $(plugin.activeForm).find('[' + plugin.selectors.results + ']').html(); |
| 49 | plugin.changed = true; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Show the default results |
| 54 | */ |
| 55 | plugin.showDefaultResults = function() |
| 56 | { |
| 57 | $(plugin.activeForm).find('[' + plugin.selectors.results + ']').html(plugin.defaultResults); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Set the options |
| 62 | */ |
| 63 | plugin.setOptions = function() |
| 64 | { |
| 65 | plugin.postType = $(plugin.activeInput).attr(plugin.selectors.input); |
| 66 | plugin.activeForm = $(plugin.activeInput).parents('[' + plugin.selectors.form + ']'); |
| 67 | } |
| 68 | |
| 69 | // Perform the search |
| 70 | plugin.performSearch = function() |
| 71 | { |
| 72 | plugin.defaultResults = $(plugin.activeForm).parent('li').siblings(plugin.selectors.defaultResults); |
| 73 | if ( $(plugin.activeForm).val().length > 2 ){ |
| 74 | $(plugin.defaultResults).hide(); |
| 75 | plugin.toggleLoading(true); |
| 76 | plugin.query(); |
| 77 | return; |
| 78 | } |
| 79 | plugin.toggleLoading(false); |
| 80 | $(plugin.defaultResults).show(); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | // Query Search |
| 85 | plugin.query = function() |
| 86 | { |
| 87 | $(plugin.activeForm).find('[' + plugin.selectors.results + ']').empty(); |
| 88 | plugin.toggleLoading(true); |
| 89 | $.ajax({ |
| 90 | url: NestedPages.jsData.ajaxurl, |
| 91 | type: 'post', |
| 92 | datatype: 'json', |
| 93 | data: { |
| 94 | action : NestedPages.formActions.postSearch, |
| 95 | nonce : NestedPages.jsData.nonce, |
| 96 | term : $(plugin.activeInput).val(), |
| 97 | postType : plugin.postType |
| 98 | }, |
| 99 | success: function(data){ |
| 100 | if ( data.results ){ |
| 101 | plugin.results = data.results; |
| 102 | plugin.loadResults(); |
| 103 | plugin.toggleLoading(false); |
| 104 | } else { |
| 105 | plugin.toggleLoading(false); |
| 106 | $(plugin.activeForm).find('[' + plugin.selectors.noResults + ']').show(); |
| 107 | } |
| 108 | } |
| 109 | }); |
| 110 | } |
| 111 | |
| 112 | // Load the results into view |
| 113 | plugin.loadResults = function() |
| 114 | { |
| 115 | var html = "<ul>"; |
| 116 | $.each(plugin.results, function(i, v){ |
| 117 | html += '<li><a href="#" data-assignment-page-id="' + v.ID + '" data-assignment-page-title="' + v.post_title + '">' + v.post_title + '</a></li>'; |
| 118 | }); |
| 119 | html += '</ul>'; |
| 120 | $(plugin.activeForm).find('[' + plugin.selectors.results + ']').html(html); |
| 121 | plugin.toggleLoading(false); |
| 122 | } |
| 123 | |
| 124 | // Toggle the loading indicator |
| 125 | plugin.toggleLoading = function(loading) |
| 126 | { |
| 127 | var loadingIndicator = $(plugin.activeForm).find('[' + plugin.selectors.loadingIndicator + ']'); |
| 128 | var noResults = $(plugin.activeForm).find('[' + plugin.selectors.noResults + ']'); |
| 129 | $(noResults).hide(); |
| 130 | if ( loading ){ |
| 131 | $(loadingIndicator).show(); |
| 132 | return; |
| 133 | } |
| 134 | $(loadingIndicator).hide(); |
| 135 | } |
| 136 | |
| 137 | return plugin.bindEvents(); |
| 138 | } |