source: Dev/trunk/src/client/dijit/_editor/plugins/Print.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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