Framed.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the Clear BSD license.
  3. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Popup/Anchored.js
  7. */
  8. /**
  9. * Class: OpenLayers.Popup.Framed
  10. *
  11. * Inherits from:
  12. * - <OpenLayers.Popup.Anchored>
  13. */
  14. OpenLayers.Popup.Framed =
  15. OpenLayers.Class(OpenLayers.Popup.Anchored, {
  16. /**
  17. * Property: imageSrc
  18. * {String} location of the image to be used as the popup frame
  19. */
  20. imageSrc: null,
  21. /**
  22. * Property: imageSize
  23. * {<OpenLayers.Size>} Size (measured in pixels) of the image located
  24. * by the 'imageSrc' property.
  25. */
  26. imageSize: null,
  27. /**
  28. * APIProperty: isAlphaImage
  29. * {Boolean} The image has some alpha and thus needs to use the alpha
  30. * image hack. Note that setting this to true will have no noticeable
  31. * effect in FF or IE7 browsers, but will all but crush the ie6
  32. * browser.
  33. * Default is false.
  34. */
  35. isAlphaImage: false,
  36. /**
  37. * Property: positionBlocks
  38. * {Object} Hash of different position blocks (Object/Hashs). Each block
  39. * will be keyed by a two-character 'relativePosition'
  40. * code string (ie "tl", "tr", "bl", "br"). Block properties are
  41. * 'offset', 'padding' (self-explanatory), and finally the 'blocks'
  42. * parameter, which is an array of the block objects.
  43. *
  44. * Each block object must have 'size', 'anchor', and 'position'
  45. * properties.
  46. *
  47. * Note that positionBlocks should never be modified at runtime.
  48. */
  49. positionBlocks: null,
  50. /**
  51. * Property: blocks
  52. * {Array[Object]} Array of objects, each of which is one "block" of the
  53. * popup. Each block has a 'div' and an 'image' property, both of
  54. * which are DOMElements, and the latter of which is appended to the
  55. * former. These are reused as the popup goes changing positions for
  56. * great economy and elegance.
  57. */
  58. blocks: null,
  59. /**
  60. * APIProperty: fixedRelativePosition
  61. * {Boolean} We want the framed popup to work dynamically placed relative
  62. * to its anchor but also in just one fixed position. A well designed
  63. * framed popup will have the pixels and logic to display itself in
  64. * any of the four relative positions, but (understandably), this will
  65. * not be the case for all of them. By setting this property to 'true',
  66. * framed popup will not recalculate for the best placement each time
  67. * it's open, but will always open the same way.
  68. * Note that if this is set to true, it is generally advisable to also
  69. * set the 'panIntoView' property to true so that the popup can be
  70. * scrolled into view (since it will often be offscreen on open)
  71. * Default is false.
  72. */
  73. fixedRelativePosition: false,
  74. /**
  75. * Constructor: OpenLayers.Popup.Framed
  76. *
  77. * Parameters:
  78. * id - {String}
  79. * lonlat - {<OpenLayers.LonLat>}
  80. * contentSize - {<OpenLayers.Size>}
  81. * contentHTML - {String}
  82. * anchor - {Object} Object to which we'll anchor the popup. Must expose
  83. * a 'size' (<OpenLayers.Size>) and 'offset' (<OpenLayers.Pixel>)
  84. * (Note that this is generally an <OpenLayers.Icon>).
  85. * closeBox - {Boolean}
  86. * closeBoxCallback - {Function} Function to be called on closeBox click.
  87. */
  88. initialize:function(id, lonlat, contentSize, contentHTML, anchor, closeBox,
  89. closeBoxCallback) {
  90. OpenLayers.Popup.Anchored.prototype.initialize.apply(this, arguments);
  91. if (this.fixedRelativePosition) {
  92. //based on our decided relativePostion, set the current padding
  93. // this keeps us from getting into trouble
  94. this.updateRelativePosition();
  95. //make calculateRelativePosition always return the specified
  96. // fixed position.
  97. this.calculateRelativePosition = function(px) {
  98. return this.relativePosition;
  99. };
  100. }
  101. this.contentDiv.style.position = "absolute";
  102. this.contentDiv.style.zIndex = 1;
  103. if (closeBox) {
  104. this.closeDiv.style.zIndex = 1;
  105. }
  106. this.groupDiv.style.position = "absolute";
  107. this.groupDiv.style.top = "0px";
  108. this.groupDiv.style.left = "0px";
  109. this.groupDiv.style.height = "100%";
  110. this.groupDiv.style.width = "100%";
  111. },
  112. /**
  113. * APIMethod: destroy
  114. */
  115. destroy: function() {
  116. this.imageSrc = null;
  117. this.imageSize = null;
  118. this.isAlphaImage = null;
  119. this.fixedRelativePosition = false;
  120. this.positionBlocks = null;
  121. //remove our blocks
  122. for(var i = 0; i < this.blocks.length; i++) {
  123. var block = this.blocks[i];
  124. if (block.image) {
  125. block.div.removeChild(block.image);
  126. }
  127. block.image = null;
  128. if (block.div) {
  129. this.groupDiv.removeChild(block.div);
  130. }
  131. block.div = null;
  132. }
  133. this.blocks = null;
  134. OpenLayers.Popup.Anchored.prototype.destroy.apply(this, arguments);
  135. },
  136. /**
  137. * APIMethod: setBackgroundColor
  138. */
  139. setBackgroundColor:function(color) {
  140. //does nothing since the framed popup's entire scheme is based on a
  141. // an image -- changing the background color makes no sense.
  142. },
  143. /**
  144. * APIMethod: setBorder
  145. */
  146. setBorder:function() {
  147. //does nothing since the framed popup's entire scheme is based on a
  148. // an image -- changing the popup's border makes no sense.
  149. },
  150. /**
  151. * Method: setOpacity
  152. * Sets the opacity of the popup.
  153. *
  154. * Parameters:
  155. * opacity - {float} A value between 0.0 (transparent) and 1.0 (solid).
  156. */
  157. setOpacity:function(opacity) {
  158. //does nothing since we suppose that we'll never apply an opacity
  159. // to a framed popup
  160. },
  161. /**
  162. * APIMethod: setSize
  163. * Overridden here, because we need to update the blocks whenever the size
  164. * of the popup has changed.
  165. *
  166. * Parameters:
  167. * contentSize - {<OpenLayers.Size>} the new size for the popup's
  168. * contents div (in pixels).
  169. */
  170. setSize:function(contentSize) {
  171. OpenLayers.Popup.Anchored.prototype.setSize.apply(this, arguments);
  172. this.updateBlocks();
  173. },
  174. /**
  175. * Method: updateRelativePosition
  176. * When the relative position changes, we need to set the new padding
  177. * BBOX on the popup, reposition the close div, and update the blocks.
  178. */
  179. updateRelativePosition: function() {
  180. //update the padding
  181. this.padding = this.positionBlocks[this.relativePosition].padding;
  182. //update the position of our close box to new padding
  183. if (this.closeDiv) {
  184. // use the content div's css padding to determine if we should
  185. // padd the close div
  186. var contentDivPadding = this.getContentDivPadding();
  187. this.closeDiv.style.right = contentDivPadding.right +
  188. this.padding.right + "px";
  189. this.closeDiv.style.top = contentDivPadding.top +
  190. this.padding.top + "px";
  191. }
  192. this.updateBlocks();
  193. },
  194. /**
  195. * Method: calculateNewPx
  196. * Besides the standard offset as determined by the Anchored class, our
  197. * Framed popups have a special 'offset' property for each of their
  198. * positions, which is used to offset the popup relative to its anchor.
  199. *
  200. * Parameters:
  201. * px - {<OpenLayers.Pixel>}
  202. *
  203. * Returns:
  204. * {<OpenLayers.Pixel>} The the new px position of the popup on the screen
  205. * relative to the passed-in px.
  206. */
  207. calculateNewPx:function(px) {
  208. var newPx = OpenLayers.Popup.Anchored.prototype.calculateNewPx.apply(
  209. this, arguments
  210. );
  211. newPx = newPx.offset(this.positionBlocks[this.relativePosition].offset);
  212. return newPx;
  213. },
  214. /**
  215. * Method: createBlocks
  216. */
  217. createBlocks: function() {
  218. this.blocks = [];
  219. //since all positions contain the same number of blocks, we can
  220. // just pick the first position and use its blocks array to create
  221. // our blocks array
  222. var firstPosition = null;
  223. for(var key in this.positionBlocks) {
  224. firstPosition = key;
  225. break;
  226. }
  227. var position = this.positionBlocks[firstPosition];
  228. for (var i = 0; i < position.blocks.length; i++) {
  229. var block = {};
  230. this.blocks.push(block);
  231. var divId = this.id + '_FrameDecorationDiv_' + i;
  232. block.div = OpenLayers.Util.createDiv(divId,
  233. null, null, null, "absolute", null, "hidden", null
  234. );
  235. var imgId = this.id + '_FrameDecorationImg_' + i;
  236. var imageCreator =
  237. (this.isAlphaImage) ? OpenLayers.Util.createAlphaImageDiv
  238. : OpenLayers.Util.createImage;
  239. block.image = imageCreator(imgId,
  240. null, this.imageSize, this.imageSrc,
  241. "absolute", null, null, null
  242. );
  243. block.div.appendChild(block.image);
  244. this.groupDiv.appendChild(block.div);
  245. }
  246. },
  247. /**
  248. * Method: updateBlocks
  249. * Internal method, called on initialize and when the popup's relative
  250. * position has changed. This function takes care of re-positioning
  251. * the popup's blocks in their appropropriate places.
  252. */
  253. updateBlocks: function() {
  254. if (!this.blocks) {
  255. this.createBlocks();
  256. }
  257. if (this.size && this.relativePosition) {
  258. var position = this.positionBlocks[this.relativePosition];
  259. for (var i = 0; i < position.blocks.length; i++) {
  260. var positionBlock = position.blocks[i];
  261. var block = this.blocks[i];
  262. // adjust sizes
  263. var l = positionBlock.anchor.left;
  264. var b = positionBlock.anchor.bottom;
  265. var r = positionBlock.anchor.right;
  266. var t = positionBlock.anchor.top;
  267. //note that we use the isNaN() test here because if the
  268. // size object is initialized with a "auto" parameter, the
  269. // size constructor calls parseFloat() on the string,
  270. // which will turn it into NaN
  271. //
  272. var w = (isNaN(positionBlock.size.w)) ? this.size.w - (r + l)
  273. : positionBlock.size.w;
  274. var h = (isNaN(positionBlock.size.h)) ? this.size.h - (b + t)
  275. : positionBlock.size.h;
  276. block.div.style.width = (w < 0 ? 0 : w) + 'px';
  277. block.div.style.height = (h < 0 ? 0 : h) + 'px';
  278. block.div.style.left = (l != null) ? l + 'px' : '';
  279. block.div.style.bottom = (b != null) ? b + 'px' : '';
  280. block.div.style.right = (r != null) ? r + 'px' : '';
  281. block.div.style.top = (t != null) ? t + 'px' : '';
  282. block.image.style.left = positionBlock.position.x + 'px';
  283. block.image.style.top = positionBlock.position.y + 'px';
  284. }
  285. this.contentDiv.style.left = this.padding.left + "px";
  286. this.contentDiv.style.top = this.padding.top + "px";
  287. }
  288. },
  289. CLASS_NAME: "OpenLayers.Popup.Framed"
  290. });