StyleMap.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/BaseTypes/Class.js
  7. * @requires OpenLayers/Style.js
  8. * @requires OpenLayers/Feature/Vector.js
  9. */
  10. /**
  11. * Class: OpenLayers.StyleMap
  12. */
  13. OpenLayers.StyleMap = OpenLayers.Class({
  14. /**
  15. * Property: styles
  16. * Hash of {<OpenLayers.Style>}, keyed by names of well known
  17. * rendering intents (e.g. "default", "temporary", "select", "delete").
  18. */
  19. styles: null,
  20. /**
  21. * Property: extendDefault
  22. * {Boolean} if true, every render intent will extend the symbolizers
  23. * specified for the "default" intent at rendering time. Otherwise, every
  24. * rendering intent will be treated as a completely independent style.
  25. */
  26. extendDefault: true,
  27. /**
  28. * Constructor: OpenLayers.StyleMap
  29. *
  30. * Parameters:
  31. * style - {Object} Optional. Either a style hash, or a style object, or
  32. * a hash of style objects (style hashes) keyed by rendering
  33. * intent. If just one style hash or style object is passed,
  34. * this will be used for all known render intents (default,
  35. * select, temporary)
  36. * options - {Object} optional hash of additional options for this
  37. * instance
  38. */
  39. initialize: function (style, options) {
  40. this.styles = {
  41. "default": new OpenLayers.Style(
  42. OpenLayers.Feature.Vector.style["default"]),
  43. "select": new OpenLayers.Style(
  44. OpenLayers.Feature.Vector.style["select"]),
  45. "temporary": new OpenLayers.Style(
  46. OpenLayers.Feature.Vector.style["temporary"]),
  47. "delete": new OpenLayers.Style(
  48. OpenLayers.Feature.Vector.style["delete"])
  49. };
  50. // take whatever the user passed as style parameter and convert it
  51. // into parts of stylemap.
  52. if(style instanceof OpenLayers.Style) {
  53. // user passed a style object
  54. this.styles["default"] = style;
  55. this.styles["select"] = style;
  56. this.styles["temporary"] = style;
  57. this.styles["delete"] = style;
  58. } else if(typeof style == "object") {
  59. for(var key in style) {
  60. if(style[key] instanceof OpenLayers.Style) {
  61. // user passed a hash of style objects
  62. this.styles[key] = style[key];
  63. } else if(typeof style[key] == "object") {
  64. // user passsed a hash of style hashes
  65. this.styles[key] = new OpenLayers.Style(style[key]);
  66. } else {
  67. // user passed a style hash (i.e. symbolizer)
  68. this.styles["default"] = new OpenLayers.Style(style);
  69. this.styles["select"] = new OpenLayers.Style(style);
  70. this.styles["temporary"] = new OpenLayers.Style(style);
  71. this.styles["delete"] = new OpenLayers.Style(style);
  72. break;
  73. }
  74. }
  75. }
  76. OpenLayers.Util.extend(this, options);
  77. },
  78. /**
  79. * Method: destroy
  80. */
  81. destroy: function() {
  82. for(var key in this.styles) {
  83. this.styles[key].destroy();
  84. }
  85. this.styles = null;
  86. },
  87. /**
  88. * Method: createSymbolizer
  89. * Creates the symbolizer for a feature for a render intent.
  90. *
  91. * Parameters:
  92. * feature - {<OpenLayers.Feature>} The feature to evaluate the rules
  93. * of the intended style against.
  94. * intent - {String} The intent determines the symbolizer that will be
  95. * used to draw the feature. Well known intents are "default"
  96. * (for just drawing the features), "select" (for selected
  97. * features) and "temporary" (for drawing features).
  98. *
  99. * Returns:
  100. * {Object} symbolizer hash
  101. */
  102. createSymbolizer: function(feature, intent) {
  103. if(!feature) {
  104. feature = new OpenLayers.Feature.Vector();
  105. }
  106. if(!this.styles[intent]) {
  107. intent = "default";
  108. }
  109. feature.renderIntent = intent;
  110. var defaultSymbolizer = {};
  111. if(this.extendDefault && intent != "default") {
  112. defaultSymbolizer = this.styles["default"].createSymbolizer(feature);
  113. }
  114. return OpenLayers.Util.extend(defaultSymbolizer,
  115. this.styles[intent].createSymbolizer(feature));
  116. },
  117. /**
  118. * Method: addUniqueValueRules
  119. * Convenience method to create comparison rules for unique values of a
  120. * property. The rules will be added to the style object for a specified
  121. * rendering intent. This method is a shortcut for creating something like
  122. * the "unique value legends" familiar from well known desktop GIS systems
  123. *
  124. * Parameters:
  125. * renderIntent - {String} rendering intent to add the rules to
  126. * property - {String} values of feature attributes to create the
  127. * rules for
  128. * symbolizers - {Object} Hash of symbolizers, keyed by the desired
  129. * property values
  130. * context - {Object} An optional object with properties that
  131. * symbolizers' property values should be evaluated
  132. * against. If no context is specified, feature.attributes
  133. * will be used
  134. */
  135. addUniqueValueRules: function(renderIntent, property, symbolizers, context) {
  136. var rules = [];
  137. for (var value in symbolizers) {
  138. rules.push(new OpenLayers.Rule({
  139. symbolizer: symbolizers[value],
  140. context: context,
  141. filter: new OpenLayers.Filter.Comparison({
  142. type: OpenLayers.Filter.Comparison.EQUAL_TO,
  143. property: property,
  144. value: value
  145. })
  146. }));
  147. }
  148. this.styles[renderIntent].addRules(rules);
  149. },
  150. CLASS_NAME: "OpenLayers.StyleMap"
  151. });