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
4 days 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
6 years ago
nestedpages.settings-reset.js
6 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.menu-search.js
129 lines
| 1 | var NestedPages = NestedPages || {}; |
| 2 | |
| 3 | /** |
| 4 | * Menu Item Search in Modal Link Form |
| 5 | * @package Nested Pages |
| 6 | * @author Kyle Phillips - https://github.com/kylephillips/wp-nested-pages |
| 7 | */ |
| 8 | NestedPages.MenuSearch = function() |
| 9 | { |
| 10 | var plugin = this; |
| 11 | var $ = jQuery; |
| 12 | |
| 13 | plugin.selectors = { |
| 14 | searchForms : '*[data-np-menu-search]', // Search form selector |
| 15 | defaultResults : '[data-default-result]', // Default results list items |
| 16 | loadingIndicator : '.np-menu-search-loading', // loading indicator |
| 17 | noResults : '.np-menu-search-noresults', // No results |
| 18 | searchType : 'data-search-type', // The search object type (post_type, taxonomy) |
| 19 | searchObject : 'data-search-object', // The object to search (post, category, etc) |
| 20 | searchResults : '[data-np-search-result]', // Appended search result rows |
| 21 | } |
| 22 | |
| 23 | plugin.activeForm = ''; // The active form |
| 24 | plugin.results = ''; // Search results |
| 25 | plugin.defaultResults = ''; // The default, loaded results |
| 26 | plugin.searchType = ''; // The type of search (post_type, taxonomy) |
| 27 | plugin.searchObject = ''; // The object being searched (post, category, post_tag, etc…) |
| 28 | |
| 29 | plugin.formatter = new NestedPages.Formatter; |
| 30 | |
| 31 | plugin.bindEvents = function() |
| 32 | { |
| 33 | $(document).on('keyup', plugin.selectors.searchForms, function(){ |
| 34 | plugin.activeForm = $(this); |
| 35 | $(plugin.selectors.searchResults).remove(); |
| 36 | plugin.performSearch(); |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | // Perform the search |
| 42 | plugin.performSearch = function() |
| 43 | { |
| 44 | plugin.defaultResults = $(plugin.activeForm).parent('li').siblings(plugin.selectors.defaultResults); |
| 45 | if ( $(plugin.activeForm).val().length > 2 ){ |
| 46 | $(plugin.defaultResults).hide(); |
| 47 | plugin.toggleLoading(true); |
| 48 | plugin.query(); |
| 49 | return; |
| 50 | } |
| 51 | plugin.toggleLoading(false); |
| 52 | $(plugin.defaultResults).show(); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | // Query Search |
| 57 | plugin.query = function() |
| 58 | { |
| 59 | plugin.searchType = $(plugin.activeForm).attr(plugin.selectors.searchType); |
| 60 | plugin.searchObject = $(plugin.activeForm).attr(plugin.selectors.searchObject); |
| 61 | $.ajax({ |
| 62 | url: NestedPages.jsData.ajaxurl, |
| 63 | type: 'post', |
| 64 | datatype: 'json', |
| 65 | data: { |
| 66 | action : NestedPages.formActions.search, |
| 67 | nonce : NestedPages.jsData.nonce, |
| 68 | term : $(plugin.activeForm).val(), |
| 69 | searchType : plugin.searchType, |
| 70 | searchObject : plugin.searchObject, |
| 71 | }, |
| 72 | success: function(data){ |
| 73 | if ( data.results ){ |
| 74 | plugin.results = data.results; |
| 75 | plugin.toggleLoading(false); |
| 76 | if ( plugin.searchType === 'post_type' ){ |
| 77 | plugin.appendPosts(); |
| 78 | } else { |
| 79 | plugin.appendTaxonomies(); |
| 80 | } |
| 81 | } else { |
| 82 | plugin.toggleLoading(false); |
| 83 | $(plugin.activeForm).siblings(plugin.selectors.noResults).show(); |
| 84 | } |
| 85 | } |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | // Append post type results |
| 91 | plugin.appendPosts = function() |
| 92 | { |
| 93 | $('[data-np-search-result]').remove(); |
| 94 | var html = ""; |
| 95 | $.each(plugin.results, function(i, v){ |
| 96 | html = '<li data-np-search-result><a href="#" data-np-menu-object="' + plugin.searchObject + '" data-np-menu-type="post_type" data-np-menu-objectid="' + v.ID + '" data-np-permalink="' + v.permalink + '" data-np-object-name="' + v.singular_name + '" data-np-menu-selection></a></li>'; |
| 97 | $(html).insertAfter($(plugin.activeForm).parent('li')); |
| 98 | $('[data-np-menu-objectid="' + v.ID + '"').text(v.post_title); |
| 99 | }); |
| 100 | plugin.toggleLoading(false); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | // Append taxonomy results |
| 105 | plugin.appendTaxonomies = function() |
| 106 | { |
| 107 | var html = ""; |
| 108 | $.each(plugin.results, function(i, v){ |
| 109 | html += '<li data-np-search-result><a href="#" data-np-menu-object="' + plugin.searchObject + '" data-np-menu-type="post_type" data-np-menu-objectid="' + v.term_id + '" data-np-permalink="' + v.permalink + '" data-np-object-name="' + v.taxonomy + '" data-np-menu-selection>' + v.name + '</a></li>'; |
| 110 | }); |
| 111 | $(html).insertAfter($(plugin.activeForm).parent('li')); |
| 112 | plugin.toggleLoading(false); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | // Toggle the loading indicator |
| 117 | plugin.toggleLoading = function(loading) |
| 118 | { |
| 119 | var loadingIndicator = $(plugin.activeForm).siblings(plugin.selectors.loadingIndicator); |
| 120 | $(plugin.selectors.noResults).hide(); |
| 121 | if ( loading ){ |
| 122 | $(loadingIndicator).show(); |
| 123 | return; |
| 124 | } |
| 125 | $(loadingIndicator).hide(); |
| 126 | } |
| 127 | |
| 128 | return plugin.bindEvents(); |
| 129 | } |