Source: data/dasf/widgets/timerange.js

  1. import { TimeSlider } from "../../../ui/slider/TimeSlider";
  2. /**
  3. * Represents a time range widget.
  4. */
  5. export class TimeRangeWidget {
  6. tmin = "tmin"
  7. tmax = "tmax"
  8. description = "Select time range"
  9. /**
  10. * Initializes the plot for the element defined by id and given options.
  11. *
  12. * @param {object} instanceData - The instance data used to initialize the widget.
  13. */
  14. constructor(instanceData) {
  15. if (instanceData) {
  16. this.deserialize_(instanceData);
  17. }
  18. }
  19. /**
  20. * Helper function to create a new object from the data.
  21. * @private
  22. */
  23. deserialize_(instanceData) {
  24. // Note this.active will not be listed in keys since it's declared, but not defined
  25. const keys = Object.keys(this);
  26. for (const key of keys) {
  27. if (instanceData.hasOwnProperty(key)) {
  28. this[key] = instanceData[key];
  29. }
  30. }
  31. }
  32. /**
  33. * Sets up the schema for the widget.
  34. *
  35. * @param {object} schema - The schema object.
  36. */
  37. setupSchema(schema) {
  38. if (!('options' in schema.properties[this.tmin])) {
  39. schema.properties[this.tmin].options = {};
  40. }
  41. if (!('options' in schema.properties[this.tmax])) {
  42. schema.properties[this.tmax].options = {};
  43. }
  44. schema.properties[this.tmin].options.hidden = true;
  45. schema.properties[this.tmax].options.hidden = true;
  46. }
  47. /**
  48. * Creates the element for the widget.
  49. *
  50. * @param {object} editor - The editor object.
  51. * @param {object} schema - The schema object.
  52. */
  53. createElement(editor, schema) {
  54. var sliderDiv = document.createElement("div");
  55. var labelDiv = document.createElement("label");
  56. labelDiv.innerHTML = this.description;
  57. sliderDiv.appendChild(labelDiv);
  58. const vmin = new Date(schema.properties[this.tmin].minimum)
  59. const vmax = new Date(schema.properties[this.tmax].maximum)
  60. var slider = new TimeSlider(
  61. sliderDiv,
  62. {
  63. change: (data) => {
  64. const tminEditor = editor.getEditor(`root.${this.tmin}`);
  65. if (tminEditor) {
  66. tminEditor.setValue(data.left.toISOString());
  67. }
  68. const tmaxEditor = editor.getEditor(`root.${this.tmax}`);
  69. if (tmaxEditor) {
  70. tmaxEditor.setValue(data.right.toISOString());
  71. }
  72. },
  73. min: vmin,
  74. max: vmax,
  75. value: {
  76. left: vmin,
  77. right: vmax,
  78. },
  79. handles: 2,
  80. }
  81. );
  82. editor.on('ready', () => {
  83. const tminEditor = editor.getEditor(`root.${this.tmin}`);
  84. tminEditor.container.parentNode.parentNode.appendChild(sliderDiv);
  85. })
  86. }
  87. }