All files / src/fieldcontrols ContentReference.ts

80% Statements 40/50
68.18% Branches 15/22
75% Functions 9/12
77.78% Lines 35/45
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110          1x   1x 1x     1x     1x     8x       8x     9x     9x           9x     3x   3x 1x 1x 1x     3x       3x 3x               1x   1x 1x 1x 1x 1x   1x       2x 1x   2x 2x       1x 1x 1x   1x       1x 1x                                              
/**
 * @module FieldControls
 * 
 */ /** */
 
import { FieldBaseControl } from './FieldBaseControl';
import { FieldSettings, ContentReferenceField, SavedContent, QueryResult, IContent } from 'sn-client-js';
import { customElement, bindable } from 'aurelia-framework';
import { Observable } from 'rxjs/Observable';
 
@customElement('content-reference')
export class ContentReference extends FieldBaseControl<ContentReferenceField<IContent>, FieldSettings.ReferenceFieldSetting> {
 
    @bindable
    Item: SavedContent;
 
    @bindable
    public availableValues: SavedContent[] = [];
 
 
    @bindable
    public searchString: string = '';
 
    @bindable
    isOpened: boolean = false;
 
    @bindable
    isFocused: boolean = false;
 
    searchInput: HTMLInputElement;
 
 
    @bindable
    selectionIndex: number = 0;
 
    searchStringChanged(newValue: string): Observable<QueryResult> {
        const req = newValue && this.value && this.value.Search(newValue, 10, 0, { select: 'all' })
            .Exec();
        req && req.subscribe(res => {
            this.availableValues = res.Result.filter(a => a !== this.content);
            this.isOpened = true;
            this.selectionIndex = 0;
        }, err => {
        }) || (this.availableValues = []);
        return req || Observable.of<QueryResult>({Count: 0, Result: []});
    }
 
    activate(model) {
        super.activate(model);
        this.value && this.value.GetContent && this.value.GetContent({
            select: 'all'
        }).subscribe(c => {
            this.Item = c;
        });
    }
 
    pickValue(content: SavedContent) {
        this.Item = content;
 
        this.searchString = '';
        this.isOpened = false;
        this.isFocused = false;
        Eif (this.rules.length)
            this.controller.validate();
 
        this.value.SetContent(this.Item);
    }
 
    focusIn() {
        if (this.searchString.length > 0) {
            this.isOpened = true;
        }
        this.isFocused = true;
        this.searchInput.focus();
    }
 
    focusOut() {
        this.isOpened = false;
        Eif (this.rules.length)
            this.controller.validate();
 
        this.isFocused = false;
    }
 
    removeItem(){
        this.Item = null as any;
        this.value.SetContent(null as any);
    }
 
    handleSearchKeyUp($event: KeyboardEvent) {
        switch ($event.keyCode) {
            case 38: {
                this.selectionIndex = Math.max(this.selectionIndex - 1, 0);
                break;
            }
            case 40: {
                this.selectionIndex = Math.min(this.selectionIndex + 1, this.availableValues.length - 1);
                break;
            }
            case 13: {
                if (this.availableValues[this.selectionIndex]) {
                    this.pickValue(this.availableValues[this.selectionIndex])
                    $event.preventDefault();
                    $event.stopPropagation();
                }
            }
 
        }
    }
}