File: D:/HostingSpaces/SBogers10/topswtwmobile.komma.pro/wwwroot/js/app/searchbar/searchbar.ng.js
define([],
function () {
'use strict';
var app = angular.module('searchbar', []);
app.config(['$httpProvider', function ($httpProvider) {
$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
}]);
app.controller('TopsSearch', ['$scope', '$http', '$element', 'filterFilter', 'translationsService', '$location', '$q', function ($scope, $http, $element, filterFilter, translationsService, $location, $q) {
//Set the ng_model on undefined
$scope.selected = undefined;
//Set search_val to the value of the search bar element
if (angular.element('#top-search-bar').val()) {
//if it is not empty, set the model to the value (instead of placeholder)
$scope.selected = angular.element('#top-search-bar').val();
}
$scope.selectItem = function ($item, $model, $label) {
// Ga naar zoekresultaten
if ($scope.selected.searchAll) {
if (typeof $scope.selected.route != undefined) {
window.location.href = $('form', $element).attr('action') + $scope.selected.route;
return;
}
$('form', $element).first().submit();
return;
}
// Ga naar item url
$scope.selected = $model;
//$scope.$apply();
window.location.href = '/' + $scope.selected.route;
return;
};
$scope.searchItem = function ($item, $model, $label) {
if (!$scope.selected.searchAll) {
// Use item name as search query
$scope.selected = $model;
$scope.$apply();
}
$('form', $element).first().submit();
};
//empty global canceller
var canceller = $q.defer();
//create the cancel function
$scope.cancel = function () {
canceller.resolve("user cancelled");
};
$scope.getLocation = function (val, url) {
//stop any gets that are running
$scope.cancel();
//reset the canceler
canceller = $q.defer();
return $http.get(url, {
params: {
q: val,
ajax: true
},
timeout: canceller.promise
}).then(function (response) {
var data = [];
// fill products
first = true;
if (response.data.productsResults.result) {
data = data.concat(response.data.productsResults.result.map(function (item) {
var text = [];
//if(item._score) text.push(item._score);
var link_text = '';
if (item.brand_name) link_text += item.brand_name;
if (item.name) link_text += ' ' + item.name;
if (item.article_number) link_text += ' <span class="article_number">' + item.article_number + '</span>';
text.push(link_text);
var header = null;
header
if (first) {
header = translationsService.get('cart.products') + (response.data.productsResults.count > 5 ? ' (' + response.data.productsResults.count + ')' : '');
first = false;
}
return {
text: text.join(' - '),
header: header,
route: item.route
};
})
);
if (response.data.productsResults.count > 5) {
data.push({
text: translationsService.get('cart.show_all_products') + (response.data.productsResults.count > 5 ? ' (' + response.data.productsResults.count + ')' : ''),
route: '?q=' + val + '&show=products',
searchAll: true
});
}
}
// fill categories
var first = true;
if (response.data.categoriesResults.result) {
data = data.concat(response.data.categoriesResults.result.map(function (item) {
var text = [];
if (item.name) text.push(item.name);
var header = null;
if (first) {
header = translationsService.get('cart.categories') + (response.data.categoriesResults.count > 5 ? ' (' + response.data.categoriesResults.count + ')' : '');
first = false;
}
return {
text: text.join(' - '),
header: header,
route: item.route
};
}));
}
if (response.data.categoriesResults.count > 5) {
data.push({
text: translationsService.get('cart.show_all_categories') + (response.data.categoriesResults.count > 5 ? ' (' + response.data.categoriesResults.count + ')' : ''),
route: '?q=' + val + '&show=categories',
searchAll: true
});
}
if (response.data.pagesResults.result) {
// fill pages
first = true;
data = data.concat(response.data.pagesResults.result.map(function (item) {
var text = [];
//if(item._score) text.push(item._score);
if (item.name) text.push(item.name);
var header = null;
if (first) {
header = translationsService.get('cart.pages') + (response.data.pagesResults.count > 5 ? ' (' + response.data.pagesResults.count + ')' : '');
;
first = false;
}
return {
text: text.join(' - '),
header: header,
route: item.route
};
}));
}
if (response.data.pagesResults.count > 5) {
data.push({
text: translationsService.get('cart.show_all_pages') + (response.data.pagesResults.count > 5 ? ' (' + response.data.pagesResults.count + ')' : ''),
route: '?q=' + val + '&show=pages',
searchAll: true
});
}
//if (data.length > 0) {
// data.push({
// text: translationsService.get('cart.show_all'),
// searchAll: true
// });
//}
return data;
}
)
;
};
}]);
return app;
}
)
;