Passed
Push — master ( 923e12...5d294b )
by Jonathan
26:47
created

EntityField.initEntityFields   A

Complexity

Conditions 5

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
c 0
b 0
f 0
dl 0
loc 27
rs 9.1333
1
import {Datatable} from './datatable'
2
3
export class EntityField {
4
    constructor() {
5
        this.initEntityFields()
6
        this.initListener()
7
    }
8
9
    /**
10
     * Initalize datatable for all entity fields
11
     */
12
    initEntityFields() {
13
        if ($('table[data-filter-type="related-list"]').length == 0) {
0 ignored issues
show
Best Practice introduced by
Comparing $("table[data-filter-typ...related-list"]").length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
14
            return
15
        }
16
17
        $('table[data-filter-type="related-list"]').each((index, el) => {
18
            // Click callback
19
            let rowClickCallback = (event, datatable, recordId, recordLabel) => {
20
                this.selectRelatedModule(datatable, recordId, recordLabel)
21
            }
22
23
            let datatable = new Datatable()
24
            datatable.init(el, rowClickCallback)
25
            datatable.makeQuery()
26
        })
27
28
        $('.delete-related-record').on('click', (event) => {
29
            const modal = $(event.currentTarget).parents('.modal:first');
30
            const fieldName = modal.attr('data-field')
31
32
            $(`[name='${fieldName}']`).val('')
33
            $(`#${fieldName}_display`).val('')
34
            $(`#${fieldName}_display`).parent().find('label').removeClass('active')
35
36
            $(modal).modal('close')
37
        })
38
    }
39
40
    selectRelatedModule(datatable, recordId, recordLabel) {
41
        const modal = $(datatable.table).parents('.modal:first');
42
        const fieldName = modal.attr('data-field')
43
44
        $(`[name='${fieldName}']`).val(recordId).trigger('keyup')
45
        $(`#${fieldName}_display`).val(recordLabel)
46
        $(`#${fieldName}_display`).parent().find('label').addClass('active')
47
48
        $(modal).modal('close')
49
    }
50
51
    initListener() {
52
        $('a.entity-modal').on('click', function() {
53
            let tableId = $(this).attr('data-table')
54
            let event = new CustomEvent('uccello.list.refresh', { detail: tableId })
0 ignored issues
show
Bug introduced by
The variable CustomEvent seems to be never declared. If this is a global, consider adding a /** global: CustomEvent */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
55
            dispatchEvent(event)
56
        })
57
    }
58
}