PageSourceSearch

https://www.haggisadventures.com/scripts/AngularFrontEnd/Components/Controllers/productSearchCtrl.js

js haggisadventures.com collected 2026-09-26 10:20:28 UTC 18,890 bytes, 440 lines download raw bytes

1(function () {
2    "use strict";
3
4    angular
5        .module("productComponents")
6        .controller("productSearchCtrl", productSearchCtrl);
7
8    productSearchCtrl.$inject = ["$scope", "searchInfo", "productService", "MONTH_OPTIONS", "MONTHS"];
9    
10    function productSearchCtrl($scope, searchInfo, productService, MONTH_OPTIONS, MONTHS) {
11        var searchData = "";
12        $scope.searchHighlightSelection = [];
13        $scope.searchDurationSelection = [];
14        $scope.searchMonthSelection = [];
15
16        $scope.selectedProducts = [];
17        $scope.selectedHighlight = "";
18        $scope.selectedDuration = "";
19        $scope.selectedMonth = "";
20        $scope.selectedSearchBar = "";
21        $scope.test = "";
22
23        $scope.monthOptions = MONTH_OPTIONS;
24        $scope.months = MONTHS;
25
26        $scope.durationProductIds = [];
27        $scope.monthProductIds = [];
28        $scope.highlightProductIds = [];
29        $scope.productsToDisplay = [];
30        $scope.highlightMetaData = [];
31        $scope.dateProductIds = [];
32        $scope.filteredProducts = [];
33
34        $scope.sortType = '';
35        $scope.sortReverse = false;
36        $scope.currency = searchInfo.customerCurrency;
37        $scope.submitted = false;
38        $scope.monthSubmitted = false;
39        $scope.productsFiltered = false;
40        $scope.resultsFromFilterChoice = false;
41        $scope.loading = false;
42        $scope.dateSearchedAndNoResults = false;
43        $scope.anyMonthSubmitted = false;
44        $scope.allProductsById = [];
45        $scope.selectedDate = "";
46        $scope.restartedSearch = false;
47
48        productService.getFilters(searchInfo.modelId, searchInfo.currentCulture, searchInfo.customerCurrency, searchInfo.brand).then(function (response) {
49
50            searchData = response.data;
51            var highlightOptions = [];
52            var durationOptions = [];
53            angular.forEach(searchData.highlights, function (highlight) {
54                highlightOptions.push(highlight['nameShort']);
55            });
56
57            angular.forEach(searchData.durations, function (durationGroup) {
58                durationOptions.push(durationGroup['durationGrouping']);
59            });
60
61            $scope.highlightOptions = highlightOptions;
62            $scope.durationOptions = durationOptions;
63        });
64
65        productService.getAllFilterProductData(searchInfo.modelId, searchInfo.currentCulture, searchInfo.customerCurrency, searchInfo.brand).then(function (response) {
66            $scope.allTourProductData = response.data;
67        });
68
69        $scope.blankCheck = function (propertyToCheck) {
70            if (propertyToCheck == "" || propertyToCheck == null || propertyToCheck == undefined)
71                return true;
72            else
73                return false;
74        }
75
76        $scope.returnToStart = function () {
77            $scope.restartedSearch = true;
78            window.location.href = '/';
79        }
80
81        $scope.SearchUsersTerms = function (tagList, product) {
82            var found = false;
83            var searchWords = $scope.selectedSearchBar.split(" ");
84            angular.forEach(searchWords, function (word) {
85                if (tagList.toUpperCase().includes(word.toUpperCase()) && tagList.toUpperCase().includes(product.id.toUpperCase())) {
86                    found = true;
87                }
88            });
89            return found;
90        }
91
92
93        $scope.landingSearch = function (searchBarTerm, productTags) {
94            //if all boxes empty display full list
95            if ($scope.blankCheck($scope.selectedMonth) && $scope.blankCheck($scope.selectedDuration) && $scope.blankCheck($scope.selectedSearchBar)) {
96                $scope.selectedMonth = "Any";
97                $scope.searchMonth();
98            }
99            else {
100
101                if ($scope.blankCheck($scope.selectedMonth))
102                {
103                    $scope.selectedMonth = "Any";
104                }
105
106                var IdandTags = productTags.split("|");
107
108                $scope.searchDurationSelection.push($scope.selectedDuration);
109                $scope.searchMonthSelection.push($scope.selectedMonth);
110                var numericSelectedMonth = $scope.months[$scope.searchMonthSelection];
111                $scope.$broadcast('setSearchMonthVariable', numericSelectedMonth);
112                angular.forEach($scope.allTourProductData, function (product) {
113                    //if tags are not empty
114                    if (!$scope.blankCheck(IdandTags)) {
115                        //if search tags contain search bar text
116                        angular.forEach(IdandTags, function (tagList) {
117                            if ($scope.SearchUsersTerms(tagList, product)) {
118                                if ($scope.searchMonthSelection[0] === "Any") {
119                                    $scope.productsToDisplay.push(product);
120                                    $scope.allProductsById.push(product.id);
121                                    $scope.anyMonthSubmitted = true;
122
123                                }
124                                else if ($scope.searchMonthSelection.length > 0) {
125
126                                    angular.forEach(product.months, function (date) {
127                                        if (date[0] === numericSelectedMonth && !$scope.monthProductIds.includes(product.id)) {
128                                            $scope.monthProductIds.push(product.id);
129                                            $scope.productsToDisplay.push(product);
130                                        }
131                                    });
132                                }
133                            }
134                        })
135                        
136                    }
137                    //if tags empty, ignore search bar
138                    else {
139                        if ($scope.searchMonthSelection[0] === "Any") {
140                            $scope.productsToDisplay.push(product);
141                            $scope.allProductsById.push(product.id);
142                            $scope.anyMonthSubmitted = true;
143
144                        }
145                        else if ($scope.searchMonthSelection.length > 0) {
146
147                            angular.forEach(product.months, function (date) {
148                                if (date[0] === numericSelectedMonth && !$scope.monthProductIds.includes(product.id)) {
149                                    $scope.monthProductIds.push(product.id);
150                                    $scope.productsToDisplay.push(product);
151                                }
152                            });
153                        }
154                    }
155                });
156                $scope.searchMonthSelection = [];
157                $scope.monthSubmitted = true;
158                if ($scope.selectedMonth !== "") {
159                    setTimeout(function () {
160                        $("html, body").animate({
161                            scrollTop: 200
162                        }, 1000);
163                    }, 500);
164                }
165                if ($scope.selectedProducts.toString() == null || $scope.selectedProducts.toString() == "") {
166                    $scope.selectedDuration = "Any";
167                }
168                else {
169                    $scope.selectedDuration = $scope.selectedProducts;
170                }
171                $scope.submit();
172            }
173
174        }
175
176        $scope.searchMonth = function () {
177
178            $scope.searchMonthSelection.push($scope.selectedMonth);
179            var numericSelectedMonth = $scope.months[$scope.searchMonthSelection];
180            $scope.$broadcast('setSearchMonthVariable', numericSelectedMonth);
181
182            angular.forEach($scope.allTourProductData, function (product) {
183                if ($scope.searchMonthSelection[0] === "Any") {
184                    $scope.productsToDisplay.push(product);
185                    $scope.allProductsById.push(product.id);
186                    $scope.anyMonthSubmitted = true;
187
188                }
189                else if ($scope.searchMonthSelection.length > 0) {
190
191                    angular.forEach(product.months, function (date) {
192                        if (date[0] === numericSelectedMonth && !$scope.monthProductIds.includes(product.id)) {
193                            $scope.monthProductIds.push(product.id);
194                            $scope.productsToDisplay.push(product);
195                        }
196                    });
197                }
198            });
199            //console.log($scope.monthProductIds);
200            //console.log($scope.productsToDisplay);
201            //$scope.selectedMonth = '';
202            $scope.searchMonthSelection = [];
203            $scope.monthSubmitted = true;
204            if ($scope.selectedMonth !== "") {
205                setTimeout(function () {
206                    $("html, body").animate({
207                        scrollTop: 200
208                    }, 1000);
209                }, 500);
210            }
211        };
212
213        $scope.searchDate = function (date) {
214            $scope.loading = true;
215            var d = moment(date, "DD-MM-YYYY");
216            $scope.selectedDate = d.format('LL');
217            var selectedDay = d.date();
218            var selectedMonth = d.month() + 1;
219            var selectedYear = d.year();
220
221            productService.getTMSProductAvailabilityForDate(searchInfo.brand, selectedYear, selectedMonth, selectedDay).then(function (response) {
222                $scope.dateProductIds = response.data;
223
224                $scope.loading = false;
225
226                if ($scope.dateProductIds.length === 0) {
227                    $scope.dateSearchedAndNoResults = true;
228                }
229                else {
230                    $scope.dateSearchedAndNoResults = false;
231                }
232                //console.log("dateProductIds", $scope.dateProductIds);
233                $scope.submit();
234
235            });
236        };
237        ///should be submitSearchFilters
238        $scope.submit = function () {
239            $scope.clearFilters();
240            $scope.submitted = true;
241            $scope.highlightMetaData = [];
242
243            //duration
244            if ($scope.selectedDuration) {
245                //console.log($scope.selectedDuration);
246                $scope.searchDurationSelection.push($scope.selectedDuration);
247                angular.forEach($scope.searchDurationSelection, function (value, key) {
248                    angular.forEach(searchData.durations, function (d) {
249                        if (d['durationGrouping'] === value) {
250                            $scope.durationProductIds = d['productIds'];
251                        }
252                    });
253                });
254                $scope.searchDurationSelection = [];
255            }
256
257            //highlights
258            if ($scope.selectedHighlight) {
259                //console.log($scope.selectedHighlight);
260                $scope.searchHighlightSelection.push($scope.selectedHighlight);
261                angular.forEach($scope.searchHighlightSelection, function (value, key) {
262                    angular.forEach(searchData.highlights, function (h) {
263                        if (h['nameShort'] === value) {
264                            $scope.highlightProductIds = h['productIds'];
265                            $scope.highlightMetaData = h['highlightMetaData'];
266                        }
267                    });
268                });
269                $scope.searchHighlightSelection = [];
270            }
271
272            if ($scope.dateSearchedAndNoResults) {
273                $scope.productsFiltered = true;
274                $scope.filteredProducts = [];
275                $scope.selectedProducts = [];
276            }
277            else {
278                if ($scope.dateProductIds.length > 0 && $scope.durationProductIds.length === 0 && $scope.highlightProductIds.length === 0) {
279                    $scope.productsFiltered = true;
280                    $scope.selectedProducts = $scope.dateProductIds;
281                }
282                if ($scope.dateProductIds.length > 0 && $scope.durationProductIds.length > 0 && $scope.highlightProductIds.length === 0) {
283                    $scope.productsFiltered = true;
284                    $scope.selectedProducts = diff($scope.dateProductIds, $scope.durationProductIds);
285                }
286                if ($scope.dateProductIds.length > 0 && $scope.durationProductIds.length === 0 && $scope.highlightProductIds.length > 0) {
287                    $scope.productsFiltered = true;
288                    $scope.selectedProducts = diff($scope.dateProductIds, $scope.highlightProductIds);
289                }
290                if ($scope.dateProductIds.length > 0 && $scope.durationProductIds.length > 0 && $scope.highlightProductIds.length > 0) {
291                    $scope.productsFiltered = true;
292                    var intermediaryArray = diff($scope.dateProductIds, $scope.durationProductIds);
293                    $scope.selectedProducts = diff(intermediaryArray, $scope.highlightProductIds);
294                }
295                if ($scope.dateProductIds.length === 0) {
296                    //filter product ids based on combination of search f
296ields selected.
297                    if ($scope.durationProductIds.length > 0) {
298                        $scope.productsFiltered = true;
299
300                        if ($scope.highlightProductIds.length > 0) {
301                            $scope.selectedProducts = diff($scope.durationProductIds, $scope.highlightProductIds);
302                            //console.log("duration and month and highlight" + $scope.selectedProducts);
303                        }
304                        if ($scope.highlightProductIds.length === 0) {
305                            $scope.selectedProducts = $scope.durationProductIds;
306                            //console.log("duration and month and highlight" + $scope.selectedProducts);
307                        }
308                    }
309                    else if ($scope.durationProductIds.length === 0 && $scope.highlightProductIds.length > 0) {
310                        $scope.selectedProducts = $scope.highlightProductIds;
311                        $scope.productsFiltered = true;
312                    }
313                    else {
314                        $scope.selectedProducts = [];
315                    }
316                }
317
318            }
319
320
321            $scope.durationProductIds = [];
322            $scope.highlightProductIds = [];
323            if ($scope.selectedProducts.length > 0) {
324                if ($scope.anyMonthSubmitted && $scope.allProductsById.length > 0) {
325                    $scope.filteredProducts = diff($scope.allProductsById, $scope.selectedProducts);
326                }
327                else {
328                    $scope.filteredProducts = diff($scope.selectedProducts, $scope.monthProductIds);
329                }
330
331            }
332
333            if ($scope.productsFiltered && $scope.filteredProducts.length === 0 && $scope.productsToDisplay.length > 0) {
334                $scope.resultsFromFilterChoice = false;
335            }
336            else if ($scope.productsFiltered && $scope.filteredProducts.length > 0 && $scope.productsToDisplay.length > 0) {
337                $scope.resultsFromFilterChoice = true;
338            }
339
340            $scope.selectedProducts = [];
341            $(".product-search-results").fadeOut("slow").fadeIn("slow");
342        };
343
344        $scope.clearSearch = function (triggeredFromDatePicker = false) {
345            $scope.selectedDuration = '';
346            $scope.selectedHighlight = '';
347            $scope.selectedMonth = '';
348
349            $scope.searchHighlightSelection = [];
350            $scope.searchDurationSelection = [];
351            $scope.searchMonthSelection = [];
352
353            $scope.durationProductIds = [];
354            $scope.monthProductIds = [];
355            $scope.highlightProductIds = [];
356
357            $scope.highlightMetaData = [];
358
359            $scope.selectedProducts = [];
360            $scope.productsToDisplay = [];
361            $scope.submitted = false;
362            $scope.monthSubmitted = false;
363            $scope.productsFiltered = false;
364            $scope.filteredProducts = [];
365            if (!triggeredFromDatePicker) {
366                $scope.$broadcast('resetProductSearchDatePicker');
367            }
368            $scope.anyMonthSubmitted = false;
369            $scope.allProductsById = [];
370            $scope.selectedDate = "";
371        };
372
373        $scope.resetFilters = function () {
374            $scope.selectedHighlight = '0';
375            $scope.selectedDuration = '0';
376            $scope.selectedDate = "";
377            $scope.dateSearchedAndNoResults = false;
378            $scope.clearFilters();
379        };
380
381        ///These are all filters except from the date filter...
382        $scope.clearFilters = function () {
383            $scope.submitted = false;
384            $scope.searchHighlightSelection = [];
385            $scope.searchDurationSelection = [];
386            $scope.searchMonthSelection = [];
387
388            $scope.durationProductIds = [];
389            $scope.highlightProductIds = [];
390
391            $scope.highlightMetaData = [];
392
393            $scope.selectedProducts = [];
394            $scope.productsFiltered = false;
395            $scope.filteredProducts = [];
396            $scope.resultsFromFilterChoice = false;
397        };
398
399        $scope.clearAllFilters = function () {
400            $scope.resetFilters();
401            $scope.$broadcast('resetProductSearchDatePicker');
402            $scope.dateProductIds = [];
403        };
404
405
406        //refactor
407        var diff = function (arr1, arr2) {
408            var ret = [];
409            for (var i in arr1) {
410                if (arr2.indexOf(arr1[i]) > -1) {
411                    ret.push(arr1[i]);
412                }
413            }
414            return ret;
415        };
416
417        $scope.productFilter = function (item) {
418            if ($scope.productsFiltered && $scope.filteredProducts.length > 0 ) {
419                //var arr = [1, 25, 8]; //Your integer array here
420                return $scope.filteredProducts.indexOf(item.id) > -1; //returns true if exists
421            }
422
423        };
424
425        $scope.clearDate = function () {
426            $scope.$broadcast('resetProductSearchDatePicker');
427            $scope.dateProductIds = [];
428            $scope.submit();
429        };
430
431        $scope.$on('resetProductSearch', function (event, data) {
432            $scope.clearSearch(true);
433        });
434
435        $scope.$on('searchDate', function (event, data) {
436            $scope.searchDate(data);
437            console.log('search value', data);
438        });
439    }
440}());

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.