buli
2023-07-05 af7b716a60d889acea95560abba0e46eee008b8f
force-app/main/default/lwc/lexLookup/lexLookup.js
@@ -11,8 +11,7 @@
const VARIANT_LABEL_INLINE = 'label-inline';
const VARIANT_LABEL_HIDDEN = 'label-hidden';
const REGEX_SOSL_RESERVED =
    /(\?|&|\||!|\{|\}|\[|\]|\(|\)|\^|~|\*|:|"|\+|-|\\)/g;
const REGEX_SOSL_RESERVED = /(\?|&|\||!|\{|\}|\[|\]|\(|\)|\^|~|\*|:|"|\+|-|\\)/g;
const REGEX_EXTRA_TRAP = /(\$|\\)/g;
export default class LexLookup extends NavigationMixin(LightningElement) {
@@ -46,13 +45,15 @@
    _curSelection = [];
    _focusedResultIndex = null;
    // PUBLIC FUNCTIONS AND GETTERS/SETTERS
    @api
    set selection(initialSelection) {
        if (initialSelection) {
            this._curSelection = Array.isArray(initialSelection)
                ? initialSelection
                : [initialSelection];
            this._curSelection = Array.isArray(initialSelection) ? initialSelection : [initialSelection];
            this.processSelectionUpdate(false);
        }
    }
@@ -92,13 +93,9 @@
        let resultsLocal = JSON.parse(JSON.stringify(results));
        // Remove selected items from search results
        const selectedIds = this._curSelection.map((sel) => sel.id);
        resultsLocal = resultsLocal.filter(
            (result) => selectedIds.indexOf(result.id) === -1
        );
        resultsLocal = resultsLocal.filter((result) => selectedIds.indexOf(result.id) === -1);
        // Format results
        const cleanSearchTerm = this._searchTerm
            .replace(REGEX_SOSL_RESERVED, '.?')
            .replace(REGEX_EXTRA_TRAP, '\\$1');
        const cleanSearchTerm = this._searchTerm.replace(REGEX_SOSL_RESERVED, '.?').replace(REGEX_EXTRA_TRAP, '\\$1');
        const regex = new RegExp(`(${cleanSearchTerm})`, 'gi');
        this._searchResults = resultsLocal.map((result) => {
            // Format title and subtitle
@@ -127,8 +124,7 @@
                result,
                state: {},
                get classes() {
                    let cls =
                        'slds-media slds-media_center slds-listbox__option slds-listbox__option_entity';
                    let cls = 'slds-media slds-media_center slds-listbox__option slds-listbox__option_entity';
                    if (result.subtitleFormatted) {
                        cls += ' slds-listbox__option_has-meta';
                    }
@@ -143,7 +139,7 @@
    @api
    getSelection() {
        console.log('get selection:' + this._curSelection);
        console.log('get selection:' +this._curSelection);
        return this._curSelection;
    }
@@ -160,11 +156,9 @@
        this.template.querySelector('input')?.blur();
    }
    connectedCallback() {
    connectedCallback(){
        console.log('LexLookup accountValue = ' + this.accountValue);
        console.log(
            'isDisabledForDealerText = ' + this.isDisabledForDealerText
        );
        console.log('isDisabledForDealerText = ' + this.isDisabledForDealerText);
    }
    // INTERNAL FUNCTIONS
@@ -172,10 +166,7 @@
        this._searchTerm = newSearchTerm;
        // Compare clean new search term with current one and abort if identical
        const newCleanSearchTerm = newSearchTerm
            .trim()
            .replace(REGEX_SOSL_RESERVED, '?')
            .toLowerCase();
        const newCleanSearchTerm = newSearchTerm.trim().replace(REGEX_SOSL_RESERVED, '?').toLowerCase();
        if (this._cleanSearchTerm === newCleanSearchTerm) {
            return;
        }
@@ -184,10 +175,7 @@
        this._cleanSearchTerm = newCleanSearchTerm;
        // Ignore search terms that are too small after removing special characters
        if (
            newCleanSearchTerm.replace(/\?/g, '').length <
            this.minSearchTermLength
        ) {
        if (newCleanSearchTerm.replace(/\?/g, '').length < this.minSearchTermLength) {
            this.setSearchResults(this._defaultSearchResults);
            return;
        }
@@ -207,9 +195,7 @@
                    detail: {
                        searchTerm: this._cleanSearchTerm,
                        rawSearchTerm: newSearchTerm,
                        selectedIds: this._curSelection.map(
                            (element) => element.id
                        )
                        selectedIds: this._curSelection.map((element) => element.id)
                    }
                });
                this.dispatchEvent(searchEvent);
@@ -243,9 +229,7 @@
        // If selection was changed by user, notify parent components
        if (isUserInteraction) {
            const selectedIds = this._curSelection.map((sel) => sel.id);
            this.dispatchEvent(
                new CustomEvent('selectionchange', { detail: selectedIds })
            );
            this.dispatchEvent(new CustomEvent('selectionchange', { detail: selectedIds }));
        }
    }
@@ -277,18 +261,12 @@
                this._focusedResultIndex = this._searchResults.length - 1;
            }
            event.preventDefault();
        } else if (
            event.keyCode === KEY_ENTER &&
            this._hasFocus &&
            this._focusedResultIndex >= 0
        ) {
        } else if (event.keyCode === KEY_ENTER && this._hasFocus && this._focusedResultIndex >= 0) {
            // If the user presses enter, and the box is open, and we have used arrows,
            // treat this just like a click on the listbox item
            const selectedId = this._searchResults[this._focusedResultIndex].id;
            console.log('selectedid:' + selectedId);
            this.template
                .querySelector(`[data-recordid="${selectedId}"]`)
                .click();
            console.log('selectedid:'+selectedId);
            this.template.querySelector(`[data-recordid="${selectedId}"]`).click();
            event.preventDefault();
        }
    }
@@ -297,9 +275,7 @@
        const recordId = event.currentTarget.dataset.recordid;
        // Save selection
        const selectedItem = this._searchResults.find(
            (result) => result.id === recordId
        );
        const selectedItem = this._searchResults.find((result) => result.id === recordId);
        if (!selectedItem) {
            return;
        }
@@ -343,7 +319,7 @@
        });
        this.dispatchEvent(blurEvent);
        this._hasFocus = false;
        if (!this.hasSelection()) {
        if(!this.hasSelection()){
            this._searchTerm = '';
        }
    }
@@ -353,9 +329,7 @@
            return;
        }
        const recordId = event.currentTarget.name;
        this._curSelection = this._curSelection.filter(
            (item) => item.id !== recordId
        );
        this._curSelection = this._curSelection.filter((item) => item.id !== recordId);
        // Process selection update
        this.processSelectionUpdate(true);
    }
@@ -370,9 +344,7 @@
    handleNewRecordClick(event) {
        const objectApiName = event.currentTarget.dataset.sobject;
        const selection = this.newRecordOptions.find(
            (option) => option.value === objectApiName
        );
        const selection = this.newRecordOptions.find((option) => option.value === objectApiName);
        const preNavigateCallback = selection.preNavigateCallback
            ? selection.preNavigateCallback
@@ -398,15 +370,11 @@
    }
    get isListboxOpen() {
        const isSearchTermValid =
            this._cleanSearchTerm &&
            this._cleanSearchTerm.length >= this.minSearchTermLength;
        const isSearchTermValid = this._cleanSearchTerm && this._cleanSearchTerm.length >= this.minSearchTermLength;
        return (
            this._hasFocus &&
            this.isSelectionAllowed() &&
            (isSearchTermValid ||
                this.hasResults ||
                this.newRecordOptions?.length > 0)
            (isSearchTermValid || this.hasResults || this.newRecordOptions?.length > 0)
        );
    }
@@ -435,8 +403,7 @@
    }
    get getDropdownClass() {
        let css =
            'slds-combobox slds-dropdown-trigger slds-dropdown-trigger_click ';
        let css = 'slds-combobox slds-dropdown-trigger slds-dropdown-trigger_click ';
        if (this.isListboxOpen) {
            css += 'slds-is-open';
        }
@@ -448,16 +415,11 @@
        if (this._hasFocus && this.hasResults) {
            css += 'slds-has-focus ';
        }
        if (
            this._errors.length > 0 ||
            (this._isDirty && this.required && !this.hasSelection())
        ) {
        if (this._errors.length > 0 || (this._isDirty && this.required && !this.hasSelection())) {
            css += 'has-custom-error ';
        }
        if (!this.isMultiEntry) {
            css +=
                'slds-combobox__input-value ' +
                (this.hasSelection() ? 'has-custom-border' : '');
            css += 'slds-combobox__input-value ' + (this.hasSelection() ? 'has-custom-border' : '');
        }
        return css;
    }
@@ -467,9 +429,7 @@
        if (this.isMultiEntry) {
            css += 'slds-input-has-icon_right';
        } else {
            css += this.hasSelection()
                ? 'slds-input-has-icon_left-right'
                : 'slds-input-has-icon_right';
            css += this.hasSelection() ? 'slds-input-has-icon_left-right' : 'slds-input-has-icon_right';
        }
        return css;
    }
@@ -490,32 +450,23 @@
    }
    get getSelectIconName() {
        if (this._curSelection[0])
            console.log(
                'this._curSelection[0].icon = ' + this._curSelection[0].icon
            );
        return this.hasSelection()
            ? this._curSelection[0].icon
            : 'standard:default';
        if(this._curSelection[0])
            console.log('this._curSelection[0].icon = ' + this._curSelection[0].icon);
        return this.hasSelection() ? this._curSelection[0].icon : 'standard:default';
    }
    get getSelectIconClass() {
        return (
            'slds-combobox__input-entity-icon ' +
            (this.hasSelection() ? '' : 'slds-hide')
        );
        return 'slds-combobox__input-entity-icon ' + (this.hasSelection() ? '' : 'slds-hide');
    }
    get getInputValue() {
        if (this.isMultiEntry) {
            return this._searchTerm;
        }
        if (this.accountValue != '') {
        if(this.accountValue != ''){
            return this.accountValue;
        }
        return this.hasSelection()
            ? this._curSelection[0].title
            : this._searchTerm;
        return this.hasSelection() ? this._curSelection[0].title : this._searchTerm;
    }
    get getInputTitle() {
@@ -528,9 +479,7 @@
    get getListboxClass() {
        return (
            'slds-dropdown ' +
            (this.scrollAfterNItems
                ? `slds-dropdown_length-with-icon-${this.scrollAfterNItems} `
                : '') +
            (this.scrollAfterNItems ? `slds-dropdown_length-with-icon-${this.scrollAfterNItems} ` : '') +
            'slds-dropdown_fluid'
        );
    }