summaryrefslogtreecommitdiff
path: root/javascript/libs
diff options
context:
space:
mode:
authorTyler Bello <tylerbello@users.sourceforge.net>2009-07-03 20:02:35 +0000
committerTyler Bello <tylerbello@users.sourceforge.net>2009-07-03 20:02:35 +0000
commit1cabe0cc0862ee71ef98ee24676aad2512c7fd4d (patch)
tree6e102779cc35b97ad95d8dac5e3cfd2f6107f30d /javascript/libs
parent01a5b504e0bf7c89eb6d00250c8540c08fb21f30 (diff)
downloadutil-1cabe0cc0862ee71ef98ee24676aad2512c7fd4d.tar.gz
util-1cabe0cc0862ee71ef98ee24676aad2512c7fd4d.tar.bz2
util-1cabe0cc0862ee71ef98ee24676aad2512c7fd4d.zip
Add uploadProgress script
Diffstat (limited to 'javascript/libs')
-rw-r--r--javascript/libs/jquery/plugins/jquery.uploadProgress.js117
1 files changed, 117 insertions, 0 deletions
diff --git a/javascript/libs/jquery/plugins/jquery.uploadProgress.js b/javascript/libs/jquery/plugins/jquery.uploadProgress.js
new file mode 100644
index 0000000..7759214
--- /dev/null
+++ b/javascript/libs/jquery/plugins/jquery.uploadProgress.js
@@ -0,0 +1,117 @@
+/*
+* jquery.uploadProgress
+*
+* Copyright (c) 2008 Piotr Sarnacki (drogomir.com)
+*
+* Licensed under the MIT license:
+* http://www.opensource.org/licenses/mit-license.php
+*
+*/
+(function($) {
+ $.fn.uploadProgress = function(options) {
+ options = $.extend({
+ dataType: "json",
+ interval: 2000,
+ progressBar: "#progressbar",
+ progressUrl: "/progress",
+ start: function() {},
+ uploading: function() {},
+ complete: function() {},
+ success: function() {},
+ error: function() {},
+ preloadImages: [],
+ uploadProgressPath: '/javascripts/jquery.uploadProgress.js',
+ jqueryPath: '/javascripts/jquery.js',
+ timer: ""
+ }, options);
+
+ $(function() {
+ //preload images
+ for(var i = 0; i<options.preloadImages.length; i++)
+ {
+ options.preloadImages[i] = $("<img>").attr("src", options.preloadImages[i]);
+ }
+ /* tried to add iframe after submit (to not always load it) but it won't work.
+safari can't get scripts properly while submitting files */
+ if($.browser.safari && top.document == document) {
+ /* iframe to send ajax requests in safari
+thanks to Michele Finotto for idea */
+ iframe = document.createElement('iframe');
+ iframe.name = "progressFrame";
+ $(iframe).css({width: '0', height: '0', position: 'absolute', top: '-3000px'});
+ document.body.appendChild(iframe);
+
+ var d = iframe.contentWindow.document;
+ d.open();
+ /* weird - safari won't load scripts without this lines... */
+ d.write('<html><head></head><body></body></html>');
+ d.close();
+
+ var b = d.body;
+ var s = d.createElement('script');
+ s.src = options.jqueryPath;
+ /* must be sure that jquery is loaded */
+ s.onload = function() {
+ var s1 = d.createElement('script');
+ s1.src = options.uploadProgressPath;
+ b.appendChild(s1);
+ }
+ b.appendChild(s);
+ }
+ });
+
+ return this.each(function(){
+ $(this).bind('submit', function() {
+ var uuid = "";
+ for (i = 0; i < 32; i++) { uuid += Math.floor(Math.random() * 16).toString(16); }
+
+ /* update uuid */
+ options.uuid = uuid;
+ /* start callback */
+ options.start();
+
+ /* patch the form-action tag to include the progress-id if X-Progress-ID has been already added just replace it */
+ if(old_id = /X-Progress-ID=([^&]+)/.exec($(this).attr("action"))) {
+ var action = $(this).attr("action").replace(old_id[1], uuid);
+ $(this).attr("action", action);
+ } else {
+ $(this).attr("action", jQuery(this).attr("action") + "?X-Progress-ID=" + uuid);
+ }
+ var uploadProgress = $.browser.safari ? progressFrame.jQuery.uploadProgress : jQuery.uploadProgress;
+ options.timer = window.setInterval(function() { uploadProgress(this, options) }, options.interval);
+ });
+ });
+ };
+
+jQuery.uploadProgress = function(e, options) {
+ jQuery.ajax({
+ type: "GET",
+ url: options.progressUrl + "?X-Progress-ID=" + options.uuid,
+ dataType: options.dataType,
+ success: function(upload) {
+ if (upload.state == 'uploading') {
+ upload.percents = Math.floor((upload.received / upload.size)*1000)/10;
+
+ var bar = $.browser.safari ? $(options.progressBar, parent.document) : $(options.progressBar);
+ bar.css({width: upload.percents+'%'});
+ options.uploading(upload);
+ }
+
+ if (upload.state == 'done' || upload.state == 'error') {
+ window.clearTimeout(options.timer);
+ options.complete(upload);
+ }
+
+ if (upload.state == 'done') {
+ options.success(upload);
+ }
+
+ if (upload.state == 'error') {
+ options.error(upload);
+ }
+ }
+ });
+};
+
+})(jQuery);
+