From 89b3019cde57f560a93cfc1a26fbe3542724f49d Mon Sep 17 00:00:00 2001 From: Mike Adair Date: Fri, 22 Jun 2012 05:50:14 -0400 Subject: [PATCH 1/3] return correct map overlay elem --- src/ol/Map.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/ol/Map.js b/src/ol/Map.js index 2f58ca8138..c01b56a9c5 100644 --- a/src/ol/Map.js +++ b/src/ol/Map.js @@ -379,6 +379,25 @@ ol.Map.prototype.moveByPx = function(dx, dy) { // call moveByPx on renderers }; +/** + * @param {ol.Loc} loc the location being requested + * @returns {Object} the + */ +ol.Map.prototype.getViewportPosition = function(loc) { + //TODO: delegate this to the renderers + //stub for now to get popups working + return {x: 200, y: 300}; +}; + +/** + * @returns {Element} the map overlay element + */ +ol.Map.prototype.getMapOverlay = function() { + //TODO: delegate this to the renderers + //stub for now to get popups working + return this.mapOverlay_ +}; + /** * @export */ From 368bb2867107a6441c3d018414575121d08877fa Mon Sep 17 00:00:00 2001 From: Mike Adair Date: Fri, 22 Jun 2012 08:13:34 -0400 Subject: [PATCH 2/3] initial version of popups --- css/img/close.gif | Bin 0 -> 1078 bytes css/ol.css | 120 ++++++++++++++- src/api/popup.js | 130 +++++++++++++++++ src/ol.js | 1 + src/ol/Popup.js | 281 ++++++++++++++++++++++++++++++++++++ src/ol/geom/Geometry.js | 9 ++ test/api.html | 1 + test/ol.html | 1 + test/spec/api/popup.test.js | 280 +++++++++++++++++++++++++++++++++++ test/spec/ol/Popup.test.js | 96 ++++++++++++ 10 files changed, 918 insertions(+), 1 deletion(-) create mode 100644 css/img/close.gif create mode 100644 src/api/popup.js create mode 100644 src/ol/Popup.js create mode 100644 test/spec/api/popup.test.js create mode 100644 test/spec/ol/Popup.test.js diff --git a/css/img/close.gif b/css/img/close.gif new file mode 100644 index 0000000000000000000000000000000000000000..a8958de9b429ae00f56bb282c4b133ecbaf334bd GIT binary patch literal 1078 zcmZ?wbhEHb6l4%&_|5bmP+{ z8=fxT^nBIU?+>5;c=qzk<7f96^e*$8T;?~uENp&7)bfg`)n!quD`Hlc#jGv?Npb7T z5;m8BNYduAlsu4mLePHK3b)AGKc z>vvYy?~Ja`8C~BqdcMbXy-w@-o;M0SZs2#)(C>ti{{_>K3ud9`%tFqahh1=qzu=N^ z(KYdcYr!BJ0Fg_9;)jvY-3rJ@d_b z;g|p1FaMce!3+O_m%+ua)0*F9w!O`4eV5h#CadjzZr8iq&UblTZ>lH1shjq;e)_w{ z8SffrzH6KFdg79A^EdrixaIreZQqw{`|CMFrPsOqym&D7&!hjSaZsFY+6^S6WB|jz|Trx{Mnn_1s z(GvD44549L9x7aLo?~ylhNYlD*^7bAh~uKBLckGOm;R=dk17q5h291JB`GLjy9F{oMb+*G{n z$vj&$l*#1B#1xKc48bu6R^EK+G(&`W$q9!^nnL`#vm$DCCS2l?64B-|G'; + } + } +}; + + +/** + * @return {string|undefined} the anchor . + */ +ol.Popup.prototype.getTemplate = function() { + return this.template_; +}; + +/** + * @param {string} template the map object to hold this popup. + */ +ol.Popup.prototype.setTemplate = function(template) { + this.template_ = template; +}; + +/** + * Open the popup. + * @export + * @param {ol.Feature|ol.Loc} opt_arg feature or location for the anchor + */ +ol.Popup.prototype.open = function(opt_arg) { + if (goog.isDef(opt_arg)) { + this.setAnchor(opt_arg); + } + + //create popup container if it's not created already + if (goog.isNull(this.container_)) { + this.container_ = goog.dom.createElement('div'); + goog.dom.classes.add(this.container_, + ol.Popup.CLASS_NAME, ol.Popup.CLASS_NAME+'-'+this.placement_); + + if (this.closeButton_) { + var closeButton = goog.dom.createElement('div'); + goog.dom.appendChild(this.container_, closeButton); + goog.dom.classes.add(closeButton, ol.Popup.CLASS_NAME+'-close'); + } + this.map_.getEvents().register('click', this.clickHandler, this); + goog.dom.appendChild(this.map_.getMapOverlay(), this.container_); + } + + this.childContent_=goog.dom.htmlToDocumentFragment(this.generateContent_()); + goog.dom.appendChild(this.container_, this.childContent_); + + //position the element + if (this.anchor_ instanceof ol.Feature) { + this.pos_ = this.anchor_.getGeometry().getCentroid(); + } else { + this.pos_ = this.anchor_; + } + var popupPosPx = this.map_.getViewportPosition(this.pos_); + var popupSize = goog.style.getSize(this.container_); + + switch(this.placement_) { + default: + case 'auto': + //TODO: switch based on map quadrant + break; + case 'top': + case 'bottom': + popupPosPx.x -= popupSize.width / 2.0; + + if (this.placement_ == "bottom") { + popupPosPx.y -= popupSize.height + this.arrowOffset_; + } else { + popupPosPx.y += this.arrowOffset_; + } + break; + case 'left': + case 'right': + popupPosPx.y -= popupSize.height / 2.0; + + if (this.placement_ == "right") { + popupPosPx.x -= popupSize.width + this.arrowOffset_; + } else { + popupPosPx.x += this.arrowOffset_; + } + break; + }; + this.moveTo_(popupPosPx); + +}; + +/** + * @param px - {goog.} the top and left position of the popup div. + */ +ol.Popup.prototype.moveTo_ = function(px) { + if (goog.isDefAndNotNull(px)) { + goog.style.setPosition(this.container_, px.x, px.y); + } +}; + +/** + * Click handler + * @param {Event} evt the event generated by a click + */ +ol.Popup.prototype.clickHandler = function(evt) { + var target = /** @type {Node} */ evt.target; + if (goog.dom.classes.has(target,ol.Popup.CLASS_NAME+'-close')) { + this.close(); + } +}; + +/** + * Clean up. + * @export + */ +ol.Popup.prototype.close = function() { + goog.dom.removeChildren(this.container_); + goog.dom.removeNode(this.container_); +}; + +/** + * Clean up. + * @export + */ +ol.Popup.prototype.destroy = function() { + for (var key in this) { + delete this[key]; + } +}; diff --git a/src/ol/geom/Geometry.js b/src/ol/geom/Geometry.js index b24e76d1ed..9378df6ebc 100644 --- a/src/ol/geom/Geometry.js +++ b/src/ol/geom/Geometry.js @@ -32,3 +32,12 @@ ol.geom.Geometry.prototype.setBounds = function(bounds) { this.bounds_ = bounds; return this; }; + +/** + * @returns ol.Loc + */ +ol.geom.Geometry.prototype.getCentroid = function() { + //FIXME: stub only to get popups working + return new ol.Loc(-76,45); +}; + diff --git a/test/api.html b/test/api.html index 7a70332a77..3cb67c3177 100644 --- a/test/api.html +++ b/test/api.html @@ -58,6 +58,7 @@ + +