|
| 1 | +/*! |
| 2 | + * A front-end script for uploadify.swf (http://www.uploadify.com) |
| 3 | + * Experimental stuff. Used by Flowplayer setup in 2010 |
| 4 | + * |
| 5 | + * http://flowplayer.org/setup/ |
| 6 | + * |
| 7 | + * @author Tero Piirainen |
| 8 | + * @license MIT, GPL2+ |
| 9 | + * |
| 10 | + * TODO |
| 11 | + * - :file input replaceWith(wrap) |
| 12 | + */ |
| 13 | +(function() { |
| 14 | + |
| 15 | + $.tools = $.tools || {version: '@VERSION'}; |
| 16 | + |
| 17 | + var tool = $.tools.upload = { |
| 18 | + |
| 19 | + conf: { |
| 20 | + pagepath: '/setup/upload/', |
| 21 | + buttonText: 'Select file', |
| 22 | + script: '/setup/actions/uploadMedia', |
| 23 | + folder: '', |
| 24 | + method: 'POST', |
| 25 | + |
| 26 | + queueSizeLimit: 1, |
| 27 | + simUploadLimit: 1, |
| 28 | + |
| 29 | + sizeLimit: 6000, |
| 30 | + fileDataName: 'Filedata', |
| 31 | + |
| 32 | + // JQT stuff |
| 33 | + autoStart: false, |
| 34 | + progress: null, |
| 35 | + |
| 36 | + css: { |
| 37 | + root: 'uploadRoot', |
| 38 | + progress: 'uploadProgress', |
| 39 | + items: 'uploadItems', |
| 40 | + item: 'uploadItem', |
| 41 | + active: 'active' |
| 42 | + }, |
| 43 | + |
| 44 | + swf: { |
| 45 | + width: 110, |
| 46 | + height: 30, |
| 47 | + src: null, |
| 48 | + version: [9, 24] |
| 49 | + } |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + |
| 54 | + function prettyPrint(file) { |
| 55 | + var name = file.name, size = file.size / 1024; |
| 56 | + |
| 57 | + if (name.length > 20) { name = file.name.substring(0, 20) + " …"; } |
| 58 | + size = size > 1000 ? Math.round(size / 10) * .01 + " Mb" : Math.round(size) + " kb"; |
| 59 | + |
| 60 | + return "<strong>" + name + "</strong> (" + size + ")"; |
| 61 | + } |
| 62 | + |
| 63 | + function findOrCreate(context, query) { |
| 64 | + if (query.substring(0, 1) == '#') { return $(query); } |
| 65 | + var el = context.parent().find("." + query); |
| 66 | + |
| 67 | + if (!el.length) { |
| 68 | + el = $("<div/>").addClass(query); |
| 69 | + context.after(el); |
| 70 | + return el; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + function Upload(input, conf, index) { |
| 75 | + |
| 76 | + var self = this, |
| 77 | + swfWrap = input.parent().next(), |
| 78 | + css = conf.css; |
| 79 | + |
| 80 | + // id attribute required for input field |
| 81 | + conf.uploadifyID = input.attr("id") || "upload"; |
| 82 | + input.attr("id", conf.uploadifyID).hide(); |
| 83 | + |
| 84 | + conf.script += "?name=" + input.attr("name"); |
| 85 | + conf.swf.id = conf.swf.id || 'foo'; |
| 86 | + |
| 87 | + |
| 88 | + // progress and info elements |
| 89 | + var progress = findOrCreate(swfWrap, css.progress); |
| 90 | + |
| 91 | + // install SWF component |
| 92 | + var api = flashembed(swfWrap.get(0), conf.swf, conf).getApi(); |
| 93 | + |
| 94 | + |
| 95 | + // The Upload API |
| 96 | + $.extend(self, { |
| 97 | + |
| 98 | + getConf: function() { |
| 99 | + return conf; |
| 100 | + }, |
| 101 | + |
| 102 | + getRoot: function() { |
| 103 | + return swfWrap; |
| 104 | + }, |
| 105 | + |
| 106 | + getProgress: function() { |
| 107 | + return progress; |
| 108 | + }, |
| 109 | + |
| 110 | + getProgressItems: function() { |
| 111 | + return progress.find("." + css.items); |
| 112 | + }, |
| 113 | + |
| 114 | + start: function() { |
| 115 | + var e = new $.Event("uploadifyBeforeStart"); |
| 116 | + input.trigger(e); |
| 117 | + |
| 118 | + if (!e.isDefaultPrevented()) { |
| 119 | + input.trigger("uploadifyStart"); |
| 120 | + api.startFileUpload(null, true); |
| 121 | + } |
| 122 | + }, |
| 123 | + |
| 124 | + // bind / unbind |
| 125 | + bind: function(name, fn) { |
| 126 | + input.bind(name.replace("on", "uploadify"), function() { |
| 127 | + fn.apply(self, arguments); |
| 128 | + }); |
| 129 | + return self; |
| 130 | + }, |
| 131 | + |
| 132 | + unbind: function(name) { |
| 133 | + input.unbind(name.replace("on", "uploadify")); |
| 134 | + return self; |
| 135 | + } |
| 136 | + |
| 137 | + }); |
| 138 | + |
| 139 | + |
| 140 | + // define callbacks |
| 141 | + $.each("Select,BeforeStart,Start,Cancel,Error,Progress,Complete,AllComplete".split(","), function(i, name) { |
| 142 | + |
| 143 | + name = "on" + name; |
| 144 | + |
| 145 | + // configuration |
| 146 | + if ($.isFunction(conf[name])) { |
| 147 | + self.bind(name, conf[name]); |
| 148 | + } |
| 149 | + |
| 150 | + // API method |
| 151 | + self[name] = function(fn) { |
| 152 | + return self.bind(name, fn); |
| 153 | + }; |
| 154 | + |
| 155 | + }); |
| 156 | + |
| 157 | + |
| 158 | + // assign callbacks (to the end of queue) |
| 159 | + self.onSelect(function(event, fileId, file) { |
| 160 | + |
| 161 | + // root for the items |
| 162 | + var items = self.getProgressItems(); |
| 163 | + |
| 164 | + if (!items.length) { |
| 165 | + items = $("<div/>").addClass(css.items); |
| 166 | + progress.append(items); |
| 167 | + } |
| 168 | + |
| 169 | + // single item |
| 170 | + var item = $("#" + fileId), am = items.find("." + css.item).length; |
| 171 | + |
| 172 | + |
| 173 | + // queue is full |
| 174 | + if (am == conf.queueSizeLimit) { |
| 175 | + var old = items.children(":first"); |
| 176 | + api.cancelFileUpload(old.attr("id"), true, true); |
| 177 | + old.remove(); |
| 178 | + } |
| 179 | + |
| 180 | + // add to queue |
| 181 | + if (!item.length) { |
| 182 | + item = $("<div><small/><span/></div>").attr("id", fileId).addClass(css.item); |
| 183 | + item.find("small").html(prettyPrint(file)); |
| 184 | + items.append(item); |
| 185 | + } |
| 186 | + |
| 187 | + if (conf.autoStart) { |
| 188 | + self.start(); |
| 189 | + } |
| 190 | + |
| 191 | + }); |
| 192 | + |
| 193 | + |
| 194 | + self.onStart(function() { |
| 195 | + progress.addClass(css.active); |
| 196 | + }); |
| 197 | + |
| 198 | + self.onComplete(function() { |
| 199 | + progress.removeClass(css.active); |
| 200 | + api.clearFileUploadQueue(false); |
| 201 | + }); |
| 202 | + |
| 203 | + self.onProgress(function(event, fileId, file, data) { |
| 204 | + var item = $("#" + fileId); |
| 205 | + item.find("span").css({display: 'block', width: data.percentage + "%"}); |
| 206 | + }); |
| 207 | + |
| 208 | + } |
| 209 | + |
| 210 | + /* Flash API |
| 211 | + cancelFileUpload(key, true, true) |
| 212 | + startFileUpload(fileId || null, true) |
| 213 | + startFileUpload(null, true) |
| 214 | + updateSettings(settingName, settingValue) |
| 215 | + startFileUpload(ID, false) |
| 216 | + cancelFileUpload(ID, true, false) |
| 217 | + clearFileUploadQueue(false) |
| 218 | + */ |
| 219 | + |
| 220 | + // jQuery plugin implementation |
| 221 | + $.fn.upload = function(conf) { |
| 222 | + |
| 223 | + // already constructed --> return API |
| 224 | + var el = this.eq(typeof conf == 'number' ? conf : 0).data("upload"); |
| 225 | + if (el) { return el; } |
| 226 | + |
| 227 | + conf = $.extend(true, $.extend({}, tool.conf), conf); |
| 228 | + |
| 229 | + this.each(function(index) { |
| 230 | + el = new Upload($(this), conf); |
| 231 | + $(this).data("upload", el, index); |
| 232 | + }); |
| 233 | + |
| 234 | + return conf.api ? el: this; |
| 235 | + |
| 236 | + }; |
| 237 | + |
| 238 | +})(); |
| 239 | + |
| 240 | + |
| 241 | + |
| 242 | + |
| 243 | + |
| 244 | + |
0 commit comments