/**
* @ngdoc directives
* @name RotasOlimpicas
* @module s4c.directives.rotasOlimpicas.RotasOlimpicas
*
* @description
* `RotasOlimpicas` Controller da funcionalidade de Rotas Olímpicas
*
*
*/
(function () {
'use strict';
angular.module('s4c.components.rotas-olimpicas', []).service('RotasOlimpicas', RotasOlimpicas)
.directive('rotasOlimpicas', RotasOlimpicas)
.directive('reachedToBottom', function ($window, $document) {
return {
link: function (scope, element, attrs) {
element.on('scroll', function () {
if (element[0].scrollTop + element.innerHeight() == element[0].scrollHeight) {
scope.$eval(attrs.reachedToBottom);
}
})
}
}
});
RotasOlimpicasCtrl.$inject = [
'$scope',
'$filter',
'$timeout',
'$anchorScroll',
'MapaService',
'RotasOlimpicasService',
'DetalhamentoRotasOlimpicasManager',
'RotasOlimpicasFilter',
'CommService',
'localize'
];
function RotasOlimpicas($rootScope) {
return {
restrict: 'EA',
templateUrl: 'app/directives/rotas-olimpicas/rotas-olimpicas.html',
scope: {},
replace: true,
controller: RotasOlimpicasCtrl,
setActive: setActive
};
function setActive(rt) {
$rootScope.$broadcast('eventFired', {
rota: rt
});
}
}
function RotasOlimpicasCtrl(
$scope,
$filter,
$timeout,
$anchorScroll,
MapaService,
RotasOlimpicasService,
DetalhamentoRotasOlimpicasManager,
RotasOlimpicasFilter,
CommService,
localize
) {
$scope.res = $scope.$root.res;
RotasOlimpicasFilter.getInstance('rotas-olimpicas-filter').on('filterChanged', filter);
$scope.$on('eventFired', function (event, rota) {
setActive(rota);
})
/**
* @method filter
*/
function filter() {
var dataRange = RotasOlimpicasFilter.getInstance('rotas-olimpicas-filter').dataRange;
var start = new Date(dataRange.start).getTime();
var end = new Date(dataRange.end).getTime();
$scope.rotas = $scope.memoryRotas;
var result = _.chain($scope.rotas).filter(function (item) {
var inicioDb = new Date(item.inicio).getTime();
var fimDb = new Date(item.fim).getTime();
return (fimDb >= start && inicioDb <= end);
// 1) data inicial do banco tem que ser maior que a inicial digitada e a data final do
// banco tem que ser menor ou igual a final digitada
// 2) data inicial do banco tem que ser apenas menor do que a final digitada
// 3) data final do banco tem que ser apenas maior que a inicial
return (inicioDb >= start && fimDb <= end) ||
(inicioDb <= end && inicioDb >= start) ||
(fimDb >= start)
}).value();
$scope.rotas = result;
}
var rotasAtivas = {};
$scope.currentPage = 1;
$scope.pages = 20 * $scope.currentPage;
$scope.loadMore = function () {
$scope.currentPage++;
$scope.pages = $scope.currentPage * 20;
};
/**
* @method pegarRotas
*/
RotasOlimpicasService.pegarRotas().then(function (resp) {
$scope.rotas = _.map(resp, function (r) {
r.color = $scope.fillColor();
r.formatDtinicio = moment(r.inicio).format('DD/MM - HH:mm');
r.formatDtfim = moment(r.fim).format('DD/MM - HH:mm');
r.active = false;
return r;
});
$scope.memoryRotas = $scope.rotas;
});
/**
* @method fillColor
*/
$scope.fillColor = function () {
return '#' + Math.random().toString(16).slice(2, 8).toUpperCase();
};
/**
* @method removeLayer
*/
$scope.removeLayer = function (via) {
MapaService.removerCamada(via.geom);
};
$scope.search = '';
/**
* @method printGeoM
*/
$scope.printGeoM = function (geom, color) {
console.log({
geom: geom
});
/* random color line */
geom.setStyle({ color: color });
MapaService.adicionarLayer(geom);
MapaService.fitBounds(geom);
};
/**
* @method mapearRota
*/
$scope.mapearRota = function (via) {
if (via.active) {
/* se o geo nao existir, requisita o geom, passando o id */
if (!rotasAtivas.hasOwnProperty(via.id)) {
RotasOlimpicasService.pegarShape(via.id).then(function (resp) {
$timeout(function () {
var layer = L.geoJson(JSON.parse(resp.geom));
rotasAtivas[via.id] = layer;
rotasAtivas[via.id].on('click', function (e) {
DetalhamentoRotasOlimpicasManager.abrir(via);
});
$scope.printGeoM(rotasAtivas[via.id], via.color);
}, 0);
});
RotasOlimpicasService.pegarIncidentes(via.id).then(function (resp) {
via.incidentes = resp;
});
} else {
console.log(rotasAtivas[via.id]);
$scope.printGeoM(rotasAtivas[via.id], via.color);
}
} else {
MapaService.removerCamada(rotasAtivas[via.id]);
}
}
/**
* @method setActive
* @param {*} rota
*/
function setActive(rota) {
var result = _.chain($scope.rotas).filter(function (item) {
return item.id == rota.rota.id;
}).value();
result[0].active = true;
$scope.mapearRota(result[0]);
}
}
}());