+var FeatureMgr = {
+ reset: function() {
+ this.commError("");
+ },
+
+ add: function(evt) {
+ var map = Admin.map;
+ var pos = map.getLonLatFromViewPortPx(evt.xy);
+ feature = this.update (null, pos, "", "", "");
+ Admin.addFeatureControl.deactivate();
+ Admin.selFeatureControl.select(feature);
+ },
+
+ move: function (feature, aLocation) {
+ if (!feature || !aLocation) {
+ return;
+ }
+ var curLoc = feature.geometry;
+ feature.geometry.move(aLocation.x - curLoc.x, aLocation.y - curLoc.y);
+ feature.layer.drawFeature(feature);
+ },
+
+ update: function(feature, lonlat, imgurl, title, description) {
+ var point = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
+ if (!feature) {
+ feature = new OpenLayers.Feature.Vector(point);
+ Admin.dataLayer.addFeatures([feature]);
+ } else {
+ this.move (feature, point);
+ }
+ feature.attributes.name = title;
+ feature.attributes.description = "<p>" + Admin.Utils.escapeHTML(description) + "</p>"
+ + "<img src=\"" + imgurl + "\">"
+ return feature;
+ },
+
+ del: function (feature) {
+ var form = $("#feature_delete");
+ form.find('input[name="fid"]').val(feature.fid);
+ AjaxMgr.add({
+ form: form,
+ oncomplete: OpenLayers.Function.bind(this.ajaxReply, this),
+ throbberid: "editor_throbber"
+ });
+ },
+
+ save: function (feature) {
+ var x = feature.geometry.x;
+ var y = feature.geometry.y;
+
+ var mapProj = feature.layer.map.getProjectionObject();
+ var lonlat = new OpenLayers.LonLat(x, y).
+ transform(mapProj,
+ new OpenLayers.Projection("EPSG:4326"));
+ var form = $("#feature_update");
+ form.find('input[name="lon"]').val(lonlat.lon);
+ form.find('input[name="lat"]').val(lonlat.lat);
+ form.find('input[name="fid"]').val(feature.fid);
+ form.find('input[name="keep_img"]').val(
+ $("#img").attr("src") ? "yes": "no"
+ );
+
+ if (feature.fid) {
+ form.find('input[name="request"]').val("update");
+ } else {
+ form.find('input[name="request"]').val("add");
+ }
+ AjaxMgr.add({
+ form: form,
+ oncomplete: OpenLayers.Function.bind(this.ajaxReply, this),
+ throbberid: "editor_throbber"
+ });
+ },
+
+ ajaxReply: function (data) {
+ if (!data) {
+ this.commError(SypStrings.ServerError);
+ return;
+ }
+
+ var xml = new OpenLayers.Format.XML().read(data);
+ if (!xml.documentElement) {
+ this.commError(SypStrings.UnconsistentError);
+ $("title").focus();
+ return;
+ }
+
+ switch (xml.documentElement.nodeName.toLowerCase()) {
+ case "error":
+ switch (xml.documentElement.getAttribute("reason")) {
+ case "unauthorized":
+ pwdMgr.reset();
+ $("#cookie_warning").show();
+ this.reset();
+ Admin.cancelCurrentFeature();
+ Admin.reset();
+ userMgr.uninit();
+ break;
+ case "server":
+ this.commError(SypStrings.ServerError);
+ $("title").focus();
+ break;
+ case "unreferenced":
+ this.commError(SypStrings.UnreferencedError);
+ Admin.reloadLayer(Admin.dataLayer);
+ Admin.closeEditor();
+ break;
+ case "nochange":
+ this.commError(SypStrings.NochangeError);
+ Admin.closeEditor();
+ break;
+ case "request":
+ this.commError(SypStrings.RequestError);
+ $("title").focus();
+ break;
+ case "toobig":
+ this.commError(SypStrings.ToobigError);
+ $("#image_file").parent().html($("#image_file").parent().html());
+ $("#image_file").focus();
+ break;
+ case "notimage":
+ this.commError(SypStrings.NotimageError);
+ $("#image_file").parent().html($("#image_file").parent().html());
+ $("#image_file").focus();
+ break;
+ default:
+ this.commError(SypStrings.UnconsistentError);
+ $("title").focus();
+ break;
+ }
+ break;
+ case "success":
+ switch (xml.documentElement.getAttribute("request")) {
+ case "del":
+ this.commSuccess(SypStrings.DelSucces);
+ var someFeature = false;
+ var self = this;
+ $.each($(xml).find("FEATURE,feature"), function () {
+ someFeature = true;
+ var id = parseFloat($(this).find("ID:first,id:first").text());
+ if ((id === null) || isNaN (id)) {
+ return;;
+ }
+ var features = Admin.dataLayer.features;
+ for (var idx = 0; idx < features.length; idx++) {
+ if (features[idx].fid == id) {
+ Admin.dataLayer.removeFeatures([features[idx]]);
+ }
+ }
+ });
+ if (someFeature == false) {
+ this.commError(SypStrings.UnconsistentError);
+ } else {
+ Admin.closeEditor();
+ }
+ break;
+ case "update":
+ case "add":
+ var someFeature = false;
+ var self = this;
+ $.each($(xml).find("FEATURE,feature"), function () {
+ someFeature = true;
+ var id = parseFloat($(this).find("ID:first,id:first").text());
+ if ((id === null) || isNaN (id)) {
+ return;;
+ }
+
+ var lon = parseFloat($(this).find("LON:first,lon:first").text());
+ if ((typeof (lon) != "number") || isNaN (lon) ||
+ (lon < -180) || (lon > 180)) {
+ return;;
+ }
+
+ var lat = parseFloat($(this).find("LAT:first,lat:first").text());
+ if ((typeof (lat) != "number") || isNaN (lat) ||
+ (lat < -90) || (lat > 90)) {
+ return;;
+ }
+
+ var mapProj = Admin.map.getProjectionObject();
+ var lonlat = new OpenLayers.LonLat (lon, lat).
+ transform( new OpenLayers.Projection("EPSG:4326"), mapProj);
+
+ var imgurl = $(this).find("IMGURL:first,imgurl:first").text();
+ var title = $(this).find("HEADING:first,heading:first").text();
+ var description = $(this).find("DESCRIPTION:first,description:first").text();
+
+ feature = self.update (Admin.currentFeature, lonlat, imgurl, title, description);
+ feature.fid = id;
+ });
+
+ if (someFeature == false) {
+ this.commError(SypStrings.UnconsistentError);
+ } else {
+ this.commSuccess(SypStrings.UpdateSucces);
+ Admin.closeEditor();
+ }
+
+ break;
+ default:
+ this.commError(SypStrings.UnconsistentError);
+ break;
+ }
+ break;
+ default:
+ this.commError(SypStrings.UnconsistentError);
+ break;
+ }
+ },
+
+ commSuccess: function (message) {
+ $("#server_comm").text(message);
+ $("#server_comm").removeClass("error success").addClass("success");
+ },
+
+ commError: function (message) {
+ $("#server_comm").text(message);
+ $("#server_comm").removeClass("error success").addClass("error");
+ }
+}
+
+/* maintains a queue of ajax queries, so I'm sure they all execute in the same
+ * order they were defined */
+var AjaxMgr = {
+ _queue: [],
+
+ running: false,
+
+ add: function(query) {
+ this._queue.push(query);
+ if (this._queue.length > 1) {
+ return;
+ } else {
+ this._runQuery(query);
+ }
+ },
+
+ _runQuery: function(query) {
+ var self = this;
+ $('#api_frame').one("load", function() {
+ self.running = false;
+ self._reqEnd();
+ if (query.throbberid) {
+ $("#" + query.throbberid).css("visibility", "hidden");
+ }
+ if (typeof (query.oncomplete) == "function") {
+ var body = null;
+ try {
+ if (this.contentDocument) {
+ body = this.contentDocument.body;
+ } else if (this.contentWindow) {
+ body = this.contentWindow.document.body;
+ } else {
+ body = document.frames[this.id].document.body;
+ }
+ } catch (e) {}
+ if (body) {
+ query.oncomplete(body.innerHTML);
+ } else {
+ query.oncomplete(null);
+ }
+ }
+ });
+ query.form.attr("action", "api.php");
+ query.form.attr("target", "api_frame");
+ query.form.attr("method", "post");
+ this.running = true;
+ query.form.get(0).submit();
+ if (query.throbberid) {
+ $("#" + query.throbberid).css("visibility", "visible");
+ }
+ if (typeof (query.onsend) == "function") {
+ query.onsend();
+ }
+ },
+
+ _reqEnd: function() {
+ this._queue.shift();
+ if (this._queue.length > 0) {
+ this._reqEnd(this._queue[0]);
+ }
+ }
+}
+