DojoSlider.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. define([
  2. "dojo/_base/declare",
  3. "dojo/_base/lang",
  4. "dojo/_base/array",
  5. "dojo/dom",
  6. "dojo/dom-construct",
  7. "dojo/dom-style",
  8. "dijit/form/HorizontalSlider",
  9. "dijit/form/HorizontalRule",
  10. "dijit/form/HorizontalRuleLabels",
  11. "./DojoD3",
  12. "./Mapping"
  13. ], function (declare, lang, arrayUtil, dom, domConstruct, domStyle,
  14. HorizontalSlider, HorizontalRule, HorizontalRuleLabels,
  15. DojoD3, Mapping) {
  16. return declare([Mapping, DojoD3], {
  17. constructor: function (mappings, target) {
  18. this.setDatasetMappings({
  19. slider: {
  20. display: "Slider Data",
  21. fields: {
  22. label: "Label",
  23. }
  24. }
  25. });
  26. this.setFieldMappings(mappings);
  27. this.renderTo(target);
  28. },
  29. setValue: function (value) {
  30. this.vertSlider.set("value", value);
  31. },
  32. renderTo: function (_target) {
  33. _target = lang.mixin({
  34. css: ""
  35. }, _target);
  36. this._target = _target;
  37. this.calcGeom();
  38. this.display();
  39. },
  40. display: function (data) {
  41. this.inherited(arguments);
  42. if (data)
  43. this.setData(data);
  44. var data = this.getMappedData();
  45. if (!data || !data.length) {
  46. return
  47. }
  48. var labels = [];
  49. arrayUtil.forEach(data, lang.hitch(this, function (item, idx) {
  50. labels.push(item.label);
  51. }));
  52. var value = labels[0];
  53. // Create the rules
  54. // Create the labels
  55. if (this.sliderNode) {
  56. value = this.vertSlider.get("value");
  57. this.vertSlider.destroyRecursive();
  58. this.sliderRules.destroyRecursive();
  59. this.sliderLabels.destroyRecursive();
  60. domConstruct.destroy(this.sliderNode);
  61. }
  62. this.sliderNode = domConstruct.create("div", {}, dom.byId(this.target.domNodeID), "first");
  63. this.labelsNode = domConstruct.create("div", {}, this.sliderNode, "first");
  64. this.sliderLabels = new HorizontalRuleLabels({
  65. container: "bottomDecoration",
  66. labelStyle: "font-style: italic; height: 2em;",
  67. //numericMargin: 1,
  68. labels: labels
  69. }, this.labelsNode);
  70. this.rulesNode = domConstruct.create("div", {}, this.sliderNode, "first");
  71. this.sliderRules = new HorizontalRule({
  72. container: "bottomDecoration",
  73. count: labels.length,
  74. style: "height: 5px;"
  75. }, this.rulesNode);
  76. // Create the vertical slider programmatically
  77. this.vertSlider = new HorizontalSlider({
  78. minimum: labels[0],
  79. maximum: labels[labels.length - 1],
  80. //pageIncrement: 20,
  81. value: value,
  82. intermediateChanges: false,
  83. discreteValues: labels.length,
  84. //style: "height: " + this.target.height + "px;"
  85. onChange: lang.hitch(this, function (value) {
  86. var evt = {};
  87. evt[this.getFieldMapping("label")] = value;
  88. this.emit("click", evt);
  89. })
  90. }, this.sliderNode);
  91. // Start up the widgets
  92. this.vertSlider.startup();
  93. this.sliderRules.startup();
  94. this.sliderLabels.startup();
  95. }
  96. });
  97. });