var mobirise_swift = angular.module('Mobirise'); // Controller mobirise_swift.controller('OfferSearchCtrl', function($scope,$http,$attrs,$window,$rootScope,$timeout,$location) { $scope.init = function() { console.info('Hello from OfferSearchCtrl'); $scope.httpconfig = { headers:{ 'Authorization': 'Bearer ' + gJWT } }; $scope.matchinggroups = [ { ID:0, properties:{ name:'CATEGORIE', categories:'' } } ]; $scope.regions = [ { ID:0, properties:{ name:'REGION', regions:'' } } ]; // $scope.sellupper = 1000000; // $scope.sellupper = {offer_search::sellupper}; $scope.ranges = { 'Vente':{ lower:1000, upper:2000000, min:0, max:5000000, gap:10000, step:10000 }, 'Location':{ lower:10, upper:10000, min:0, max:10000, gap:100, step:100 }, 'Viager':{ lower:1000, upper:2000000, min:0, max:5000000, gap:10000, step:10000 } }; $scope.tabs = []; $scope.regionid = 0; $scope.matchinggroupid = 0; $scope.forceType = ('swiftType' in $attrs) ? $attrs.swiftType : ''; if ($scope.forceType == '') { $scope.type = 'Vente'; } else { $scope.type = $scope.forceType; } $scope.publish = ('swiftActive' in $attrs) ? ($attrs.swiftActive.length != 0) ? ($attrs.swiftActive == 'true') ? 'Yes' : 'No' : null : null; $scope.bought = ('swiftSold' in $attrs) ? ($attrs.swiftSold.length != 0) ? ($attrs.swiftSold == 'true') ? 'Yes' : 'No' : null : null; $scope.emitsearch = ('swiftEmitSearch' in $attrs) ? ($attrs.swiftEmitSearch.toLowerCase() == 'true') ? true : false : true; $scope.reference = ''; $scope.quickparams = {}; $scope.selectedIndex = 0; $scope.storedparamtimeout = ('swiftParamsTimeout' in $attrs && $attrs.swiftParamsTimeout.length != 0) ? parseInt($attrs.swiftParamsTimeout) * 60 : (60 * 60 * 24); // 1 day default, attr param is in minutes var urlparams = window.location.search.substring(1); var storedparams = null; if (typeof(Storage) !== "undefined") { // Code for localStorage storedparams = window.localStorage.getItem('swiftimmo-search'); if (storedparams !== null) { storedparams = JSON.parse(storedparams); if (typeof(storedparams.pricerange) !== 'undefined' && typeof(storedparams.pricerange.min) === 'undefined') { storedparams = null; } if (storedparams !== null) { if (typeof(storedparams.timestamp) == 'undefined') { // previous version, reset and add timestamp storedparams = null; } else { var previoustimestamp = storedparams.timestamp; var now = parseInt((Date.now()) / 1000); if ((now - previoustimestamp) > $scope.storedparamtimeout) // > day ? { storedparams = null; console.log("stored search params has been reset"); } } } } } if (urlparams.length > 0) { $scope.params = urlparams.split('=')[1].split('|'); $scope.regionid = parseInt($scope.params[1]); $scope.matchinggroupid = parseInt($scope.params[2]); if ($scope.params[0] == 'Vente') { $scope.selectedIndex = 0; $scope.type = 'Vente'; $scope.forceType = 'Vente'; } if ($scope.params[0] == 'Location') { $scope.selectedIndex = 1; $scope.type = 'Location'; $scope.forceType = 'Location'; } if ($scope.params[0] == 'Viager') { $scope.selectedIndex = 2; $scope.type = 'Viager'; $scope.forceType = 'Viager'; } $scope.pricerange = $scope.ranges[$scope.type]; $scope.pricerange.lower = parseInt($scope.params[3]); $scope.pricerange.upper = parseInt($scope.params[4]); console.log($scope.params); } else if (storedparams !== null) { // Load from localStorage $scope.regionid = storedparams.region; $scope.matchinggroupid = storedparams.group; $scope.type = storedparams.type; // $scope.forceType = storedparams.type; if (storedparams.type == 'Vente') { $scope.selectedIndex = 0; } if (storedparams.type == 'Location') { $scope.selectedIndex = 1; } if (storedparams.type == 'Viager') { $scope.selectedIndex = 2; } $scope.ranges = storedparams.ranges; $scope.pricerange = $scope.ranges[$scope.type]; $scope.pricerange.lower = storedparams.pricerange.lower; $scope.pricerange.upper = storedparams.pricerange.upper; $scope.pricerange.min = storedparams.pricerange.min; $scope.pricerange.max = storedparams.pricerange.max; } else { $scope.pricerange = $scope.ranges[$scope.type]; } // Load real ranges $scope.loadRanges(); $scope.delayOfSearch = 250; $scope.counters = { sell:0, selllive:0, rent:0 }; $scope.sortRegions = ('swiftSortRegions' in $attrs) ? $attrs.swiftSortRegions.toLowerCase() : 'asc'; $scope.sortMatchingGroups = ('swiftSortMatchinggroups' in $attrs) ? $attrs.swiftSortMatchinggroups.toLowerCase() : 'asc'; $scope.loadMatchingGroups(); $scope.loadRegions(); $scope.loadOfferCounts(); $scope.setWatching(); }; $scope.setWindowScroll = function() { if ($scope.emitsearch) { $window.scroll(0, $scope.scrollYOffset); } }; $scope.tabSelected = function(tabselected) { // Store back previous $scope.ranges[$scope.type] = $scope.pricerange; $scope.type = tabselected.type; $scope.pricerange = $scope.ranges[$scope.type]; }; $scope.loadMatchingGroups = function() { $scope.scrollYOffset = $window.pageYOffset; var url = 'https://keyrealestate.swiftimmo.eu/api/v1/fr/' + gDomainId + '/offer/categories?sort=' + $scope.sortMatchingGroups; // console.log(url); $http.get(url,$scope.httpconfig).then(function (success){ $scope.matchinggroups = $scope.matchinggroups.concat(success.data); }); }; $scope.loadRegions = function() { $scope.scrollYOffset = $window.pageYOffset; var url = 'https://keyrealestate.swiftimmo.eu/api/v1/fr/' + gDomainId + '/offer/regions?sort=' + $scope.sortRegions; // console.log(url); $http.get(url,$scope.httpconfig).then(function (success){ $scope.regions = $scope.regions.concat(success.data); }); }; $scope.loadOfferCounts = function() { $scope.scrollYOffset = $window.pageYOffset; var url = 'https://keyrealestate.swiftimmo.eu/api/v1/fr/' + gDomainId + '/offer/count/all'; // console.log(url); var tmpTabs = []; $http.get(url,$scope.httpconfig).then(function (success){ $scope.counters = success.data; if (typeof($scope.counters.sell) != 'undefined' && $scope.counters.sell != 0) { if ($scope.forceType == '' || $scope.forceType == 'Vente') { tmpTabs.push({label:'VENTE',type:'Vente'}); } } if (typeof($scope.counters.rent) != 'undefined' && $scope.counters.rent != 0) { if ($scope.forceType == '' || $scope.forceType == 'Location') { tmpTabs.push({label:'LOCATION',type:'Location'}); } } if (typeof($scope.counters.selllife) != 'undefined' && $scope.counters.selllife != 0) { if ($scope.forceType == '' || $scope.forceType == 'Viager') { tmpTabs.push({label:'VIAGER',type:'Viager'}); } } }); $scope.tabs = tmpTabs; } $scope.loadRanges = function() { var url = 'https://keyrealestate.swiftimmo.eu/api/v1/fr/' + gDomainId + '/offer/pricerange/all'; var params = []; if ($scope.publish !== null) { params.push('publish=' + $scope.publish); } if ($scope.bought !== null) { params.push('bought=' + $scope.bought); } if (params.length != 0) { url += '?' + params.join('&'); } // console.log(url); $http.get(url,$scope.httpconfig).then(function (success){ if (typeof(success.data.sell) != 'undefined' || success.data.sell.max != 0) { var current = $scope.ranges['Vente']; current.min = success.data.sell.min; current.max = success.data.sell.max; $scope.ranges['Ventes'] = current; } if (typeof(success.data.rent) != 'undefined' || success.data.rent.max != 0) { var current = $scope.ranges['Location']; current.min = success.data.rent.min; current.max = success.data.rent.max; $scope.ranges['Location'] = current; } if (typeof(success.data.selllife) != 'undefined' || success.data.selllife.max != 0) { var current = $scope.ranges['Viager']; current.min = success.data.selllife.min; current.max = success.data.selllife.max; $scope.ranges['Viager'] = current; } $scope.pricerange = $scope.ranges[$scope.type]; }); } // Search $scope.delayedSearch = function() { if ($scope.emitsearch === false) { return; } if ($scope.searchDelay) { $timeout.cancel($scope.searchDelay); } $scope.searchDelay = $timeout(function() { var searchParams = { pricerange:{ upper:$scope.pricerange.upper, lower:$scope.pricerange.lower }, region:parseInt($scope.regionid), group:parseInt($scope.matchinggroupid), type:$scope.type }; $rootScope.$emit('search',searchParams); // Store search in local storage if (typeof(Storage) !== "undefined") { // Code for localStorage var paramsToStore = searchParams; paramsToStore.pricerange.min = $scope.pricerange.min; paramsToStore.pricerange.max = $scope.pricerange.max; paramsToStore.ranges = $scope.ranges; paramsToStore.timestamp = parseInt((Date.now()) / 1000); window.localStorage.setItem('swiftimmo-search', JSON.stringify(paramsToStore)); } }, $scope.delayOfSearch); // delay is based on the params being modified }; $scope.search = function() { if ($scope.reference.length != 0) { console.log('Search reference'); var url = 'https://keyrealestate.swiftimmo.eu/api/v1/fr/' + gDomainId + '/offer/search/reference?value=' + $scope.reference; // console.log(url); $http.get(url,$scope.httpconfig).then(function (success){ console.log(success); if (success.data.length == 0) { console.log('Not found'); $("#reference-search").css('border-color','red'); } else { // Redirect to offer.html page var offer = success.data[0]; var offerid = offer.ID; window.location.href = 'offer.html?context=offerid;' + offerid; } }); return; } console.log('send params to offers'); var params = $scope.type; params += '|' + parseInt($scope.regionid); params += '|' + parseInt($scope.matchinggroupid); params += '|' + $scope.pricerange.lower; params += '|' + $scope.pricerange.upper; window.location.href = 'offers.html?sparams=' + params; }; $scope.setWatching = function() { // Watch slider values $scope.$watch( function watchPriceUpper( scope ) { // Return the "result" of the watch expression. return( scope.pricerange.upper ); }, function handlePriceUpperChange( newValue, oldValue ) { // Filter offer grid $scope.delayOfSearch = 500; $scope.delayedSearch(); console.log( "fn( scope.pricerange.upper ):", newValue ); } ); $scope.$watch( function watchPriceLower( scope ) { // Return the "result" of the watch expression. return( scope.pricerange.lower ); }, function handlePriceLowerChange( newValue, oldValue ) { // Filter offer grid $scope.delayOfSearch = 500; $scope.delayedSearch(); console.log( "fn( scope.pricerange.lower ):", newValue ); } ); // Watch select values $scope.$watch( function watchMatchingGroup( scope ) { // Return the "result" of the watch expression. return( scope.matchinggroupid ); }, function handleMatchingGroup( newValue, oldValue ) { // Filter offer grid $scope.delayOfSearch = 250; $scope.delayedSearch(); console.log( "fn( scope.matchinggroupid ):", newValue ); } ); $scope.$watch( function watchRegion( scope ) { // Return the "result" of the watch expression. return( scope.regionid ); }, function handleRegion( newValue, oldValue ) { // Filter offer grid $scope.delayOfSearch = 250; $scope.delayedSearch(); console.log( "fn( scope.regionid ):", newValue ); } ); $scope.$watch( function watchType( scope ) { // Return the "result" of the watch expression. return( scope.type ); }, function handleType( newValue, oldValue ) { // Filter offer grid $scope.delayOfSearch = 250; $scope.delayedSearch(); console.log( "fn( scope.type ):", newValue ); } ); }; });