X-Git-Url: https://dev.renevier.net/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=js%2Fadmin.js;h=48c1eed55637c4af755de547eca9c3f1a9e5d1cf;hb=c58963cfcd8a6ec3cd5374af8412c883b38e1a38;hp=0709878efef7476b55ea339c6e93b5166df4bdd3;hpb=7282fabcfef34ef95b8c6bd414f34d77037451e1;p=syp.git
diff --git a/js/admin.js b/js/admin.js
index 0709878..48c1eed 100644
--- a/js/admin.js
+++ b/js/admin.js
@@ -1,177 +1,76 @@
/* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
* license. */
-OpenLayers.Control.SelectDragFeature =
- OpenLayers.Class (OpenLayers.Control.SelectFeature, {
-
- lastPixel: null,
- dragFeature: null,
-
- startPixel : null,
+// drag feature with tolerance
+OpenLayers.Control.SypDragFeature = OpenLayers.Class (OpenLayers.Control.DragFeature, {
+ startPixel: null,
+ dragStart: null,
pixelTolerance : 0,
timeTolerance: 300,
- dragStart: null,
-
- onComplete: function (feature, pixel) {},
- onCancel: function (feature, pixel) {},
-
- initialize: function (layers, options) {
- var callbacks = {
- over: this.overFeature,
- out: this.outFeature
- };
- this.callbacks = OpenLayers.Util.extend(callbacks, this.callbacks);
-
- OpenLayers.Control.SelectFeature.prototype.initialize.apply(this,
- arguments);
-
- var handlers = {
- drag: new OpenLayers.Handler.Drag(
- this, OpenLayers.Util.extend({
- down: this.downFeature,
- move: this.moveFeature,
- up: this.upFeature,
- out: this.cancel,
- done: this.doneDragging
- })
- )
- };
- this.handlers = OpenLayers.Util.extend(handlers, this.handlers);
- },
-
- doneDragging: function (pixel) {
- var passesTimeTolerance =
- (new Date ()).getTime () > this.dragStart + this.timeTolerance;
- var xDiff = this.startPixel.x - pixel.x;
- var yDiff = this.startPixel.y - pixel.y;
- var passesPixelTolerance = Math.sqrt(Math.pow(xDiff,2) + Math.pow(yDiff,2))
- > this.pixelTolerance;
- if(passesTimeTolerance && passesPixelTolerance){
- this.onComplete(this.dragFeature, pixel);
- } else {
- var res = this.map.getResolution ();
- this.dragFeature.geometry.move(res * (this.startPixel.x - pixel.x),
- res * (pixel.y - this.startPixel.y));
- this.onCancel(this.dragFeature,pixel);
- }
- this.layer.drawFeature(this.dragFeature, "select");
+ downFeature: function(pixel) {
+ OpenLayers.Control.DragFeature.prototype.downFeature.apply(this, arguments);
+ this.dragStart = (new Date()).getTime();
+ this.startPixel = pixel;
},
- cancel: function () {
- this.handlers.drag.deactivate ();
- this.over = false;
- },
+ doneDragging: function(pixel) {
+ OpenLayers.Control.DragFeature.prototype.doneDragging.apply(this, arguments);
+ // Check tolerance.
+ var passesTimeTolerance =
+ (new Date()).getTime() > this.dragStart + this.timeTolerance;
- clickFeature: function (feature) {
- OpenLayers.Control.SelectFeature.prototype.clickFeature.apply(this,
- arguments);
- if (Admin.Utils.indexOf(this.layer.selectedFeatures, feature) == -1) {
- this.dragDisable ();
- }
- },
+ var xDiff = this.startPixel.x - pixel.x;
+ var yDiff = this.startPixel.y - pixel.y;
- upFeature: function (pixel) {
- },
+ var passesPixelTolerance =
+ Math.sqrt(Math.pow(xDiff,2) + Math.pow(yDiff,2)) > this.pixelTolerance;
- moveFeature: function (pixel) {
- if (Admin.Utils.indexOf(this.layer.selectedFeatures,
- this.dragFeature) != -1) {
- var res = this.map.getResolution ();
- this.dragFeature.geometry.move(res * (pixel.x - this.lastPixel.x),
- res * (this.lastPixel.y - pixel.y));
- this.layer.drawFeature(this.dragFeature, "temporary");
- this.lastPixel = pixel;
+ if(passesTimeTolerance && passesPixelTolerance){
+ this.onComplete(this.feature, pixel);
+ } else {
+ var feature = this.feature;
+ var res = this.map.getResolution();
+ this.feature.geometry.move(res * (this.startPixel.x - this.lastPixel.x),
+ res * (this.lastPixel.y - this.startPixel.y));
+ this.layer.drawFeature(this.feature);
}
+ this.layer.drawFeature(this.feature, "select");
},
- downFeature: function (pixel) {
- this.handlers.feature.down = pixel;
-
- if (Admin.Utils.indexOf(this.layer.selectedFeatures,
- this.dragFeature) != -1) {
- this.dragStart = (new Date ()).getTime ();
- this.startPixel = pixel;
- this.lastPixel = pixel;
- }
- },
-
- deactivate: function () {
- this.dragDisable ();
- return OpenLayers.Control.SelectFeature.prototype.deactivate.apply(
- this, arguments
- );
+ moveFeature: function(pixel) {
+ OpenLayers.Control.DragFeature.prototype.moveFeature.apply(this, arguments);
+ this.layer.drawFeature(this.feature, "temporary");
},
overFeature: function (feature) {
- if (Admin.Utils.indexOf(this.layer.selectedFeatures, feature) != -1) {
- this.dragEnable(feature);
- }
- OpenLayers.Control.SelectFeature.prototype.overFeature.apply(this,
- arguments);
- },
-
- outFeature: function (feature) {
- this.dragDisable ();
- OpenLayers.Control.SelectFeature.prototype.outFeature.apply(this,
- arguments);
- },
-
- select: function (feature) {
- this.dragEnable(feature);
- OpenLayers.Control.SelectFeature.prototype.select.apply(this,
- arguments);
- },
-
- dragDisable: function () {
- this.over = false;
- if(!this.handlers.drag.dragging) {
- this.handlers.drag.deactivate ();
- OpenLayers.Element.removeClass(
- this.map.viewPortDiv, this.displayClass + "Over"
- );
- this.dragFeature = null;
- }
- },
-
- dragEnable: function (feature) {
- if(!this.handlers.drag.dragging) {
- this.dragFeature = feature;
- this.handlers.drag.activate ();
- this.over = true;
- OpenLayers.Element.addClass(this.map.viewPortDiv, this.displayClass + "Over");
- } else {
- if(this.dragFeature.id == feature.id) {
- this.over = true;
- } else {
- this.over = false;
- }
+ // can only drag and drop currently selected feature
+ if (feature != Admin.currentFeature) {
+ return;
}
+ OpenLayers.Control.DragFeature.prototype.overFeature.apply(this, arguments);
},
- setMap: function (map) {
- this.handlers.drag.setMap(map);
- OpenLayers.Control.SelectFeature.prototype.setMap.apply(this, arguments);
- },
-
- CLASS_NAME: "OpenLayers.Control.SelectDragFeature"
+ CLASS_NAME: "OpenLayers.Control.SypDragFeature"
});
var Admin = {
Settings: {
- MARKER_ICON: "openlayers/img/marker-blue.png",
- MARKER_ICON_HEIGHT: 25,
- MARKER_SELECT_ICON: "openlayers/img/marker-green.png",
- MARKER_SELECT_ICON_HEIGHT: 25,
- MARKER_TEMPORARY_ICON: "openlayers/img/marker-gold.png",
- MARKER_TEMPORARY_ICON_HEIGHT: 25
+ MARKER_ICON: "media/marker-normal.png",
+ MARKER_SELECT_ICON: "media/marker-selected.png",
+ MARKER_TEMPORARY_ICON: "media/marker-temp.png",
+ MARKER_HEIGHT: 25
},
map: null,
baseLayer: null,
dataLayer: null,
- selectControl: null,
- clickControl: null,
+ selFeatureControl: null,
+ moveFeatureControl: null,
+ addFeatureControl: null,
+
+ currentFeature: null,
+ currentFeatureLocation: null,
init: function () {
this.map = new OpenLayers.Map ("map", {
@@ -187,14 +86,16 @@ var Admin = {
this.dataLayer = this.createDataLayer ();
this.map.addLayers([this.baseLayer, this.dataLayer]);
- this.selectControl = this.createSelectDragControl ();
- this.map.addControl(this.selectControl);
- this.selectControl.activate ();
+ // controls
+ this.selFeatureControl = this.createSelectFeatureControl();
+ this.map.addControl(this.selFeatureControl);
+ this.moveFeatureControl = this.createMoveFeatureControl();
+ this.map.addControl(this.moveFeatureControl);
+ this.addFeatureControl = this.createNewfeatureControl();
+ this.map.addControl(this.addFeatureControl);
- this.clickControl = this.createClickControl ();
- this.map.addControl(this.clickControl);
-
- var centerBounds = new OpenLayers.Bounds ();
+ // position
+ var centerBounds = new OpenLayers.Bounds();
var mapProj = this.map.getProjectionObject();
var sypOrigProj = new OpenLayers.Projection("EPSG:4326");
@@ -206,7 +107,24 @@ var Admin = {
centerBounds.extend(bottomLeft);
centerBounds.extend(topRight);
+
+ // at that moment, ie does not know size of the map, we need to update
+ // manually
+ this.map.updateSize();
this.map.zoomToExtent(centerBounds);
+
+ this.reset();
+ },
+
+ reset: function() {
+ this.addFeatureControl.deactivate();
+ this.moveFeatureControl.deactivate();
+ this.selFeatureControl.activate();
+ this.checkForFeatures();
+ $("#newfeature_button").show().val(SypStrings.AddItem);
+ $("#newfeature_button").unbind("click").click(function () {
+ Admin.addNewFeature();
+ });
},
createBaseLayer: function () {
@@ -217,18 +135,15 @@ var Admin = {
var styleMap = new OpenLayers.StyleMap (
{"default": {
externalGraphic: this.Settings.MARKER_ICON,
- graphicHeight: this.Settings.MARKER_ICON_HEIGHT
- || 32
+ graphicHeight: this.Settings.MARKER_HEIGHT || 32
},
"temporary": {
externalGraphic: this.Settings.MARKER_TEMPORARY_ICON,
- graphicHeight: this.Settings.MARKER_TEMPORARY_ICON_HEIGHT
- || 32
+ graphicHeight: this.Settings.MARKER_HEIGHT || 32
},
"select": {
externalGraphic: this.Settings.MARKER_SELECT_ICON,
- graphicHeight: this.Settings.MARKER_SELECT_ICON_HEIGHT
- || 32
+ graphicHeight: this.Settings.MARKER_HEIGHT || 32
}});
var layer = new OpenLayers.Layer.GML("KML", "items.php",
@@ -237,332 +152,150 @@ var Admin = {
format: OpenLayers.Format.KML,
projection: this.map.displayProjection,
eventListeners: { scope: this,
- loadend: this.checkForFeatures,
- featureremoved: this.checkForFeatures,
- featureadded: this.checkForFeatures
+ loadend: this.checkForFeatures
}
});
return layer;
},
- createSelectDragControl: function () {
- var control = new OpenLayers.Control.SelectDragFeature(
+ createMoveFeatureControl: function () {
+ var control = new OpenLayers.Control.SypDragFeature(
this.dataLayer, {
- onSelect: this.onFeatureSelect,
- onUnselect: this.onFeatureUnselect,
- onComplete: OpenLayers.Function.bind(this.onDragComplete.action,
- this.onDragComplete),
- toggle: true,
- clickout: false
- });
+ });
return control;
},
- checkForFeatures: function () {
- var features = this.dataLayer.features;
- if (features.length == 0) {
- $("#modify_howto").css("visibility", "hidden");
- } else {
- $("#modify_howto").css("visibility", "visible");
- }
+ createSelectFeatureControl: function () {
+ var control = new OpenLayers.Control.SelectFeature(
+ this.dataLayer, {
+ onSelect: OpenLayers.Function.bind(this.onFeatureSelect, this)
+ });
+ return control;
},
- createClickControl: function () {
+ createNewfeatureControl: function () {
var control = new OpenLayers.Control ();
- var handler = new OpenLayers.Handler.Click(control, {});
+ var handler = new OpenLayers.Handler.Click(control, {
+ 'click': OpenLayers.Function.bind(FeatureMgr.add, FeatureMgr)
+ });
control.handler = handler;
return control;
},
- addMarkerNewImage: function (imgurl) {
- return function (evt) {
- FeatureMgr.itemDeleter.lock(imgurl);
- var pos = this.map.getLonLatFromViewPortPx(evt.xy);
- var point = new OpenLayers.Geometry.Point(pos.lon, pos.lat);
- var desc = '';
- var feature = new OpenLayers.Feature.Vector(point, {
- name: '',
- description: desc
- });
- Admin.dataLayer.addFeatures([feature]);
- Admin.selectControl.activate ();
-
- // When adding a feature and then selecting it, we set render style
- // to "default" and right after, we set it to "select". When
- // rendering backend is SVG, that triggers a modification of href
- // attribute right after having inserted image with a different
- // href. Unfortunately, webkit does not like that (see
- // webkit#26392). That's why we need to wrap selection in a
- // timeout.
- var renderer = Admin.dataLayer.renderer;
- if (renderer && renderer.CLASS_NAME == "OpenLayers.Renderer.SVG") {
- window.setTimeout(function () {
- Admin.selectControl.select(feature);
- Admin.saveFeature(feature);
- } ,0);
- } else {
- Admin.selectControl.select(feature);
- Admin.saveFeature(feature);
- }
- $("#addphoto_button").val("ajouter une autre image");
- }
- },
-
onFeatureSelect: function (feature) {
- Admin.closeNewimage();
-
- $("#img").attr('src', '');
- $("#editor").show();
- $("#features_success").css("visibility", "hidden");
-
- // we use the real onclick method otherwise: jquery method would not
- // execute because of input.change method
- $("#deletephoto_button").get(0).onclick = function () {
- Admin.clearChangeTimeouts();
- var imgurl = $("#img").attr("src");
- Admin.deleteFeature(feature, imgurl);
- };
-
- $("#title_input").val(feature.attributes.name);
-
- var fullDesc = $(feature.attributes.description).parent();
- $("#desc_input").val(fullDesc.find('p').text());
- $("#img").attr('src', fullDesc.find('img').attr('src'));
-
- $(".input").each(function () {
- this.curVal = this.value;
- });
-
- // Change event happens if an input has change if we leave that field.
- // But we want data to be saved even if user does not blur input field
- // (for example, he types something in the box and then does not touch
- // it's computer). So, every 60 seconds, we check if value of input
- // has changed. More precisely, every 60 seconds, we wait 3 seconds to
- // see if input value is still changing and if not, it probably means
- // user is not modifying data anymore. In that case, we save.
-
- $(".input").focus(function (evt) {
- var self = this;
- this.curVal = this.value;
- this.checkTimer = window.setInterval(function () {
- if (self.value != self.curVal) {
- $("#features_success").css("visibility", "hidden");
- var newVal = self.value;
- this.saveTimeout = window.setTimeout(function () {
- if (self.value == newVal) {
- self.curVal = self.value;
- Admin.saveFeature(feature);
- }
- }, 3 * 1000);
- }
- }, 6 * 1000);
- })
-
- $(".input").blur(function (evt) {
- if (this.checkTimer) {
- window.clearInterval(this.checkTimer);
- this.checkTimer = null;
- }
- if (this.saveTimeout) {
- window.clearTimeout(this.saveTimeout);
- this.saveTimeout = null;
- }
- });
-
- $(".input").change(function (evt) {
- if (this.curVal != this.value) {
- $("#features_success").css("visibility", "hidden");
- this.curVal = this.value;
- Admin.saveFeature(feature);
- }
- });
-
- $("#title_input").blur()
- $("#title_input").focus()
- $("#title_input").select()
-
- $("#dragdrop_howto").css("visibility", "visible");
+ this.showEditor(feature);
+ FeatureMgr.reset();
+ this.selFeatureControl.deactivate();
+ this.moveFeatureControl.activate();
},
- onFeatureUnselect: function (feature) {
- Admin.closeEditor();
- $("#features_connect_error").hide();
- $("#deletephoto_button").get(0).onclick = null;
-
- // if user unselects feature, save modifications without waiting
- var needsSaving = Admin.onDragComplete.timeout ? true: false;
- $(".input").each(function () {
- if (this.value != this.curVal) {
- needsSaving = true;
- }
- if (this.checkTimer) {
- window.clearInterval(this.checkTimer);
- this.checkTimer = null;
- }
- if (this.saveTimeout) {
- window.clearTimeout(this.saveTimeout);
- this.saveTimeout = null;
- }
- });
-
- if (needsSaving) {
- $("#features_success").css("visibility", "hidden");
- Admin.saveFeature(feature);
+ closeEditor: function() {
+ if (this.currentFeature && this.currentFeature.layer) {
+ this.selFeatureControl.unselect(this.currentFeature);
}
-
- window.clearTimeout(Admin.onDragComplete.timeout);
- Admin.onDragComplete.timeout = null;
- },
-
- addNewFeature: function () {
- this.selectControl.unselectAll();
- $("#features_success").css("visibility", "hidden");
- $("#features_connect_error").hide();
- $("#addphoto_button").attr("disabled", "disabled");
- $("#newimage").show();
- $("#file_form").show();
- $("#file_form").get(0).reset();
- $("#newimage_input").change(function () {
- $("#newimage_error").hide();
- if (OpenLayers.Util.getBrowserName() == "msie") {
- if ($("#file_form").find('input[type="submit"]').length == 0) {
- $("#file_form").append(
- '
" + - this.Utils.escapeHTML(description) + - "
" + - "" + Admin.Utils.escapeHTML(description) + "
" + + "