1. /**
  2. * @module meteoJS/thermodynamicDiagram/diagramParcel
  3. */
  4. import addEventFunctions from '../Events.js';
  5. import Unique from '../base/Unique.js';
  6. import {
  7. getNormalizedLineOptions,
  8. updateLineOptions
  9. } from '../thermodynamicDiagram/Functions.js';
  10. /**
  11. * Change visibility event. Only triggered, if the visibility of the parcel
  12. * changes.
  13. *
  14. * @event module:meteoJS/thermodynamicDiagram/diagramParcel#change:visible
  15. */
  16. /**
  17. * Change options event.
  18. *
  19. * @event module:meteoJS/thermodynamicDiagram/diagramParcel#change:options
  20. */
  21. /**
  22. * Style/visibility options for a parcel in the thermodynamic diagram.
  23. *
  24. * @typedef {Object}
  25. * module:meteoJS/thermodynamicDiagram/diagramParcel~parcelOptions
  26. * @property {boolean} [visible=true]
  27. * Visibility in the thermodynamic diagram.
  28. * @property {module:meteoJS/thermodynamicDiagram~lineOptions}
  29. * [temp] - Options for the temperature curve.
  30. * @property {module:meteoJS/thermodynamicDiagram~lineOptions}
  31. * [dewp] - Options for the dewpoint curve.
  32. */
  33. /**
  34. * Definition of the options for the constructor.
  35. *
  36. * @typedef {module:meteoJS/base/unique~options}
  37. * module:meteoJS/thermodynamicDiagram/diagramParcel~options
  38. * @property {module:meteoJS/sounding/parcel.Parcel} [parcel] - Parcel object.
  39. * @property {boolean} [visible=true] - Visibility of the parcel.
  40. * @property {module:meteoJS/thermodynamicDiagram~lineOptions}
  41. * [temp] - Options for the temperature curve.
  42. * @property {module:meteoJS/thermodynamicDiagram~lineOptions}
  43. * [dewp] - Options for the dewpoint curve.
  44. */
  45. /**
  46. * Representation of a plotted parcel (data and display options)
  47. *
  48. * <pre><code>import DiagramParcel from 'meteojs/thermodynamicDiagram/DiagramParcel';</code></pre>
  49. *
  50. * @extends module:meteoJS/base/unique.Unique
  51. * @fires module:meteoJS/thermodynamicDiagram/diagramParcel#change:visible
  52. * @fires module:meteoJS/thermodynamicDiagram/diagramParcel#change:options
  53. */
  54. export class DiagramParcel extends Unique {
  55. /**
  56. * @param {module:meteoJS/thermodynamicDiagram/diagramParcel~options} [options] - Options.
  57. */
  58. constructor({
  59. parcel = undefined,
  60. visible = true,
  61. temp = {},
  62. dewp = {},
  63. ...rest
  64. } = {}) {
  65. super(rest);
  66. /**
  67. * @type module:meteoJS/sounding/parcel.Parcel
  68. * @private
  69. */
  70. this._parcel = parcel;
  71. if (this.id === undefined && parcel !== undefined)
  72. this.id = parcel.id;
  73. /**
  74. * @type {module:meteoJS/thermodynamicDiagram/diagramParcel~parcelOptions}
  75. * @private
  76. */
  77. this._options = {
  78. visible,
  79. temp: getNormalizedLineOptions(temp, {
  80. style: {
  81. color: 'rgb(255, 153, 0)',
  82. width: 3,
  83. linecap: 'round'
  84. }
  85. }),
  86. dewp: getNormalizedLineOptions(dewp, {
  87. style: {
  88. color: 'rgb(255, 153, 0)',
  89. width: 3,
  90. linecap: 'round'
  91. }
  92. })
  93. };
  94. }
  95. /**
  96. * Parcel object.
  97. *
  98. * @type module:meteoJS/sounding/parcel.Parcel
  99. * @readonly
  100. */
  101. get parcel() {
  102. return this._parcel;
  103. }
  104. /**
  105. * Visibility of the parcel.
  106. *
  107. * @type {boolean}
  108. * @fires module:meteoJS/thermodynamicDiagram/diagramParcel#change:visible
  109. */
  110. get visible() {
  111. return this._options.visible;
  112. }
  113. set visible(visible) {
  114. let oldVisible = this._options.visible;
  115. this._options.visible = visible ? true : false;
  116. if (oldVisible != this._options.visible)
  117. this.trigger('change:visible');
  118. }
  119. /**
  120. * Style options for the parcel.
  121. *
  122. * @type {module:meteoJS/thermodynamicDiagram/diagramParcel~parcelOptions}
  123. * @readonly
  124. */
  125. get options() {
  126. return this._options;
  127. }
  128. /**
  129. * Updates the style options for the parcel.
  130. *
  131. * @param {module:meteoJS/thermodynamicDiagram/diagramParcel~parcelOptions}
  132. * [options] - Options.
  133. * @fires module:meteoJS/thermodynamicDiagram/diagramParcel#change:visible
  134. * @fires module:meteoJS/thermodynamicDiagram/diagramParcel#change:options
  135. */
  136. update({
  137. visible = undefined,
  138. temp = undefined,
  139. dewp = undefined
  140. } = {}) {
  141. let willTrigger = false;
  142. if (temp === undefined)
  143. temp = {};
  144. else
  145. willTrigger = true;
  146. if (dewp === undefined)
  147. dewp = {};
  148. else
  149. willTrigger = true;
  150. this._options.temp = updateLineOptions(this._options.temp, temp);
  151. this._options.dewp = updateLineOptions(this._options.dewp, dewp);
  152. if (willTrigger)
  153. this.trigger('change:options');
  154. if (visible !== undefined)
  155. this.visible = visible;
  156. }
  157. }
  158. addEventFunctions(DiagramParcel.prototype);
  159. export default DiagramParcel;