]>
Commit | Line | Data |
---|---|---|
1354d172 AD |
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 | /*===== | |
13 | var _Plugin = dijit._editor._Plugin; | |
14 | =====*/ | |
15 | ||
16 | // module: | |
17 | // dijit/_editor/plugins/Print | |
18 | // summary: | |
19 | // This plugin provides Print capability to the editor. When | |
20 | // clicked, the document in the editor frame will be printed. | |
21 | ||
22 | ||
23 | var Print = declare("dijit._editor.plugins.Print",_Plugin,{ | |
24 | // summary: | |
25 | // This plugin provides Print capability to the editor. When | |
26 | // clicked, the document in the editor frame will be printed. | |
27 | ||
28 | _initButton: function(){ | |
29 | // summary: | |
30 | // Over-ride for creation of the Print button. | |
31 | var strings = i18n.getLocalization("dijit._editor", "commands"), | |
32 | editor = this.editor; | |
33 | this.button = new Button({ | |
34 | label: strings["print"], | |
35 | dir: editor.dir, | |
36 | lang: editor.lang, | |
37 | showLabel: false, | |
38 | iconClass: this.iconClassPrefix + " " + this.iconClassPrefix + "Print", | |
39 | tabIndex: "-1", | |
40 | onClick: lang.hitch(this, "_print") | |
41 | }); | |
42 | }, | |
43 | ||
44 | setEditor: function(/*dijit.Editor*/ editor){ | |
45 | // summary: | |
46 | // Tell the plugin which Editor it is associated with. | |
47 | // editor: Object | |
48 | // The editor object to attach the print capability to. | |
49 | this.editor = editor; | |
50 | this._initButton(); | |
51 | ||
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); | |
58 | } | |
59 | }) | |
60 | ); | |
61 | }, | |
62 | ||
63 | updateState: function(){ | |
64 | // summary: | |
65 | // Over-ride for button state control for disabled to work. | |
66 | var disabled = this.get("disabled"); | |
67 | if(!this.editor.iframe.contentWindow["print"]){ | |
68 | disabled = true; | |
69 | } | |
70 | this.button.set("disabled", disabled); | |
71 | }, | |
72 | ||
73 | _print: function(){ | |
74 | // summary: | |
75 | // Function to trigger printing of the editor document | |
76 | // tags: | |
77 | // private | |
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 | |
82 | // no special casing. | |
83 | if(!has("opera") && !has("chrome")){ | |
84 | focus.focus(edFrame); | |
85 | edFrame.contentWindow.print(); | |
86 | }else{ | |
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: ''", | |
96 | "", | |
97 | "status=0,menubar=0,location=0,toolbar=0," + | |
98 | "width=1,height=1,resizable=0,scrollbars=0"); | |
99 | win.document.open(); | |
100 | win.document.write(content); | |
101 | win.document.close(); | |
102 | ||
103 | var styleNodes = edDoc.getElementsByTagName("style"); | |
104 | if(styleNodes){ | |
105 | // Clone over any editor view styles, since we can't print the iframe | |
106 | // directly. | |
107 | var i; | |
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); | |
113 | } | |
114 | } | |
115 | win.print(); | |
116 | win.close(); | |
117 | } | |
118 | } | |
119 | } | |
120 | }); | |
121 | ||
122 | // Register this plugin. | |
123 | _Plugin.registry["print"] = function(){ | |
124 | return new Print({command: "print"}); | |
125 | }; | |
126 | ||
127 | ||
128 | return Print; | |
129 | }); |