/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk

« back to all changes in this revision

Viewing changes to loggerhead/static/javascript/yui/build/io/io-upload-iframe.js

  • Committer: Jelmer Vernooij
  • Date: 2020-06-04 19:43:36 UTC
  • mto: This revision was merged to the branch mainline in revision 500.
  • Revision ID: jelmer@jelmer.uk-20200604194336-ahskrf4rmy1qaxzs
Port loggerhead from YUI to jQuery.

YUI has been deprecated since 2014 and is due to be removed from Debian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3
 
Code licensed under the BSD License:
4
 
http://developer.yahoo.net/yui/license.txt
5
 
version: 3.0.0pr2
6
 
*/
7
 
YUI.add('io-upload-iframe', function(Y) {
8
 
 
9
 
   /*
10
 
        * Extends the IO base class to enable file uploads, with HTML forms,
11
 
        * using an iframe as the transport medium.
12
 
        * @module io-base
13
 
        * @submodule io-upload-iframe
14
 
        */
15
 
 
16
 
        var w = Y.config.win;
17
 
   /**
18
 
        * @description Parses the POST data object and creates hidden form elements
19
 
        * for each key-value, and appends them to the HTML form object.
20
 
        * @method appendData
21
 
        * @private
22
 
        * @static
23
 
        * @param {object} d The key-value hash map.
24
 
        * @return {array} e Array of created fields.
25
 
        */
26
 
 
27
 
        function _addData(f, d) {
28
 
                var e = [], i;
29
 
                for (p in d) {
30
 
                        if (d.hasOwnProperty(d, p)) {
31
 
                                e[i] = document.createElement('input');
32
 
                                e[i].type = 'hidden';
33
 
                                e[i].name = p;
34
 
                                e[i].value = d[p].
35
 
                                f.appendChild(e[i]);
36
 
                        }
37
 
                }
38
 
 
39
 
                return e;
40
 
        };
41
 
 
42
 
        function _removeData(f, e) {
43
 
                var i, l;
44
 
                if (e && e.length > 0) {
45
 
                        for(i = 0, l = e.length; i < l; i++){
46
 
                                f.removeChild(e[i]);
47
 
                        }
48
 
                }
49
 
        };
50
 
 
51
 
        function _create(o, c) {
52
 
                var UPLOAD_IFRAME = '<iframe id="ioupload' + o.id + '" name="ioupload' + o.id + '" />',
53
 
                cfg = {
54
 
                        position: 'absolute',
55
 
                        top: '-1000',
56
 
                        left: '-1000'
57
 
                },
58
 
                i = Y.Node.create(UPLOAD_IFRAME);
59
 
                i.setStyles(cfg);
60
 
 
61
 
                Y.get('body').appendChild(i);
62
 
 
63
 
                // Bind the onload handler to the iframe to detect the file upload response.
64
 
                Y.on("load", function() { _handle(o, c) }, '#ioupload' + o.id);
65
 
        };
66
 
 
67
 
        // Create the upload callback handler that fires when the iframe
68
 
        // receives the load event.  Subsequently, the event handler is detached
69
 
        // and the iframe removed from the document.
70
 
        function _handle(o, c) {
71
 
 
72
 
                if (c.timeout) {
73
 
                        _clearTimeout(o.id);
74
 
                }
75
 
 
76
 
                var p, d = Y.get('#ioupload' + o.id).get('contentWindow.document'),
77
 
                        d = Y.get('#ioupload' + o.id).get('contentWindow'),
78
 
                b = d.get('body');
79
 
 
80
 
                if (b) {
81
 
                        p = b.getElementsByTagName('pre');
82
 
                        o.c.responseText = (p) ? p.item(0).get('innerHTML') : b.get('innerHTML');
83
 
                }
84
 
 
85
 
                Y.io.complete(o, c);
86
 
 
87
 
                setTimeout( function() { _destroy(o.id); }, 100);
88
 
        };
89
 
 
90
 
   /**
91
 
        * @description Starts timeout count if the configuration object
92
 
        * has a defined timeout property.
93
 
        *
94
 
        * @method _startTimeout
95
 
        * @private
96
 
        * @static
97
 
    * @param {object} o - Transaction object generated by _create().
98
 
    * @param {object} c - Configuration object passed to YUI.io().
99
 
    * @return void
100
 
        */
101
 
        function _startTimeout(o, c) {
102
 
                Y.io._timeout[o.id] = w.setTimeout(function() { Y.io.abort(o, c); }, c.timeout);
103
 
        };
104
 
 
105
 
   /**
106
 
        * @description Clears the timeout interval started by _startTimeout().
107
 
        *
108
 
        * @method _clearTimeout
109
 
        * @private
110
 
        * @static
111
 
    * @param {number} id - Transaction id.
112
 
    * @return void
113
 
        */
114
 
        function _clearTimeout(id) {
115
 
                w.clearTimeout(Y.io._timeout[id]);
116
 
                delete Y.io._timeout[id];
117
 
        };
118
 
 
119
 
        function _destroy(id) {
120
 
                Y.Event.purgeElement('#ioupload' + id, false, "load");
121
 
                Y.get('body').removeChild(Y.get('#ioupload' + id));
122
 
        };
123
 
 
124
 
        Y.mix(Y.io, {
125
 
 
126
 
           /**
127
 
                * @description Uploads HTML form, inclusive of files/attachments, using the
128
 
                * iframe created in createFrame to facilitate the transaction.
129
 
                * @method _upload
130
 
                * @private
131
 
                * @static
132
 
                * @param {o} o The transaction object
133
 
                * @param {object} uri Qualified path to transaction resource.
134
 
                * @param {object} c - configuration object for the transaction.
135
 
                * @return {void}
136
 
                */
137
 
                _upload: function(o, uri, c) {
138
 
                        var f = (typeof c.form.id === 'string') ? document.getElementById(c.form.id) : c.form.id,
139
 
                        e, fields, i, p, attr;
140
 
 
141
 
                        _create(o, c);
142
 
                        // Track original HTML form attribute values.
143
 
                        attr = {
144
 
                                action: f.getAttribute('action'),
145
 
                                target: f.getAttribute('target')
146
 
                        };
147
 
 
148
 
                        // Initialize the HTML form properties in case they are
149
 
                        // not defined in the HTML form.
150
 
                        f.setAttribute('action', uri);
151
 
                        f.setAttribute('method', 'POST');
152
 
                        f.setAttribute('target', 'ioupload' + o.id );
153
 
                        f.setAttribute((Y.UA.ie) ? 'encoding' : 'enctype', 'multipart/form-data');
154
 
 
155
 
                        if (c.data) {
156
 
                                fields = _addData(f, c.data);
157
 
                        }
158
 
 
159
 
                        // Start polling if a callback is present and the timeout
160
 
                        // property has been defined.
161
 
                        if (c.timeout) {
162
 
                                _startTimeout(o, c);
163
 
                        }
164
 
 
165
 
                        // Start file upload.
166
 
                        f.submit();
167
 
 
168
 
                        Y.io.start(o.id, c);
169
 
 
170
 
                        if (c.data) {
171
 
                                _removeData(f, fields);
172
 
                        }
173
 
 
174
 
                        // Restore HTML form attributes to their original
175
 
                        // values prior to file upload.
176
 
                        for (p in attr) {
177
 
                                if (attr.hasOwnProperty(attr, p)) {
178
 
                                        if (attr[p]) {
179
 
                                                f.setAttribute(p, f[prop]);
180
 
                                        }
181
 
                                        else {
182
 
                                                f.removeAttribute(p);
183
 
                                        }
184
 
                                }
185
 
                        }
186
 
                }
187
 
        });
188
 
 
189
 
 
190
 
 
191
 
}, '3.0.0pr2' ,{requires:['io-base']});