Project

General

Profile

Download (7.28 KB) Statistics
| Branch: | Revision:
1
/*global plupload:false, escape:false, alert:false */
2
/*jslint evil: true */
3
define([
4
'plupload/plupload',
5
'java/applet'
6
], function(plupload, applet){
7

    
8
  var uploadInstances = {};
9
      
10
  plupload.applet = {
11

    
12
    pluploadjavatrigger : function(eventname, id, fileobjstring) {
13
      // FF / Safari mac breaks down if it's not detached here
14
      // can't do java -> js -> java
15
      setTimeout(function() {
16
          var uploader = uploadInstances[id], i, args;
17
          var file = fileobjstring ? eval('(' + fileobjstring + ')') : "";
18
          if (uploader) {
19
            uploader.trigger('applet:' + eventname, file);
20
          }
21
      }, 0);
22
    }
23
  };
24

    
25
  plupload.runtimes.Applet = plupload.addRuntime("java", {
26

    
27
    /**
28
     * Returns supported features for the Java runtime.
29
     * 
30
     * @return {Object} Name/value object with supported features.
31
     */                                                   
32

    
33
    getFeatures : function() {
34
      return {
35
        java: applet.hasVersion('1.5'),
36
        chunks: true,
37
        progress: true,
38
        dragdrop: false
39
      };
40
    },    
41

    
42
    initialized : false,
43
    init : function(uploader, callback) {
44

    
45
      var _applet,
46
          appletContainer, 
47
          appletVars, 
48
          lookup = {},
49
          initialized, 
50
          waitCount = 0, 
51
          container = document.body,
52
          features = this.getFeatures(),
53
          url = uploader.settings.java_applet_url;
54

    
55
  // Commented out to allow upload without Java
56
      //if(!features.java){
57
      //  callback({success : false});
58
      //  return;
59
      //}
60

    
61
      function getApplet() {
62
        if(!_applet){
63
          _applet = document.getElementById(uploader.id);
64
        }
65
        return _applet;
66
      }
67

    
68
      function waitForAppletToLoadIn5SecsErrorOtherwise() {
69
          // Wait for applet init in 5 secs.
70
          waitCount += 500;
71
          if (waitCount > 5000) {
72
              IRIGO.toaster([{
73
                  message: '\If direct upload (Java) is not starting, please click here ->\
74
                <object\
75
                classid="java:plupload.Plupload.class"\
76
                codebase="/stabile/static/applet/"\
77
                codetype="application/x-java-applet"\
78
                archive="plupload.java.jar"\
79
                standby="The Java applet is loading..."\
80
                callback=""\
81
                name="javatest"\
82
                height="30"\
83
                width="30"\
84
                style="vertical-align:middle;">\
85
                    Plupload\
86
                </object>',
87
                  type: "message",
88
                  duration: 20000
89
              }]);
90
          }
91
          if (waitCount > 60000) {
92
              callback({success : false});
93
              console.log("giving up on Java applet...");
94
              return;
95
          }
96

    
97
          // Commented out to allow upload without Java
98
//          if (!initialized) {
99
          if (!initialized && features.java) {
100
              setTimeout(waitForAppletToLoadIn5SecsErrorOtherwise, 500);
101
          }
102
      }
103
      
104
      uploadInstances[uploader.id] = uploader;
105
      appletContainer = document.createElement('div');
106
      appletContainer.id = uploader.id + '_applet_container';
107
      appletContainer.className = 'plupload applet';
108

    
109
      plupload.extend(appletContainer.style, {
110
        // move the 1x1 pixel out of the way. 
111
        position : 'absolute',
112
        left: '-9999px',
113
        zIndex : -1
114
      });
115

    
116
      uploader.bind("Applet:Init", function() {
117
        var filters;
118
        initialized = uploader.initialized = true;
119
        if(uploader.settings.filters){
120
          filters = [];
121
          for(var i = 0, len = uploader.settings.filters.length; i < len; i++){
122
            filters.push(uploader.settings.filters[i].extensions);
123
          }
124
          getApplet().setFileFilters(filters.join(","));
125
          console.log("Applet inited");
126
        }
127
        callback({success : true});
128
      });
129

    
130
      document.body.appendChild(appletContainer);
131

    
132
      applet.inject(appletContainer, {
133
        archive: url,
134
        id: escape(uploader.id),
135
        code: 'plupload.Plupload',
136
        callback: 'plupload.applet.pluploadjavatrigger'
137
      });
138

    
139
      uploader.bind("UploadFile", function(up, file) {
140
          console.log('UploadFile', up, file);
141
          var settings = up.settings,
142
              abs_url = location.protocol + '//' + location.host;
143

    
144
          if(settings.url.charAt(0) === "/"){
145
            abs_url += settings.url;
146
          }
147
          else if(settings.url.slice(0,4) === "http"){
148
            abs_url = settings.url;
149
          }
150
          else{
151
            // relative
152
            abs_url += location.pathname.slice(0, location.pathname.lastIndexOf('/')) + '/' + settings.url;
153
          }
154
          if(file.url) {
155
              var download = new plupload.Downloader(file);
156
              download.start();
157
          } else {
158
              // converted to string since number type conversion is buggy in MRJ runtime
159
              // In Firefox Mac (MRJ) runtime every number is a double
160
              getApplet().uploadFile(lookup[file.id] + "", abs_url, document.cookie, settings.chunk_size, (settings.retries || 3));
161
          }
162
      });
163
   
164
      uploader.bind("SelectFiles", function(up){
165
        getApplet().openFileDialog();
166
      });
167

    
168
      uploader.bind("Applet:UploadProcess", function(up, javaFile) {
169
        var file = up.getFile(lookup[javaFile.id]);
170
        var finished = javaFile.chunk === javaFile.chunks;
171

    
172
        if (file.status != plupload.FAILED) {
173
          file.loaded = javaFile.loaded;
174
          file.size = javaFile.size;
175
          up.trigger('UploadProgress', file);
176
        }
177
        else{
178
            IRIGO.toast("uploadProcess status failed");
179
        }
180

    
181
        if (finished) {
182
          file.status = plupload.DONE;
183
          up.trigger('FileUploaded', file, {
184
            response : "File uploaded"
185
          });
186
        }
187
      });
188

    
189
      uploader.bind("Applet:SelectFiles", function(up, file) {
190
        var i, files = [], id;
191
        id = plupload.guid();
192
        lookup[id] = file.id;
193
        lookup[file.id] = id;
194

    
195
        var match = false;
196
        jQuery.each(uploader.files, function(i, val) {if (val.name == file.name) match=true;});
197
        if (match) {
198
          IRIGO.toast("You have already added a file with that name");
199
        } else {
200
            files.push(new plupload.File(id, file.name, file.size));
201
            // Trigger FilesAdded event if we added any
202
            if (files.length) {
203
                uploader.trigger("FilesAdded", files);
204
            }
205
        }
206
      });
207
      
208
      uploader.bind("Applet:GenericError", function(up, err) {
209
        uploader.trigger('Error', {
210
          code : plupload.GENERIC_ERROR,
211
          message : 'Generic error.',
212
          details : err.message,
213
          file : uploader.getFile(lookup[err.id])
214
        });
215
      });
216

    
217
      uploader.bind("Applet:IOError", function(up, err) {
218
        uploader.trigger('Error', {
219
          code : plupload.IO_ERROR,
220
          message : 'IO error.',
221
          details : err.message,
222
          file : uploader.getFile(lookup[err.id])
223
        });
224
      });
225

    
226
      uploader.bind("FilesRemoved", function(up, files) {
227
        for (var i = 0, len = files.length; i < len; i++) {
228
          getApplet().removeFile(lookup[files[i].id]);
229
        }
230
      });
231

    
232
      waitForAppletToLoadIn5SecsErrorOtherwise();
233

    
234
    }// end object arg
235
  });// end add runtime
236

    
237

    
238
});
239

    
240

    
241

    
242

    
(1-1/4)