ToggleDir.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dijit._editor.plugins.ToggleDir"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dijit._editor.plugins.ToggleDir"] = true;
  8. dojo.provide("dijit._editor.plugins.ToggleDir");
  9. dojo.require("dijit._editor._Plugin");
  10. dojo.require("dijit.form.ToggleButton");
  11. dojo.experimental("dijit._editor.plugins.ToggleDir");
  12. dojo.require("dijit._editor._Plugin");
  13. dojo.require("dijit.form.ToggleButton");
  14. dojo.declare("dijit._editor.plugins.ToggleDir",
  15. dijit._editor._Plugin,
  16. {
  17. // summary:
  18. // This plugin is used to toggle direction of the edited document,
  19. // independent of what direction the whole page is.
  20. // Override _Plugin.useDefaultCommand: processing is done in this plugin
  21. // rather than by sending commands to the Editor
  22. useDefaultCommand: false,
  23. command: "toggleDir",
  24. // Override _Plugin.buttonClass to use a ToggleButton for this plugin rather than a vanilla Button
  25. buttonClass: dijit.form.ToggleButton,
  26. _initButton: function(){
  27. // Override _Plugin._initButton() to setup handler for button click events.
  28. this.inherited(arguments);
  29. this.editor.onLoadDeferred.addCallback(dojo.hitch(this, function(){
  30. var editDoc = this.editor.editorObject.contentWindow.document.documentElement;
  31. //IE direction has to toggle on the body, not document itself.
  32. //If you toggle just the document, things get very strange in the
  33. //view. But, the nice thing is this works for all supported browsers.
  34. editDoc = editDoc.getElementsByTagName("body")[0];
  35. var isLtr = dojo.getComputedStyle(editDoc).direction == "ltr";
  36. this.button.set("checked", !isLtr);
  37. this.connect(this.button, "onChange", "_setRtl");
  38. }));
  39. },
  40. updateState: function(){
  41. // summary:
  42. // Over-ride for button state control for disabled to work.
  43. this.button.set("disabled", this.get("disabled"));
  44. },
  45. _setRtl: function(rtl){
  46. // summary:
  47. // Handler for button click events, to switch the text direction of the editor
  48. var dir = "ltr";
  49. if(rtl){
  50. dir = "rtl";
  51. }
  52. var editDoc = this.editor.editorObject.contentWindow.document.documentElement;
  53. editDoc = editDoc.getElementsByTagName("body")[0];
  54. editDoc.dir/*html node*/ = dir;
  55. }
  56. }
  57. );
  58. // Register this plugin.
  59. dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
  60. if(o.plugin){ return; }
  61. switch(o.args.name){
  62. case "toggleDir":
  63. o.plugin = new dijit._editor.plugins.ToggleDir({command: o.args.name});
  64. }
  65. });
  66. }