[483] | 1 | define(["exports", "dojo/_base/lang", "../util/oo", "../stencil/Text"], |
---|
| 2 | function(exports, lang, oo, Text){ |
---|
| 3 | |
---|
| 4 | // TODO: why not just return Label? |
---|
| 5 | |
---|
| 6 | exports.Label = oo.declare( |
---|
| 7 | Text, |
---|
| 8 | function(/*Object*/options){ |
---|
| 9 | // options: Object |
---|
| 10 | // One key value: the stencil that called this. |
---|
| 11 | |
---|
| 12 | this.master = options.stencil; |
---|
| 13 | this.labelPosition = options.labelPosition || "BR"; // TL, TR, BR, BL, or function |
---|
| 14 | if(lang.isFunction(this.labelPosition)){ |
---|
| 15 | this.setLabel = this.setLabelCustom; |
---|
| 16 | } |
---|
| 17 | this.setLabel(options.text || ""); |
---|
| 18 | this.connect(this.master, "onTransform", this, "setLabel"); |
---|
| 19 | this.connect(this.master, "destroy", this, "destroy"); |
---|
| 20 | |
---|
| 21 | if(this.style.labelSameColor){ |
---|
| 22 | this.connect(this.master, "attr", this, "beforeAttr"); |
---|
| 23 | } |
---|
| 24 | },{ |
---|
| 25 | // summary: |
---|
| 26 | // An annotation called internally to label an Stencil. |
---|
| 27 | // description: |
---|
| 28 | // Annotation is positioned with dojox.drawing.util.positioning.label |
---|
| 29 | // That method should be overwritten for custom placement. Or, |
---|
| 30 | // add a 'setLabelCustom' method to the Stencil and it will be used. |
---|
| 31 | |
---|
| 32 | _align:"start", |
---|
| 33 | drawingType:"label", |
---|
| 34 | |
---|
| 35 | setLabelCustom: function(/* ? String */text){ |
---|
| 36 | // summary: |
---|
| 37 | // Attaches to custom positioning within a Stencil |
---|
| 38 | |
---|
| 39 | var d = lang.hitch(this.master, this.labelPosition)(); |
---|
| 40 | this.setData({ |
---|
| 41 | x:d.x, |
---|
| 42 | y:d.y, |
---|
| 43 | width:d.w || this.style.text.minWidth, |
---|
| 44 | height:d.h || this._lineHeight |
---|
| 45 | }); |
---|
| 46 | |
---|
| 47 | // is an event, not text, so keep the old label: |
---|
| 48 | if(text && !text.split){ text = this.getText(); } |
---|
| 49 | |
---|
| 50 | this.render(this.typesetter(text)); |
---|
| 51 | }, |
---|
| 52 | |
---|
| 53 | setLabel: function(/* String */text){ |
---|
| 54 | // summary: |
---|
| 55 | // Sets the text of the label. Not called directly. Should |
---|
| 56 | // be called within Stencil. See stencil._Base |
---|
| 57 | |
---|
| 58 | // onTransform will pass an object here |
---|
| 59 | var x, y, box = this.master.getBounds(); |
---|
| 60 | |
---|
| 61 | if(/B/.test(this.labelPosition)){ |
---|
| 62 | y = box.y2 - this._lineHeight; |
---|
| 63 | }else{ |
---|
| 64 | y = box.y1; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | if(/R/.test(this.labelPosition)){ |
---|
| 68 | x = box.x2; |
---|
| 69 | }else{ |
---|
| 70 | y = box.y1; |
---|
| 71 | this._align = "end"; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | if(!this.labelWidth || (text && text.split && text != this.getText())){ |
---|
| 75 | this.setData({ |
---|
| 76 | x:x, |
---|
| 77 | y:y, |
---|
| 78 | height:this._lineHeight, |
---|
| 79 | width:this.style.text.minWidth |
---|
| 80 | }); |
---|
| 81 | |
---|
| 82 | this.labelWidth = this.style.text.minWidth; |
---|
| 83 | this.render(this.typesetter(text)); |
---|
| 84 | |
---|
| 85 | }else{ |
---|
| 86 | |
---|
| 87 | this.setData({ |
---|
| 88 | x:x, |
---|
| 89 | y:y, |
---|
| 90 | height:this.data.height, |
---|
| 91 | width:this.data.width |
---|
| 92 | }); |
---|
| 93 | |
---|
| 94 | this.render(); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | }, |
---|
| 98 | beforeAttr: function(key, value){ |
---|
| 99 | if(value!==undefined){ |
---|
| 100 | // make it an object |
---|
| 101 | var k = key; key = {}; key[k] = value; |
---|
| 102 | } |
---|
| 103 | delete key.x; |
---|
| 104 | delete key.y; |
---|
| 105 | delete key.width; |
---|
| 106 | delete key.height; |
---|
| 107 | this.attr(key); |
---|
| 108 | // FIXME: this.created should already be set, shouldn't it? |
---|
| 109 | !this.created && this.render(); |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | ); |
---|
| 114 | }); |
---|