Subversion-Projekte lars-tiefland.webanos.zeldi.de

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
4 lars 1
/* @license
2
 * jQuery.print, version 1.3.0
3
 *  (c) Sathvik Ponangi, Doers' Guild
4
 * Licence: CC-By (http://creativecommons.org/licenses/by/3.0/)
5
 *--------------------------------------------------------------------------*/
6
(function ($) {
7
    "use strict";
8
    // A nice closure for our definitions
9
    function getjQueryObject(string) {
10
        // Make string a vaild jQuery thing
11
        var jqObj = $("");
12
        try {
13
            jqObj = $(string)
14
                .clone();
15
        } catch (e) {
16
            jqObj = $("<span />")
17
                .html(string);
18
        }
19
        return jqObj;
20
    }
21
 
22
    function printFrame(frameWindow) {
23
        // Print the selected window/iframe
24
        var def = $.Deferred();
25
        try {
26
            setTimeout(function () {
27
                // Fix for IE : Allow it to render the iframe
28
                frameWindow.focus();
29
                try {
30
                    // Fix for IE11 - printng the whole page instead of the iframe content
31
                    if (!frameWindow.document.execCommand('print', false, null)) {
32
                        // document.execCommand returns false if it failed -http://stackoverflow.com/a/21336448/937891
33
                        frameWindow.print();
34
                    }
35
                } catch (e) {
36
                    frameWindow.print();
37
                }
38
                frameWindow.close();
39
                deferred.resolve();
40
            }, 250);
41
        } catch (err) {
42
            def.reject(err);
43
        }
44
        return def;
45
    }
46
 
47
    function printContentInNewWindow(content) {
48
        // Open a new window and print selected content
49
        var w = window.open();
50
        w.document.write(content);
51
        w.document.close();
52
        return printFrame(w);
53
    }
54
 
55
    function isNode(o) {
56
        /* http://stackoverflow.com/a/384380/937891 */
57
        return !!(typeof Node === "object" ? o instanceof Node : o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string");
58
    }
59
    $.print = $.fn.print = function () {
60
        // Print a given set of elements
61
        var options, $this, self = this;
62
        // console.log("Printing", this, arguments);
63
        if (self instanceof $) {
64
            // Get the node if it is a jQuery object
65
            self = self.get(0);
66
        }
67
        if (isNode(self)) {
68
            // If `this` is a HTML element, i.e. for
69
            // $(selector).print()
70
            $this = $(self);
71
            if (arguments.length > 0) {
72
                options = arguments[0];
73
            }
74
        } else {
75
            if (arguments.length > 0) {
76
                // $.print(selector,options)
77
                $this = $(arguments[0]);
78
                if (isNode($this[0])) {
79
                    if (arguments.length > 1) {
80
                        options = arguments[1];
81
                    }
82
                } else {
83
                    // $.print(options)
84
                    options = arguments[0];
85
                    $this = $("html");
86
                }
87
            } else {
88
                // $.print()
89
                $this = $("html");
90
            }
91
        }
92
        // Default options
93
        var defaults = {
94
            globalStyles: true,
95
            mediaPrint: false,
96
            stylesheet: null,
97
            noPrintSelector: ".no-print",
98
            iframe: true,
99
            append: null,
100
            prepend: null,
101
            manuallyCopyFormValues: true,
102
            deferred: $.Deferred()
103
        };
104
        // Merge with user-options
105
        options = $.extend({}, defaults, (options || {}));
106
        var $styles = $("");
107
        if (options.globalStyles) {
108
            // Apply the stlyes from the current sheet to the printed page
109
            $styles = $("style, link, meta, title");
110
        } else if (options.mediaPrint) {
111
            // Apply the media-print stylesheet
112
            $styles = $("link[media=print]");
113
        }
114
        if (options.stylesheet) {
115
            // Add a custom stylesheet if given
116
            $styles = $.merge($styles, $('<link rel="stylesheet" href="' + options.stylesheet + '">'));
117
        }
118
        // Create a copy of the element to print
119
        var copy = $this.clone();
120
        // Wrap it in a span to get the HTML markup string
121
        copy = $("<span/>")
122
            .append(copy);
123
        // Remove unwanted elements
124
        copy.find(options.noPrintSelector)
125
            .remove();
126
        // Add in the styles
127
        copy.append($styles.clone());
128
        // Appedned content
129
        copy.append(getjQueryObject(options.append));
130
        // Prepended content
131
        copy.prepend(getjQueryObject(options.prepend));
132
        if (options.manuallyCopyFormValues) {
133
            // Manually copy form values into the HTML for printing user-modified input fields
134
            // http://stackoverflow.com/a/26707753
135
            copy.find("input, select, textarea")
136
                .each(function () {
137
                    var $field = $(this);
138
                    if ($field.is("[type='radio']") || $field.is("[type='checkbox']")) {
139
                        if ($field.prop("checked")) {
140
                            $field.attr("checked", "checked");
141
                        }
142
                    } else if ($field.is("select")) {
143
                        $field.find(":selected")
144
                            .attr("selected", "selected");
145
                    } else {
146
                        $field.attr("value", $field.val());
147
                    }
148
                });
149
        }
150
        // Get the HTML markup string
151
        var content = copy.html();
152
        // Notify with generated markup & cloned elements - useful for logging, etc
153
        try {
154
            options.deferred.notify('generated_markup', content, copy);
155
        } catch (err) {
156
            console.warn('Error notifying deferred', err);
157
        }
158
        // Destroy the copy
159
        copy.remove();
160
        if (options.iframe) {
161
            // Use an iframe for printing
162
            try {
163
                var $iframe = $(options.iframe + "");
164
                var iframeCount = $iframe.length;
165
                if (iframeCount === 0) {
166
                    // Create a new iFrame if none is given
167
                    $iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>')
168
                        .prependTo('body')
169
                        .css({
170
                            "position": "absolute",
171
                            "top": -999,
172
                            "left": -999
173
                        });
174
                }
175
                var w, wdoc;
176
                w = $iframe.get(0);
177
                w = w.contentWindow || w.contentDocument || w;
178
                wdoc = w.document || w.contentDocument || w;
179
                wdoc.open();
180
                wdoc.write(content);
181
                wdoc.close();
182
                printFrame(w)
183
                    .done(function () {
184
                        // Success
185
                        setTimeout(function () {
186
                            // Wait for IE
187
                            if (iframeCount === 0) {
188
                                // Destroy the iframe if created here
189
                                $iframe.remove();
190
                            }
191
                        }, 100);
192
                    })
193
                    .fail(function () {
194
                        // Use the pop-up method if iframe fails for some reason
195
                        console.error("Failed to print from iframe", e.stack, e.message);
196
                        printContentInNewWindow(content);
197
                    })
198
                    .always(function () {
199
                        try {
200
                            options.deferred.resolve();
201
                        } catch (err) {
202
                            console.warn('Error notifying deferred', err);
203
                        }
204
                    });
205
            } catch (e) {
206
                // Use the pop-up method if iframe fails for some reason
207
                console.error("Failed to print from iframe", e.stack, e.message);
208
                printContentInNewWindow(content)
209
                    .always(function () {
210
                        try {
211
                            options.deferred.resolve();
212
                        } catch (err) {
213
                            console.warn('Error notifying deferred', err);
214
                        }
215
                    });
216
            }
217
        } else {
218
            // Use a new window for printing
219
            printContentInNewWindow(content)
220
                .always(function () {
221
                    try {
222
                        options.deferred.resolve();
223
                    } catch (err) {
224
                        console.warn('Error notifying deferred', err);
225
                    }
226
                });
227
        }
228
        return this;
229
    };
230
})(jQuery);