Debian Codegraph @

dojox/charting/bidi/Chart.js

raw symbols SCIP
1 define(["dojox/main", "dojo/_base/declare", "dojo/_base/lang", "dojo/dom-style", "dojo/_base/array", "dojo/sniff",
2 "dojo/dom","dojo/dom-construct",
3 "dojox/gfx", "dojox/gfx/_gfxBidiSupport", "../axis2d/common", "dojox/string/BidiEngine",
4 "dojox/lang/functional","dojo/dom-attr","./_bidiutils"],
5 function(dojox, declare, lang, domStyle, arr, has, dom, domConstruct, g, gBidi, da, BidiEngine, df, domAttr,utils){
6 // module:
7 // dojox/charting/bidi/Chart
8 var bidiEngine = new BidiEngine();
9 var dc = lang.getObject("charting", true, dojox);
10 function validateTextDir(textDir){
11 return /^(ltr|rtl|auto)$/.test(textDir) ? textDir : null;
12 };
13
14 return declare(null, {
15 // textDir: String
16 // Bi-directional support, the main variable which is responsible for the direction of the text.
17 // The text direction can be different than the GUI direction by using this parameter.
18 // Allowed values:
19 //
20 // 1. "ltr"
21 // 2. "rtl"
22 // 3. "auto" - contextual the direction of a text defined by first strong letter.
23 //
24 // By default is as the page direction.
25 textDir:"",
26
27 // dir: String
28 // Mirroring support, the main variable which is responsible for the direction of the chart.
29 //
30 // Allowed values:
31 // 1. "ltr"
32 // 2. "rtl"
33 //
34 // By default is ltr.
35 dir: "",
36 isMirrored: false,
37
38 getTextDir: function(text){
39 // summary:
40 // Return direction of the text.
41 // description:
42 // If textDir is ltr or rtl returns the value.
43 // If it's auto, calls to another function that responsible
44 // for checking the value, and defining the direction.
45 // text:
46 // Used in case textDir is "auto", this case the direction is according to the first
47 // strong (directionally - which direction is strong defined) letter.
48 // tags:
49 // protected.
50 var textDir = this.textDir == "auto" ? bidiEngine.checkContextual(text) : this.textDir;
51 // providing default value
52 if(!textDir){
53 textDir = domStyle.get(this.node, "direction");
54 }
55 return textDir;
56 },
57
58 postscript: function(node,args){
59 // summary:
60 // Kicks off chart instantiation.
61 // description:
62 // Used for setting the textDir of the chart.
63 // tags:
64 // private
65
66 // validate textDir
67 var textDir = args ? (args["textDir"] ? validateTextDir(args["textDir"]) : "") : "";
68 // if textDir wasn't defined or was defined wrong, apply default value
69 textDir = textDir ? textDir : domStyle.get(this.node, "direction");
70 this.textDir = textDir;
71
72 this.surface.textDir = textDir;
73
74 // two data structures, used for storing data for further enablement to change
75 // textDir dynamically
76 this.htmlElementsRegistry = [];
77 this.truncatedLabelsRegistry = [];
78 // chart mirroring starts
79 var chartDir = "ltr";
80 if(domAttr.has(node, "direction")){
81 chartDir = domAttr.get(node, "direction");
82 }
83 this.setDir(args ? (args.dir ? args.dir: chartDir) : chartDir);
84 // chart mirroring ends
85 },
86
87 setTextDir: function(newTextDir, obj){
88 // summary:
89 // Setter for the textDir attribute.
90 // description:
91 // Allows dynamically set the textDir, goes over all the text-children and
92 // updates their base text direction.
93 // tags:
94 // public
95
96 if(newTextDir == this.textDir){
97 return this;
98 }
99 if(validateTextDir(newTextDir) != null){
100 this.textDir = newTextDir;
101
102 // set automatically all the gfx objects that were created by this surface
103 // (groups, text objects)
104 this.surface.setTextDir(newTextDir);
105
106 // truncated labels that were created with gfx creator need to recalculate dir
107 // for case like: "111111A" (A stands for bidi character) and the truncation
108 // is "111..." If the textDir is auto, the display should be: "...111" but in gfx
109 // case we will get "111...". Because this.surface.setTextDir will calculate the dir of truncated
110 // label, which value is "111..." but th real is "111111A".
111 // each time we created a gfx truncated label we stored it in the truncatedLabelsRegistry, so update now
112 // the registry.
113 if(this.truncatedLabelsRegistry && newTextDir == "auto"){
114 arr.forEach(this.truncatedLabelsRegistry, function(elem){
115 var tDir = this.getTextDir(elem["label"]);
116 if(elem["element"].textDir != tDir){
117 elem["element"].setShape({textDir: tDir});
118 }
119 }, this);
120 }
121
122 // re-render axes with html labels. for recalculation of the labels
123 // positions etc.
124 // create array of keys for all the axis in chart
125 var axesKeyArr = df.keys(this.axes);
126 if(axesKeyArr.length > 0){
127 // iterate over the axes, and for each that have html labels render it.
128 arr.forEach(axesKeyArr, function(key, index, arr){
129 // get the axis
130 var axis = this.axes[key];
131 // if the axis has html labels
132 if(axis.htmlElements[0]){
133 axis.dirty = true;
134 axis.render(this.dim, this.offsets);
135 }
136 },this);
137
138 // recreate title
139 if(this.title){
140 this._renderTitle(this.dim, this.offsets);
141 }
142 }else{
143 // case of pies, spiders etc.
144 arr.forEach(this.htmlElementsRegistry, function(elem, index, arr){
145 var tDir = newTextDir == "auto" ? this.getTextDir(elem[4]) : newTextDir;
146 if(elem[0].children[0] && elem[0].children[0].dir != tDir){
147 domConstruct.destroy(elem[0].children[0]);
148 elem[0].children[0] = da.createText["html"]
149 (this, this.surface, elem[1], elem[2], elem[3], elem[4], elem[5], elem[6]).children[0];
150 }
151 },this);
152 }
153 }
154 return this;
155 },
156
157 setDir : function(dir){
158 // summary:
159 // Setter for the dir attribute.
160 // description:
161 // Allows dynamically set the dri attribute, which will used to
162 // updates the chart rendering direction.
163 // dir : the desired chart direction [rtl: for right to left ,ltr: for left to right]
164
165 if(dir == "rtl" || dir == "ltr"){
166 if(this.dir != dir){
167 this.isMirrored = true;
168 this.dirty = true;
169 }
170 this.dir = dir;
171 }
172 return this;
173 },
174
175 isRightToLeft: function(){
176 // summary:
177 // check the direction of the chart.
178 // description:
179 // check the dir attribute to determine the rendering direction
180 // of the chart.
181 return this.dir == "rtl";
182 },
183
184 applyMirroring: function(plot, dim, offsets){
185 // summary:
186 // apply the mirroring operation to the current chart plots.
187 //
188 utils.reverseMatrix(plot, dim, offsets, this.dir == "rtl");
189 //force the direction of the node to be ltr to properly render the axes and the plots labels.
190 domStyle.set(this.node, "direction", "ltr");
191 return this;
192 },
193
194 formatTruncatedLabel: function(element, label, labelType){
195 this.truncateBidi(element, label, labelType);
196 },
197
198 truncateBidi: function(elem, label, labelType){
199 // summary:
200 // Enables bidi support for truncated labels.
201 // description:
202 // Can be two types of labels: html or gfx.
203 //
204 // ####gfx labels:
205 //
206 // Need to be stored in registry to be used when the textDir will be set dynamically.
207 // Additional work on truncated labels is needed for case as 111111A (A stands for "bidi" character rtl directioned).
208 // let's say in this case the truncation is "111..." If the textDir is auto, the display should be: "...111" but in gfx
209 // case we will get "111...". Because this.surface.setTextDir will calculate the dir of truncated
210 // label, which value is "111..." but th real is "111111A".
211 // each time we created a gfx truncated label we store it in the truncatedLabelsRegistry.
212 //
213 // ####html labels:
214 //
215 // no need for repository (stored in another place). Here we only need to update the current dir according to textDir.
216 // tags:
217 // private
218
219 if(labelType == "gfx"){
220 // store truncated gfx labels in the data structure.
221 this.truncatedLabelsRegistry.push({element: elem, label: label});
222 if(this.textDir == "auto"){
223 elem.setShape({textDir: this.getTextDir(label)});
224 }
225 }
226 if(labelType == "html" && this.textDir == "auto"){
227 elem.children[0].dir = this.getTextDir(label);
228 }
229 },
230
231 render: function(){
232 this.inherited(arguments);
233 this.isMirrored = false;
234 return this;
235 },
236
237 _resetLeftBottom: function(axis){
238 if(axis.vertical && this.isMirrored){
239 axis.opt.leftBottom = !axis.opt.leftBottom;
240 }
241 }
242 });
243 });
244