Move to sane directory structure. Don't make 'cms' the top level of the silverstripe...
[silverstripe-elijah.git] / silverstripe-gsoc / cms / javascript / Upload.js
blob213f6bb0b84c2b1fae3c8da3e6df6a34e73528b0
1 var Upload = {\r
2     initialize: function() {\r
3         iframe = window.top.document.getElementById('AssetAdmin_upload');\r
4         Upload.fileUploaded = 0;\r
5         Upload.uploadMessage = '';\r
6         Upload.onLoad();\r
7     },\r
8     \r
9     onLoad: function() {\r
10         path = 'http://' + window.location.host + window.location.pathname;\r
11         if(path[path.length-1] == '/') path = path.substring(0,path.length-1);\r
12         var start = document.cookie.indexOf('PHPSESSID')+10;\r
13         var end = document.cookie.indexOf(';',start);\r
14         if(end == -1) end = document.cookie.length;\r
15         sessId = document.cookie.substring(start,end);\r
16         swfu = new SWFUpload({\r
17                 upload_target_url: path + '/index/root?executeForm=UploadForm&PHPSESSID=' + sessId,   // Relative to the SWF file\r
18                 file_post_name: 'Files',\r
19                 // Flash file settings\r
20                 file_size_limit : '1024000',  // 10 MB\r
21                 file_types : '*.*', // or you could use something like: '*.doc;*.wpd;*.pdf',\r
22                 file_types_description : 'All Files',\r
23                 file_upload_limit : '6',\r
24                 //file_queue_limit : '1', // this isn't needed because the upload_limit will automatically place a queue limit\r
25                 begin_upload_on_queue : false,\r
26                 use_server_data_event : true,\r
27                 validate_files: false,\r
28                 // Event handler settings\r
29                 file_queued_handler : Upload.uploadFileQueuedCallback,\r
30                 file_validation_handler : Prototype.emptyFunction,\r
31                 file_progress_handler : Upload.uploadProgressCallback,\r
32                 file_cancelled_handler : Upload.uploadFileCancelCallback,\r
33                 file_complete_handler : Upload.uploadFileCompleteCallback,\r
34                 queue_complete_handler : Upload.uploadQueueCompleteCallback,\r
35                 error_handler : Upload.uploadErrorCallback,\r
36                 // Flash Settings\r
37                 flash_url : 'jsparty/SWFUpload/SWFUpload.swf',    // Relative to this file\r
38                 // UI settings\r
39                 ui_function: Upload.extendForm,\r
40                 ui_container_id : 'Form_EditForm',\r
41                 degraded_container_id : 'Form_EditForm',\r
42                 // Debug settings\r
43                 debug: false\r
44             });\r
45     },\r
46     \r
47     extendForm: function() {\r
48         if(iframe.contentDocument == undefined) iframe.contentDocument = document.frames[0].document;//IE HACK   \r
49         element = iframe.contentDocument.getElementById('Form_UploadForm');\r
50         inputFile = iframe.contentDocument.getElementById('Form_UploadForm_Files-0');\r
51         inputFileParent = inputFile.parentNode;\r
52         inputFileParent.removeChild(inputFile);\r
53         inputFile = iframe.contentDocument.createElement('input');\r
54         inputFile.type = 'text';\r
55         inputFile.id = 'Form_UploadForm_Files-0';\r
56         inputFileParent.appendChild(inputFile);\r
57         inputButton = iframe.contentDocument.getElementById('Form_UploadForm_Files-1');\r
58         if(inputButton != null) inputButton.parentNode.removeChild(inputButton);\r
59         inputButton = iframe.contentDocument.createElement('input');\r
60         inputButton.type = 'button';\r
61         inputButton.id = 'Form_UploadForm_Files-1';\r
62         inputButton.value = ' Browse...';\r
63         inputButton.style.width = '66px';\r
64         inputButton.style.height = '19px';\r
65         inputButton.style.position = 'relative';\r
66         inputButton.style.top = '1px';\r
67         inputButton.style.fontFamily = 'Arial';\r
68         inputButton.style.fontSize = '1.06em';\r
69         inputFileParent.appendChild(inputButton);\r
70         Event.observe(inputButton,'click',function() {swfu.browse();});\r
71         Event.observe(iframe.contentDocument.getElementById('Form_UploadForm_action_upload'),'click',function(event) {\r
72                                                swfu.startUpload();\r
73                                                Event.stop(event);\r
74                                            });\r
75     },\r
76     \r
77     uploadFileQueuedCallback: function(file,queueLength) {\r
78         iframe.contentDocument.getElementById('Form_UploadForm_action_upload').disabled = false;\r
79         Upload.addFileParam(file);\r
80         var fileContainer = iframe.contentDocument.getElementById('Form_UploadForm_FilesList');\r
81         if(fileContainer == null) {\r
82            fileContainer = iframe.contentDocument.createElement('div');\r
83            fileContainer.id = 'Form_UploadForm_FilesList';\r
84            iframe.contentDocument.getElementById('Form_UploadForm').appendChild(fileContainer);\r
85         }\r
86         \r
87         var fileToUpload = iframe.contentDocument.createElement('div');\r
88         fileToUpload.id = 'Form_UploadForm_FilesList_' + file.id;\r
89         fileToUpload.style.marginBottom = '3px';\r
90         fileContainer.appendChild(fileToUpload);\r
91         \r
92         var fileName = iframe.contentDocument.createElement('div');\r
93         fileName.id = 'Form_UploadForm_FilesList_Name_' + file.id;\r
94         fileName.style.position = 'relative';\r
95         fileName.style.top = '-4px';\r
96         fileName.style.display = 'inline';\r
97         fileName.style.padding = '2px';\r
98         fileName.innerHTML = file.name;\r
99         fileName.style.height = Element.getDimensions(fileName).height + 1 + 'px';//IE hack\r
100         fileToUpload.appendChild(fileName);\r
101         \r
102         var fileProgress = iframe.contentDocument.createElement('div');\r
103         fileProgress.id = 'Form_UploadForm_FilesList_Progress_' + file.id;\r
104         Position.clone(fileName,fileProgress);       \r
105         fileProgress.style.backgroundColor = 'black';\r
106         fileProgress.style.display = 'inline';\r
107         fileProgress.style.position = 'absolute';\r
108         fileProgress.style.left = '5px';\r
109         fileProgress.style.width = '0px';\r
110         fileProgress.finished = false;        \r
111         switch(BrowserDetect.browser) {\r
112             case 'Explorer':\r
113                 fileProgress.style.top = parseInt(fileProgress.style.top) + 6 + 'px';\r
114             break;\r
115             case 'Safari':\r
116                 fileProgress.style.top = parseInt(fileProgress.style.top) + 4 + 'px';\r
117             break;\r
118             case 'Firefox':  \r
119                 fileProgress.style.top = parseInt(fileProgress.style.top) + 8 + 'px';\r
120             break;\r
121         }\r
122         fileProgress.style.height = Element.getDimensions(fileName).height + 1 + 'px';        \r
123         fileToUpload.appendChild(fileProgress);\r
124         \r
125         var fileDelete = iframe.contentDocument.createElement('input');\r
126         fileDelete.id = file.id;\r
127         fileDelete.type = 'button';\r
128         fileDelete.value = 'Delete';\r
129         Element.addClassName(fileDelete,'delete');\r
130         fileToUpload.appendChild(fileDelete);\r
131         Event.observe(fileDelete,'click',Upload.uploadFileCancelCallback);\r
132     },\r
133     \r
134     uploadProgressCallback: function(file,bytesLoaded) {\r
135         fileName = iframe.contentDocument.getElementById('Form_UploadForm_FilesList_Name_' + file.id);\r
136         fileName.style.border = 'solid 1px black';\r
137         fileProgress = iframe.contentDocument.getElementById('Form_UploadForm_FilesList_Progress_' + file.id);\r
138         fileProgress.style.opacity = 0.3;\r
139         fileProgress.style.filter = 'alpha(opacity=30)';\r
140         if(!fileProgress.cloned) {\r
141             Position.clone(fileName,fileProgress);\r
142             fileProgress.style.width = '0px';\r
143             fileProgress.cloned = true;\r
144         }\r
145         fileProgress.style.width = (bytesLoaded / file.size) * Element.getDimensions(fileName).width - 1 + 'px';\r
146     },\r
147     \r
148     uploadFileCompleteCallback: function(file,serverData) {\r
149         Upload.fileUploaded++;\r
150         toEval = serverData.substr(serverData.indexOf('<script'));\r
151         toEval = toEval.replace('<script type="text/javascript">','');\r
152         toEval = toEval.replace('</script>','');\r
153         Upload.uploadMessage = toEval;\r
154         iframe.contentDocument.getElementById('Form_UploadForm_FilesList_Progress_' + file.id).finished = true;\r
155     },\r
156     \r
157     uploadFileCancelCallback: function(event) {\r
158         element = Event.element(event);\r
159         fileId = element.id;\r
160         swfu.cancelUpload(fileId);\r
161         fileContainer = iframe.contentDocument.getElementById('Form_UploadForm_FilesList');\r
162         elementToDelete = iframe.contentDocument.getElementById('Form_UploadForm_FilesList_' + fileId);\r
163         elementToDelete.parentNode.removeChild(elementToDelete);\r
164         filesToUpload = fileContainer.childNodes.length;\r
165         if(filesToUpload > 0) {\r
166             iframe.contentDocument.getElementById('Form_UploadForm_action_upload').disabled = false;\r
167         } else {\r
168             iframe.contentDocument.getElementById('Form_UploadForm_action_upload').disabled = true;\r
169         }\r
170         $A(fileContainer.childNodes).each(\r
171             function(item) {\r
172                 $A(item.childNodes).each(\r
173                     function(item) {\r
174                         if(item.id.indexOf('Name') != -1) {\r
175                             fileName = item;\r
176                         }\r
177                         if(item.id.indexOf('Progress') != -1) {\r
178                             fileProgress = item;\r
179                         }\r
180                     });\r
181                  Position.clone(fileName,fileProgress);\r
182                  if(fileProgress.finished == false) fileProgress.style.width = '0px';\r
183             }\r
184         );\r
185     },\r
186     \r
187     uploadQueueCompleteCallback: function() {\r
188         eval(Upload.uploadMessage.replace('1',Upload.fileUploaded));\r
189     },\r
190     \r
191     uploadErrorCallback: function(error_code, file, message) {\r
192         swfu.cancelQueue();\r
193         switch(error_code) {\r
194                 case SWFUpload.ERROR_CODE_HTTP_ERROR:\r
195                     alert('You have encountered an error. File hasn\'t been uploaded. Please hit the "Refresh" button in your web browser');\r
196                 break;\r
197                 case SWFUpload.ERROR_CODE_IO_ERROR:\r
198                     alert('You have encountered an error. File hasn\'t been uploaded. Please hit the "Refresh" button in your web browser');\r
199             break;\r
200                 case SWFUpload.ERROR_CODE_SECURITY_ERROR:\r
201                     alert('You have encountered an error. File hasn\'t been uploaded. Please hit the "Refresh" button in your web browser');\r
202                 break;\r
203                 case SWFUpload.ERROR_CODE_FILE_EXCEEDS_SIZE_LIMIT:\r
204                     alert('Files cannot be bigger than 10MB.');\r
205                 break;\r
206                 case SWFUpload.ERROR_CODE_ZERO_BYTE_FILE:\r
207                     alert('Files cannot be empty');\r
208                 break;\r
209                 case SWFUpload.ERROR_CODE_QUEUE_LIMIT_EXCEEDED:\r
210                     alert('You can only have six files in queue');\r
211                 break;\r
212                 case SWFUpload.ERROR_CODE_UPLOAD_FAILED:\r
213                     alert('You have encountered an error. File hasn\'t has been uploaded. Please hit the "Refresh" button in your web browser');\r
214                 break;\r
215                 case SWFUpload.ERROR_CODE_SPECIFIED_FILE_NOT_FOUND:\r
216                     alert('You have encountered an error. File hasn\'t has been uploaded. Please hit the "Refresh" button in your web browser');\r
217                 break;\r
218             default:\r
219                 alert('You have encountered an error. File hasn\'t has been uploaded. Please hit the "Refresh" button in your web browser');\r
220         }\r
221     },\r
222      \r
223     addFileParam: function(file) {\r
224         swfu.addFileParam(file.id,'ID',iframe.contentDocument.getElementById('Form_UploadForm_ID').value);\r
225         swfu.addFileParam(file.id,'action_doUpload','1');\r
226         swfu.addFileParam(file.id,'Files',file.name);\r
227         swfu.addFileParam(file.id,'MAX_FILE_SIZE','1073741824');\r
228     }\r
230 window.top.document.Upload = Upload;