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