Subversion Repositories SmartDukaan

Rev

Rev 33973 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 33973 Rev 34070
Line -... Line 1...
-
 
1
 
-
 
2
function initializeSelect(selector, type='catalogs') {
-
 
3
    if (type === 'catalogs') {
-
 
4
        $(selector).multiselect({
-
 
5
            includeSelectAllOption: false,
-
 
6
            multiple: false,
-
 
7
            maxHeight: 200,
-
 
8
            buttonWidth: '100%',
-
 
9
            numberDisplayed: 1,
-
 
10
            nonSelectedText: '-- Select One --',
-
 
11
            nSelectedText: ' - Selected',
-
 
12
            enableFiltering: true,
-
 
13
            enableCaseInsensitiveFiltering: true
-
 
14
        });
-
 
15
    } else {
-
 
16
        $(selector).select2({
-
 
17
            placeholder: "Select or add new",
-
 
18
            tags: true,
-
 
19
            createTag: params => ({ id: params.term, text: params.term, newOption: true }),
-
 
20
            templateResult: data => data.newOption ? $('<span class="new-option">Add: ' + data.text + '</span>') : data.text,
-
 
21
        });
-
 
22
    }
-
 
23
}
-
 
24
 
-
 
25
function validateRequiredFields(fields) {
-
 
26
    let isValid = true;
-
 
27
    fields.each(function () {
-
 
28
        if (!$(this).val()) {
-
 
29
            isValid = false;
-
 
30
            $(this).addClass('is-invalid');
-
 
31
        } else {
-
 
32
            $(this).removeClass('is-invalid');
-
 
33
        }
-
 
34
    });
-
 
35
    return isValid;
-
 
36
}
-
 
37
 
-
 
38
function loadCatalogOptions(brandId, targetElement, type = 'catalogs') {
-
 
39
    $.get(`/super-catalog/brand/${brandId}`, function (response) {
-
 
40
        let options = `<option value="">-- Select --</option>`;
-
 
41
        if (type === 'catalogs'){
-
 
42
            response.catalogs.forEach(row => { options += `<option value="${row.id}">${row.description}</option>`; });
-
 
43
        } else {
-
 
44
            response.superCatalogs.forEach(row => { options += `<option value="${row.id}">${row.superCatalogName}</option>`; });
-
 
45
        }
-
 
46
        $(targetElement).html(options);
-
 
47
        initializeSelect(targetElement, type);
-
 
48
    }).fail(() => {
-
 
49
        alert("Error: Unable to load catalogs for the selected brand.");
-
 
50
    });
-
 
51
}
-
 
52
 
1
$(document).on('click', ".super-catalog-list", function () {
53
$(document).on('click', ".super-catalog-list", function () {
-
 
54
 
-
 
55
    const freshTr = `
-
 
56
            <tr>
-
 
57
                <td>
-
 
58
                    <div class="form-group">
-
 
59
                        <select id="catalog_id" class="form-control"></select>
-
 
60
                    </div>
-
 
61
                </td>
-
 
62
                <td>
-
 
63
                    <div class="form-group">
-
 
64
                        <input id="variant_name" class="form-control" placeholder="Variant Name">
-
 
65
                        <input id="mapping_id" type="hidden" value="0">
-
 
66
                    </div>
-
 
67
                </td>
-
 
68
                <td>
-
 
69
                    <button class="btn btn-danger removeExisting"><i class="fa fa-minus"></i></button>
-
 
70
                </td>
-
 
71
            </tr>`;
-
 
72
 
2
    doGetAjaxRequestHandler(context + "/super-catalog",
73
    doGetAjaxRequestHandler(context + "/super-catalog",
3
        (response) => {
74
        (response) => {
4
            $('#' + 'main-content').html(response);
75
            $('#main-content').html(response);
-
 
76
 
-
 
77
            // Initialize multiSelect
-
 
78
            initializeSelect('#brand_id');
-
 
79
 
-
 
80
            // Add new catalog mapping row
-
 
81
            $('#manageSuperCatalogModal').on('click', '.addNew', function () {
-
 
82
                const brandId = $('#brand_id').val();
-
 
83
                if (brandId) {
-
 
84
                    $('#catalogMappingTable tbody').append(freshTr);
-
 
85
                    const lastRow = $('#catalogMappingTable tbody tr:last');
-
 
86
                    loadCatalogOptions(brandId, lastRow.find('#catalog_id'));
-
 
87
                } else {
-
 
88
                    alert("Please select a brand first.");
-
 
89
                }
-
 
90
            });
-
 
91
 
-
 
92
            // Remove catalog mapping row
-
 
93
            $('#manageSuperCatalogModal').on('click', '.removeExisting', function () {
-
 
94
                if (confirm("Are you sure you want to remove this row?")) {
-
 
95
                    $(this).closest('tr').remove();
-
 
96
                }
-
 
97
            });
-
 
98
 
-
 
99
            // Save super catalog
-
 
100
            $('.save_super_model').on('click', function () {
-
 
101
                const requiredFields = $('#brand_id, #super_catalog_name');
-
 
102
                if (!validateRequiredFields(requiredFields)) {
-
 
103
                    alert('Please fill all required fields.');
-
 
104
                    return;
-
 
105
                }
-
 
106
                alert('Super Catalog saved successfully!');
-
 
107
            });
-
 
108
 
-
 
109
            $('#super-catalog').on('click', '.view-super-catalog', function (ev) {
-
 
110
                ev.preventDefault();
-
 
111
                let superCatalogId = $(this).parent().parent('tr').data('id');
-
 
112
                doGetAjaxRequestHandler(context + "/super-catalog/"+superCatalogId,(response) => {
-
 
113
                    $('#manageSuperCatalogModalBody').empty();
-
 
114
                    $('#manageSuperCatalogModalBody').html(response);
-
 
115
                    initializeSelect('.selectOrCreate', 'superCatalog');
-
 
116
                    initializeSelect('#catalog_id, #brand_id');
-
 
117
                    $('#manageSuperCatalogModal').modal('show');
-
 
118
                });
-
 
119
            });
-
 
120
 
-
 
121
            // Initialize DataTable
-
 
122
            $('#super-catalog').DataTable();
-
 
123
 
5
        });
124
        });
6
});
125
});
7
 
126
 
8
$(document).on('click', ".save_super_model", function () {
127
$(document).on('click', ".save_super_model", function () {
9
    let mappingArr = [];
128
    let mappingArr = [];
Line 13... Line 132...
13
        const mappingId = $(this).find('input#mapping_id').val();
132
        const mappingId = $(this).find('input#mapping_id').val();
14
        if (catalogId > 0) {
133
        if (catalogId > 0) {
15
            mappingArr.push({
134
            mappingArr.push({
16
                catalogId: catalogId,
135
                catalogId: catalogId,
17
                variantName: variantName,
136
                variantName: variantName,
18
                superCatalogId: parseInt($('#super_catalog_name').val()),
137
                superCatalogId: parseInt($('#super_catalog_name').val()) || null,
19
                id: mappingId
138
                id: mappingId
20
            })
139
            })
21
        }
140
        }
22
    });
141
    });
23
    const params = {
142
    const params = {
Line 57... Line 176...
57
            });
176
            });
58
        } else {
177
        } else {
59
            $(this).parent('td').closest('tr').remove();
178
            $(this).parent('td').closest('tr').remove();
60
        }
179
        }
61
    }
180
    }
62
});
-
 
63
181
});
-
 
182
 
-
 
183
 
-
 
184
$(document).on('change', "#brand_id", function() {
-
 
185
    loadCatalogOptions($(this).val(), 'select#super_catalog_name', 'superCatalogs');
-
 
186
});
-
 
187