resources/assets/js/core/detail.js   A
last analyzed

Complexity

Total Complexity 14
Complexity/F 1.56

Size

Lines of Code 129
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 63
c 0
b 0
f 0
dl 0
loc 129
rs 10
mnd 5
bc 5
fnc 9
bpm 0.5555
cpm 1.5555
noi 5

4 Functions

Rating   Name   Duplication   Size   Complexity  
A Detail.constructor 0 4 1
A Detail.initRelatedLists 0 15 3
A Detail.relatedListNNRowClickCallback 0 29 4
B Detail.initRelatedListSelectionModalListener 0 61 6
1
import { Datatable } from './datatable'
2
3
export class Detail {
4
    constructor() {
5
        this.initRelatedLists()
6
        this.initRelatedListSelectionModalListener()
7
    }
8
9
    /**
10
     * Initalize datatable for all related lists
11
     */
12
    initRelatedLists() {
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
        this.relatedListDatatables = {}
18
19
        $('table[data-filter-type="related-list"]').each((index, el) => {
20
            let datatable = new Datatable()
21
            datatable.init(el)
22
            datatable.makeQuery()
23
24
            this.relatedListDatatables[$(el).attr('id')] = datatable
25
        })
26
    }
27
28
    /**
29
     * Show a selection modal and initialize datatable in it
30
     */
31
    initRelatedListSelectionModalListener() {
32
        $('.btn-relatedlist-select').on('click', (event) => {
33
            const element = event.currentTarget
34
35
            const relatedListId = $(element).data('relatedlist')
36
            const modalTitle = $(element).data('modal-title')
37
            const modalIcon = $(element).data('modal-icon')
38
            const modalBody = $(`.selection-modal-content[data-relatedlist='${relatedListId}']`).html()
39
            const relatedListTable = $(element).data('table')
40
41
            // Get modal
42
            const modal = $('#relatedListSelectionModal')
43
44
            // Change modal title
45
            $('h4 span', modal).text(modalTitle)
46
47
            // Change modal icon
48
            $('h4 i', modal).text(modalIcon)
49
50
            // Change modal body
51
            $('.modal-body', modal).html(modalBody)
52
53
            // Init datatable
54
            $('table tbody tr.record', modal).remove()
55
56
            // Display search fields
57
            $('table thead .search', modal).removeClass('hide')
58
59
            // Click callback
60
            let rowClickCallback = (event, datatable, recordId, recordLabel, oldRelatedName) => {
61
                
62
                if(oldRelatedName)
63
                {
64
                    swal({
65
                        title: uctrans.trans('uccello::default.confirm.dialog.title'),
0 ignored issues
show
Bug introduced by
The variable uctrans seems to be never declared. If this is a global, consider adding a /** global: uctrans */ 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...
66
                        text: uctrans.trans('uccello::default.entity.confirmation_override', {name: oldRelatedName, name_: oldRelatedName}),
67
                        icon: "warning",
68
                        buttons: true,
0 ignored issues
show
Unused Code Bug introduced by
The key buttons is used more than once in this object expression.
Loading history...
69
                        dangerMode: true,
70
                        buttons: [
71
                            uctrans.trans('uccello::default.button.no'),
72
                            uctrans.trans('uccello::default.button.yes')
73
                        ],
74
                    })
75
                    .then((response) => {
76
                        if (response === true) {
77
                            this.relatedListNNRowClickCallback(relatedListId, relatedListTable, datatable, recordId)
78
                        }
79
                    })
80
                }
81
                else
82
                    this.relatedListNNRowClickCallback(relatedListId, relatedListTable, datatable, recordId)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
83
84
            }
85
86
            // Init datatable for selection
87
            let datatable = new Datatable()
88
            datatable.init($('table', modal), rowClickCallback)
89
            datatable.makeQuery()
90
        })
91
    }
92
93
    /**
94
     * Callback to call when a row is clicked in a datatable for a N-N related list
95
     * @param {integer} relatedListId
96
     * @param {Element} relatedListTable
97
     * @param {Object} modalDatatableInstance
98
     * @param {integer} relatedRecordId
99
     */
100
    relatedListNNRowClickCallback(relatedListId, relatedListTable, modalDatatableInstance, relatedRecordId) {
101
        const url = $(modalDatatableInstance.table).data('add-relation-url')
102
103
        let data = {
104
            _token: $('meta[name="csrf-token"]').attr('content'),
105
            id: $('meta[name="record"]').attr('content'),
106
            relatedlist: relatedListId,
107
            related_id: relatedRecordId
108
        }
109
110
        // Ajax call to make a relation between two records
111
        $.post(url, data)
112
        .then((response) => {
113
            // Display an alert if an error occured
114
            if (response.success === false) {
115
                swal(uctrans.trans('uccello::default.dialog.error.title'), response.message, 'error')
0 ignored issues
show
Bug introduced by
The variable uctrans seems to be never declared. If this is a global, consider adding a /** global: uctrans */ 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...
116
            }
117
            else {
118
                // Refresh relatedlist datatable
119
                let relatedListDatatable = this.relatedListDatatables[relatedListTable]
120
                if (relatedListDatatable) {
121
                    relatedListDatatable.makeQuery()
122
                }
123
124
                // Hide modal
125
                $('#relatedListSelectionModal').modal('close')
126
            }
127
        })
128
    }
129
}