Print.js 3.9 KB

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