source: branches/1.2/workflow/js/jscode/wf_file_upload.js @ 1349

Revision 1349, 4.1 KB checked in by niltonneto, 15 years ago (diff)

Ticket #561 - Inclusão do módulo Workflow faltante nessa versão.

  • Property svn:executable set to *
Line 
1/**
2 * Convert a single file-input element into a 'multiple' input list
3 *
4 * Usage:
5 *
6 *   1. Create a file input element (no name)
7 *      eg. <input type="file" id="first_file_element">
8 *
9 *   2. Create a DIV for the output to be written to
10 *      eg. <div id="files_list"></div>
11 *
12 *   3. Instantiate a MultiSelector object, passing in the DIV and an (optional) maximum number of files
13 *      eg. var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 3 );
14 *
15 *   4. Add the first element
16 *      eg. multi_selector.addElement( document.getElementById( 'first_file_element' ) );
17 *
18 *   5. That's it.
19 *
20 *   You might (will) want to play around with the addListRow() method to make the output prettier.
21 *
22 *   You might also want to change the line
23 *       element.name = 'file_' + this.count;
24 *   ...to a naming convention that makes more sense to you.
25 *
26 * Licence:
27 *   Use this however/wherever you like, just don't blame me if it breaks anything.
28 *
29 * Credit:
30 *   If you're nice, you'll leave this bit:
31 * 
32 *   Class by Stickman -- http://www.the-stickman.com
33 *      with thanks to:
34 *      [for Safari fixes]
35 *         Luis Torrefranca -- http://www.law.pitt.edu
36 *         and
37 *         Shawn Parker & John Pennypacker -- http://www.fuzzycoconut.com
38 *      [for duplicate name bug]
39 *         'neal'
40 * Modified by: Sidnei Augusto Drovetto Junior
41 */
42function MultiSelector( list_target, max )
43{
44
45        // Where to write the list
46        this.list_target = list_target;
47        // How many elements?
48        this.count = 0;
49        // How many elements?
50        this.id = 0;
51        // Is there a maximum?
52        if( max )
53                this.max = max;
54        else
55                this.max = -1;
56       
57        /**
58         * Add a new file input element
59         */
60        this.addElement = function(element, name)
61        {
62                // Make sure it's a file input element
63                if( element.tagName == 'INPUT' && element.type == 'file' )
64                {
65                        // Element name -- what number am I?
66                        //element.name = 'file_' + this.id++;
67                        this.id++;
68                        element.name = name;
69
70                        // Add reference to this object
71                        element.multi_selector = this;
72
73                        // What to do when a file is selected
74                        element.onchange = function()
75                        {
76                                // New file input
77                                var new_element = document.createElement( 'input' );
78                                new_element.type = 'file';
79
80                                // Add new element
81                                this.parentNode.insertBefore( new_element, this );
82
83                                // Apply 'update' to element
84                                this.multi_selector.addElement(new_element, name);
85
86                                // Update list
87                                this.multi_selector.addListRow( this );
88
89                                // Hide this: we can't use display:none because Safari doesn't like it
90                                this.style.position = 'absolute';
91                                this.style.left = '-1000px';
92
93                        };
94                        // If we've reached maximum number, disable input element
95                        if( this.max != -1 && this.count >= this.max )
96                        {
97                                element.disabled = true;
98                        };
99
100                        // File element counter
101                        this.count++;
102                        // Most recent element
103                        this.current_element = element;
104                       
105                }
106                else
107                {
108                        // This can only be applied to file input elements!
109                        alert( 'Erro: não é um input de arquivo' );
110                };
111
112        };
113
114        /**
115         * Add a new row to the list of files
116         */
117        this.addListRow = function( element )
118        {
119                // Row div
120                var new_row = document.createElement( 'div' );
121
122                // Delete button
123                var new_row_button = document.createElement( 'a' );
124                new_row_button.href = '#';
125                new_row_button.innerHTML = 'remover';
126
127                // References
128                new_row.element = element;
129
130                // Delete function
131                new_row_button.onclick= function()
132                {
133                        // Remove element from form
134                        this.parentNode.element.parentNode.removeChild( this.parentNode.element );
135
136                        // Remove this row from the list
137                        this.parentNode.parentNode.removeChild( this.parentNode );
138
139                        // Decrement counter
140                        this.parentNode.element.multi_selector.count--;
141
142                        // Re-enable input element (if it's disabled)
143                        this.parentNode.element.multi_selector.current_element.disabled = false;
144
145                        // Appease Safari
146                        //    without it Safari wants to reload the browser window
147                        //    which nixes your already queued uploads
148                        return false;
149                };
150
151                // Set row value
152                new_row.innerHTML = element.value + '&nbsp;&nbsp;';
153
154                // Add button
155                new_row.appendChild( new_row_button );
156
157                // Add it to the list
158                this.list_target.appendChild( new_row );
159               
160        };
161
162};
Note: See TracBrowser for help on using the repository browser.