HorizontalRuleLabels.js 3.3 KB

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