summaryrefslogtreecommitdiff
path: root/js/dojo/dojox/charting/widget/BidiSupport.js
blob: 9dc32489186597469b88e942d5f532baf1a8dda3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//>>built
define("dojox/charting/widget/BidiSupport", ["dojo/dom", "dojo/_base/lang", "dojo/_base/html", "dojo/_base/array",  "dojo/_base/connect", "dojo/query",
	"dijit/_BidiSupport", "../BidiSupport", "dijit/registry", "./Chart", "./Legend"], 
	function(dom, lang, html, arrayUtil, hub, query, dBidi, cBidi, widgetManager, Chart, Legend){

	// patch only if present
	if( Legend ){
		lang.extend(Legend, {
			// summary:
			//		Add support for bidi scripts in legend.
			// description:
			//		Since dojox.charting.widget.Legend inherits from _Widget use the bidi support
			//		that introduced there.

			postMixInProperties: function(){
				// summary:
				//		Connect the setter of textDir legend to setTextDir of the chart,
				//		so _setTextDirAttr of the legend will be called after setTextDir of the chart is called.
				// tags:
				//		private

				// find the chart that is the owner of this legend, use it's 
				// textDir
				if(!this.chart){
					if(!this.chartRef){ return; }
					var chart = widgetManager.byId(this.chartRef);
					if(!chart){
						var node = dom.byId(this.chartRef);
						if(node){
							chart = widgetManager.byNode(node);
						}else{
							return;
						}
					}
					this.textDir = chart.chart.textDir;
					hub.connect(chart.chart, "setTextDir", this, "_setTextDirAttr");

				}else{
					this.textDir = this.chart.textDir;
					hub.connect(this.chart, "setTextDir", this, "_setTextDirAttr");

				}
			},

			_setTextDirAttr: function(/*String*/ textDir){
				// summary:
				//		Setter for textDir. 
				// description:
				//		Users shouldn't call this function; they should be calling
				//		set('textDir', value)
				// tags:
				//		private
				
				// only if new textDir is different from the old one
				if(validateTextDir(textDir) != null){
					if(this.textDir != textDir){
						this._set("textDir", textDir);
						// get array of all the labels
						var legendLabels = query(".dojoxLegendText", this._tr);
							// for every label calculate it's new dir.
							arrayUtil.forEach(legendLabels, function(label){
								label.dir = this.getTextDir(label.innerHTML, label.dir);
						}, this);					
					}
				}
			}
		});
	}
	
	// patch only if present
	if( Chart ){
		lang.extend( Chart ,{
			postMixInProperties: function(){
				// set initial textDir of the chart, if passed in the creation use that value
				// else use default value, following the GUI direction, this.chart doesn't exist yet
				// so can't use set("textDir", textDir). This passed to this.chart in it's future creation.
				this.textDir = this.params["textDir"] ? this.params["textDir"] : this.params["dir"];
			},

			_setTextDirAttr: function(/*String*/ textDir){
				if(validateTextDir(textDir) != null){
					this._set("textDir", textDir);
					this.chart.setTextDir(textDir);
				}
			}
		});
	}

	function validateTextDir(textDir){
		return /^(ltr|rtl|auto)$/.test(textDir) ? textDir : null;
	}
		
});