Print.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. define("dijit/_editor/plugins/Print", [
  2. "dojo/_base/declare", // declare
  3. "dojo/i18n", // i18n.getLocalization
  4. "dojo/_base/lang", // lang.hitch
  5. "dojo/_base/sniff", // has("chrome") has("opera")
  6. "../../focus", // focus.focus()
  7. "../_Plugin",
  8. "../../form/Button",
  9. "dojo/i18n!../nls/commands"
  10. ], function(declare, i18n, lang, has, focus, _Plugin, Button){
  11. /*=====
  12. var _Plugin = dijit._editor._Plugin;
  13. =====*/
  14. // module:
  15. // dijit/_editor/plugins/Print
  16. // summary:
  17. // This plugin provides Print capability to the editor. When
  18. // clicked, the document in the editor frame will be printed.
  19. var Print = declare("dijit._editor.plugins.Print",_Plugin,{
  20. // summary:
  21. // This plugin provides Print capability to the editor. When
  22. // clicked, the document in the editor frame will be printed.
  23. _initButton: function(){
  24. // summary:
  25. // Over-ride for creation of the Print button.
  26. var strings = i18n.getLocalization("dijit._editor", "commands"),
  27. editor = this.editor;
  28. this.button = new Button({
  29. label: strings["print"],
  30. dir: editor.dir,
  31. lang: editor.lang,
  32. showLabel: false,
  33. iconClass: this.iconClassPrefix + " " + this.iconClassPrefix + "Print",
  34. tabIndex: "-1",
  35. onClick: lang.hitch(this, "_print")
  36. });
  37. },
  38. setEditor: function(/*dijit.Editor*/ editor){
  39. // summary:
  40. // Tell the plugin which Editor it is associated with.
  41. // editor: Object
  42. // The editor object to attach the print capability to.
  43. this.editor = editor;
  44. this._initButton();
  45. // Set up a check that we have a print function
  46. // and disable button if we do not.
  47. this.editor.onLoadDeferred.addCallback(
  48. lang.hitch(this, function(){
  49. if(!this.editor.iframe.contentWindow["print"]){
  50. this.button.set("disabled", true);
  51. }
  52. })
  53. );
  54. },
  55. updateState: function(){
  56. // summary:
  57. // Over-ride for button state control for disabled to work.
  58. var disabled = this.get("disabled");
  59. if(!this.editor.iframe.contentWindow["print"]){
  60. disabled = true;
  61. }
  62. this.button.set("disabled", disabled);
  63. },
  64. _print: function(){
  65. // summary:
  66. // Function to trigger printing of the editor document
  67. // tags:
  68. // private
  69. var edFrame = this.editor.iframe;
  70. if(edFrame.contentWindow["print"]){
  71. // IE requires the frame to be focused for
  72. // print to work, but since this is okay for all
  73. // no special casing.
  74. if(!has("opera") && !has("chrome")){
  75. focus.focus(edFrame);
  76. edFrame.contentWindow.print();
  77. }else{
  78. // Neither Opera nor Chrome 3 et you print single frames.
  79. // So, open a new 'window', print it, and close it.
  80. // Also, can't use size 0x0, have to use 1x1
  81. var edDoc = this.editor.document;
  82. var content = this.editor.get("value");
  83. content = "<html><head><meta http-equiv='Content-Type' " +
  84. "content='text/html; charset='UTF-8'></head><body>" +
  85. content + "</body></html>";
  86. var win = window.open("javascript: ''",
  87. "",
  88. "status=0,menubar=0,location=0,toolbar=0," +
  89. "width=1,height=1,resizable=0,scrollbars=0");
  90. win.document.open();
  91. win.document.write(content);
  92. win.document.close();
  93. var styleNodes = edDoc.getElementsByTagName("style");
  94. if(styleNodes){
  95. // Clone over any editor view styles, since we can't print the iframe
  96. // directly.
  97. var i;
  98. for(i = 0; i < styleNodes.length; i++){
  99. var style = styleNodes[i].innerHTML;
  100. var sNode = win.document.createElement("style");
  101. sNode.appendChild(win.document.createTextNode(style));
  102. win.document.getElementsByTagName("head")[0].appendChild(sNode);
  103. }
  104. }
  105. win.print();
  106. win.close();
  107. }
  108. }
  109. }
  110. });
  111. // Register this plugin.
  112. _Plugin.registry["print"] = function(){
  113. return new Print({command: "print"});
  114. };
  115. return Print;
  116. });