_BidiSupport.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. define("dijit/_BidiSupport", ["./_WidgetBase"], function(_WidgetBase){
  2. /*=====
  3. var _WidgetBase = dijit._WidgetBase;
  4. ====*/
  5. // module:
  6. // dijit/_BidiSupport
  7. // summary:
  8. // Module that deals with BIDI, special with the auto
  9. // direction if needed without changing the GUI direction.
  10. // Including this module will extend _WidgetBase with BIDI related methods.
  11. // description:
  12. // There's a special need for displaying BIDI text in rtl direction
  13. // in ltr GUI, sometimes needed auto support.
  14. // In creation of widget, if it's want to activate this class,
  15. // the widget should define the "textDir".
  16. _WidgetBase.extend({
  17. getTextDir: function(/*String*/ text){
  18. // summary:
  19. // Gets the right direction of text.
  20. // description:
  21. // If textDir is ltr or rtl returns the value.
  22. // If it's auto, calls to another function that responsible
  23. // for checking the value, and defining the direction.
  24. // tags:
  25. // protected.
  26. return this.textDir == "auto" ? this._checkContextual(text) : this.textDir;
  27. },
  28. _checkContextual: function(text){
  29. // summary:
  30. // Finds the first strong (directional) character, return ltr if isLatin
  31. // or rtl if isBidiChar.
  32. // tags:
  33. // private.
  34. // look for strong (directional) characters
  35. var fdc = /[A-Za-z\u05d0-\u065f\u066a-\u06ef\u06fa-\u07ff\ufb1d-\ufdff\ufe70-\ufefc]/.exec(text);
  36. // if found return the direction that defined by the character, else return widgets dir as defult.
  37. return fdc ? ( fdc[0] <= 'z' ? "ltr" : "rtl" ) : this.dir ? this.dir : this.isLeftToRight() ? "ltr" : "rtl";
  38. },
  39. applyTextDir: function(/*Object*/ element, /*String*/ text){
  40. // summary:
  41. // Set element.dir according to this.textDir
  42. // element:
  43. // The text element to be set. Should have dir property.
  44. // text:
  45. // Used in case this.textDir is "auto", for calculating the right transformation
  46. // description:
  47. // If textDir is ltr or rtl returns the value.
  48. // If it's auto, calls to another function that responsible
  49. // for checking the value, and defining the direction.
  50. // tags:
  51. // protected.
  52. var textDir = this.textDir == "auto" ? this._checkContextual(text) : this.textDir;
  53. // update only when there's a difference
  54. if(element.dir != textDir){
  55. element.dir = textDir;
  56. }
  57. }
  58. });
  59. return _WidgetBase;
  60. });