| 1 | define([], function(){ |
| 2 | |
| 3 | // module: |
| 4 | // dijit/_BidiMixin |
| 5 | |
| 6 | // UCC - constants that will be used by bidi support. |
| 7 | var bidi_const = { |
| 8 | LRM : '\u200E', |
| 9 | LRE : '\u202A', |
| 10 | PDF : '\u202C', |
| 11 | RLM : '\u200f', |
| 12 | RLE : '\u202B' |
| 13 | }; |
| 14 | |
| 15 | return { |
| 16 | // summary: |
| 17 | // When has("dojo-bidi") is true, _WidgetBase will mixin this class. It enables support for the textdir |
| 18 | // property to control text direction independently from the GUI direction. |
| 19 | // description: |
| 20 | // There's a special need for displaying BIDI text in rtl direction |
| 21 | // in ltr GUI, sometimes needed auto support. |
| 22 | // In creation of widget, if it's want to activate this class, |
| 23 | // the widget should define the "textDir". |
| 24 | |
| 25 | getTextDir: function(/*String*/ text){ |
| 26 | // summary: |
| 27 | // Gets the right direction of text. |
| 28 | // description: |
| 29 | // If textDir is ltr or rtl returns the value. |
| 30 | // If it's auto, calls to another function that responsible |
| 31 | // for checking the value, and defining the direction. |
| 32 | // tags: |
| 33 | // protected. |
| 34 | return this.textDir == "auto" ? this._checkContextual(text) : this.textDir; |
| 35 | }, |
| 36 | |
| 37 | _checkContextual: function(text){ |
| 38 | // summary: |
| 39 | // Finds the first strong (directional) character, return ltr if isLatin |
| 40 | // or rtl if isBidiChar. |
| 41 | // tags: |
| 42 | // private. |
| 43 | |
| 44 | // look for strong (directional) characters |
| 45 | var fdc = /[A-Za-z\u05d0-\u065f\u066a-\u06ef\u06fa-\u07ff\ufb1d-\ufdff\ufe70-\ufefc]/.exec(text); |
| 46 | // if found return the direction that defined by the character, else return widgets dir as defult. |
| 47 | return fdc ? ( fdc[0] <= 'z' ? "ltr" : "rtl" ) : this.dir ? this.dir : this.isLeftToRight() ? "ltr" : "rtl"; |
| 48 | }, |
| 49 | |
| 50 | applyTextDir: function(/*DOMNode*/ element, /*String?*/ text){ |
| 51 | // summary: |
| 52 | // Set element.dir according to this.textDir, assuming this.textDir has a value. |
| 53 | // element: |
| 54 | // The text element to be set. Should have dir property. |
| 55 | // text: |
| 56 | // If specified, and this.textDir is "auto", for calculating the right transformation |
| 57 | // Otherwise text read from element. |
| 58 | // description: |
| 59 | // If textDir is ltr or rtl returns the value. |
| 60 | // If it's auto, calls to another function that responsible |
| 61 | // for checking the value, and defining the direction. |
| 62 | // tags: |
| 63 | // protected. |
| 64 | |
| 65 | if(this.textDir){ |
| 66 | var textDir = this.textDir; |
| 67 | if(textDir == "auto"){ |
| 68 | // convert "auto" to either "ltr" or "rtl" |
| 69 | if(typeof text === "undefined"){ |
| 70 | // text not specified, get text from element |
| 71 | var tagName = element.tagName.toLowerCase(); |
| 72 | text = (tagName == "input" || tagName == "textarea") ? element.value : |
| 73 | element.innerText || element.textContent || ""; |
| 74 | } |
| 75 | textDir = this._checkContextual(text); |
| 76 | } |
| 77 | |
| 78 | if(element.dir != textDir){ |
| 79 | // set element's dir to match textDir, but not when textDir is null and not when it already matches |
| 80 | element.dir = textDir; |
| 81 | } |
| 82 | } |
| 83 | }, |
| 84 | |
| 85 | enforceTextDirWithUcc: function(option, text){ |
| 86 | // summary: |
| 87 | // Wraps by UCC (Unicode control characters) option's text according to this.textDir |
| 88 | // option: |
| 89 | // The element (`<option>`) we wrapping the text for. |
| 90 | // text: |
| 91 | // The text to be wrapped. |
| 92 | // description: |
| 93 | // There's a dir problem with some HTML elements. For some elements (e.g. `<option>`, `<select>`) |
| 94 | // defining the dir in different direction then the GUI orientation, won't display correctly. |
| 95 | // FF 3.6 will change the alignment of the text in option - this doesn't follow the bidi standards (static text |
| 96 | // should be aligned following GUI direction). IE8 and Opera11.10 completely ignore dir setting for `<option>`. |
| 97 | // Therefore the only solution is to use UCC (Unicode control characters) to display the text in correct orientation. |
| 98 | // This function saves the original text value for later restoration if needed, for example if the textDir will change etc. |
| 99 | if(this.textDir){ |
| 100 | if(option){ |
| 101 | option.originalText = text; |
| 102 | } |
| 103 | var dir = this.textDir == "auto" ? this._checkContextual(text) : this.textDir; |
| 104 | return (dir == "ltr" ? bidi_const.LRE : bidi_const.RLE ) + text + bidi_const.PDF; |
| 105 | } |
| 106 | return text; |
| 107 | }, |
| 108 | |
| 109 | restoreOriginalText: function(origObj){ |
| 110 | // summary: |
| 111 | // Restores the text of origObj, if needed, after enforceTextDirWithUcc, e.g. set("textDir", textDir). |
| 112 | // origObj: |
| 113 | // The element (`<option>`) to restore. |
| 114 | // description: |
| 115 | // Sets the text of origObj to origObj.originalText, which is the original text, without the UCCs. |
| 116 | // The function than removes the originalText from origObj! |
| 117 | if(origObj.originalText){ |
| 118 | origObj.text = origObj.originalText; |
| 119 | delete origObj.originalText; |
| 120 | } |
| 121 | return origObj; |
| 122 | }, |
| 123 | |
| 124 | _setTextDirAttr: function(/*String*/ textDir){ |
| 125 | // summary: |
| 126 | // Setter for textDir. |
| 127 | // description: |
| 128 | // Users shouldn't call this function; they should be calling |
| 129 | // set('textDir', value) |
| 130 | if(!this._created || this.textDir != textDir){ |
| 131 | this._set("textDir", textDir); |
| 132 | var node = null; |
| 133 | if(this.displayNode){ |
| 134 | node = this.displayNode; |
| 135 | this.displayNode.align = this.dir == "rtl" ? "right" : "left"; |
| 136 | }else{ |
| 137 | node = this.textDirNode || this.focusNode || this.textbox; |
| 138 | } |
| 139 | if(node){ |
| 140 | this.applyTextDir(node); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | }; |
| 145 | }); |