ToggleDir.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. define("dijit/_editor/plugins/ToggleDir", [
  2. "dojo/_base/declare", // declare
  3. "dojo/dom-style", // domStyle.getComputedStyle
  4. "dojo/_base/kernel", // kernel.experimental
  5. "dojo/_base/lang", // lang.hitch
  6. "../_Plugin",
  7. "../../form/ToggleButton"
  8. ], function(declare, domStyle, kernel, lang, _Plugin, ToggleButton){
  9. /*=====
  10. var _Plugin = dijit._editor._Plugin;
  11. =====*/
  12. // module:
  13. // dijit/_editor/plugins/ToggleDir
  14. // summary:
  15. // This plugin is used to toggle direction of the edited document,
  16. // independent of what direction the whole page is.
  17. kernel.experimental("dijit._editor.plugins.ToggleDir");
  18. var ToggleDir = declare("dijit._editor.plugins.ToggleDir", _Plugin, {
  19. // summary:
  20. // This plugin is used to toggle direction of the edited document,
  21. // independent of what direction the whole page is.
  22. // Override _Plugin.useDefaultCommand: processing is done in this plugin
  23. // rather than by sending commands to the Editor
  24. useDefaultCommand: false,
  25. command: "toggleDir",
  26. // Override _Plugin.buttonClass to use a ToggleButton for this plugin rather than a vanilla Button
  27. buttonClass: ToggleButton,
  28. _initButton: function(){
  29. // Override _Plugin._initButton() to setup handler for button click events.
  30. this.inherited(arguments);
  31. this.editor.onLoadDeferred.addCallback(lang.hitch(this, function(){
  32. var editDoc = this.editor.editorObject.contentWindow.document.documentElement;
  33. //IE direction has to toggle on the body, not document itself.
  34. //If you toggle just the document, things get very strange in the
  35. //view. But, the nice thing is this works for all supported browsers.
  36. editDoc = editDoc.getElementsByTagName("body")[0];
  37. var isLtr = domStyle.getComputedStyle(editDoc).direction == "ltr";
  38. this.button.set("checked", !isLtr);
  39. this.connect(this.button, "onChange", "_setRtl");
  40. }));
  41. },
  42. updateState: function(){
  43. // summary:
  44. // Over-ride for button state control for disabled to work.
  45. this.button.set("disabled", this.get("disabled"));
  46. },
  47. _setRtl: function(rtl){
  48. // summary:
  49. // Handler for button click events, to switch the text direction of the editor
  50. var dir = "ltr";
  51. if(rtl){
  52. dir = "rtl";
  53. }
  54. var editDoc = this.editor.editorObject.contentWindow.document.documentElement;
  55. editDoc = editDoc.getElementsByTagName("body")[0];
  56. editDoc.dir/*html node*/ = dir;
  57. }
  58. });
  59. // Register this plugin.
  60. _Plugin.registry["toggleDir"] = function(){
  61. return new ToggleDir({command: "toggleDir"});
  62. };
  63. return ToggleDir;
  64. });