Subversion Repositories SmartDukaan

Rev

Rev 37246 | Rev 37270 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 37246 Rev 37256
Line 1... Line 1...
1
$(function () {
1
$(function () {
2
 
2
 
3
    var hotDealModels = [];
3
    var hotDealModels = [];
-
 
4
    var dealsPage = 1;
-
 
5
    var dealsQuery = '';
-
 
6
    var searchDebounce = null;
4
 
7
 
5
    $(document).on('click', ".manage-hot-deals", function () {
8
    $(document).on('click', ".manage-hot-deals", function () {
6
        loadHotDeals("main-content");
9
        loadHotDeals("main-content");
7
    });
10
    });
8
 
11
 
-
 
12
    // Brand choice drives the add flow: model picker + slot usage for that brand
9
    $(document).on('change', "#hot-deal-brand", function () {
13
    $(document).on('change', "#hot-deal-brand", function () {
10
        var brandId = $(this).val();
14
        var brandId = $(this).val();
-
 
15
        var input = $('#hot-deal-model-search');
-
 
16
        input.val('').removeData('catalogId');
11
        if (!brandId) {
17
        if (!brandId) {
12
            $('#hot-deal-add-panel').hide();
-
 
13
            $('#hot-deals-table-container').empty();
18
            input.prop('disabled', true).attr('placeholder', 'Select a brand first...');
14
            $('#hot-deal-slots').text('');
19
            $('#hot-deal-slots').text('');
-
 
20
            $('.add-hot-deal').prop('disabled', false);
-
 
21
            $('#hot-deal-cap-notice').hide();
15
            return;
22
            return;
16
        }
23
        }
17
        loadHotDealsTable(brandId);
24
        input.prop('disabled', false).attr('placeholder', 'Type 2+ letters to search models...');
18
        loadHotDealModels(brandId);
25
        loadHotDealModels(brandId);
-
 
26
        loadSlots(brandId);
19
    });
27
    });
20
 
28
 
21
    // All five attributes are mandatory with NO defaults - the add form starts empty
29
    // All five attributes are mandatory with NO defaults - the add form starts empty
22
    function readAddAttributes() {
30
    function readAddAttributes() {
23
        var warranty = $('#hot-deal-warranty').val();
31
        var warranty = $('#hot-deal-warranty').val();
Line 58... Line 66...
58
    $(document).on('click', ".add-hot-deal", function () {
66
    $(document).on('click', ".add-hot-deal", function () {
59
        var brandId = $('#hot-deal-brand').val();
67
        var brandId = $('#hot-deal-brand').val();
60
        var catalogId = $('#hot-deal-model-search').data('catalogId');
68
        var catalogId = $('#hot-deal-model-search').data('catalogId');
61
        var startDate = $('#hot-deal-start-date').val();
69
        var startDate = $('#hot-deal-start-date').val();
62
        var endDate = $('#hot-deal-end-date').val();
70
        var endDate = $('#hot-deal-end-date').val();
-
 
71
        if (!brandId) {
-
 
72
            alert("Please select a brand");
-
 
73
            return false;
-
 
74
        }
63
        if (!catalogId) {
75
        if (!catalogId) {
64
            alert("Please select a model from the list");
76
            alert("Please select a model from the list");
65
            return false;
77
            return false;
66
        }
78
        }
67
        if (!startDate || !endDate) {
79
        if (!startDate || !endDate) {
Line 79... Line 91...
79
        doPostAjaxRequestHandler(context + "/hotDeals/add?brandId=" + brandId
91
        doPostAjaxRequestHandler(context + "/hotDeals/add?brandId=" + brandId
80
            + "&catalogItemId=" + catalogId + "&startDate=" + startDate + "&endDate=" + endDate
92
            + "&catalogItemId=" + catalogId + "&startDate=" + startDate + "&endDate=" + endDate
81
            + attrParams,
93
            + attrParams,
82
            function (response) {
94
            function (response) {
83
                if (response == 'true') {
95
                if (response == 'true') {
84
                    loadHotDealsTable(brandId);
96
                    loadDealsTable();
85
                    loadHotDealModels(brandId);
97
                    loadHotDealModels(brandId);
-
 
98
                    loadSlots(brandId);
86
                    $('#hot-deal-model-search').val('').removeData('catalogId');
99
                    $('#hot-deal-model-search').val('').removeData('catalogId');
87
                    $('#hot-deal-warranty').val('');
100
                    $('#hot-deal-warranty').val('');
88
                    $('#hot-deal-condition, #hot-deal-activated, #hot-deal-finance, #hot-deal-affordability').val('');
101
                    $('#hot-deal-condition, #hot-deal-activated, #hot-deal-finance, #hot-deal-affordability').val('');
89
                } else {
102
                } else {
90
                    alert(response);
103
                    alert(response);
91
                }
104
                }
92
            });
105
            });
93
    });
106
    });
94
 
107
 
-
 
108
    // Server-side search over the all-deals table (model or brand), debounced
-
 
109
    $(document).on('input', "#hot-deal-table-search", function () {
-
 
110
        var q = $(this).val();
-
 
111
        clearTimeout(searchDebounce);
-
 
112
        searchDebounce = setTimeout(function () {
-
 
113
            dealsQuery = q;
-
 
114
            dealsPage = 1;
-
 
115
            loadDealsTable();
-
 
116
        }, 400);
-
 
117
    });
-
 
118
 
-
 
119
    $(document).on('click', ".hot-deal-page-prev", function () {
-
 
120
        if (dealsPage > 1) {
-
 
121
            dealsPage--;
-
 
122
            loadDealsTable();
-
 
123
        }
-
 
124
    });
-
 
125
 
-
 
126
    $(document).on('click', ".hot-deal-page-next", function () {
-
 
127
        var pages = $('#hot-deals-table-panel').data('pages') || 1;
-
 
128
        if (dealsPage < pages) {
-
 
129
            dealsPage++;
-
 
130
            loadDealsTable();
-
 
131
        }
-
 
132
    });
-
 
133
 
95
    $(document).on('change', ".hot-deal-row-start, .hot-deal-row-end, .hot-deal-row-field", function () {
134
    $(document).on('change', ".hot-deal-row-start, .hot-deal-row-end, .hot-deal-row-field", function () {
96
        $(this).closest('tr').find('.save-hot-deal').show();
135
        $(this).closest('tr').find('.save-hot-deal').show();
97
    });
136
    });
98
 
137
 
99
    $(document).on('click', ".save-hot-deal", function () {
138
    $(document).on('click', ".save-hot-deal", function () {
Line 122... Line 161...
122
            + "&activated=" + row.find('.hot-deal-row-activated').val()
161
            + "&activated=" + row.find('.hot-deal-row-activated').val()
123
            + "&financeMapping=" + row.find('.hot-deal-row-finance').val()
162
            + "&financeMapping=" + row.find('.hot-deal-row-finance').val()
124
            + "&affordability=" + row.find('.hot-deal-row-affordability').val(),
163
            + "&affordability=" + row.find('.hot-deal-row-affordability').val(),
125
            function (response) {
164
            function (response) {
126
                if (response == 'true') {
165
                if (response == 'true') {
127
                    loadHotDealsTable($('#hot-deal-brand').val());
166
                    loadDealsTable();
128
                    loadHotDealModels($('#hot-deal-brand').val());
167
                    refreshBrandPanel();
129
                } else {
168
                } else {
130
                    alert(response);
169
                    alert(response);
131
                }
170
                }
132
            });
171
            });
133
    });
172
    });
Line 136... Line 175...
136
        var id = $(this).closest('tr').data('dealId');
175
        var id = $(this).closest('tr').data('dealId');
137
        if (confirm("Remove this model from hot deals?") == true) {
176
        if (confirm("Remove this model from hot deals?") == true) {
138
            doPostAjaxRequestHandler(context + "/hotDeals/remove?id=" + id,
177
            doPostAjaxRequestHandler(context + "/hotDeals/remove?id=" + id,
139
                function (response) {
178
                function (response) {
140
                    if (response == 'true') {
179
                    if (response == 'true') {
141
                        loadHotDealsTable($('#hot-deal-brand').val());
180
                        loadDealsTable();
142
                        loadHotDealModels($('#hot-deal-brand').val());
181
                        refreshBrandPanel();
143
                    } else {
182
                    } else {
144
                        alert(response);
183
                        alert(response);
145
                    }
184
                    }
146
                });
185
                });
147
        }
186
        }
148
    });
187
    });
149
 
188
 
150
    function loadHotDeals(domId) {
189
    function loadHotDeals(domId) {
151
        doGetAjaxRequestHandler(context + "/manageHotDeals", function (response) {
190
        doGetAjaxRequestHandler(context + "/manageHotDeals", function (response) {
152
            $('#' + domId).html(response);
191
            $('#' + domId).html(response);
-
 
192
            dealsPage = 1;
-
 
193
            dealsQuery = '';
153
            setDefaultHotDealDates();
194
            setDefaultHotDealDates();
-
 
195
            loadDealsTable();
154
        });
196
        });
155
    }
197
    }
156
 
198
 
157
    function loadHotDealsTable(brandId) {
199
    function loadDealsTable() {
158
        doGetAjaxRequestHandler(context + "/hotDeals?brandId=" + brandId, function (response) {
200
        doGetAjaxRequestHandler(context + "/hotDeals/all?page=" + dealsPage
-
 
201
            + "&q=" + encodeURIComponent(dealsQuery), function (response) {
159
            $('#hot-deals-table-container').html(response);
202
            $('#hot-deals-table-container').html(response);
160
            var panel = $('#hot-deals-table-panel');
203
            dealsPage = $('#hot-deals-table-panel').data('page') || 1;
-
 
204
        });
-
 
205
    }
-
 
206
 
-
 
207
    function refreshBrandPanel() {
161
            var active = panel.data('activeCount');
208
        var brandId = $('#hot-deal-brand').val();
-
 
209
        if (brandId) {
162
            var max = panel.data('maxDeals');
210
            loadHotDealModels(brandId);
163
            $('#hot-deal-slots').text(active + " / " + max + " slots used")
211
            loadSlots(brandId);
-
 
212
        }
-
 
213
    }
-
 
214
 
164
                .toggleClass('label-danger', active >= max)
215
    function loadSlots(brandId) {
165
                .toggleClass('label-default', active < max);
216
        doGetAjaxRequestHandler(context + "/hotDeals/slots?brandId=" + brandId, function (response) {
166
            $('#hot-deal-add-panel').show();
217
            $('#hot-deal-slots').text(response + " slots used");
167
            var full = active >= max;
218
            var parts = response.split('/');
-
 
219
            var full = parts.length == 2 && parseInt(parts[0], 10) >= parseInt(parts[1], 10);
-
 
220
            $('#hot-deal-slots').toggleClass('label-danger', full).toggleClass('label-default', !full);
168
            $('.add-hot-deal').prop('disabled', full);
221
            $('.add-hot-deal').prop('disabled', full);
169
            $('#hot-deal-cap-notice').toggle(full);
222
            $('#hot-deal-cap-notice').toggle(full);
170
            setDefaultHotDealDates();
-
 
171
        });
223
        });
172
    }
224
    }
173
 
225
 
174
    function loadHotDealModels(brandId) {
226
    function loadHotDealModels(brandId) {
175
        doGetAjaxRequestHandler(context + "/hotDeals/models?brandId=" + brandId, function (response) {
227
        doGetAjaxRequestHandler(context + "/hotDeals/models?brandId=" + brandId, function (response) {
176
            hotDealModels = JSON.parse(response);
228
            hotDealModels = JSON.parse(response);
177
            var input = $('#hot-deal-model-search');
229
            var input = $('#hot-deal-model-search');
178
            input.val('').removeData('catalogId');
-
 
179
            input.typeahead('destroy').typeahead({
230
            input.typeahead('destroy').typeahead({
180
                source: hotDealModels,
231
                source: hotDealModels,
181
                items: 15,
232
                items: 15,
182
                minLength: 2,
233
                minLength: 2,
183
                displayText: function (item) {
234
                displayText: function (item) {