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()
9 "dojo/i18n!../nls/commands"
10 ], function(declare, i18n, lang, has, focus, _Plugin, Button){
13 var _Plugin = dijit._editor._Plugin;
17 // dijit/_editor/plugins/Print
19 // This plugin provides Print capability to the editor. When
20 // clicked, the document in the editor frame will be printed.
23 var Print = declare("dijit._editor.plugins.Print",_Plugin,{
25 // This plugin provides Print capability to the editor. When
26 // clicked, the document in the editor frame will be printed.
28 _initButton: function(){
30 // Over-ride for creation of the Print button.
31 var strings = i18n.getLocalization("dijit._editor", "commands"),
33 this.button = new Button({
34 label: strings["print"],
38 iconClass: this.iconClassPrefix + " " + this.iconClassPrefix + "Print",
40 onClick: lang.hitch(this, "_print")
44 setEditor: function(/*dijit.Editor*/ editor){
46 // Tell the plugin which Editor it is associated with.
48 // The editor object to attach the print capability to.
52 // Set up a check that we have a print function
53 // and disable button if we do not.
54 this.editor.onLoadDeferred.addCallback(
55 lang.hitch(this, function(){
56 if(!this.editor.iframe.contentWindow["print"]){
57 this.button.set("disabled", true);
63 updateState: function(){
65 // Over-ride for button state control for disabled to work.
66 var disabled = this.get("disabled");
67 if(!this.editor.iframe.contentWindow["print"]){
70 this.button.set("disabled", disabled);
75 // Function to trigger printing of the editor document
78 var edFrame = this.editor.iframe;
79 if(edFrame.contentWindow["print"]){
80 // IE requires the frame to be focused for
81 // print to work, but since this is okay for all
83 if(!has("opera") && !has("chrome")){
85 edFrame.contentWindow.print();
87 // Neither Opera nor Chrome 3 et you print single frames.
88 // So, open a new 'window', print it, and close it.
89 // Also, can't use size 0x0, have to use 1x1
90 var edDoc = this.editor.document;
91 var content = this.editor.get("value");
92 content = "<html><head><meta http-equiv='Content-Type' " +
93 "content='text/html; charset='UTF-8'></head><body>" +
94 content + "</body></html>";
95 var win = window.open("javascript: ''",
97 "status=0,menubar=0,location=0,toolbar=0," +
98 "width=1,height=1,resizable=0,scrollbars=0");
100 win.document.write(content);
101 win.document.close();
103 var styleNodes = edDoc.getElementsByTagName("style");
105 // Clone over any editor view styles, since we can't print the iframe
108 for(i = 0; i < styleNodes.length; i++){
109 var style = styleNodes[i].innerHTML;
110 var sNode = win.document.createElement("style");
111 sNode.appendChild(win.document.createTextNode(style));
112 win.document.getElementsByTagName("head")[0].appendChild(sNode);
122 // Register this plugin.
123 _Plugin.registry["print"] = function(){
124 return new Print({command: "print"});