diff --git a/archive-template.php b/archive-template.php index 2fc56d20e5..a01f5ceca8 100644 --- a/archive-template.php +++ b/archive-template.php @@ -463,10 +463,12 @@ class="second-bar show-for-small-only center list-actions-bar"> -
- -
- -
- $field_values ): - if ( !empty( $field_values['hidden'] ) ){ - continue; - } - $has_icon = !empty( $field_values['icon'] ) || !empty( $field_values['font-icon'] ); - $option_classes = 'field-search-option' . ( $has_icon ? '' : ' no-icon' ); - ?> -
- - -
- -
-
-
+ $field_values ) { + if ( ! empty( $field_values['hidden'] ) ) { + continue; + } + $field_search_options[ $field_key ] = [ + 'label' => $field_values['name'] ?? $field_key, + 'icon' => $field_values['font-icon'] ?? $field_values['icon'] ?? '', + 'color' => $field_values['color'] ?? null, + ]; + } + ?> - -
- -
- - - - - - - - - - - - - - + +
+
+ [ + 'name' => 'field_search_input', + 'type' => 'multi_select', + 'display' => 'typeahead', + 'default' => $field_search_options, + ], + ], [ 'field_search_input' => $enabled_fields ], [ + 'hide_label' => true, + 'placeholder' => __( 'Search for fields to add...', 'disciple_tools' ), + ] ); ?> +
-
- - - @@ -973,52 +951,16 @@ class="loading-spinner">
- $field ) : - $is_multi_select = isset( $field_options[$field] ) && ( in_array( $field_options[$field]['type'], [ 'multi_select', 'tags' ] ) ); - if ( isset( $field_options[$field] ) && ( $field_options[$field]['type'] === 'connection' || $field_options[$field]['type'] === 'location' || $field_options[$field]['type'] === 'location_meta' || $field_options[$field]['type'] === 'user_select' || $is_multi_select ) ) : ?> -
-
-
- -
-
-
- - - -
-
-
-
- -

- - -

- - -

- -

- -
- - -
-
-
- $field ) : ?> +
+
+
+ +
- - -
- -
-
- -
- - - - +
$field_options[$field] + ], [], [ 'hide_label' => true, 'placeholder' => __( 'Type to search', 'disciple_tools' ) ] ); + break; + case 'tags': + DT_Components::render_tags( $field, [ + $field => $field_options[$field] + ], [], [ 'allow_add' => false, 'hide_label' => true, 'placeholder' => __( 'Type to search', 'disciple_tools' ) ] ); + break; + case 'user_select': + DT_Components::render_user_select( $field, [ + $field => $field_options[$field] + ], [], [ 'hide_label' => true, 'placeholder' => __( 'Type to search', 'disciple_tools' ) ] ); + break; + case 'connection': + DT_Components::render_connection( $field, [ + $field => $field_options[$field] + ], [], [ 'allow_add' => false, 'hide_label' => true, 'placeholder' => __( 'Type to search', 'disciple_tools' ) ] ); + ?>

+ + +

+

+ +

+ $field_options[$field] + ], [], [ 'hide_label' => true, 'placeholder' => __( 'Type to search', 'disciple_tools' ) ] ); + break; + case 'date': + DT_Components::render_date( $field . '_start', [ + $field . '_start' => [ + 'name' => $field_options[$field]['name'] . ' Start', + 'type' => $field_options[$field]['type'] + ] + ], [], [ 'hide_label' => true ] ); + ?>
- - - - - - -

- - - - -

- + [ + 'name' => $field_options[$field]['name'] . ' End', + 'type' => $field_options[$field]['type'], + 'icon' => $field_options[$field]['icon'] + ] + ], [], [ 'hide_label' => true ] ); + break; + case 'datetime': + DT_Components::render_datetime( $field, [ + $field => $field_options[$field] + ], [], [ 'hide_label' => true ] ); + break; + case 'boolean':?> +
+
+ +
+
+ +
+
$field_options[$field] + ], [], [ 'hide_label' => true, 'placeholder' => __( 'Filter by value', 'disciple_tools' ) ] ); + ?> +

+ + + + +

$field_options[$field] + ], [], [ 'hide_label' => true, 'placeholder' => __( 'Filter by value', 'disciple_tools' ) ] ); + break; + case 'number': + DT_Components::render_number( $field, [ + $field => $field_options[$field] + ], [], [ 'hide_label' => true, 'placeholder' => __( 'Filter by value', 'disciple_tools' ) ] ); + break; + case 'file_upload': + ?>

-

- -
+

Unsupported field type: ' . esc_html( $type ) . '
'; + } + ?>
- - +
diff --git a/dt-assets/build/components/index.es.js b/dt-assets/build/components/index.es.js index b3a1620b02..6797d0d28f 100644 --- a/dt-assets/build/components/index.es.js +++ b/dt-assets/build/components/index.es.js @@ -1,4 +1,4 @@ -import { F as e, E as s, y as D, C as o, D as l, a as i, b as n, d as r, e as c, f as u, w as p, x as M, B as T, g as x, h as C, i as m, j as S, z as d, k as g, u as B, r as h, t as v, l as A, m as F, n as b, o as L, p as f, A as y, q as G, c as U, v as j } from "./index-SpDyu9zt.js"; +import { F as e, E as s, y as D, C as o, D as l, a as i, b as n, d as r, e as c, f as u, w as p, x as M, B as T, g as x, h as C, i as m, j as S, z as d, k as g, u as B, r as h, t as v, l as A, m as F, n as b, o as L, p as f, A as y, q as G, c as U, v as j } from "./index-BzeFGoyc.js"; export { e as ApiService, s as ComponentService, diff --git a/dt-assets/build/components/index.js b/dt-assets/build/components/index.js index 8c8b326305..d1b78ad74e 100644 --- a/dt-assets/build/components/index.js +++ b/dt-assets/build/components/index.js @@ -909,6 +909,12 @@ var DtWebComponents=function(x){"use strict";var Mr=Object.defineProperty;var jr display: block; } .health-item { + /* Fill the square icon slot so percentage-based child heights + resolve against a definite height (rather than collapsing to + auto, which let tall SVGs overflow). */ + box-sizing: border-box; + width: 100%; + height: 100%; padding: 0.5rem; img, @@ -918,6 +924,9 @@ var DtWebComponents=function(x){"use strict";var Mr=Object.defineProperty;var jr } img { + /* Constrain the image to its square slot while preserving the + icon's aspect ratio, so non-square SVGs don't overflow. */ + object-fit: contain; /* remove color from images and then semi-transparent */ filter: grayscale(1) opacity(0.75); } @@ -1448,7 +1457,7 @@ var DtWebComponents=function(x){"use strict";var Mr=Object.defineProperty;var jr .invalid { border-color: var(--dt-form-border-color-alert, var(--alert-color)); } - `]}_clickOption(e){if(e.target&&e.target.value){const t=parseInt(e.target.value,10),i=this.filteredOptions.reduce((o,a)=>!o&&a.id==t?a:o,null);i&&this._select(i)}}_clickAddNew(e){var t,i;if(e.target){this._select({id:(t=e.target.dataset)==null?void 0:t.label,label:(i=e.target.dataset)==null?void 0:i.label,isNew:!0});const o=this.shadowRoot.querySelector("input");o&&(o.value="")}}_keyboardSelectOption(){this.activeIndex>-1&&(this.activeIndex+1>this.filteredOptions.length?this._select({id:this.query,label:this.query,isNew:!0}):this._select(this.filteredOptions[this.activeIndex]))}_remove(e){if(e.stopPropagation(),e.target&&e.target.dataset&&e.target.dataset.value){let t=e.target.dataset.value;const i=Number.parseInt(t);Number.isNaN(i)||(t=i);const o=new CustomEvent("change",{detail:{field:this.name,oldValue:this.value}});this.value=(this.value||[]).map(a=>{const n={...a};return a.id===t&&(n.delete=!0),n}),o.detail.newValue=this.value,this.dispatchEvent(o),this.open&&this.shadowRoot.querySelector("input").focus(),this._validateRequired()}document.activeElement.blur()}_filterOptions(){var t;const e=(this.value||[]).filter(i=>!i.delete).map(i=>i==null?void 0:i.id);if((t=this.options)!=null&&t.length)this.filteredOptions=(this.options||[]).filter(i=>!e.includes(i.id)&&(!this.query||i.label.toLocaleLowerCase().includes(this.query.toLocaleLowerCase())));else if(this.allOptions)this.canUpdate=!1,this.filteredOptions=this.allOptions.filter(i=>!e.includes(i.id));else if(this.open){this.canUpdate=!1,this.loading=!0,this.filteredOptions=[];const i=this,o=new CustomEvent("dt:get-data",{bubbles:!0,detail:{field:this.name,postType:this.postType,query:this.query,onSuccess:a=>{i.loading=!1,i.allOptions=a,i.filteredOptions=a.filter(n=>!e.includes(n.id))},onError:a=>{console.warn(a),i.loading=!1}}});this.dispatchEvent(o)}return this.filteredOptions}_validateRequired(){const{value:e}=this;this.required&&(!e||e.every(t=>!t||t.delete))?(this.invalid=!0,this.internals.setValidity({valueMissing:!0},this.requiredMessage||"This field is required",this._field)):(this.invalid=!1,this.internals.setValidity({}))}_renderSelectedOptions(){return(this.value||[]).filter(e=>!e.delete).map(e=>c` + `]}_clickOption(e){if(e.target&&e.target.value){const t=parseInt(e.target.value,10),i=this.filteredOptions.reduce((o,a)=>!o&&a.id==t?a:o,null);i&&this._select(i)}}_clickAddNew(e){var t,i;if(e.target){this._select({id:(t=e.target.dataset)==null?void 0:t.label,label:(i=e.target.dataset)==null?void 0:i.label,isNew:!0});const o=this.shadowRoot.querySelector("input");o&&(o.value="")}}_keyboardSelectOption(){this.activeIndex>-1&&(this.activeIndex+1>this.filteredOptions.length?this._select({id:this.query,label:this.query,isNew:!0}):this._select(this.filteredOptions[this.activeIndex]))}_remove(e){if(e.stopPropagation(),e.target&&e.target.dataset&&e.target.dataset.value){let t=e.target.dataset.value;const i=Number.parseInt(t);Number.isNaN(i)||(t=i);const o=new CustomEvent("change",{bubbles:!0,detail:{field:this.name,oldValue:this.value}});this.value=(this.value||[]).map(a=>{const n={...a};return a.id===t&&(n.delete=!0),n}),o.detail.newValue=this.value,this.dispatchEvent(o),this.open&&this.shadowRoot.querySelector("input").focus(),this._validateRequired()}document.activeElement.blur()}_filterOptions(){var t;const e=(this.value||[]).filter(i=>!i.delete).map(i=>i==null?void 0:i.id);if((t=this.options)!=null&&t.length)this.filteredOptions=(this.options||[]).filter(i=>!e.includes(i.id)&&(!this.query||i.label.toLocaleLowerCase().includes(this.query.toLocaleLowerCase())));else if(this.allOptions)this.canUpdate=!1,this.filteredOptions=this.allOptions.filter(i=>!e.includes(i.id));else if(this.open){this.canUpdate=!1,this.loading=!0,this.filteredOptions=[];const i=this,o=new CustomEvent("dt:get-data",{bubbles:!0,detail:{field:this.name,postType:this.postType,query:this.query,onSuccess:a=>{i.loading=!1,i.allOptions=a,i.filteredOptions=a.filter(n=>!e.includes(n.id))},onError:a=>{console.warn(a),i.loading=!1}}});this.dispatchEvent(o)}return this.filteredOptions}_validateRequired(){const{value:e}=this;this.required&&(!e||e.every(t=>!t||t.delete))?(this.invalid=!0,this.internals.setValidity({valueMissing:!0},this.requiredMessage||"This field is required",this._field)):(this.invalid=!1,this.internals.setValidity({}))}_renderSelectedOptions(){return(this.value||[]).filter(e=>!e.delete).map(e=>c` ${this.readonly?c`
${e.link?c`${e.label}`:c`
${e.label}
`}
`:c` @@ -1568,7 +1577,7 @@ var DtWebComponents=function(x){"use strict";var Mr=Object.defineProperty;var jr li button svg use { fill: var(--dt-users-connection-icon-fill, var(--primary-color)); } - `]}static get properties(){return{...super.properties,single:{type:Boolean}}}_select(e){const t=new CustomEvent("change",{bubbles:!0,detail:{field:this.name,oldValue:this.value}});if(this.value&&this.value.length){let i=!1,o=this.value.map(a=>{const n={...a};return a.id===e.id&&a.delete?(delete n.delete,i=!0):this.single&&!a.delete&&(n.delete=!0),n});i||o.push(e),this.single&&(o=o.filter(a=>!a.delete)),this.value=o}else this.value=[e];t.detail.newValue=this.value,this.open=!1,this.activeIndex=-1,this.canUpdate=!0,this.dispatchEvent(t),this._clearSearch()}_clickOption(e){if(e.target&&e.target.value){const t=parseInt(e.target.value,10),i=this.filteredOptions.reduce((o,a)=>!o&&a.id==t?a:o,null);i&&this._select(i),this.query=""}}_clickAddNew(e){var t,i;if(e.target){this._select({id:(t=e.target.dataset)==null?void 0:t.label,label:(i=e.target.dataset)==null?void 0:i.label,isNew:!0});const o=this.shadowRoot.querySelector("input");o&&(o.value="",this.query="")}}_keyboardSelectOption(){this.activeIndex>-1&&(this.activeIndex+1>this.filteredOptions.length?this._select({id:this.query,label:this.query,isNew:!0}):this._select(this.filteredOptions[this.activeIndex]),this.query="")}_remove(e){if(e.target&&e.target.dataset&&e.target.dataset.value){const t=new CustomEvent("change",{detail:{field:this.name,oldValue:this.value,remove:!0}});this.value=(this.value||[]).map(i=>{const o={...i};return i.id.toString()===e.target.dataset.value&&(o.delete=!0),o}),t.detail.newValue=this.value,this.dispatchEvent(t),this.open&&this.shadowRoot.querySelector("input").focus()}}_filterOptions(){var t;const e=(this.value||[]).filter(i=>!i.delete).map(i=>Number(i==null?void 0:i.id));if((t=this.options)!=null&&t.length)this.filteredOptions=(this.options||[]).filter(i=>!e.includes(i.id)&&(!this.query||i.label.toLocaleLowerCase().includes(this.query.toLocaleLowerCase())));else if(this.allOptions)this.canUpdate=!1,this.filteredOptions=this.allOptions.filter(i=>!e.includes(i.id));else if(this.open){this.canUpdate=!1,this.loading=!0,this.filteredOptions=[];const i=this,o=new CustomEvent("dt:get-data",{bubbles:!0,detail:{field:this.name,postType:this.postType,query:this.query,onSuccess:a=>{i.loading=!1,i.allOptions=a,i.filteredOptions=a.filter(n=>!e.includes(n.id))},onError:a=>{console.warn(a),i.loading=!1}}});this.dispatchEvent(o)}return this.filteredOptions}_renderSelectedOptions(){return(this.value||[]).filter(e=>!e.delete).map(e=>c` + `]}static get properties(){return{...super.properties,single:{type:Boolean}}}_select(e){const t=new CustomEvent("change",{bubbles:!0,detail:{field:this.name,oldValue:this.value}});if(this.value&&this.value.length){let i=!1,o=this.value.map(a=>{const n={...a};return a.id===e.id&&a.delete?(delete n.delete,i=!0):this.single&&!a.delete&&(n.delete=!0),n});i||o.push(e),this.single&&(o=o.filter(a=>!a.delete)),this.value=o}else this.value=[e];t.detail.newValue=this.value,this.open=!1,this.activeIndex=-1,this.canUpdate=!0,this.dispatchEvent(t),this._clearSearch()}_clickOption(e){if(e.target&&e.target.value){const t=parseInt(e.target.value,10),i=this.filteredOptions.reduce((o,a)=>!o&&a.id==t?a:o,null);i&&this._select(i),this.query=""}}_clickAddNew(e){var t,i;if(e.target){this._select({id:(t=e.target.dataset)==null?void 0:t.label,label:(i=e.target.dataset)==null?void 0:i.label,isNew:!0});const o=this.shadowRoot.querySelector("input");o&&(o.value="",this.query="")}}_keyboardSelectOption(){this.activeIndex>-1&&(this.activeIndex+1>this.filteredOptions.length?this._select({id:this.query,label:this.query,isNew:!0}):this._select(this.filteredOptions[this.activeIndex]),this.query="")}_remove(e){if(e.target&&e.target.dataset&&e.target.dataset.value){const t=new CustomEvent("change",{bubbles:!0,detail:{field:this.name,oldValue:this.value,remove:!0}});this.value=(this.value||[]).map(i=>{const o={...i};return i.id.toString()===e.target.dataset.value&&(o.delete=!0),o}),t.detail.newValue=this.value,this.dispatchEvent(t),this.open&&this.shadowRoot.querySelector("input").focus()}}_filterOptions(){var t;const e=(this.value||[]).filter(i=>!i.delete).map(i=>Number(i==null?void 0:i.id));if((t=this.options)!=null&&t.length)this.filteredOptions=(this.options||[]).filter(i=>!e.includes(i.id)&&(!this.query||i.label.toLocaleLowerCase().includes(this.query.toLocaleLowerCase())));else if(this.allOptions)this.canUpdate=!1,this.filteredOptions=this.allOptions.filter(i=>!e.includes(i.id));else if(this.open){this.canUpdate=!1,this.loading=!0,this.filteredOptions=[];const i=this,o=new CustomEvent("dt:get-data",{bubbles:!0,detail:{field:this.name,postType:this.postType,query:this.query,onSuccess:a=>{i.loading=!1,i.allOptions=a,i.filteredOptions=a.filter(n=>!e.includes(n.id))},onError:a=>{console.warn(a),i.loading=!1}}});this.dispatchEvent(o)}return this.filteredOptions}_renderSelectedOptions(){return(this.value||[]).filter(e=>!e.delete).map(e=>c` ${this.readonly?c`
${e.link?c`${e.label}`:c`
${e.label}
`}
`:c` @@ -1877,7 +1886,7 @@ var DtWebComponents=function(x){"use strict";var Mr=Object.defineProperty;var jr 8px ); } - `]}_clickOption(e){if(e.target&&e.target.value){const t=e.target.value,i=this.filteredOptions.reduce((o,a)=>!o&&a.id===t?a:o,null);this._select(i)}}_clickAddNew(e){var t,i;if(e.target){this._select({id:(t=e.target.dataset)==null?void 0:t.label,label:(i=e.target.dataset)==null?void 0:i.label,isNew:!0});const o=this.shadowRoot.querySelector("input");o&&(o.value="")}}_keyboardSelectOption(){this.activeIndex>-1&&(this.activeIndex+1>this.filteredOptions.length?this._select({id:this.query,label:this.query,isNew:!0}):this._select(this.filteredOptions[this.activeIndex]))}_remove(e){if(e.target&&e.target.dataset&&e.target.dataset.value){const t=new CustomEvent("change",{detail:{field:this.name,oldValue:this.value}});this.value=(this.value||[]).map(i=>{const o={...i};return i.id.toString()===e.target.dataset.value&&(o.delete=!0),o}),t.detail.newValue=this.value,this.dispatchEvent(t),this.open&&this.shadowRoot.querySelector("input").focus()}}updated(e){super.updated(e);const t=this.shadowRoot.querySelector(".input-group"),i=t.style.getPropertyValue("--select-width"),o=this.shadowRoot.querySelector("select");!i&&(o==null?void 0:o.clientWidth)>0&&t.style.setProperty("--select-width",`${o.clientWidth}px`)}_filterOptions(){var t;const e=(this.value||[]).filter(i=>!i.delete).map(i=>i==null?void 0:i.id.toString());if((t=this.options)!=null&&t.length)this.filteredOptions=(this.options||[]).filter(i=>!e.includes(i.id)&&(!this.query||i.label.toLocaleLowerCase().includes(this.query.toLocaleLowerCase())));else if(this.open||this.canUpdate){this.loading=!0,this.filteredOptions=[];const i=this,o=this.shadowRoot.querySelector("select"),a=new CustomEvent("dt:get-data",{bubbles:!0,detail:{field:this.name,query:this.query,filter:o==null?void 0:o.value,onSuccess:n=>{i.loading=!1,i.filteredOptions=n.filter(r=>!e.includes(r.id))},onError:n=>{console.warn(n),i.loading=!1}}});this.dispatchEvent(a)}return this.filteredOptions}_renderOption(e,t){return c` + `]}_clickOption(e){if(e.target&&e.target.value){const t=e.target.value,i=this.filteredOptions.reduce((o,a)=>!o&&a.id===t?a:o,null);this._select(i)}}_clickAddNew(e){var t,i;if(e.target){this._select({id:(t=e.target.dataset)==null?void 0:t.label,label:(i=e.target.dataset)==null?void 0:i.label,isNew:!0});const o=this.shadowRoot.querySelector("input");o&&(o.value="")}}_keyboardSelectOption(){this.activeIndex>-1&&(this.activeIndex+1>this.filteredOptions.length?this._select({id:this.query,label:this.query,isNew:!0}):this._select(this.filteredOptions[this.activeIndex]))}_remove(e){if(e.target&&e.target.dataset&&e.target.dataset.value){const t=new CustomEvent("change",{bubbles:!0,detail:{field:this.name,oldValue:this.value}});this.value=(this.value||[]).map(i=>{const o={...i};return i.id.toString()===e.target.dataset.value&&(o.delete=!0),o}),t.detail.newValue=this.value,this.dispatchEvent(t),this.open&&this.shadowRoot.querySelector("input").focus()}}updated(e){super.updated(e);const t=this.shadowRoot.querySelector(".input-group"),i=t.style.getPropertyValue("--select-width"),o=this.shadowRoot.querySelector("select");!i&&(o==null?void 0:o.clientWidth)>0&&t.style.setProperty("--select-width",`${o.clientWidth}px`)}_filterOptions(){var t;const e=(this.value||[]).filter(i=>!i.delete).map(i=>i==null?void 0:i.id.toString());if((t=this.options)!=null&&t.length)this.filteredOptions=(this.options||[]).filter(i=>!e.includes(i.id)&&(!this.query||i.label.toLocaleLowerCase().includes(this.query.toLocaleLowerCase())));else if(this.open||this.canUpdate){this.loading=!0,this.filteredOptions=[];const i=this,o=this.shadowRoot.querySelector("select"),a=new CustomEvent("dt:get-data",{bubbles:!0,detail:{field:this.name,query:this.query,filter:o==null?void 0:o.value,onSuccess:n=>{i.loading=!1,i.filteredOptions=n.filter(r=>!e.includes(r.id))},onError:n=>{console.warn(n),i.loading=!1}}});this.dispatchEvent(a)}return this.filteredOptions}_renderOption(e,t){return c`
  • - `; - - const container = $('#enabled_fields_container'); - // Remove "no fields selected" message if present - container.find('span:contains("No fields selected")').remove(); - container.append(tag); - } - - // Clear search and close dropdown - $('#field_search_input').val(''); - $('#field_search_dropdown').hide(); - }); - - // Remove field when clicking X button - $(document).on('click', '.remove-field-btn', function () { - const fieldKey = $(this).data('field-key'); - - // Remove from selected fields - let selectedFields; - try { - selectedFields = JSON.parse($('#selected_fields_input').val() || '[]'); - } catch (e) { - console.error('Error parsing selected fields JSON:', e); - selectedFields = []; - } - selectedFields = selectedFields.filter((key) => key !== fieldKey); - $('#selected_fields_input').val(JSON.stringify(selectedFields)); - - // Remove visual tag - $(this).closest('.enabled-field-tag').remove(); - - // Show "no fields selected" message if empty - if (selectedFields.length === 0) { - $('#enabled_fields_container').html( - 'No fields selected', - ); - } - - // Refresh dropdown if it's currently visible to show the newly available field - if ($('#field_search_dropdown').is(':visible')) { - const searchTerm = $('#field_search_input').val().toLowerCase(); - if (searchTerm.length === 0) { - showFieldDropdown(); - } else { - filterFieldDropdown(searchTerm); - } - } - }); - - $('#save_column_choices').on('click', function () { - let selectedFields; - try { - selectedFields = JSON.parse($('#selected_fields_input').val() || '[]'); - } catch (e) { - console.error('Error parsing selected fields JSON:', e); - selectedFields = []; - } - fields_to_show_in_table = selectedFields; window.SHAREDFUNCTIONS.save_json_cookie( 'fields_to_show_in_table', fields_to_show_in_table, @@ -1083,15 +896,11 @@ ); window.location.reload(); }); + $('#reset_column_choices').on('click', function () { - fields_to_show_in_table = []; - $('#selected_fields_input').val('[]'); - $('#enabled_fields_container').html( - 'No fields selected', - ); window.SHAREDFUNCTIONS.save_json_cookie( 'fields_to_show_in_table', - fields_to_show_in_table, + [], list_settings.post_type, ); window.location.reload(); @@ -1575,129 +1384,79 @@ list_settings, `post_type_settings.fields.${field}.type`, ); - if (type === 'connection') { - const allConnections = $(`#${field} .all-connections`); - const withoutConnections = $(`#${field} .all-without-connections`); - if (allConnections.prop('checked') === true) { - search_query.push({ [field]: [ALL_ID] }); - } else if (withoutConnections.prop('checked') === true) { - search_query.push({ [field]: [ALL_WITHOUT_ID] }); - } else { - search_query.push({ - [field]: adjust_search_query_filter_states( - field, - type, - window.lodash.map( - window.lodash.get( - window.Typeahead[`.js-typeahead-${field}`], - 'items', - ), - 'ID', - ), - ), - }); - } - } - if (type === 'user_select') { - search_query.push({ - [field]: adjust_search_query_filter_states( - field, - type, - window.lodash.map( - window.lodash.get( - window.Typeahead[`.js-typeahead-${field}`], - 'items', - ), - 'ID', - ), - ), - }); - } else if (type === 'multi_select') { - search_query.push({ - [field]: adjust_search_query_filter_states( - field, - type, - window.lodash.map( - window.lodash.get( - window.Typeahead[`.js-typeahead-${field}`], - 'items', - ), - 'key', - ), - ), - }); - } else if (type === 'tags') { - search_query.push({ - [field]: adjust_search_query_filter_states( - field, - type, - window.lodash.map( - window.lodash.get( - window.Typeahead[`.js-typeahead-${field}`], - 'items', - ), - 'key', - ), - ), - }); - } else if (type === 'location' || type === 'location_meta') { - search_query.push({ - location_grid: adjust_search_query_filter_states( - 'location_grid', - type, - window.lodash.map( - window.lodash.get( - window.Typeahead[`.js-typeahead-${field}`], - 'items', - ), - 'ID', - ), - ), - }); - } else if (type === 'date' || type === 'datetime') { - let date = {}; - let start = $( - `.dt_date_picker[data-field="${field}"][data-delimit="start"]`, - ).val(); - if (start) { - date.start = start; - } - let end = $( - `.dt_date_picker[data-field="${field}"][data-delimit="end"]`, - ).val(); - if (end) { - date.end = end; - } - search_query.push({ [field]: date }); - } else if (type === 'text' || type === 'communication_channel') { - let filter = $('#' + field + '_text_comms_filter').val(); - let value = filter; - - switch ($('.filter-by-text-comms-option:checked').val()) { - case 'all-with-set-value': { - value = '*'; - break; - } - case 'all-without-set-value': { - value = null; - break; + let customComponent = document.querySelector(`[name="${field}"]`); + + if (customComponent) { + let val = []; + + if (type === 'connection') { + const allConnections = $(`#${field} .all-connections`); + const withoutConnections = $(`#${field} .all-without-connections`); + if (allConnections.prop('checked') === true) { + search_query.push({ [field]: [ALL_ID] }); + } else if (withoutConnections.prop('checked') === true) { + search_query.push({ [field]: [ALL_WITHOUT_ID] }); + } else { + val = customComponent.value + .filter((label) => !label.delete) + .map((item) => item.id); + search_query.push({ + [field]: adjust_search_query_filter_states(field, type, val), + }); } - case 'all-with-filtered-value': { - value = filter; - break; + } else if (type === 'text' || type === 'communication_channel') { + val = customComponent.value; + + switch ( + $( + `#filter_by_text_comms_option_${field} .filter-by-text-comms-option:checked`, + ).val() + ) { + case 'all-with-set-value': { + val = '*'; + break; + } + case 'all-without-set-value': { + val = null; + break; + } + case 'all-with-filtered-value': { + val = customComponent.value; + break; + } + case 'all-without-filtered-value': { + val = '-' + customComponent.value; + break; + } } - case 'all-without-filtered-value': { - value = '-' + filter; - break; + + search_query.push({ [field]: val !== null ? [val] : [] }); + } else { + // Extract the value from each field type + if ( + type === 'user_select' || + type === 'location' || + type === 'location_meta' + ) { + val = customComponent.value + .filter((label) => !label.delete) + .map((item) => item.id); + } else if (type === 'multi_select' || type === 'tags') { + val = customComponent.value.filter( + (label) => label.charAt(0) !== '-', + ); + } else { + // For single selects or toggles + val = customComponent.value ? [customComponent.value] : []; } - } - // Package accordingly based on field type. - switch (type) { - case 'text': - case 'communication_channel': { - search_query.push({ [field]: value !== null ? [value] : [] }); - break; + if (val && val.length > 0) { + if (type === 'location_meta' || type === 'location') { + field = 'location_grid'; + } + search_query.push({ + [field]: adjust_search_query_filter_states(field, type, val), + }); } } } else if (type === 'file_upload') { @@ -1709,6 +1468,17 @@ } else if (selectedOption === 'all-without-files') { search_query.push({ [field]: [] }); } + } else if (type === 'date' || type === 'datetime') { + let date = {}; + let start = $(`#${field}_start`).val(); + if (start) { + date.start = start; + } + let end = $(`#${field}_end`).val(); + if (end) { + date.end = end; + } + search_query.push({ [field]: date }); } else { let options = []; $(`#${field}-options input:checked`).each(function () { @@ -1865,7 +1635,6 @@ // Prefix exclusion flag, accordingly return (excluded.length > 0 ? '-' : '') + value; }); - return adjusted_filters; } @@ -1905,15 +1674,14 @@ const id = without ? ALL_WITHOUT_ID : ALL_ID; const tabsPanel = $(this).closest('.tabs-panel'); const field = tabsPanel.length === 1 ? tabsPanel[0].id : ''; - const typeaheadQueryElement = tabsPanel.find('.typeahead__query'); - const typeaheadCancelButtons = tabsPanel.find('.typeahead__cancel-button'); - const typeahead = tabsPanel.find(`.js-typeahead-${field}`); + const connectionElement = tabsPanel.find(`#${field}`); toggle_all_connection_option(tabsPanel, without); if ($(this).prop('checked') === true) { - typeahead.prop('disabled', true); - typeaheadQueryElement.addClass('disabled'); + // disable connection field's input when this happens (no longer typeahead) + connectionElement.prop('disabled', true); + connectionElement.val(''); // remove the current filters and leave anything in the typeahead as it is remove_all_filter_labels(field); const { newLabel, filterName } = create_label_all( @@ -1927,14 +1695,8 @@ ); new_filter_labels.push(newLabel); } else { - typeahead.prop('disabled', false); - typeaheadQueryElement.removeClass('disabled'); + connectionElement.prop('disabled', false); remove_filter_labels(id, field); - // clear the typeahead by manually clicking each selected item. - // This is done at this point as it triggers the typeahead to open which we don't want just after we have disabled it. - typeaheadCancelButtons.each(function () { - $(this).trigger('click', { botClick: true }); - }); } } @@ -1978,24 +1740,13 @@ }; } - function create_location_label(field, id, value, listSettings) { - let name = window.lodash.get( - listSettings, - `post_type_settings.fields.location_grid.name`, - 'location_grid', - ); - return { - newLabel: { id, name: `${name}: ${value}`, field, type: 'location_grid' }, - name, - }; - } - function create_date_label(field, date, delimiter) { let field_name = window.lodash.get( list_settings, `post_type_settings.fields.${field}.name`, field, ); + let delimiter_label = list_settings.translations[`range_${delimiter}`]; return { @@ -2005,8 +1756,19 @@ field, date: date, }, - field_name, - delimiter_label, + field_name: `${field_name} ${delimiter_label}`, + }; + } + + function create_location_label(field, id, value, listSettings) { + let name = window.lodash.get( + listSettings, + `post_type_settings.fields.location_grid.name`, + field, + ); + return { + newLabel: { id, name: `${name}: ${value}`, field, type: 'location_grid' }, + name, }; } @@ -2018,67 +1780,6 @@ $('.all-without-connections').on('click', without_connections_handler); - $('.text-comms-filter-input').on('keyup', function (e) { - // Ensure to assign default settings accordingly. - const field = $(e.target).data('field'); - const panel = $(`#${field}.tabs-panel`); - const field_settings = list_settings?.post_type_settings?.fields[field]; - if (panel && field_settings && field_settings['type']) { - switch (field_settings['type']) { - case 'text': - case 'communication_channel': { - const checked_options = $(panel).find( - `.filter-by-text-comms-option:checked`, - ); - const existing_label = new_filter_labels.find( - (label) => label['field'] === field, - ); - - // Only apply default settings if unable to detect and previous selections. - if (checked_options.length === 0 && existing_label === undefined) { - const default_option = $(panel).find( - `.filter-by-text-comms-option[value="all-with-filtered-value"]`, - ); - if (default_option) { - $(default_option).prop('checked', true); - $(default_option).trigger('click'); - } - } - - // Update label with latest filtered value. - const filtered_value = $(e.target).val(); - const latest_checked_option = $(panel).find( - `.filter-by-text-comms-option:checked`, - ); - const latest_existing_label = new_filter_labels.find( - (label) => label['field'] === field, - ); - if ( - latest_checked_option.length === 1 && - latest_existing_label !== undefined && - ['all-with-filtered-value', 'all-without-filtered-value'].includes( - $(latest_checked_option).val(), - ) - ) { - const updated_label_text = `${esc(list_settings.post_type_settings.fields[field] ? list_settings.post_type_settings.fields[field].name : '')}: ${esc(filtered_value)}`; - $(selected_filters) - .find( - `.current-filter[data-id="${$(latest_checked_option).val()}"].${field}`, - ) - .text(updated_label_text); - - // Update global filter labels array. - const label_idx = new_filter_labels.findIndex( - (label) => label['field'] === field, - ); - new_filter_labels[label_idx]['name'] = updated_label_text; - } - break; - } - } - } - }); - $('.filter-by-text-comms-option').on('click', function (e) { handle_filter_by_text_comms({ id: $(this).val(), @@ -2097,76 +1798,59 @@ const { id, field } = options || { id: null, field: null }; if (id && field) { // Adjust filter text field state accordingly, based on option selection. - let filter_text_field = $('#' + field + '_text_comms_filter'); + let filter_text_field = document.querySelector(`[name="${field}"]`); $(filter_text_field).prop( 'disabled', ['all-with-set-value', 'all-without-set-value'].includes(id), ); - // Ensure duplicates are avoided. - const existing_label = new_filter_labels.find( - (label) => label['id'] === id && label['field'] === field, + // identify all old labels + let removed_old_filter_labels = new_filter_labels.filter( + (label) => label['field'] === field, ); - if (existing_label === undefined) { - // Identify stale labels to be deleted. - let removed_old_filter_labels = []; - new_filter_labels.forEach((label) => { - if (label['field'] === field) { - if (!(label['id'] === id)) { - removed_old_filter_labels.push(label); - } - } - }); - // Remove stale labels, if detected. - if (removed_old_filter_labels.length > 0) { - new_filter_labels = new_filter_labels.filter((existing_label) => { - let filtered = false; - removed_old_filter_labels.forEach((stale_label) => { - if ( - existing_label['id'] !== stale_label['id'] && - existing_label['name'] !== stale_label['name'] && - existing_label['field'] !== stale_label['field'] - ) { - filtered = true; - } - }); - - return filtered; - }); + if (removed_old_filter_labels.length > 0) { + // remove them from the active array + new_filter_labels = new_filter_labels.filter( + (existing_label) => existing_label['field'] !== field, + ); - // Remove associated ui labels. - removed_old_filter_labels.forEach((label) => { - $(selected_filters) - .find( - `.current-filter[data-id="${label['id']}"].${label['field']}`, - ) - .remove(); - }); - } + // remove associated UI labels + removed_old_filter_labels.forEach((label) => { + $(selected_filters) + .find(`.current-filter[data-id="${label['id']}"].${label['field']}`) + .remove(); + }); + } - // Create new generic filter label. - let { newLabel, filterName } = create_label_all( - field, - ['all-without-set-value', 'all-without-filtered-value'].includes(id), - id, - list_settings, - ); + // create new generic filter label. + let { newLabel, filterName } = create_label_all( + field, + ['all-without-set-value', 'all-without-filtered-value'].includes(id), + id, + list_settings, + ); - // Adjust label to reflect filtered text. - if ( - ['all-with-filtered-value', 'all-without-filtered-value'].includes(id) - ) { - let filtered_value = $(`#${field}_text_comms_filter`).val(); - newLabel['name'] = - filterName = `${esc(list_settings.post_type_settings.fields[field] ? list_settings.post_type_settings.fields[field].name : '')}: ${esc(filtered_value)}`; + // adjust label to reflect filtered text + if ( + ['all-with-filtered-value', 'all-without-filtered-value'].includes(id) + ) { + let filtered_value = document.querySelector( + `dt-multi-text#${field}, dt-text#${field}`, + ).value; + if (Array.isArray(filtered_value)) { + filtered_value = filtered_value + .filter((label) => label.value.length > 0) + .map((item) => item.value); } - - selected_filters.append( - `${filterName}`, - ); - new_filter_labels.push(newLabel); + newLabel['name'] = + filterName = `${esc(list_settings.post_type_settings.fields[field] ? list_settings.post_type_settings.fields[field].name : '')}: ${esc(filtered_value)}`; } + + selected_filters.append( + `${filterName}`, + ); + new_filter_labels.push(newLabel); } } @@ -2189,190 +1873,188 @@ (label) => label.field !== field, ); $(selected_filters).find(`.current-filter.${field}`).remove(); - - selected_filters.append( - `${filterName}`, - ); - new_filter_labels.push(newLabel); } - - let load_multi_select_typeaheads = - async function load_multi_select_typeaheads() { - for (let input of $( - '#filter-modal .multi_select .typeahead__query input', - )) { - let field = $(input).data('field'); - let typeahead_name = `.js-typeahead-${field}`; - - if (window.Typeahead[typeahead_name]) { - return; + // attach .on('change') to all dt-* fields + $(document).on( + 'change', + 'dt-location, dt-toggle, dt-tags, dt-date, dt-users-connection, dt-connection, dt-single-select, dt-multi-select, dt-multi-select-button-group, dt-multi-text, dt-text', + function (e) { + const element = e.target; + const tagName = element.tagName.toLowerCase(); + + let val = []; + const isConnectionLike = + tagName === 'dt-connection' || + tagName === 'dt-users-connection' || + tagName === 'dt-location'; + const isMultiSelectLike = + tagName === 'dt-multi-select' || + tagName === 'dt-multi-select-button-group' || + tagName === 'dt-tags'; + + // If the element creates multiple separate items (connections or multi-selects) + if (isConnectionLike || isMultiSelectLike) { + if (isConnectionLike) { + // Get array of values without 'delete' property + val = Array.isArray(e.target.value) + ? e.target.value.filter((label) => !label.delete) + : []; + } else { + // Get array of values for multi-selects and tags (filtering out negative '-' deletions) + const rawVal = Array.isArray(e.target.value) ? e.target.value : []; + val = rawVal + .filter((id) => typeof id === 'string' && id.charAt(0) !== '-') + .map((id) => { + const selectedOption = Array.from(e.target.options || []).find( + (option) => option.id === id, + ); + return { + id: id, + label: selectedOption ? selectedOption.label : id, + }; + }); } - let source_data = { data: [] }; - let field_options = window.lodash.get( - list_settings, - `post_type_settings.fields.${field}.default`, - {}, + const fieldName = e.target.name; + + // Find labels that are no longer in the component's value + const labelsToRemove = new_filter_labels.filter( + (label) => + label.field === fieldName && + !val.some((item) => item.id === label.id), ); - if (Object.keys(field_options).length > 0) { - window.lodash.forOwn(field_options, (val, key) => { - if (!val.deleted) { - source_data.data.push({ - key: key, - name: key, - value: val.label || key, - }); - } - }); - } else { - source_data = { - [field]: { - display: ['value'], - ajax: { - url: - window.wpApiShare.root + - `dt-posts/v2/${list_settings.post_type}/multi-select-values`, - data: { - s: '{{query}}', - field, - }, - beforeSend: function (xhr) { - xhr.setRequestHeader('X-WP-Nonce', window.wpApiShare.nonce); - }, - callback: { - done: function (data) { - return (data || []).map((tag) => { - let label = window.lodash.get( - field_options, - tag + '.label', - tag, - ); - return { value: label, key: tag }; - }); - }, - }, - }, - }, - }; - } - $.typeahead({ - input: `.js-typeahead-${field}`, - minLength: 0, - maxItem: 20, - searchOnFocus: true, - template: function (query, item) { - return `${window.SHAREDFUNCTIONS.escapeHTML(item.value)}`; - }, - source: source_data, - display: 'value', - templateValue: '{{value}}', - dynamic: true, - multiselect: { - matchOn: ['key'], - data: [], - callback: { - onCancel: function (node, item) { - $(`.current-filter[data-id="${item.key}"].${field}`).remove(); - window.lodash.pullAllBy( - new_filter_labels, - [{ id: item.key }], - 'id', - ); - }, - }, - }, - callback: { - onClick: function (node, a, item) { - const { newLabel, name } = create_name_value_label( - field, - item.key, - item.value, - list_settings, - ); - selected_filters.append( - `${window.SHAREDFUNCTIONS.escapeHTML(name)}:${window.SHAREDFUNCTIONS.escapeHTML(item.value)}`, - ); - new_filter_labels.push(newLabel); - }, - onResult: function (node, query, result, resultCount) { - let text = window.TYPEAHEADS.typeaheadHelpText( - resultCount, - query, - result, - ); - $(`#${field}-result-container`).html(text); - }, - onHideLayout: function () { - $(`#${field}-result-container`).html(''); - }, - }, + + labelsToRemove.forEach((label) => { + // Remove the specific span + $( + `.current-filter[data-id="${window.SHAREDFUNCTIONS.escapeHTML(label.id)}"].${window.SHAREDFUNCTIONS.escapeHTML(fieldName)}`, + ).remove(); + new_filter_labels = new_filter_labels.filter( + (existingLabel) => + existingLabel.id !== label.id || + existingLabel.field !== fieldName, + ); }); - } - }; - let load_post_type_typeaheads = () => { - $(".typeahead__query [data-type='connection']").each((key, el) => { - let field_key = $(el).data('field'); - let post_type = window.lodash.get( - list_settings, - `post_type_settings.fields.${field_key}.post_type`, - field_key, - ); - if (!window.Typeahead[`.js-typeahead-${field_key}`]) { - $.typeahead({ - input: `.js-typeahead-${field_key}`, - minLength: 0, - accent: true, - searchOnFocus: true, - maxItem: 20, - template: function (query, item) { - return `${window.SHAREDFUNCTIONS.escapeHTML(item.name)} (#${window.SHAREDFUNCTIONS.escapeHTML(item.ID)})`; - }, - source: window.TYPEAHEADS.typeaheadPostsSource(post_type), - display: 'name', - templateValue: '{{name}}', - dynamic: true, - multiselect: { - matchOn: ['ID'], - data: [], - callback: { - onCancel: function (node, item, event) { - remove_filter_labels(item.ID, field_key); - }, - }, - }, - callback: { - onResult: function (node, query, result, resultCount) { - let text = window.TYPEAHEADS.typeaheadHelpText( - resultCount, - query, - result, + // Loop through newly added labels, creating one label for each value + val.forEach((item) => { + const exists = new_filter_labels.some( + (label) => label.field === fieldName && label.id === item.id, + ); + + if (!exists) { + // Create correct label based on the tag + let newLabel, displayLabel; + + if (tagName === 'dt-location') { + const locLabel = create_location_label( + fieldName, + item.id, + item.label, + list_settings, ); - $(`#${field_key}-result-container`).html(text); - }, - onHideLayout: function () { - $(`#${field_key}-result-container`).html(''); - }, - onClick: function (node, a, item) { - const { newLabel } = create_value_label( - field_key, - item.ID, - item.name, + newLabel = locLabel.newLabel; + displayLabel = locLabel.newLabel.name; + } else if (isMultiSelectLike) { + const nameValLabel = create_name_value_label( + fieldName, + item.id, + item.label, + list_settings, ); - new_filter_labels.push(newLabel); - selected_filters.append( - `${window.SHAREDFUNCTIONS.escapeHTML(item.name)}`, + newLabel = nameValLabel.newLabel; + displayLabel = nameValLabel.newLabel.name; // prefixes with field name + } else { + const valLabel = create_value_label( + fieldName, + item.id, + item.label, ); - }, - }, + newLabel = valLabel.newLabel; + displayLabel = item.label; + } + + selected_filters.append( + `${window.SHAREDFUNCTIONS.escapeHTML(displayLabel)}`, + ); + new_filter_labels.push(newLabel); + } }); + } else if (tagName == 'dt-date') { + // Else, if the element is a date + val = e.target.value; + + const fieldName = e.target.name || ''; + const delimiter = fieldName.endsWith('_start') + ? 'start' + : fieldName.endsWith('_end') + ? 'end' + : ''; + const id = fieldName.replace(/_(start|end)$/, ''); + + const { newLabel, field_name } = create_date_label(id, val, delimiter); + remove_all_filter_labels(fieldName); + + if (val) { + selected_filters.append( + `${window.SHAREDFUNCTIONS.escapeHTML(field_name)}: ${window.SHAREDFUNCTIONS.escapeHTML(val)}`, + ); + new_filter_labels.push(newLabel); + } + } else { + // All other standard elements (e.g., dt-text, dt-single-select) + val = e.target.value; + + if (!val || val.length == 0) { + val = false; + } + + const radioContainer = $( + `#filter_by_text_comms_option_${e.target.name}`, + ); + if (radioContainer.length > 0) { + let radioChecked = radioContainer + .find('.filter-by-text-comms-option:checked') + .val(); + + // default to 'All with filtered value' if they typed but no radio was selected + if (!radioChecked) { + radioChecked = 'all-with-filtered-value'; + radioContainer + .find(`input[value="${radioChecked}"]`) + .prop('checked', true); + } + + handle_filter_by_text_comms({ + id: radioChecked, + field: e.target.name, + }); + return; // skip the generic label creation + } + + const { newLabel, name } = create_name_value_label( + e.target.name, + e.target.name, + val, + list_settings, + ); + + remove_all_filter_labels(e.target.name); + if (val) { + selected_filters.append( + `${window.SHAREDFUNCTIONS.escapeHTML(name)}: ${window.SHAREDFUNCTIONS.escapeHTML(val)}`, + ); + new_filter_labels.push(newLabel); + } } - }); - }; + }, + ); const remove_filter_labels = (id, field_key) => { $(`.current-filter[data-id="${id}"].${field_key}`).remove(); - window.lodash.pullAllBy(new_filter_labels, [{ id: id }], 'id'); + new_filter_labels = new_filter_labels.filter( + (label) => !(label.id === id && label.field === field_key), + ); }; const remove_all_filter_labels = (field_key) => { @@ -2386,68 +2068,6 @@ ids.forEach((id) => remove_filter_labels(id, field_key)); }; - let load_user_select_typeaheads = () => { - $(".typeahead__query [data-type='user_select']").each((key, el) => { - let field_key = $(el).data('field'); - if (!window.Typeahead[`.js-typeahead-${field_key}`]) { - $.typeahead({ - input: `.js-typeahead-${field_key}`, - minLength: 0, - accent: true, - searchOnFocus: true, - maxItem: 20, - template: function (query, item) { - return `${window.SHAREDFUNCTIONS.escapeHTML(item.name)} (#${window.SHAREDFUNCTIONS.escapeHTML(item.ID)})`; - }, - source: window.TYPEAHEADS.typeaheadUserSource(), - display: 'name', - templateValue: '{{name}}', - dynamic: true, - multiselect: { - matchOn: ['ID'], - data: [], - callback: { - onCancel: function (node, item) { - $( - `.current-filter[data-id="${item.ID}"].${field_key}`, - ).remove(); - window.lodash.pullAllBy( - new_filter_labels, - [{ id: item.ID }], - 'id', - ); - }, - }, - }, - callback: { - onResult: function (node, query, result, resultCount) { - let text = window.TYPEAHEADS.typeaheadHelpText( - resultCount, - query, - result, - ); - $(`#${field_key}-result-container`).html(text); - }, - onHideLayout: function () { - $(`#${field_key}-result-container`).html(''); - }, - onClick: function (node, a, item) { - const { newLabel } = create_value_label( - field_key, - item.ID, - item.name, - ); - new_filter_labels.push(newLabel); - selected_filters.append( - `${window.SHAREDFUNCTIONS.escapeHTML(item.name)}`, - ); - }, - }, - }); - } - }); - }; - /** * Location */ @@ -2455,169 +2075,33 @@ delete window.location_data; }); - let load_location_typeahead = () => { - let key = 'location_grid'; - if ($('.js-typeahead-location_grid_meta').length) { - key = 'location_grid_meta'; - } - if (!window.Typeahead[`.js-typeahead-${key}`]) { - // Ensure element is present before proceeding! - if ( - $('.js-typeahead-' + window.SHAREDFUNCTIONS.escapeHTML(key)).length > 0 - ) { - $.typeahead({ - input: `.js-typeahead-${window.SHAREDFUNCTIONS.escapeHTML(key)}`, - minLength: 0, - accent: true, - searchOnFocus: true, - maxItem: 20, - dropdownFilter: [ - { - key: 'group', - value: 'used', - template: window.SHAREDFUNCTIONS.escapeHTML( - window.wpApiShare.translations.used_locations, - ), - all: window.SHAREDFUNCTIONS.escapeHTML( - window.wpApiShare.translations.all_locations, - ), - }, - ], - source: { - used: { - display: 'name', - ajax: { - url: - window.wpApiShare.root + - 'dt/v1/mapping_module/search_location_grid_by_name', - data: { - s: '{{query}}', - filter: function () { - return window.lodash.get( - window.Typeahead[`.js-typeahead-${key}`].filters.dropdown, - 'value', - 'all', - ); - }, - }, - beforeSend: function (xhr) { - xhr.setRequestHeader('X-WP-Nonce', window.wpApiShare.nonce); - }, - callback: { - done: function (data) { - if (typeof window.typeaheadTotals !== 'undefined') { - window.typeaheadTotals.field = data.total; - } - return data.location_grid; - }, - }, - }, - }, - }, - display: 'name', - templateValue: '{{name}}', - dynamic: true, - multiselect: { - matchOn: ['ID'], - data: [], - callback: { - onCancel: function (node, item) { - $( - `.current-filter[data-id="${item.ID}"].location_grid`, - ).remove(); - window.lodash.pullAllBy( - new_filter_labels, - [{ id: item.ID }], - 'id', - ); - }, - }, - }, - callback: { - onResult: function (node, query, result, resultCount) { - let text = window.TYPEAHEADS.typeaheadHelpText( - resultCount, - query, - result, - ); - $('#location_grid-result-container').html(text); - }, - onReady() { - this.filters.dropdown = { - key: 'group', - value: 'used', - template: window.SHAREDFUNCTIONS.escapeHTML( - window.wpApiShare.translations.used_locations, - ), - }; - this.container - .removeClass('filter') - .find('.' + this.options.selector.filterButton) - .html( - window.SHAREDFUNCTIONS.escapeHTML( - window.wpApiShare.translations.used_locations, - ), - ); - }, - onHideLayout: function () { - $('#location_grid-result-container').html(''); - }, - onClick: function (node, a, item) { - const { name, newLabel } = create_location_label( - key, - item.ID, - item.name, - list_settings, - ); - new_filter_labels.push(newLabel); - selected_filters.append( - `${window.SHAREDFUNCTIONS.escapeHTML(name)}:${window.SHAREDFUNCTIONS.escapeHTML(item.name)}`, - ); - }, - }, - }); - } - } - }; - /* * Setup filter box */ - let typeaheads_loaded = null; $('#filter-modal').on('open.zf.reveal', function () { new_filter_labels = []; - load_location_typeahead(); - load_post_type_typeaheads(); - load_user_select_typeaheads(); - typeaheads_loaded = load_multi_select_typeaheads().catch((err) => { - console.error(err); - }); $('#new-filter-name').val(''); - $('#filter-modal input.dt_date_picker').each(function () { - $(this).val(''); - }); $('#filter-modal input:checked').each(function () { $(this).prop('checked', false); }); $('#filter-modal input:disabled').each(function () { $(this).prop('disabled', false); }); - $('#filter-modal .typeahead__query.disabled').each(function () { - $(this).removeClass('disabled'); - }); selected_filters.empty(); - $('.typeahead__query input').each(function () { - let typeahead = - window.Typeahead['.' + $(this).attr('class').split(/\s+/)[0]]; - if (typeahead && typeahead.items) { - for (let i = 0; i < typeahead.items.length; i) { - typeahead.cancelMultiselectItem(0); - } - typeahead.node.trigger('propertychange.typeahead'); - } - }); + }); + // On close, reset all field values + $('#filter-modal').on('closed.zf.reveal', function () { + $(this) + .find( + 'dt-location, dt-toggle, dt-tags, dt-date, dt-users-connection, dt-connection, dt-single-select, dt-multi-select, dt-multi-select-button-group, dt-multi-text, dt-text', + ) + .each(function () { + this.reset(); + }); + + // hide edit filters again & remove associated filter-id + $('#save-filter-edits').hide().removeData('filter-id'); $('#confirm-filter-records').show(); - $('#save-filter-edits').hide(); }); var clicked; @@ -2630,96 +2114,156 @@ $(document).mouseup(function (e) { clicked = null; }); - $('#filter-modal input.dt_date_picker').on('blur', function (e) { - if (clicked && clicked.closest('.ui-datepicker').length === 1) { - // we have clicked in the datepicker, so don't run the blur - return; - } - // delay the blur so that if the user has clicked we get the correct date from the input - setTimeout(() => { - if (!e.target.value) { - const clearButton = $(this).prev('.clear-date-picker'); - clearButton.click(); - return; - } - $(this).datepicker('setDate', e.target.value); - $('.ui-datepicker-current-day').click(); - }, 100); - }); function edit_saved_filter(filter) { $('#filter-modal').foundation('open'); - typeaheads_loaded.then(() => { - let connectionTypeKeys = - list_settings.post_type_settings.connection_types; - connectionTypeKeys.push('location_grid'); - filter.labels.forEach((label) => { - // Determine exclusion status - let excluded_class = is_search_query_filter_label_excluded( - filter, - label, + + let connectionTypeKeys = [ + ...list_settings.post_type_settings.connection_types, + 'location_grid', + ]; + + // Helper function to handle the new dt-* component state updates + const appendValueToDtComponent = (field, itemObject) => { + const dtComponent = document.querySelector(`[name="${field}"]`); + + if (dtComponent) { + // Assuming the dt-* component's value expects an array of selected objects + const currentValue = Array.isArray(dtComponent.value) + ? dtComponent.value + : []; + dtComponent.value = [...currentValue, itemObject]; + } else { + console.warn(`Could not find dt-* component for field: ${field}`); + } + }; + + filter.labels.forEach((label) => { + // Determine exclusion status + let excluded_class = is_search_query_filter_label_excluded(filter, label) + ? 'current-filter-excluded' + : ''; + + let type = window.lodash.get( + list_settings, + `post_type_settings.fields.${label.field}.type`, + ); + + const displayField = type === 'date' ? label.id : label.field; + + // Proceed with displaying of filter modal + selected_filters.append( + `${window.SHAREDFUNCTIONS.escapeHTML(label.name)}`, + ); + + if ( + type === 'key_select' || + type === 'boolean' || + type === 'file_upload' + ) { + // Find all checkboxes for this specific field + $( + `#filter-modal #${label.field}-options input[data-field="${label.field}"]`, ) - ? 'current-filter-excluded' - : ''; + // Filter down to the one where the JS value exactly matches label.id + .filter(function () { + return this.value === label.id; + }) + .prop('checked', true); + } else if (type === 'date' || type === 'datetime') { + const dateComponent = document.querySelector(`[name="${label.id}"]`); + if (dateComponent) { + dateComponent.value = label.date; + } + } else if (connectionTypeKeys.includes(label.field)) { + if (label.id === '*') { + const fieldAllConnectionsElement = document.querySelector( + `#filter-modal #${label.field} .all-connections`, + ); + const boundAllConnectionsClickHandler = + all_connections_click_handler.bind(fieldAllConnectionsElement); + $(fieldAllConnectionsElement).prop('checked', true); + boundAllConnectionsClickHandler(); + } else { + appendValueToDtComponent(label.field, { + id: label.id, + label: label.name, + }); + } + } else if (type === 'multi_select' || type === 'tags') { + appendValueToDtComponent(label.field, label.id); + } else if (type === 'user_select') { + appendValueToDtComponent(label.field, { + label: label.name, + id: label.id, + }); + } else if ( + type === 'text' || + type === 'communication_channel' || + type === 'textarea' || + type === 'number' + ) { + const dtComponent = document.querySelector(`[name="${label.field}"]`); - // Proceed with displaying of filter modal - selected_filters.append( - `${window.SHAREDFUNCTIONS.escapeHTML(label.name)}`, - ); - let type = window.lodash.get( - list_settings, - `post_type_settings.fields.${label.field}.type`, - ); - if ( - type === 'key_select' || - type === 'boolean' || - type === 'file_upload' - ) { - $( - `#filter-modal #${label.field}-options input[value="${label.id}"]`, - ).prop('checked', true); - } else if (type === 'date' || type === 'datetime') { - $(`#filter-modal #${label.field}-options #${label.id}`).datepicker( - 'setDate', - label.date, + let textValue = ''; + let queryArray = []; + + if (filter.query && filter.query.fields) { + const queryField = filter.query.fields.find( + (f) => f[label.field] !== undefined, ); - } else if (connectionTypeKeys.includes(label.field)) { - if (label.id === '*') { - const fieldAllConnectionsElement = document.querySelector( - `#filter-modal #${label.field} .all-connections`, - ); - const boundAllConnectionsClickHandler = - all_connections_click_handler.bind(fieldAllConnectionsElement); - $(fieldAllConnectionsElement).prop('checked', true); - boundAllConnectionsClickHandler(); + if (queryField && queryField[label.field]) { + queryArray = queryField[label.field]; + } + } else if (filter.query && filter.query[label.field]) { + queryArray = filter.query[label.field]; + } + + // Handle '-' and '*' signs + if (queryArray && queryArray.length > 0) { + let queryVal = queryArray[0]; + if (typeof queryVal === 'string') { + if (queryVal.startsWith('-') && queryVal !== '-*') { + textValue = queryVal.substring(1); + } else if (queryVal !== '*' && queryVal !== '-*') { + textValue = queryVal; + } else { + textValue = ''; + } + } + } + + if (dtComponent) { + dtComponent.value = textValue; + + if ( + ['all-with-set-value', 'all-without-set-value'].includes(label.id) + ) { + dtComponent.disabled = true; } else { - window.Typeahead[ - `.js-typeahead-${label.field}` - ].addMultiselectItemLayout({ ID: label.id, name: label.name }); + dtComponent.disabled = false; } - } else if (type === 'multi_select') { - window.Typeahead[ - `.js-typeahead-${label.field}` - ].addMultiselectItemLayout({ key: label.id, value: label.name }); - } else if (type === 'tags') { - window.Typeahead[ - `.js-typeahead-${label.field}` - ].addMultiselectItemLayout({ key: label.id, value: label.id }); - } else if (type === 'user_select') { - window.Typeahead[ - `.js-typeahead-${label.field}` - ].addMultiselectItemLayout({ name: label.name, ID: label.id }); } - }); - // moved this below the forEach as the global new_filter_labels was messing with the loop. - new_filter_labels = filter.labels; - (filter.query.combine || []).forEach((c) => { - $(`#combine_${c}`).prop('checked', true); - }); - $('#new-filter-name').val(filter.name); - $('#confirm-filter-records').hide(); - $('#save-filter-edits').data('filter-id', filter.ID).show(); + + if (type === 'text' || type === 'communication_channel') { + $( + `#filter-modal #filter_by_text_comms_option_${label.field} input[data-field="${label.field}"]`, + ) + .filter(function () { + return this.value === label.id; + }) + .prop('checked', true); + } + } }); + + new_filter_labels = filter.labels; + (filter.query.combine || []).forEach((c) => { + $(`#combine_${c}`).prop('checked', true); + }); + $('#new-filter-name').val(filter.name); + $('#confirm-filter-records').hide(); + $('#save-filter-edits').data('filter-id', filter.ID).show(); } $('#save-filter-edits').on('click', function () { @@ -2741,9 +2285,6 @@ const panel = $(`#${field}.tabs-panel`); $(`.tabs-panel`).removeClass('is-active'); $(panel).addClass('is-active'); - if (field && window.Typeahead[`.js-typeahead-${field}`]) { - window.Typeahead[`.js-typeahead-${field}`].adjustInputSize(); - } }); //watch all other checkboxes @@ -2795,45 +2336,6 @@ } }); - $('#filter-modal .dt_date_picker').datepicker({ - constrainInput: false, - dateFormat: 'yy-mm-dd', - onSelect: function (date) { - let id = $(this).data('field'); - let delimiter = $(this).data('delimit'); - //remove existing filters - window.lodash.pullAllBy( - new_filter_labels, - [{ id: `${id}_${delimiter}` }], - 'id', - ); - $(`.current-filter[data-id="${id}_${delimiter}"]`).remove(); - const { newLabel, field_name, delimiter_label } = create_date_label( - id, - date, - delimiter, - ); - //add new filters - new_filter_labels.push(newLabel); - selected_filters.append(` - - ${field_name} ${delimiter_label}:${date} - - `); - }, - changeMonth: true, - changeYear: true, - yearRange: '-20:+10', - }); - - $('#filter-modal .clear-date-picker').on('click', function () { - let id = $(this).data('for'); - $(`#filter-modal #${id}`).datepicker('setDate', null); - window.lodash.pullAllBy(new_filter_labels, [{ id: `${id}` }], 'id'); - $(`.current-filter[data-id="${id}"]`).remove(); - }); - //save the filter in the user meta $(`#confirm-filter-save`).on('click', function () { let filterName = $('#filter-name').val(); diff --git a/dt-assets/scss/_list.scss b/dt-assets/scss/_list.scss index 89f4c40d3d..b11ccab3c8 100644 --- a/dt-assets/scss/_list.scss +++ b/dt-assets/scss/_list.scss @@ -259,9 +259,12 @@ nav.second-bar.show-for-small-only { } .show-closed-switch { - display: inline-block; + display: flex; + justify-content: flex-end; + gap: 0.5rem; .switch { - display: inline-block; + display: flex; + align-items: center; margin-bottom: 0; vertical-align: middle; } diff --git a/dt-core/utilities/dt-components.php b/dt-core/utilities/dt-components.php index d9bf1e9aa6..59a0085466 100644 --- a/dt-core/utilities/dt-components.php +++ b/dt-core/utilities/dt-components.php @@ -269,9 +269,17 @@ public static function render_multi_select( $field_key, $fields, $post, $params public static function render_tags( $field_key, $fields, $post, $params = [] ) { $shared_attributes = self::shared_attributes( $field_key, $fields, $post, $params ); + $raw_value = $post[$field_key] ?? []; + $array_value = is_array( $raw_value ) ? $raw_value : []; + $value = array_map(function ( $value ) { return $value; - }, $post[$field_key] ?? []); + }, $array_value); + + $allow_add = true; + if ( isset( $params['allow_add'] ) ) { + $allow_add = $params['allow_add']; + } $options = null; if ( isset( $params['static_options'] ) && $params['static_options'] ) { @@ -289,9 +297,8 @@ public static function render_tags( $field_key, $fields, $post, $params = [] ) { ?> value='' - placeholder="" - allowAdd> + > - > + > value="" - single> + >