ToggleDir.js.uncompressed.js 2.3 KB

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