BidiSupport.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: cclcore
  4. //
  5. // (C) Copyright IBM Corp. 2012, 2013
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. dojo.provide("bidi.dojo.editor.plugins.BidiSupport");
  9. dojo.require("dijit._editor._Plugin");
  10. dojo.declare("bidi.dojo.editor.plugins.BidiSupport",dijit._editor._Plugin,{
  11. // summary:
  12. // This plugin provides 2 toolbar buttons making possible to switch RTE text direction
  13. // between LTR and RTL mode
  14. command: "bidiSupport",
  15. _LTR: 'ltr',
  16. _RTL: 'rtl',
  17. _transText: {},
  18. setEditor: function(/*dijit.Editor*/ editor){
  19. // summary:
  20. // Tell the plugin which Editor it is associated with.
  21. this.editor = editor;
  22. this._initButton();
  23. },
  24. _initButton: function(){
  25. // summary:
  26. // Override _Plugin._initButton(). Creates two buttons, used for
  27. // setting text direction to left-to-right and right-to-left.
  28. this._transText[this._LTR] = this._LTR;
  29. this._transText[this._RTL] = this._RTL;
  30. var bMenu = new dijit.Menu({});
  31. bMenu.addChild(new dijit.MenuItem({label:this._transText[this._LTR], iconClass:this.iconClassPrefix+" "+this.iconClassPrefix + "LeftToRight", onClick:dojo.hitch(this, "_changeDir", this._LTR)}));
  32. bMenu.addChild(new dijit.MenuItem({label:this._transText[this._RTL], iconClass:this.iconClassPrefix+" "+this.iconClassPrefix + "RightToLeft", onClick:dojo.hitch(this, "_changeDir", this._RTL)}));
  33. var direction = dojo.getComputedStyle(this.editor.domNode).direction;
  34. this.button = new dijit.form.DropDownButton(dojo.mixin({
  35. label: this._transText[direction],
  36. lang: this.editor.lang,
  37. dropDown: bMenu,
  38. showLabel: false,
  39. iconClass: this.iconClassPrefix+" "+this.iconClassPrefix + ((direction === this._LTR) ? "LeftToRight" : "RightToLeft")
  40. }, this.params || {}));
  41. this.editor.onLoadDeferred.addCallback(dojo.hitch(this, function(){
  42. var h = dojo.hitch(this,this._toggleDir);
  43. this.editor.addKeyHandler('d', true, false, h);
  44. }));
  45. },
  46. _toggleDir: function(){
  47. // summary:
  48. // Called in result of using Ctrl+Shift+X shortcut.
  49. // Toggles the Editor direction
  50. var dir = this.editor.editorObject.contentWindow.document.documentElement.getElementsByTagName("body")[0].dir;
  51. dir = (dir === this._LTR) ? this._RTL : this._LTR;
  52. this._changeDir(dir);
  53. },
  54. _changeDir: function(dir){
  55. // summary:
  56. // Handler for button click events, to switch the text direction of the editor
  57. if(dir){
  58. var editDoc = this.editor.editorObject.contentWindow.document.documentElement.getElementsByTagName("body")[0];
  59. editDoc.dir = dir;
  60. if(dojo.isIE && editDoc.firstChild) {
  61. editDoc.firstChild.dir = "";
  62. }
  63. var updatedClass = this.iconClassPrefix+" "+this.iconClassPrefix + ((dir === this._LTR)? "LeftToRight" : "RightToLeft");
  64. dojo.attr(this.button,"iconClass",updatedClass);
  65. dojo.attr(this.button,"label",this._transText[dir]);
  66. }
  67. }
  68. });
  69. dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
  70. if(o.plugin){ return; }
  71. if (o.args.name === "bidiSupport") {
  72. o.plugin = new bidi.dojo.editor.plugins.BidiSupport({command: o.args.name});
  73. }
  74. });