svg/pointFeature.js

  1. var inherit = require('../inherit');
  2. var registerFeature = require('../registry').registerFeature;
  3. var pointFeature = require('../pointFeature');
  4. /**
  5. * Create a new instance of svg.pointFeature.
  6. *
  7. * @class
  8. * @alias geo.svg.pointFeature
  9. * @extends geo.pointFeature
  10. * @extends geo.svg.object
  11. * @param {geo.pointFeature.spec} arg
  12. * @returns {geo.svg.pointFeature}
  13. */
  14. var svg_pointFeature = function (arg) {
  15. 'use strict';
  16. if (!(this instanceof svg_pointFeature)) {
  17. return new svg_pointFeature(arg);
  18. }
  19. var svg_object = require('./object');
  20. var timestamp = require('../timestamp');
  21. arg = arg || {};
  22. pointFeature.call(this, arg);
  23. svg_object.call(this);
  24. /**
  25. * @private
  26. */
  27. var m_this = this,
  28. s_init = this._init,
  29. s_update = this._update,
  30. m_buildTime = timestamp(),
  31. m_style = {};
  32. /**
  33. * Initialize.
  34. *
  35. * @param {geo.pointFeature.spec} arg The feature specification.
  36. * @returns {this}
  37. */
  38. this._init = function (arg) {
  39. s_init.call(m_this, arg);
  40. return m_this;
  41. };
  42. /**
  43. * Build. Create the necessary elements to render points.
  44. *
  45. * @returns {this}
  46. */
  47. this._build = function () {
  48. var data = m_this.data(),
  49. s_style = m_this.style.get(),
  50. m_renderer = m_this.renderer(),
  51. pos_func = m_this.position();
  52. // call super-method
  53. s_update.call(m_this);
  54. // default to empty data array
  55. if (!data) { data = []; }
  56. // fill in svg renderer style object defaults
  57. m_style.id = m_this._svgid();
  58. m_style.data = data;
  59. m_style.append = 'circle';
  60. m_style.attributes = {
  61. r: m_renderer._convertScale(s_style.radius),
  62. cx: function (d) {
  63. return m_this.featureGcsToDisplay(pos_func(d)).x;
  64. },
  65. cy: function (d) {
  66. return m_this.featureGcsToDisplay(pos_func(d)).y;
  67. }
  68. };
  69. m_style.style = s_style;
  70. m_style.classes = ['svgPointFeature'];
  71. m_style.visible = m_this.visible;
  72. // pass to renderer to draw
  73. m_this.renderer()._drawFeatures(m_style);
  74. // update time stamps
  75. m_buildTime.modified();
  76. m_this.updateTime().modified();
  77. return m_this;
  78. };
  79. /**
  80. * Update. Rebuild if necessary.
  81. *
  82. * @returns {this}
  83. */
  84. this._update = function () {
  85. s_update.call(m_this);
  86. if (m_this.timestamp() >= m_buildTime.timestamp()) {
  87. m_this._build();
  88. }
  89. return m_this;
  90. };
  91. this._init(arg);
  92. return this;
  93. };
  94. inherit(svg_pointFeature, pointFeature);
  95. var capabilities = {};
  96. capabilities[pointFeature.capabilities.stroke] = true;
  97. // Now register it
  98. registerFeature('svg', 'point', svg_pointFeature, capabilities);
  99. module.exports = svg_pointFeature;