Skip to content

Commit

Permalink
Merge pull request #6342 from tomachalek/improv_la_autocomplete_b
Browse files Browse the repository at this point in the history
Improv la autocomplete (release)
  • Loading branch information
tomachalek authored Nov 4, 2024
2 parents 6b2d3a8 + 4b076ce commit e826d37
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 36 deletions.
13 changes: 10 additions & 3 deletions lib/plugin_types/live_attributes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class AttrValuesResponse:
attr_values: Optional[Dict[str, Union[AttrValue, Dict[str, int], int]]] = None
aligned: Optional[List[str]] = None
poscount: Optional[int] = None
applied_cutoff: Optional[int] = None
error: Optional[str] = None
details: Optional[List[str]] = None

Expand All @@ -73,9 +74,15 @@ async def is_enabled_for(self, plugin_ctx: AbstractCorpusPluginCtx, corpora: Lis

@abc.abstractmethod
async def get_attr_values(
self, plugin_ctx: AbstractCorpusPluginCtx, corpus: AbstractKCorpus, attr_map: Dict[str, Union[str, List[str]]],
aligned_corpora: Optional[List[str]] = None, autocomplete_attr: Optional[str] = None,
limit_lists: bool = True) -> AttrValuesResponse:
self,
plugin_ctx: AbstractCorpusPluginCtx,
corpus: AbstractKCorpus,
attr_map: Dict[str, Union[str, List[str]]],
aligned_corpora: Optional[List[str]] = None,
autocomplete_attr: Optional[str] = None,
apply_cutoff: Optional[bool] = False,
limit_lists: bool = True
) -> AttrValuesResponse:
"""
Find all the available values of remaining attributes according to the
provided attr_map and aligned_corpora
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/masm_live_attributes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ async def is_enabled_for(self, plugin_ctx, corpora):
return bool((await self.corparch.get_corpus_info(plugin_ctx, corpora[0])).metadata.database)

async def get_attr_values(
self, plugin_ctx, corpus, attr_map, aligned_corpora=None, autocomplete_attr=None, limit_lists=True):
json_body = {'attrs': attr_map}
self, plugin_ctx, corpus, attr_map, aligned_corpora=None, autocomplete_attr=None, apply_cutoff=False, limit_lists=True):
json_body = {'attrs': attr_map, 'applyCutoff': apply_cutoff}
if aligned_corpora:
json_body['aligned'] = aligned_corpora
if autocomplete_attr:
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/sqlite_live_attributes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ async def get_subc_size(self, plugin_ctx, corpora, attr_map):

@cached
async def get_attr_values(
self, plugin_ctx, corpus, attr_map, aligned_corpora=None, autocomplete_attr=None, limit_lists=True):
self, plugin_ctx, corpus, attr_map, aligned_corpora=None, autocomplete_attr=None, apply_cutoff=False, limit_lists=True):
"""
Finds all the available values of remaining attributes according to the
provided attr_map and aligned_corpora
Expand Down
8 changes: 6 additions & 2 deletions lib/views/tt_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ async def attr_val_autocomplete(amodel: CorpusActionModel, req: KRequest, resp:
aligned = json.loads(req.form.get('aligned', '[]'))
with plugins.runtime.LIVE_ATTRIBUTES as lattr:
return await lattr.get_attr_values(
amodel.plugin_ctx, corpus=amodel.corp, attr_map=attrs,
aligned_corpora=aligned, autocomplete_attr=req.form.get('patternAttr'))
amodel.plugin_ctx,
corpus=amodel.corp,
attr_map=attrs,
aligned_corpora=aligned,
autocomplete_attr=req.form.get('patternAttr'),
apply_cutoff=True)


@bp.route('/fill_attrs', methods=['POST'])
Expand Down
1 change: 1 addition & 0 deletions public/files/js/models/textTypes/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export class Actions {
attrName:string;
autoCompleteData:Array<TextTypes.AutoCompleteItem>;
filterData:SelectionFilterMap;
appliedCutoff:number|undefined;
}> = {
name: 'TT_ATTRIBUTE_TEXT_INPUT_AUTOCOMPLETE_REQUEST_DONE'
};
Expand Down
4 changes: 3 additions & 1 deletion public/files/js/models/textTypes/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ function createTextInputAttributeSelection(
docLabel: attrItem.attr_doc_label
},
autoCompleteHints: [],
autocompleteCutoff: undefined,
values: List.map(
value => ({
value,
Expand All @@ -193,7 +194,7 @@ function createTextInputAttributeSelection(
textFieldValue: '',
excludeSelection,
type: 'text',
metaInfo: null,
metaInfo: null
};
}
return {
Expand All @@ -207,6 +208,7 @@ function createTextInputAttributeSelection(
docLabel: attrItem.attr_doc_label
},
autoCompleteHints: [],
autocompleteCutoff: undefined,
values: [],
definesSubcorpus,
textFieldValue: '',
Expand Down
18 changes: 11 additions & 7 deletions public/files/js/models/textTypes/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,16 +444,18 @@ export class TextTypesModel extends StatefulModel<TextTypesModelState>
Actions.AttributeTextInputAutocompleteRequestDone,
_ => !this.readonlyMode,
action => {
if (!action.error) {
this.changeState(state => {
state.busyAttributes[action.payload.attrName] = false;
this.changeState(state => {
state.busyAttributes[action.payload.attrName] = false;
if (!action.error) {
this.setAutoComplete(
state,
action.payload.attrName,
action.payload.autoCompleteData,
action.payload.appliedCutoff
);
});
} else {
}
});
if (action.error) {
this.pluginApi.showMessage('error', action.error);
}
}
Expand Down Expand Up @@ -1122,11 +1124,13 @@ export class TextTypesModel extends StatefulModel<TextTypesModelState>
setAutoComplete(
state:TextTypesModelState,
attrName:string,
values:Array<TextTypes.AutoCompleteItem>
values:Array<TextTypes.AutoCompleteItem>,
cutoff:number|undefined
):void {
const attrIdx = this.getAttributeIdx(state, attrName);
if (attrIdx > -1) {
state.attributes[attrIdx] = TTSelOps.setAutoComplete(state.attributes[attrIdx], values);
state.attributes[attrIdx] = TTSelOps.setAutoComplete(
state.attributes[attrIdx], values, cutoff);
}
}

Expand Down
6 changes: 4 additions & 2 deletions public/files/js/models/textTypes/selectionOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,15 @@ export class TTSelOps {

static setAutoComplete(
sel:TextTypes.AnyTTSelection,
values:Array<TextTypes.AutoCompleteItem>
values:Array<TextTypes.AutoCompleteItem>,
cutoff:number|undefined
):TextTypes.AnyTTSelection {

if (sel.type === 'text') {
return {
...sel,
autoCompleteHints: values
autoCompleteHints: values,
autocompleteCutoff: cutoff
};

} else {
Expand Down
19 changes: 13 additions & 6 deletions public/files/js/plugins/liveAttributes/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ import { DownloadType } from '../../app/page';


interface ServerRefineResponse extends Kontext.AjaxResponse {
error?:string;
aligned:Array<string>;
poscount:number;
attr_values:TextTypes.ValueDomainsSizes;
applied_cutoff?:number;
error?:string;
}

interface ServerBibInfoResponse extends Kontext.AjaxResponse {
Expand Down Expand Up @@ -380,15 +381,21 @@ export class LiveAttrsModel extends StatelessModel<LiveAttrsModelState> implemen
autoCompleteData: List.map(
v => ({ident: v[1], label: v[2]}),
values
)
),
appliedCutoff: resp.applied_cutoff
}
});

} else {
dispatch<typeof TTActions.AttributeTextInputAutocompleteRequestDone>({
name: TTActions.AttributeTextInputAutocompleteRequestDone.name,
error: new Error('Did not recieve list of items but a summary instead')
});
dispatch(
TTActions.AttributeTextInputAutocompleteRequestDone,
{
attrName: action.payload.attrName,
filterData: {},
autoCompleteData: []
},
new Error(this.pluginApi.translate('ucnkLA__too_many_autocomplete_values'))
);
}
},
error: err => {
Expand Down
1 change: 1 addition & 0 deletions public/files/js/translations/messages.cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@
"query__tt_exact_value": "Přesná hodnota...",
"query__tt_regexp_value_label": "Regulární výraz",
"query__tt_negative_selection": "Negativní výběr",
"query__tt_too_many_autocomplete_items_{num_items}": "Příliš mnoho odpovídajících položek. Zobrazuji pouze {num_items, plural, one{první # záznam} few{první # záznamy} other{prvních # záznamů}}.",
"query__span_from": "od",
"query__span_to": "do",
"query__include_kwic": "včetně KWIC",
Expand Down
1 change: 1 addition & 0 deletions public/files/js/translations/messages.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@
"query__tt_exact_value": "Exact value...",
"query__tt_regexp_value_label": "Regular expression",
"query__tt_negative_selection": "Negative selection",
"query__tt_too_many_autocomplete_items_{num_items}": "Too many matching items. Showing only {num_items, plural, one{first # record} few{first # records} other{first # records}}.",
"query__span_from": "From",
"query__span_to": "To",
"query__include_kwic": "Include KWIC",
Expand Down
1 change: 1 addition & 0 deletions public/files/js/translations/messages.sl.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
"query__tt_exact_value": "Exact value... UNTRANSLATED",
"query__tt_regexp_value_label": "Regular expression UNTRANSLATED",
"query__tt_negative_selection": "Negative selection UNTRANSLATED",
"query__tt_too_many_autocomplete_items_{num_items}": "Too many matching items. Showing only {num_items, plural, one{first # record} few{first # records} other{first # records}}. UNTRANSLATED",
"concview__invalid_page_num_err": "Neveljavna številka strani",
"concview__click_for_details": "Kliknite za ogled podrobnosti",
"concview__sort_jump_to": "Skoči na",
Expand Down
1 change: 1 addition & 0 deletions public/files/js/translations/messages.szl.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@
"query__tt_exact_value": "Exact value... UNTRANSLATED",
"query__tt_regexp_value_label": "Regular expression UNTRANSLATED",
"query__tt_negative_selection": "Negative selection UNTRANSLATED",
"query__tt_too_many_autocomplete_items_{num_items}": "Too many matching items. Showing only {num_items, plural, one{first # record} few{first # records} other{first # records}}. UNTRANSLATED",
"query__span_from": "ôd",
"query__span_to": "do",
"query__include_kwic": "Użyj tyż KWIC",
Expand Down
1 change: 1 addition & 0 deletions public/files/js/types/textTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export interface TextInputAttributeSelection extends BaseAttributeSelection {
isInterval:boolean;
isNumeric:boolean;
autoCompleteHints:Array<AutoCompleteItem>;
autocompleteCutoff:number|undefined;
values:Array<AttributeValue>; // it supports appending values via a single text input
textFieldValue:string;
excludeSelection:boolean;
Expand Down
34 changes: 22 additions & 12 deletions public/files/js/views/textTypes/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,28 @@ export function init(dispatcher:IActionDispatcher, he:Kontext.ComponentHelpers):
render() {
const data = TTSelOps.getAutoComplete(this.props.attrObj);
return (
<ul className="auto-complete"
onClick={this._handleAutoCompleteAreaClick}>
{data.map((item) => {
return (
<li key={item.ident}>
<a onClick={()=>this._handleAutoCompleteHintClick(item)}>
{item.label}
</a>
</li>
);
})}
</ul>
<>
<ul className="auto-complete"
onClick={this._handleAutoCompleteAreaClick}>
{data.map((item) => {
return (
<li key={item.ident}>
<a onClick={()=>this._handleAutoCompleteHintClick(item)}>
{item.label}
</a>
</li>
);
})}
</ul>
{this.props.attrObj.autocompleteCutoff > 0 ?
<p>
{he.translate(
'query__tt_too_many_autocomplete_items_{num_items}',
{num_items: this.props.attrObj.autocompleteCutoff})}
</p> :
null
}
</>
);
}
}
Expand Down

0 comments on commit e826d37

Please sign in to comment.