| 1 |
/** |
| 2 |
* Submissions list page - export and essential columns functionality. |
| 3 |
* |
| 4 |
* @package ContactForms |
| 5 |
* @since 2.2.5 |
| 6 |
*/ |
| 7 |
/* global jQuery, ajaxurl, pagenow, accuaSubmissionsList */ |
| 8 |
( function( $ ) { |
| 9 |
var config = accuaSubmissionsList || {}; |
| 10 |
|
| 11 |
window.set_parameter = function( sel_column ) { |
| 12 |
var stringa = ''; |
| 13 |
var name_action = ajaxurl + '?action=accua_forms_submission_page_save_excel' |
| 14 |
+ config.exportParams |
| 15 |
+ '&_wpnonce=' + config.exportNonce; |
| 16 |
|
| 17 |
if ( sel_column ) { |
| 18 |
$( '#adv-settings input[type=checkbox]:checked' ).each( function() { |
| 19 |
stringa += this.value + '%2C'; |
| 20 |
} ); |
| 21 |
} else { |
| 22 |
$( '#adv-settings input[type=checkbox]' ).each( function() { |
| 23 |
stringa += this.value + '%2C'; |
| 24 |
} ); |
| 25 |
} |
| 26 |
|
| 27 |
var search = $( '#search_id-search-input' ).val(); |
| 28 |
var pid = $( 'select[name="pid"]' ).val(); |
| 29 |
var fid = $( 'select[name="fid"]' ).val(); |
| 30 |
var year = $( 'select[name="year"]' ).val(); |
| 31 |
var month = $( 'select[name="month"]' ).val(); |
| 32 |
if ( search ) name_action += '&s=' + encodeURIComponent( search ); |
| 33 |
if ( pid && pid !== '-1' ) name_action += '&pid=' + pid; |
| 34 |
if ( fid && fid !== '-1' ) name_action += '&fid=' + fid; |
| 35 |
if ( year && year !== '-1' ) name_action += '&year=' + year; |
| 36 |
if ( month && month !== '-1' ) name_action += '&month=' + month; |
| 37 |
if ( stringa ) name_action += '&accua_show_field=' + stringa; |
| 38 |
|
| 39 |
if ( sel_column ) { |
| 40 |
$( '#esporta_link_visible_column' ).attr( 'href', name_action ); |
| 41 |
} else { |
| 42 |
$( '#esporta_link_all_column' ).attr( 'href', name_action ); |
| 43 |
} |
| 44 |
}; |
| 45 |
|
| 46 |
window.accuaToggleAllRows = function( open ) { |
| 47 |
$( '#accua_forms_submissions_list_page .wp-list-table .accua-expandable-cell' ).each( function() { |
| 48 |
if ( this.classList.contains( 'open' ) !== open ) { |
| 49 |
this.querySelector( '.accua-expandable-toggle' ).click(); |
| 50 |
} |
| 51 |
} ); |
| 52 |
}; |
| 53 |
|
| 54 |
/** |
| 55 |
* Sticky horizontal scrollbar for the submissions table. |
| 56 |
* |
| 57 |
* The table lives in .accua-table-scroll (see the display() override) so |
| 58 |
* that a table wider than the screen scrolls on its own instead of |
| 59 |
* dragging the whole admin page sideways. That box is as tall as the |
| 60 |
* table, though, so its scrollbar sits at the foot of up to a hundred |
| 61 |
* rows: invisible, and out of reach for anyone without a trackpad or the |
| 62 |
* shift+wheel habit. |
| 63 |
* |
| 64 |
* So: a proxy scrollbar that rides the bottom of the viewport while the |
| 65 |
* table is on screen, scrolling the box as it moves and following it back. |
| 66 |
* The real scrollbar is hidden only once this is in place (the class is |
| 67 |
* added here, not in the stylesheet), so the native one still works if |
| 68 |
* this script never runs. |
| 69 |
*/ |
| 70 |
function initStickyScrollbar() { |
| 71 |
var box = document.querySelector( '#accua_forms_submissions_list_page .accua-table-scroll' ); |
| 72 |
if ( ! box || box.nextElementSibling && box.nextElementSibling.classList.contains( 'accua-table-scrollbar' ) ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
var bar = document.createElement( 'div' ); |
| 77 |
bar.className = 'accua-table-scrollbar'; |
| 78 |
// It duplicates a scroll the box already offers; keep it out of the |
| 79 |
// accessibility tree and out of the tab order. |
| 80 |
bar.setAttribute( 'aria-hidden', 'true' ); |
| 81 |
var inner = document.createElement( 'div' ); |
| 82 |
bar.appendChild( inner ); |
| 83 |
box.insertAdjacentElement( 'afterend', bar ); |
| 84 |
|
| 85 |
// Mirroring scrollLeft both ways means each assignment fires a scroll |
| 86 |
// event on the other element, which would bounce straight back. The |
| 87 |
// guard is the comparison, not a flag: by the time the echo arrives |
| 88 |
// the two already agree and it stops there. A flag would be worse than |
| 89 |
// useless here - scroll events are asynchronous, and assigning a value |
| 90 |
// the element already has fires nothing at all, so a flag set on the |
| 91 |
// way out could sit raised forever and swallow the next real scroll. |
| 92 |
function mirror( from, to ) { |
| 93 |
return function () { |
| 94 |
if ( to.scrollLeft !== from.scrollLeft ) { |
| 95 |
to.scrollLeft = from.scrollLeft; |
| 96 |
} |
| 97 |
}; |
| 98 |
} |
| 99 |
bar.addEventListener( 'scroll', mirror( bar, box ) ); |
| 100 |
box.addEventListener( 'scroll', mirror( box, bar ) ); |
| 101 |
|
| 102 |
function refresh() { |
| 103 |
var overflows = box.scrollWidth > box.clientWidth; |
| 104 |
bar.style.display = overflows ? '' : 'none'; |
| 105 |
box.classList.toggle( 'has-sticky-scrollbar', overflows ); |
| 106 |
inner.style.width = box.scrollWidth + 'px'; |
| 107 |
} |
| 108 |
|
| 109 |
refresh(); |
| 110 |
window.addEventListener( 'resize', refresh ); |
| 111 |
|
| 112 |
// Expanding a cell rewraps its content, which can change how wide the |
| 113 |
// table wants to be; so can showing or hiding a column. |
| 114 |
if ( window.ResizeObserver ) { |
| 115 |
new window.ResizeObserver( refresh ).observe( box.firstElementChild || box ); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
$( window ).on( 'load', function () { |
| 120 |
// expandable-cells.js measures and rewrites cells on load too; let it |
| 121 |
// finish first so the width read here is the final one. |
| 122 |
window.setTimeout( initStickyScrollbar, 0 ); |
| 123 |
} ); |
| 124 |
|
| 125 |
window.setEssentialColumns = function() { |
| 126 |
// Canonical list comes from Accua_Forms_Submissions_List_Table::essential_columns() |
| 127 |
// (always-essential main columns + the fields flagged "Show in essential |
| 128 |
// columns" on the Fields page); the literal is only a fallback. |
| 129 |
var essentialCols = config.essentialCols || [ 'singlesub', 'ID', 'form_title', 'submitted' ]; |
| 130 |
var changed = false; |
| 131 |
|
| 132 |
$( '#adv-settings input.hide-column-tog' ).each( function() { |
| 133 |
var col = this.value; |
| 134 |
var shouldBeVisible = essentialCols.indexOf( col ) !== -1; |
| 135 |
if ( $( this ).prop( 'checked' ) !== shouldBeVisible ) { |
| 136 |
changed = true; |
| 137 |
$( this ).prop( 'checked', shouldBeVisible ); |
| 138 |
$( 'th[id="' + col + '"]' ).toggleClass( 'hidden', ! shouldBeVisible ); |
| 139 |
} |
| 140 |
} ); |
| 141 |
|
| 142 |
if ( ! changed ) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
var hidden = $( '.manage-column[id]' ).filter( '.hidden' ).map( function() { |
| 147 |
return this.id; |
| 148 |
} ).get().join( ',' ); |
| 149 |
|
| 150 |
$.post( ajaxurl, { |
| 151 |
action: 'hidden-columns', |
| 152 |
hidden: hidden, |
| 153 |
screenoptionnonce: $( '#screenoptionnonce' ).val(), |
| 154 |
page: pagenow |
| 155 |
} ).always( function() { |
| 156 |
location.reload(); |
| 157 |
} ); |
| 158 |
}; |
| 159 |
}( jQuery ) ); |
| 160 |
|