Subversion Repositories SmartDukaan

Rev

Rev 37270 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
37212 amit 1
$(function () {
2
 
3
    var hotDealModels = [];
37256 amit 4
    var dealsPage = 1;
5
    var dealsQuery = '';
6
    var searchDebounce = null;
37212 amit 7
 
8
    $(document).on('click', ".manage-hot-deals", function () {
9
        loadHotDeals("main-content");
10
    });
11
 
37246 amit 12
    // All five attributes are mandatory with NO defaults - the add form starts empty
13
    function readAddAttributes() {
14
        var warranty = $('#hot-deal-warranty').val();
15
        var condition = $('#hot-deal-condition').val();
37270 amit 16
        var fresh = $('#hot-deal-fresh').val();
37246 amit 17
        var finance = $('#hot-deal-finance').val();
18
        var affordability = $('#hot-deal-affordability').val();
19
        if (warranty === '' || warranty === null) {
20
            alert("Warranty months is required (0 = no warranty, up to 12)");
21
            return null;
22
        }
23
        var warrantyNum = parseInt(warranty, 10);
24
        if (isNaN(warrantyNum) || warrantyNum < 0 || warrantyNum > 12) {
25
            alert("Warranty months must be between 0 and 12");
26
            return null;
27
        }
28
        if (!condition) {
29
            alert("Condition is required");
30
            return null;
31
        }
37270 amit 32
        if (!fresh) {
33
            alert("Fresh (Yes/No) is required");
37246 amit 34
            return null;
35
        }
36
        if (!finance) {
37
            alert("Finance mapping (Yes/No) is required");
38
            return null;
39
        }
40
        if (!affordability) {
41
            alert("Affordability (Yes/No) is required");
42
            return null;
43
        }
44
        return "&warrantyMonths=" + warrantyNum + "&condition=" + condition
37270 amit 45
            + "&fresh=" + fresh + "&financeMapping=" + finance
37246 amit 46
            + "&affordability=" + affordability;
47
    }
48
 
37212 amit 49
    $(document).on('click', ".add-hot-deal", function () {
50
        var catalogId = $('#hot-deal-model-search').data('catalogId');
51
        if (!catalogId) {
52
            alert("Please select a model from the list");
53
            return false;
54
        }
37246 amit 55
        var attrParams = readAddAttributes();
56
        if (attrParams == null) {
57
            return false;
58
        }
37643 amit 59
        // Upsert: the server creates the row if this model has no attributes yet.
60
        var oemCatalogId = $('#hot-deal-oem-search').data('catalogId');
61
        doPostAjaxRequestHandler(context + "/hotDeals/saveAttributes?catalogItemId=" + catalogId
62
            + attrParams
63
            + (oemCatalogId ? "&oemCatalogId=" + oemCatalogId : ""),
37212 amit 64
            function (response) {
65
                if (response == 'true') {
37256 amit 66
                    loadDealsTable();
37643 amit 67
                    loadHotDealModels();
37212 amit 68
                    $('#hot-deal-model-search').val('').removeData('catalogId');
37643 amit 69
                    $('#hot-deal-oem-search').val('').removeData('catalogId')
70
                        .prop('disabled', true).attr('placeholder', 'Pick a Hot Deal model first...');
37246 amit 71
                    $('#hot-deal-warranty').val('');
37270 amit 72
                    $('#hot-deal-condition, #hot-deal-fresh, #hot-deal-finance, #hot-deal-affordability').val('');
37212 amit 73
                } else {
74
                    alert(response);
75
                }
76
            });
77
    });
78
 
37256 amit 79
    // Server-side search over the all-deals table (model or brand), debounced
80
    $(document).on('input', "#hot-deal-table-search", function () {
81
        var q = $(this).val();
82
        clearTimeout(searchDebounce);
83
        searchDebounce = setTimeout(function () {
84
            dealsQuery = q;
85
            dealsPage = 1;
86
            loadDealsTable();
87
        }, 400);
88
    });
89
 
90
    $(document).on('click', ".hot-deal-page-prev", function () {
91
        if (dealsPage > 1) {
92
            dealsPage--;
93
            loadDealsTable();
94
        }
95
    });
96
 
97
    $(document).on('click', ".hot-deal-page-next", function () {
98
        var pages = $('#hot-deals-table-panel').data('pages') || 1;
99
        if (dealsPage < pages) {
100
            dealsPage++;
101
            loadDealsTable();
102
        }
103
    });
104
 
37270 amit 105
    // Model selection for the manual Solr push
106
    function refreshPushButton() {
107
        $('.push-solr').prop('disabled', $('.hot-deal-select:checked').length === 0);
108
    }
109
 
110
    $(document).on('change', "#hot-deal-select-all", function () {
111
        $('.hot-deal-select').prop('checked', $(this).prop('checked'));
112
        refreshPushButton();
113
    });
114
 
115
    $(document).on('change', ".hot-deal-select", refreshPushButton);
116
 
117
    $(document).on('click', ".push-solr", function () {
118
        var catalogIds = $('.hot-deal-select:checked').map(function () {
119
            return $(this).closest('tr').data('catalogId');
120
        }).get();
121
        if (catalogIds.length === 0) {
122
            alert("Select at least one model to push");
123
            return false;
124
        }
125
        if (!confirm("Push " + catalogIds.length + " model(s) to Solr now?")) {
126
            return false;
127
        }
128
        var btn = $(this);
129
        btn.prop('disabled', true).text('Pushing...');
130
        doPostAjaxRequestHandler(context + "/hotDeals/pushSolr?catalogIds=" + catalogIds.join(','),
131
            function (response) {
132
                btn.prop('disabled', false).html('<i class="fa fa-cloud-upload"></i> Push to Solr');
133
                alert(response);
134
                refreshPushButton();
135
            });
136
    });
137
 
37643 amit 138
    $(document).on('change', ".hot-deal-row-field", function () {
37212 amit 139
        $(this).closest('tr').find('.save-hot-deal').show();
140
    });
141
 
142
    $(document).on('click', ".save-hot-deal", function () {
143
        var row = $(this).closest('tr');
37643 amit 144
        var catalogId = row.data('catalogId');
37246 amit 145
        var warranty = row.find('.hot-deal-row-warranty').val();
146
        var warrantyNum = parseInt(warranty, 10);
147
        if (warranty === '' || isNaN(warrantyNum) || warrantyNum < 0 || warrantyNum > 12) {
148
            alert("Warranty months must be between 0 and 12");
149
            return false;
150
        }
37643 amit 151
        doPostAjaxRequestHandler(context + "/hotDeals/saveAttributes?catalogItemId=" + catalogId
37246 amit 152
            + "&warrantyMonths=" + warrantyNum
153
            + "&condition=" + row.find('.hot-deal-row-condition').val()
37270 amit 154
            + "&fresh=" + row.find('.hot-deal-row-fresh').val()
37246 amit 155
            + "&financeMapping=" + row.find('.hot-deal-row-finance').val()
156
            + "&affordability=" + row.find('.hot-deal-row-affordability').val(),
37212 amit 157
            function (response) {
158
                if (response == 'true') {
37256 amit 159
                    loadDealsTable();
37212 amit 160
                } else {
161
                    alert(response);
162
                }
163
            });
164
    });
165
 
166
    $(document).on('click', ".remove-hot-deal", function () {
167
        var id = $(this).closest('tr').data('dealId');
168
        if (confirm("Remove this model from hot deals?") == true) {
169
            doPostAjaxRequestHandler(context + "/hotDeals/remove?id=" + id,
170
                function (response) {
171
                    if (response == 'true') {
37256 amit 172
                        loadDealsTable();
37643 amit 173
                        loadHotDealModels();
37212 amit 174
                    } else {
175
                        alert(response);
176
                    }
177
                });
178
        }
179
    });
180
 
181
    function loadHotDeals(domId) {
182
        doGetAjaxRequestHandler(context + "/manageHotDeals", function (response) {
183
            $('#' + domId).html(response);
37256 amit 184
            dealsPage = 1;
185
            dealsQuery = '';
186
            loadDealsTable();
37643 amit 187
            loadHotDealModels();
37212 amit 188
        });
189
    }
190
 
37643 amit 191
    /**
192
     * OEM mapping autosuggest, scoped to the brand the selected Hot Deal model came from.
193
     * Optional: a model with no channel twin is simply left unmapped.
194
     */
195
    function loadOemModels(oemBrand) {
196
        var input = $('#hot-deal-oem-search');
197
        input.val('').removeData('catalogId');
198
        if (!oemBrand) {
199
            input.prop('disabled', true).attr('placeholder', 'No OEM brand recorded');
200
            return;
201
        }
202
        doGetAjaxRequestHandler(context + "/hotDeals/oemModels?brand=" + encodeURIComponent(oemBrand),
203
            function (response) {
204
                var oemModels = JSON.parse(response);
205
                input.prop('disabled', false)
206
                    .attr('placeholder', 'Optional - search ' + oemBrand + ' models...');
207
                input.typeahead('destroy').typeahead({
208
                    source: oemModels,
209
                    items: 15,
210
                    minLength: 2,
211
                    displayText: function (item) {
212
                        return item.description;
213
                    },
214
                    autoSelect: true,
215
                    afterSelect: function (item) {
216
                        input.data('catalogId', item.catalogId);
217
                    }
218
                });
219
            });
220
    }
221
 
37256 amit 222
    function loadDealsTable() {
223
        doGetAjaxRequestHandler(context + "/hotDeals/all?page=" + dealsPage
224
            + "&q=" + encodeURIComponent(dealsQuery), function (response) {
37212 amit 225
            $('#hot-deals-table-container').html(response);
37256 amit 226
            dealsPage = $('#hot-deals-table-panel').data('page') || 1;
227
        });
228
    }
229
 
37643 amit 230
    // Every Hot Deal-brand model. One brand, so nothing to scope by.
231
    function loadHotDealModels() {
232
        doGetAjaxRequestHandler(context + "/hotDeals/models", function (response) {
37212 amit 233
            hotDealModels = JSON.parse(response);
234
            var input = $('#hot-deal-model-search');
235
            input.typeahead('destroy').typeahead({
236
                source: hotDealModels,
237
                items: 15,
238
                minLength: 2,
239
                displayText: function (item) {
240
                    return item.description;
241
                },
242
                autoSelect: true,
243
                afterSelect: function (item) {
244
                    input.data('catalogId', item.catalogId);
37643 amit 245
                    loadOemModels(item.oemBrand);
37212 amit 246
                }
247
            });
248
        });
249
    }
250
 
251
});