HorizontalRuleLabels.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. define("dijit/form/HorizontalRuleLabels", [
  2. "dojo/_base/declare", // declare
  3. "dojo/number", // number.format
  4. "dojo/query", // query
  5. "./HorizontalRule"
  6. ], function(declare, number, query, HorizontalRule){
  7. /*=====
  8. var HorizontalRule = dijit.form.HorizontalRule;
  9. =====*/
  10. // module:
  11. // dijit/form/HorizontalRuleLabels
  12. // summary:
  13. // Labels for `dijit.form.HorizontalSlider`
  14. return declare("dijit.form.HorizontalRuleLabels", HorizontalRule, {
  15. // summary:
  16. // Labels for `dijit.form.HorizontalSlider`
  17. templateString: '<div class="dijitRuleContainer dijitRuleContainerH dijitRuleLabelsContainer dijitRuleLabelsContainerH"></div>',
  18. // labelStyle: String
  19. // CSS style to apply to individual text labels
  20. labelStyle: "",
  21. // labels: String[]?
  22. // Array of text labels to render - evenly spaced from left-to-right or bottom-to-top.
  23. // Alternately, minimum and maximum can be specified, to get numeric labels.
  24. labels: [],
  25. // numericMargin: Integer
  26. // Number of generated numeric labels that should be rendered as '' on the ends when labels[] are not specified
  27. numericMargin: 0,
  28. // numericMinimum: Integer
  29. // Leftmost label value for generated numeric labels when labels[] are not specified
  30. minimum: 0,
  31. // numericMaximum: Integer
  32. // Rightmost label value for generated numeric labels when labels[] are not specified
  33. maximum: 1,
  34. // constraints: Object
  35. // pattern, places, lang, et al (see dojo.number) for generated numeric labels when labels[] are not specified
  36. constraints: {pattern:"#%"},
  37. _positionPrefix: '<div class="dijitRuleLabelContainer dijitRuleLabelContainerH" style="left:',
  38. _labelPrefix: '"><div class="dijitRuleLabel dijitRuleLabelH">',
  39. _suffix: '</div></div>',
  40. _calcPosition: function(pos){
  41. // summary:
  42. // Returns the value to be used in HTML for the label as part of the left: attribute
  43. // tags:
  44. // protected extension
  45. return pos;
  46. },
  47. _genHTML: function(pos, ndx){
  48. return this._positionPrefix + this._calcPosition(pos) + this._positionSuffix + this.labelStyle + this._labelPrefix + this.labels[ndx] + this._suffix;
  49. },
  50. getLabels: function(){
  51. // summary:
  52. // Overridable function to return array of labels to use for this slider.
  53. // Can specify a getLabels() method instead of a labels[] array, or min/max attributes.
  54. // tags:
  55. // protected extension
  56. // if the labels array was not specified directly, then see if <li> children were
  57. var labels = this.labels;
  58. if(!labels.length){
  59. // for markup creation, labels are specified as child elements
  60. labels = query("> li", this.srcNodeRef).map(function(node){
  61. return String(node.innerHTML);
  62. });
  63. }
  64. this.srcNodeRef.innerHTML = '';
  65. // if the labels were not specified directly and not as <li> children, then calculate numeric labels
  66. if(!labels.length && this.count > 1){
  67. var start = this.minimum;
  68. var inc = (this.maximum - start) / (this.count-1);
  69. for(var i=0; i < this.count; i++){
  70. labels.push((i < this.numericMargin || i >= (this.count-this.numericMargin)) ? '' : number.format(start, this.constraints));
  71. start += inc;
  72. }
  73. }
  74. return labels;
  75. },
  76. postMixInProperties: function(){
  77. this.inherited(arguments);
  78. this.labels = this.getLabels();
  79. this.count = this.labels.length;
  80. }
  81. });
  82. });