Making WordPress.org

Changeset 12775


Ignore:
Timestamp:
08/01/2023 10:17:14 PM (19 months ago)
Author:
coffee2code
Message:

Photo Directory, Admin JS/CSS: Highlight taxonomy metaboxes lacking any set terms.

See #7171.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/assets
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/assets/css/admin.css

    r12694 r12775  
    6060    margin-top: 1rem;
    6161}
     62
     63.photo-missing-taxonomy {
     64    border-color: orange;
     65    background-color: antiquewhite;
     66}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/assets/js/admin.js

    r12774 r12775  
    2323        document.getElementById('photo_orientationdiv').hidden = true;
    2424    }
     25
     26    // Highlight custom taxonomies lacking terms.
     27    highlightCustomTaxonomiesWithoutTerms();
    2528}, false);
     29
     30/**
     31 * Highlights when a custom taxonomy lacks at least one term assignment.
     32 */
     33function highlightCustomTaxonomiesWithoutTerms() {
     34    // Highlight/unhighlight custom taxonomies lacking any term assignments.
     35    const customTaxMetaboxes = [
     36        ['photo_categorydiv', 'tax_input[photo_category][]', 'checkbox'],
     37        ['photo_colordiv', 'tax_input[photo_color][]', 'checkbox'],
     38        ['photo_orientationdiv', 'tax_input[photo_orientation][]', 'checkbox'],
     39        ['tagsdiv-photo_tag', 'newtag[photo_tag]', 'text', '.tagchecklist'],
     40    ];
     41
     42    customTaxMetaboxes.forEach(element => {
     43        // Decide on highlight initially.
     44        setMetaboxHighlight(element);
     45
     46        // Listen for changes in value to re-determine highlight.
     47        document.querySelectorAll('input[name="' + element[1] + '"]').forEach(item => {
     48            item.addEventListener('input', customTaxChangeCB);
     49        });
     50
     51        // The tag list is handled differently. Re-determine as tags are added/removed.
     52        if (element[3]) {
     53            const observer = new MutationObserver(function(mutations_list) {
     54                mutations_list.forEach(function(mutation) {
     55                    if (mutation.addedNodes.length > 0 || mutation.removedNodes.length > 0) {
     56                        setMetaboxHighlight(element);
     57                    }
     58                });
     59            });
     60
     61            observer.observe(document.querySelector(element[3]), { subtree: false, childList: true });
     62        }
     63    });
     64
     65    // Callback to call setMetaboxHighlight() for proper event targets.
     66    function customTaxChangeCB(e) {
     67        const name = e.target.name;
     68        let customTaxMetabox = customTaxMetaboxes.find(n => { return n[1] === name; });
     69        if (!customTaxMetabox) {
     70            return;
     71        }
     72        setMetaboxHighlight(customTaxMetabox)
     73    }
     74
     75    // Highlights or unhighlights a metabox based on presence of terms.
     76    function setMetaboxHighlight(customTaxMetabox) {
     77        [metaboxID, inputName, inputType, tagListClass] = customTaxMetabox;
     78        const missingTaxClass = 'photo-missing-taxonomy';
     79        const div = document.getElementById(metaboxID);
     80
     81        let selector = 'input[name="' + inputName + '"]';
     82        if ('checkbox' === inputType) {
     83            selector += ':checked';
     84        }
     85        let value = document.querySelector(selector)?.value;
     86        // If tagListClass is present, see if it has any values if one not already found.
     87        if (!value && tagListClass) {
     88            value = document.querySelector(tagListClass).hasChildNodes();
     89        }
     90
     91        value ? div?.classList.remove(missingTaxClass) : div?.classList.add(missingTaxClass);
     92    }
     93}
Note: See TracChangeset for help on using the changeset viewer.