Skip to content

Commit

Permalink
Правки по сохранению регионов
Browse files Browse the repository at this point in the history
  • Loading branch information
klimashkin committed Jan 12, 2014
1 parent 8eda4a6 commit c8cff52
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 29 deletions.
4 changes: 2 additions & 2 deletions commons/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,11 @@ Utils.geo = (function () {

//Проверка на валидность bbox [leftlng, bottomlat, rightlng, toplat]
function checkbbox(bbox) {
return Array.isArray(bbox) && bbox.length === 4 && check([bbox[0], bbox[1]]) && check([bbox[2], bbox[3]]) && bbox[0] < bbox[2] && bbox[1] < bbox[3];
return Array.isArray(bbox) && bbox.length === 4 && check([bbox[0], bbox[1]]) && check([bbox[2], bbox[3]]) && bbox[1] < bbox[3];
}
//Проверка на валидность bbox [bottomlat, leftlng, toplat, rightlng]
function checkbboxLatLng(bbox) {
return Array.isArray(bbox) && bbox.length === 4 && checkLatLng([bbox[0], bbox[1]]) && checkLatLng([bbox[2], bbox[3]]) && bbox[0] < bbox[2] && bbox[1] < bbox[3];
return Array.isArray(bbox) && bbox.length === 4 && checkLatLng([bbox[0], bbox[1]]) && checkLatLng([bbox[2], bbox[3]]) && bbox[0] < bbox[2];
}
//Переставляет местами lat и lng в bbox
function bboxReverse(bbox) {
Expand Down
32 changes: 24 additions & 8 deletions controllers/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,11 +685,19 @@ function saveRegion(socket, data, cb) {
if (region.center) {
region.center.reverse();
}
if (region.bbox) {
region.bbox = Utils.geo.bboxReverse(region.bbox);
if (region.bbox !== undefined) {
if (Utils.geo.checkbbox(region.bbox)) {
region.bbox = Utils.geo.bboxReverse(region.bbox);
} else {
delete region.bbox;
}
}
if (region.bboxhome) {
region.bboxhome = Utils.geo.bboxReverse(region.bboxhome);
if (region.bboxhome !== undefined) {
if (Utils.geo.checkbbox(region.bboxhome)) {
region.bboxhome = Utils.geo.bboxReverse(region.bboxhome);
} else {
delete region.bboxhome;
}
}

cb({childLenArr: childLenArr, region: region, resultStat: resultStat});
Expand Down Expand Up @@ -863,11 +871,19 @@ function getRegion(socket, data, cb) {
if (region.center) {
region.center.reverse();
}
if (region.bbox) {
region.bbox = Utils.geo.bboxReverse(region.bbox);
if (region.bbox !== undefined) {
if (Utils.geo.checkbbox(region.bbox)) {
region.bbox = Utils.geo.bboxReverse(region.bbox);
} else {
delete region.bbox;
}
}
if (region.bboxhome) {
region.bboxhome = Utils.geo.bboxReverse(region.bboxhome);
if (region.bboxhome !== undefined) {
if (Utils.geo.checkbbox(region.bboxhome)) {
region.bboxhome = Utils.geo.bboxReverse(region.bboxhome);
} else {
delete region.bboxhome;
}
}

cb({childLenArr: childLenArr, region: region});
Expand Down
19 changes: 15 additions & 4 deletions public/js/lib/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,13 @@ define(['jquery', 'underscore', 'underscore.string', 'lib/jquery/plugins/extends

return {
toPrecision: toPrecision,
toPrecisionRound: toPrecisionRound
toPrecisionRound: toPrecisionRound,
toPrecision6: function (number) {
return toPrecision(number, 6);
},
toPrecisionRound6: function (number) {
return toPrecisionRound(number, 6);
}
};
}()),

Expand Down Expand Up @@ -738,11 +744,15 @@ define(['jquery', 'underscore', 'underscore.string', 'lib/jquery/plugins/extends

//Проверка на валидность bbox [leftlng, bottomlat, rightlng, toplat]
function checkbbox(bbox) {
return Array.isArray(bbox) && bbox.length === 4 && check([bbox[0], bbox[1]]) && check([bbox[2], bbox[3]]) && bbox[0] < bbox[2] && bbox[1] < bbox[3];
return Array.isArray(bbox) && bbox.length === 4 && check([bbox[0], bbox[1]]) && check([bbox[2], bbox[3]]) && bbox[1] < bbox[3];
}
//Проверка на валидность bbox [bottomlat, leftlng, toplat, rightlng]
function checkbboxLatLng(bbox) {
return Array.isArray(bbox) && bbox.length === 4 && checkLatLng([bbox[0], bbox[1]]) && checkLatLng([bbox[2], bbox[3]]) && bbox[0] < bbox[2] && bbox[1] < bbox[3];
return Array.isArray(bbox) && bbox.length === 4 && checkLatLng([bbox[0], bbox[1]]) && checkLatLng([bbox[2], bbox[3]]) && bbox[0] < bbox[2];
}
//Переставляет местами lat и lng в bbox
function bboxReverse(bbox) {
return [bbox[1], bbox[0], bbox[3], bbox[2]];
}

return {
Expand All @@ -755,7 +765,8 @@ define(['jquery', 'underscore', 'underscore.string', 'lib/jquery/plugins/extends
check: check,
checkLatLng: checkLatLng,
checkbbox: checkbbox,
checkbboxLatLng: checkbboxLatLng
checkbboxLatLng: checkbboxLatLng,
bboxReverse: bboxReverse
};
}()),

Expand Down
55 changes: 40 additions & 15 deletions public/js/module/admin/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ define([
this.childLenArr = ko.observableArray();
this.geoStringOrigin = null;
this.geoObj = null;

this.bboxLBound = null;
this.bboxhomeLBound = null;
this.bboxAuto = this.co.bboxAuto = ko.computed({
read: function () {
return !this.region.bboxhome();
Expand All @@ -139,6 +141,7 @@ define([
this.markerLayer = L.layerGroup();
this.layerGeo = null;
this.layerBBOX = null;
this.layerBBOXHome = null;

this.centerValid = ko.observable(true);
this.bboxhomeValid = ko.observable(true);
Expand Down Expand Up @@ -209,6 +212,7 @@ define([
resetData: function () {
this.removeLayers();
this.bboxLBound = null;
this.bboxhomeLBound = null;

this.regionOrigin = regionDef;
ko_mapping.fromJS(regionDef, this.region);
Expand All @@ -229,7 +233,7 @@ define([
}
},

fillData: function (data) {
fillData: function (data, needRedraw) {
var region = data.region;

this.regionOrigin = region;
Expand All @@ -240,6 +244,8 @@ define([
[region.bbox[0], region.bbox[1]],
[region.bbox[2], region.bbox[3]]
];
} else {
this.bboxLBound = null;
}

this.childLenArr(data.childLenArr || []);
Expand All @@ -263,10 +269,8 @@ define([
return false;
}
}
this.drawData();

if (region.bboxhome) {
this.bboxhomeSet(region.bboxhome);
if (needRedraw) {
this.drawData();
}

return true;
Expand All @@ -290,6 +294,9 @@ define([
style: {color: "#F00", weight: 2, opacity: 0.8, clickable: false}
}).addTo(this.map);
}
if (this.region.bboxhome()) {
this.bboxhomeSet(this.region.bboxhome());
}

this.centerMarkerCreate();
}.bind(this);
Expand All @@ -310,7 +317,7 @@ define([
return;
}

this.map = new L.map(this.$dom.find('.map')[0], {center: [36, -25], zoom: 2, minZoom: 2, maxZoom: 15, trackResize: false});
this.map = new L.map(this.$dom.find('.map')[0], {center: [36, -25], zoom: 3, minZoom: 2, maxZoom: 15, trackResize: false});
if (this.bboxLBound) {
this.map.fitBounds(this.bboxLBound);
}
Expand Down Expand Up @@ -394,6 +401,16 @@ define([
return this;
},
bboxhomeSet: function (bbox) {
if (bbox[1] < -180) {
bbox[1] += 360;
} else if (bbox[1] > 180) {
bbox[1] -= 360;
}
if (bbox[3] < -180) {
bbox[3] += 360;
} else if (bbox[3] > 180) {
bbox[3] -= 360;
}
this.region.bboxhome(bbox);
this.bboxhomeLBound = [
[bbox[0], bbox[1]],
Expand All @@ -414,7 +431,9 @@ define([
//Переключаем вид домашнего положения bbox
bboxHomeToggle: function () {
if (this.bboxAuto()) {
this.bboxhomeSet(this.regionOrigin.bboxhome || this.region.bbox() || []);
//Если было оригинальное значение, возвращаем его. Если его не было нет - значение bbox
//Если нет и bbox(режим создания), то берем ученьшенный bounds экрана карты
this.bboxhomeSet(this.regionOrigin.bboxhome || this.region.bbox() || Utils.geo.bboxReverse(this.map.getBounds().pad(-0.2).toBBoxString().split(',')).map(Utils.math.toPrecision6));
} else {
this.bboxhomeUnSet();
}
Expand All @@ -427,7 +446,9 @@ define([
if (error) {
window.noty({text: data && data.message || 'Error occurred', type: 'error', layout: 'center', timeout: 4000, force: true});
} else {
error = !this.fillData(data);
//Выборке региона подставляем дефолтные значения
_.defaults(data.region, regionDef);
error = !this.fillData(data, true);
}

if (Utils.isType('function', cb)) {
Expand All @@ -442,6 +463,7 @@ define([
}

var saveData = ko_mapping.toJS(this.region),
needRedraw,
parentIsChanged;

if (!saveData.geo) {
Expand All @@ -457,6 +479,13 @@ define([
return false;
}

if (!saveData.bboxhome && this.regionOrigin.bboxhome) {
saveData.bboxhome = null; //Если bboxhome был и ставим auto, то надо передать на сервер null, чтобы обнулить его
}

//Перерисовка будет нужна, если изменился geojson(сл-во и bbox) или расчет центра поставили auto
needRedraw = !!saveData.geo || (saveData.centerAuto && !this.regionOrigin.centerAuto);

if (this.haveParent() === '1') {
saveData.parent = Number(this.parentCid());
if (!saveData.parent) {
Expand All @@ -478,13 +507,9 @@ define([
processSave(this);
}

if (!saveData.bboxhome && this.regionOrigin.bboxhome) {
saveData.bboxhome = null; //Если bboxhome был и ставим auto, то надо передать на сервер null, чтобы обнулить его
}

function processSave(ctx) {
ctx.exe(true);
ctx.sendSave(saveData, function (data, error) {
ctx.sendSave(saveData, needRedraw, function (data, error) {
var resultStat = data && data.resultStat;

if (!error) {
Expand Down Expand Up @@ -523,7 +548,7 @@ define([

return false;
},
sendSave: function (saveData, cb, ctx) {
sendSave: function (saveData, needRedraw, cb, ctx) {
socket.once('saveRegionResult', function (data) {
var error = !data || !!data.error || !data.region;

Expand All @@ -536,7 +561,7 @@ define([
//Если регион успешно создан, но переходим на его cid, и через роутер он нарисуется
globalVM.router.navigateToUrl('/admin/region/' + data.region.cid);
} else {
this.fillData(data);
this.fillData(data, needRedraw);
}
}

Expand Down

0 comments on commit c8cff52

Please sign in to comment.