Отформатировал JS с пробелов на табы

This commit is contained in:
Krivchikov Dmitry 2014-05-03 22:44:05 +04:00
parent 4b4514d559
commit 202a2ffb47
1 changed files with 207 additions and 208 deletions

View File

@ -12,66 +12,64 @@ var route = false;
// -------------------------------- Libs --------------------------------------- // -------------------------------- Libs ---------------------------------------
function getURLParameter(name) { function getURLParameter(name) {
return decodeURI( return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]);
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
function init() { function init() {
myMap = new ymaps.Map("map", { myMap = new ymaps.Map("map", {
center: [ymaps.geolocation.latitude, ymaps.geolocation.longitude], center: [ymaps.geolocation.latitude, ymaps.geolocation.longitude],
zoom: 12, zoom: 12,
behaviors: ['default', 'scrollZoom'] behaviors: ['default', 'scrollZoom']
}); });
myMap.controls.add('mapTools'); myMap.controls.add('mapTools');
myMap.controls.add('typeSelector'); myMap.controls.add('typeSelector');
myMap.controls.add('smallZoomControl'); myMap.controls.add('smallZoomControl');
myMap.controls.add('scaleLine'); myMap.controls.add('scaleLine');
initCategories() initCategories()
getPoints(); getPoints();
myMap.events.add('click', function (e) { myMap.events.add('click', function(e) {
if (!myMap.balloon.isOpen()) {
if ( $('#addPointButton').hasClass('btn-success') )
{
coords = e.get('coordPosition');
$('#addPointLat').val(coords[0].toPrecision(11));
$('#addPointLng').val(coords[1].toPrecision(11));
$('#addPointModal').modal('show');
}
}
else {
myMap.balloon.close();
}
});
myMap.events.add('boundschange', function(e){
if (!myMap.balloon.isOpen()) { if (!myMap.balloon.isOpen()) {
history.pushState({}, document.title, "/?lat="+e.get('newCenter')[0]+"&lng="+e.get('newCenter')[1]+"&zoom="+e.get('newZoom')); if ($('#addPointButton').hasClass('btn-success'))
{
coords = e.get('coordPosition');
$('#addPointLat').val(coords[0].toPrecision(11));
$('#addPointLng').val(coords[1].toPrecision(11));
$('#addPointModal').modal('show');
}
}
else {
myMap.balloon.close();
} }
}); });
points.im = new ymaps.Placemark( myMap.events.add('boundschange', function(e) {
[ymaps.geolocation.latitude, ymaps.geolocation.longitude], if (!myMap.balloon.isOpen()) {
{ history.pushState({}, document.title, "/?lat=" + e.get('newCenter')[0] + "&lng=" + e.get('newCenter')[1] + "&zoom=" + e.get('newZoom'));
balloonContentHeader: 'Вы здесь', }
});
points.im = new ymaps.Placemark(
[ymaps.geolocation.latitude, ymaps.geolocation.longitude],
{
balloonContentHeader: 'Вы здесь',
hintContent: 'Ваше местоположение', hintContent: 'Ваше местоположение',
balloonContent: balloonContent:
'<button type="button" class="btn btn-default btn-xs" onclick="addToRoute(\'im\')">' + '<button type="button" class="btn btn-default btn-xs" onclick="addToRoute(\'im\')">' +
'<span class="glyphicon glyphicon-plus"></span> Добавить в маршрут' + '<span class="glyphicon glyphicon-plus"></span> Добавить в маршрут' +
'</button>' '</button>'
}, },
{ {
iconImageHref: '/ico/man.png', iconImageHref: '/ico/man.png',
iconImageSize: [32, 37], iconImageSize: [32, 37],
iconImageOffset: [-16, -33], iconImageOffset: [-16, -33],
} }
); );
myMap.geoObjects.add(points.im); myMap.geoObjects.add(points.im);
$("#wayPoints").sortable(); $("#wayPoints").sortable();
$("#wayPoints").disableSelection(); $("#wayPoints").disableSelection();
@ -79,223 +77,224 @@ function init() {
function constructPoint(data) function constructPoint(data)
{ {
id = data['id']; id = data['id'];
points[id] = new ymaps.Placemark( points[id] = new ymaps.Placemark(
[data['lat'], data['lng']], { [data['lat'], data['lng']], {
hintContent: data['name'], hintContent: data['name'],
id: id id: id
}, },
{ {
iconImageHref: data['categoryIcon'], iconImageHref: data['categoryIcon'],
iconImageSize: [32, 37], iconImageSize: [32, 37],
iconImageOffset: [-16, -33], iconImageOffset: [-16, -33],
openEmptyBalloon: true, openEmptyBalloon: true,
balloonCloseButton: true balloonCloseButton: true
}); });
points[id].events.add('click', function (e) { points[id].events.add('click', function(e) {
showInfo(e.get('target').properties.get('id')); showInfo(e.get('target').properties.get('id'));
}); });
return points[id]; return points[id];
} }
function getPoints() function getPoints()
{ {
$.ajax({ $.ajax({
url: "/json/points/", url: "/json/points/",
success: function(data){ success: function(data) {
for(var k in data) { for (var k in data) {
myMap.geoObjects.add(constructPoint(data[k])); myMap.geoObjects.add(constructPoint(data[k]));
} }
if (getURLParameter('point') != 'null') if (getURLParameter('point') != 'null')
{ {
showInfo(getURLParameter('point')); showInfo(getURLParameter('point'));
} else if ( (getURLParameter('lat') != 'null')&&(getURLParameter('lng') != 'null')&&(getURLParameter('zoom') != 'null') ) } else if ((getURLParameter('lat') != 'null') && (getURLParameter('lng') != 'null') && (getURLParameter('zoom') != 'null'))
{ {
myMap.setCenter([getURLParameter('lat'), getURLParameter('lng')], getURLParameter('zoom')); myMap.setCenter([getURLParameter('lat'), getURLParameter('lng')], getURLParameter('zoom'));
} }
} }
}); });
return true; return true;
} }
function showInfo(id) function showInfo(id)
{ {
if (!(id in points) || points[id].properties.get('balloonContent') == undefined) if (!(id in points) || points[id].properties.get('balloonContent') == undefined)
{ {
points[id].properties.set('balloonContent', 'Загрузка...'); points[id].properties.set('balloonContent', 'Загрузка...');
$.ajax({ $.ajax({
url: "/json/point/id/" + id, url: "/json/point/id/" + id,
success: function(data) { success: function(data) {
description = '<div class="infoCard">' + description = '<div class="infoCard">' +
'<a href="/?point='+id+'">' + '<a href="/?point=' + id + '">' +
'<h3>' + data.name + '</h3>' + '<h3>' + data.name + '</h3>' +
'</a>' + '</a>' +
'<img src="' + data.img + '" />' + '<img src="' + data.img + '" />' +
'<button type="button" class="btn btn-default btn-xs" onclick="addToRoute(' + id + ')">' + '<button type="button" class="btn btn-default btn-xs" onclick="addToRoute(' + id + ')">' +
'<span class="glyphicon glyphicon-plus"></span> Добавить в маршрут' + '<span class="glyphicon glyphicon-plus"></span> Добавить в маршрут' +
'</button><br/>' + '</button><br/>' +
data.description + data.description +
' <a href="' + data.source + '" target="blank">Подробнее...</a></div>' ' <a href="' + data.source + '" target="blank">Подробнее...</a></div>'
points[id].properties.set('balloonContent', description); points[id].properties.set('balloonContent', description);
point = getURLParameter('point'); point = getURLParameter('point');
if (point != null && id == point) if (point != null && id == point)
{ {
points[point].balloon.open(); points[point].balloon.open();
} }
} }
}); });
} }
history.pushState({}, document.title, "/?point="+id); history.pushState({}, document.title, "/?point=" + id);
return true; return true;
} }
function initCategories() function initCategories()
{ {
$.ajax({ $.ajax({
url: "/json/categories/", url: "/json/categories/",
success: function(data){ success: function(data) {
categories = data; categories = data;
} }
}); });
return true; return true;
} }
function searchAdderess() function searchAdderess()
{ {
var myGeocoder = ymaps.geocode($('#addressField').val()); var myGeocoder = ymaps.geocode($('#addressField').val());
myGeocoder.then( myGeocoder.then(
function (res) { function(res) {
search = new ymaps.Placemark(res.geoObjects.get(0).geometry.getCoordinates()); search = new ymaps.Placemark(res.geoObjects.get(0).geometry.getCoordinates());
myMap.geoObjects.add(search); myMap.geoObjects.add(search);
myMap.setCenter(res.geoObjects.get(0).geometry.getCoordinates(), 10); myMap.setCenter(res.geoObjects.get(0).geometry.getCoordinates(), 10);
}, },
function (err) {} function(err) {
); }
);
} }
function showMeOnTheMap() function showMeOnTheMap()
{ {
navigator.geolocation.getCurrentPosition(function(pos) { navigator.geolocation.getCurrentPosition(function(pos) {
points.im.geometry.setCoordinates([pos.coords.latitude, pos.coords.longitude]); points.im.geometry.setCoordinates([pos.coords.latitude, pos.coords.longitude]);
myMap.setCenter(points.im.geometry.getCoordinates()); myMap.setCenter(points.im.geometry.getCoordinates());
}); });
myMap.setCenter(points.im.geometry.getCoordinates()); myMap.setCenter(points.im.geometry.getCoordinates());
} }
function activateAddPoint() function activateAddPoint()
{ {
$('#addPointButton').toggleClass('btn-success').toggleClass('btn-default'); $('#addPointButton').toggleClass('btn-success').toggleClass('btn-default');
if ( $('#addPointButton').hasClass('btn-success') ) if ($('#addPointButton').hasClass('btn-success'))
myMap.cursors.push('crosshair'); myMap.cursors.push('crosshair');
else else
myMap.cursors.push('arrow'); myMap.cursors.push('arrow');
} }
function addPoint() function addPoint()
{ {
$('.form-group').removeClass('has-error'); $('.form-group').removeClass('has-error');
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: "/json/addpoint/", url: "/json/addpoint/",
data: { data: {
name: $('#addPointName').val(), name: $('#addPointName').val(),
img: $('#addPointImg').val(), img: $('#addPointImg').val(),
description: $('#addPointDescription').val(), description: $('#addPointDescription').val(),
categoryId: $('#addPointCategoryId').val(), categoryId: $('#addPointCategoryId').val(),
source: $('#addPointSource').val(), source: $('#addPointSource').val(),
lat: $('#addPointLat').val(), lat: $('#addPointLat').val(),
lng: $('#addPointLng').val() lng: $('#addPointLng').val()
}, },
success: function(data) { success: function(data) {
balloon = myMap.balloon.open([$('#addPointLat').val(), $('#addPointLng').val()], {content: 'Точка успешно добавлена!'}, {closeButton: false}); balloon = myMap.balloon.open([$('#addPointLat').val(), $('#addPointLng').val()], {content: 'Точка успешно добавлена!'}, {closeButton: false});
setTimeout(function() { setTimeout(function() {
balloon.close(); balloon.close();
}, 3000); }, 3000);
obj = {}; obj = {};
obj.id = data; obj.id = data;
obj.name = $('#addPointName').val(); obj.name = $('#addPointName').val();
obj.lat = $('#addPointLat').val(); obj.lat = $('#addPointLat').val();
obj.lng = $('#addPointLng').val(); obj.lng = $('#addPointLng').val();
obj.categoryIcon = categories[$('#addPointCategoryId').val()].icon; obj.categoryIcon = categories[$('#addPointCategoryId').val()].icon;
setTimeout(function(){ setTimeout(function() {
myMap.geoObjects.add(constructPoint(obj)); myMap.geoObjects.add(constructPoint(obj));
}, 3000); }, 3000);
$('#addPointModal').modal('hide'); $('#addPointModal').modal('hide');
// Clear form // Clear form
$('#addPointForm input').val(''); $('#addPointForm input').val('');
$('#addPointForm textarea').val(''); $('#addPointForm textarea').val('');
$('.form-group').removeClass('has-error'); $('.form-group').removeClass('has-error');
$('#addPointButton').removeClass('btn-success').addClass('btn-default'); $('#addPointButton').removeClass('btn-success').addClass('btn-default');
myMap.cursors.push('arrow'); myMap.cursors.push('arrow');
}, },
error: function(data) { error: function(data) {
response = data.responseJSON; response = data.responseJSON;
message = ''; message = '';
for (var k in response) { for (var k in response) {
if (typeof response[k] !== 'function') { if (typeof response[k] !== 'function') {
if (k == 1) if (k == 1)
$('#addPointName').parent().addClass('has-error'); $('#addPointName').parent().addClass('has-error');
else else
if (k == 2) if (k == 2)
$('#addPointImg').parent().addClass('has-error'); $('#addPointImg').parent().addClass('has-error');
else else
if (k == 3) if (k == 3)
$('#addPointDescription').parent().addClass('has-error'); $('#addPointDescription').parent().addClass('has-error');
else else
message += response[k] + '; '; message += response[k] + '; ';
} }
} }
if (message != '') if (message != '')
alert(message); alert(message);
} }
}); });
} }
function buildRoute() function buildRoute()
{ {
wayPoints = Array(); wayPoints = Array();
if (route) if (route)
myMap.geoObjects.remove(route); myMap.geoObjects.remove(route);
$('#wayPoints div').each(function(i, el) $('#wayPoints div').each(function(i, el)
{ {
wayPoints.push(points[$(el).attr('pointId')].geometry.getCoordinates()); wayPoints.push(points[$(el).attr('pointId')].geometry.getCoordinates());
}); });
ymaps.route(wayPoints, { ymaps.route(wayPoints, {
mapStateAutoApply: true mapStateAutoApply: true
}).then(function(newRoute) { }).then(function(newRoute) {
route = newRoute; route = newRoute;
route.options.set({ route.options.set({
// в балуне выводим только информацию о времени движения с учетом пробок // в балуне выводим только информацию о времени движения с учетом пробок
balloonContenBodyLayout: ymaps.templateLayoutFactory.createClass('$[properties.humanJamsTime]'), balloonContenBodyLayout: ymaps.templateLayoutFactory.createClass('$[properties.humanJamsTime]'),
strokeColor: '9000ffff', strokeColor: '9000ffff',
opacity: 0.9 opacity: 0.9
}); });
myMap.geoObjects.add(route); myMap.geoObjects.add(route);
distance = Math.round(route.getLength() / 1000); distance = Math.round(route.getLength() / 1000);
$('#routeDistance').text(distance + ' км.') $('#routeDistance').text(distance + ' км.')
}); });
} }
function addToRoute(id) function addToRoute(id)
{ {
$('#wayPoints').append('<div pointid="'+id+'" class="wayPoint"><span onclick="$(this).parent().remove()">&times;</span> ' + points[id].properties.get('hintContent') + '</div>'); $('#wayPoints').append('<div pointid="' + id + '" class="wayPoint"><span onclick="$(this).parent().remove()">&times;</span> ' + points[id].properties.get('hintContent') + '</div>');
myMap.balloon.close(); myMap.balloon.close();
$('#routeWindow').show(200); $('#routeWindow').show(200);
return true; return true;
} }