github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/qiniu-js-sdk-master/demo/js/ui.js (about) 1 /*global plupload */ 2 /*global qiniu */ 3 function FileProgress(file, targetID) { 4 this.fileProgressID = file.id; 5 this.file = file; 6 7 this.opacity = 100; 8 this.height = 0; 9 this.fileProgressWrapper = $('#' + this.fileProgressID); 10 if (!this.fileProgressWrapper.length) { 11 // <div class="progress"> 12 // <div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" style="width: 20%"> 13 // <span class="sr-only">20% Complete</span> 14 // </div> 15 // </div> 16 17 this.fileProgressWrapper = $('<tr></tr>'); 18 var Wrappeer = this.fileProgressWrapper; 19 Wrappeer.attr('id', this.fileProgressID).addClass('progressContainer'); 20 21 var progressText = $("<td/>"); 22 progressText.addClass('progressName').text(file.name); 23 24 25 var fileSize = plupload.formatSize(file.size).toUpperCase(); 26 var progressSize = $("<td/>"); 27 progressSize.addClass("progressFileSize").text(fileSize); 28 29 var progressBarTd = $("<td/>"); 30 var progressBarBox = $("<div/>"); 31 progressBarBox.addClass('info'); 32 var progressBarWrapper = $("<div/>"); 33 progressBarWrapper.addClass("progress progress-striped"); 34 35 36 var progressBar = $("<div/>"); 37 progressBar.addClass("progress-bar progress-bar-info") 38 .attr('role', 'progressbar') 39 .attr('aria-valuemax', 100) 40 .attr('aria-valuenow', 0) 41 .attr('aria-valuein', 0) 42 .width('0%'); 43 44 var progressBarPercent = $('<span class=sr-only />'); 45 progressBarPercent.text(fileSize); 46 47 48 var progressCancel = $('<a href=# />'); 49 progressCancel.hide().addClass('progressCancel').text(''); 50 51 52 progressBar.append(progressBarPercent); 53 progressBarWrapper.append(progressBar); 54 progressBarBox.append(progressBarWrapper); 55 progressBarBox.append(progressCancel); 56 57 58 var progressBarStatus = $('<div class="status text-center"/>'); 59 progressBarBox.append(progressBarStatus); 60 progressBarTd.append(progressBarBox); 61 62 63 Wrappeer.append(progressText); 64 Wrappeer.append(progressSize); 65 Wrappeer.append(progressBarTd); 66 67 $('#' + targetID).append(Wrappeer); 68 } else { 69 this.reset(); 70 } 71 72 this.height = this.fileProgressWrapper.offset().top; 73 this.setTimer(null); 74 } 75 76 FileProgress.prototype.setTimer = function(timer) { 77 this.fileProgressWrapper.FP_TIMER = timer; 78 }; 79 80 FileProgress.prototype.getTimer = function(timer) { 81 return this.fileProgressWrapper.FP_TIMER || null; 82 }; 83 84 FileProgress.prototype.reset = function() { 85 this.fileProgressWrapper.attr('class', "progressContainer"); 86 this.fileProgressWrapper.find('td .progress .progress-bar-info').attr('aria-valuenow', 0).width('0%').find('span').text(''); 87 this.appear(); 88 }; 89 90 FileProgress.prototype.setChunkProgess = function(chunk_size) { 91 var chunk_amount = Math.ceil(this.file.size / chunk_size); 92 if (chunk_amount === 1) { 93 return false; 94 } 95 96 var viewProgess = $('<button class="btn btn-default">查看分块上传进度</button>'); 97 98 var progressBarChunkTr = $('<tr class="chunk-status-tr"><td colspan=3></td></tr>'); 99 var progressBarChunk = $('<div/>'); 100 for (var i = 1; i <= chunk_amount; i++) { 101 var col = $('<div class="col-md-2"/>'); 102 var progressBarWrapper = $('<div class="progress progress-striped"></div'); 103 104 var progressBar = $("<div/>"); 105 progressBar.addClass("progress-bar progress-bar-info text-left") 106 .attr('role', 'progressbar') 107 .attr('aria-valuemax', 100) 108 .attr('aria-valuenow', 0) 109 .attr('aria-valuein', 0) 110 .width('0%') 111 .attr('id', this.file.id + '_' + i) 112 .text(''); 113 114 var progressBarStatus = $('<span/>'); 115 progressBarStatus.addClass('chunk-status').text(); 116 117 progressBarWrapper.append(progressBar); 118 progressBarWrapper.append(progressBarStatus); 119 120 col.append(progressBarWrapper); 121 progressBarChunk.append(col); 122 } 123 this.fileProgressWrapper.find('td>div').append(viewProgess); 124 125 progressBarChunkTr.hide().find('td').append(progressBarChunk); 126 127 progressBarChunkTr.insertAfter(this.fileProgressWrapper); 128 }; 129 130 FileProgress.prototype.setProgress = function(percentage, speed, chunk_size) { 131 this.fileProgressWrapper.attr('class', "progressContainer green"); 132 133 var file = this.file; 134 var uploaded = file.loaded; 135 136 var size = plupload.formatSize(uploaded).toUpperCase(); 137 var formatSpeed = plupload.formatSize(speed).toUpperCase(); 138 var progressbar = this.fileProgressWrapper.find('td .progress').find('.progress-bar-info'); 139 this.fileProgressWrapper.find('.status').text("已上传: " + size + " 上传速度: " + formatSpeed + "/s"); 140 percentage = parseInt(percentage, 10); 141 if (file.status !== plupload.DONE && percentage === 100) { 142 percentage = 99; 143 } 144 progressbar.attr('aria-valuenow', percentage).css('width', percentage + '%'); 145 146 if (chunk_size) { 147 var chunk_amount = Math.ceil(file.size / chunk_size); 148 if (chunk_amount === 1) { 149 return false; 150 } 151 var current_uploading_chunk = Math.ceil(uploaded / chunk_size); 152 var pre_chunk, text; 153 154 for (var index = 0; index < current_uploading_chunk; index++) { 155 pre_chunk = $('#' + file.id + "_" + index); 156 pre_chunk.width('100%').removeClass().addClass('alert-success').attr('aria-valuenow', 100); 157 text = "块" + index + "上传进度100%"; 158 pre_chunk.next().html(text); 159 } 160 161 var currentProgessBar = $('#' + file.id + "_" + current_uploading_chunk); 162 var current_chunk_percent; 163 if (current_uploading_chunk < chunk_amount) { 164 if (uploaded % chunk_size) { 165 current_chunk_percent = ((uploaded % chunk_size) / chunk_size * 100).toFixed(2); 166 } else { 167 current_chunk_percent = 100; 168 currentProgessBar.removeClass().addClass('alert-success'); 169 } 170 } else { 171 var last_chunk_size = file.size - chunk_size * (chunk_amount - 1); 172 var left_file_size = file.size - uploaded; 173 if (left_file_size % last_chunk_size) { 174 current_chunk_percent = ((uploaded % chunk_size) / last_chunk_size * 100).toFixed(2); 175 } else { 176 current_chunk_percent = 100; 177 currentProgessBar.removeClass().addClass('alert-success'); 178 } 179 } 180 currentProgessBar.width(current_chunk_percent + '%'); 181 currentProgessBar.attr('aria-valuenow', current_chunk_percent); 182 text = "块" + current_uploading_chunk + "上传进度" + current_chunk_percent + '%'; 183 currentProgessBar.next().html(text); 184 } 185 186 this.appear(); 187 }; 188 189 FileProgress.prototype.setComplete = function(up, info) { 190 var td = this.fileProgressWrapper.find('td:eq(2) .progress'); 191 192 var res = $.parseJSON(info); 193 var url; 194 if (res.url) { 195 url = res.url; 196 str = "<div><strong>Link:</strong><a href=" + res.url + " target='_blank' > " + res.url + "</a></div>" + 197 "<div class=hash><strong>Hash:</strong>" + res.hash + "</div>"; 198 } else { 199 var domain = up.getOption('domain'); 200 url = domain + encodeURI(res.key); 201 var link = domain + res.key; 202 str = "<div><strong>Link:</strong><a href=" + url + " target='_blank' > " + link + "</a></div>" + 203 "<div class=hash><strong>Hash:</strong>" + res.hash + "</div>"; 204 } 205 206 td.html(str).removeClass().next().next('.status').hide(); 207 208 var progressNameTd = this.fileProgressWrapper.find('.progressName'); 209 var imageView = '?imageView2/1/w/100/h/100'; 210 211 var isImage = function(url) { 212 var res, suffix = ""; 213 var imageSuffixes = ["png", "jpg", "jpeg", "gif", "bmp"]; 214 var suffixMatch = /\.([a-zA-Z0-9]+)(\?|\@|$)/; 215 216 if (!url || !suffixMatch.test(url)) { 217 return false; 218 } 219 res = suffixMatch.exec(url); 220 suffix = res[1].toLowerCase(); 221 for (var i = 0, l = imageSuffixes.length; i < l; i++) { 222 if (suffix === imageSuffixes[i]) { 223 return true; 224 } 225 } 226 return false; 227 }; 228 229 var isImg = isImage(url); 230 231 var Wrapper = $('<div class="Wrapper"/>'); 232 var imgWrapper = $('<div class="imgWrapper col-md-3"/>'); 233 var linkWrapper = $('<a class="linkWrapper" target="_blank"/>'); 234 var showImg = $('<img src="loading.gif"/>'); 235 236 progressNameTd.append(Wrapper); 237 238 if (!isImg) { 239 showImg.attr('src', 'default.png'); 240 Wrapper.addClass('default'); 241 242 imgWrapper.append(showImg); 243 Wrapper.append(imgWrapper); 244 } else { 245 linkWrapper.append(showImg); 246 imgWrapper.append(linkWrapper); 247 Wrapper.append(imgWrapper); 248 249 var img = new Image(); 250 if (!/imageView/.test(url)) { 251 url += imageView 252 } 253 $(img).attr('src', url); 254 255 var height_space = 340; 256 $(img).on('load', function() { 257 showImg.attr('src', url); 258 259 linkWrapper.attr('href', url).attr('title', '查看原图'); 260 261 function initImg(url, key, height) { 262 $('#myModal-img').modal(); 263 var modalBody = $('#myModal-img').find('.modal-body'); 264 if (height <= 300) { 265 $('#myModal-img').find('.text-warning').show(); 266 } 267 var newImg = new Image(); 268 modalBody.find('img').attr('src', 'loading.gif'); 269 newImg.onload = function() { 270 modalBody.find('img').attr('src', url).data('key', key).data('h', height); 271 modalBody.find('.modal-body-wrapper').find('a').attr('href', url); 272 }; 273 newImg.src = url; 274 } 275 276 var infoWrapper = $('<div class="infoWrapper col-md-6"></div>'); 277 278 279 var fopLink = $('<a class="fopLink"/>'); 280 fopLink.attr('data-key', res.key).text('查看处理效果'); 281 infoWrapper.append(fopLink); 282 fopLink.on('click', function() { 283 var key = $(this).data('key'); 284 var height = parseInt($(this).parents('.Wrapper').find('.origin-height').text(), 10); 285 if (height > $(window).height() - height_space) { 286 height = parseInt($(window).height() - height_space, 10); 287 } else { 288 height = parseInt(height, 10) || 300; 289 //set a default height 300 for ie9- 290 } 291 var fopArr = []; 292 fopArr.push({ 293 fop: 'imageView2', 294 mode: 3, 295 h: height, 296 q: 100, 297 format: 'png' 298 }); 299 fopArr.push({ 300 fop: 'watermark', 301 mode: 1, 302 image: 'http://www.b1.qiniudn.com/images/logo-2.png', 303 dissolve: 100, 304 gravity: 'SouthEast', 305 dx: 100, 306 dy: 100 307 }); 308 var url = Qiniu.pipeline(fopArr, key); 309 $('#myModal-img').on('hide.bs.modal', function() { 310 $('#myModal-img').find('.btn-default').removeClass('disabled'); 311 $('#myModal-img').find('.text-warning').hide(); 312 }).on('show.bs.modal', function() { 313 $('#myModal-img').find('.imageView').find('a:eq(0)').addClass('disabled'); 314 $('#myModal-img').find('.watermark').find('a:eq(3)').addClass('disabled'); 315 $('#myModal-img').find('.text-warning').hide(); 316 }); 317 318 initImg(url, key, height); 319 320 return false; 321 }); 322 323 var ie = Qiniu.detectIEVersion(); 324 if (!(ie && ie <= 9)) { 325 var exif = Qiniu.exif(res.key); 326 if (exif) { 327 var exifLink = $('<a href="" target="_blank">查看exif</a>'); 328 exifLink.attr('href', url + '?exif'); 329 infoWrapper.append(exifLink); 330 } 331 332 var imageInfo = Qiniu.imageInfo(res.key); 333 var infoArea = $('<div/>'); 334 var infoInner = '<div>格式:<span class="origin-format">' + imageInfo.format + '</span></div>' + 335 '<div>宽度:<span class="orgin-width">' + imageInfo.width + 'px</span></div>' + 336 '<div>高度:<span class="origin-height">' + imageInfo.height + 'px</span></div>'; 337 infoArea.html(infoInner); 338 339 infoWrapper.append(infoArea); 340 } 341 342 Wrapper.append(infoWrapper); 343 344 }).on('error', function() { 345 showImg.attr('src', 'default.png'); 346 Wrapper.addClass('default'); 347 }); 348 } 349 }; 350 FileProgress.prototype.setError = function() { 351 this.fileProgressWrapper.find('td:eq(2)').attr('class', 'text-warning'); 352 this.fileProgressWrapper.find('td:eq(2) .progress').css('width', 0).hide(); 353 this.fileProgressWrapper.find('button').hide(); 354 this.fileProgressWrapper.next('.chunk-status-tr').hide(); 355 }; 356 357 FileProgress.prototype.setCancelled = function(manual) { 358 var progressContainer = 'progressContainer'; 359 if (!manual) { 360 progressContainer += ' red'; 361 } 362 this.fileProgressWrapper.attr('class', progressContainer); 363 this.fileProgressWrapper.find('td .progress .progress-bar-info').css('width', 0); 364 }; 365 366 FileProgress.prototype.setStatus = function(status, isUploading) { 367 if (!isUploading) { 368 this.fileProgressWrapper.find('.status').text(status).attr('class', 'status text-left'); 369 } 370 }; 371 372 373 FileProgress.prototype.appear = function() { 374 if (this.getTimer() !== null) { 375 clearTimeout(this.getTimer()); 376 this.setTimer(null); 377 } 378 379 if (this.fileProgressWrapper[0].filters) { 380 try { 381 this.fileProgressWrapper[0].filters.item("DXImageTransform.Microsoft.Alpha").opacity = 100; 382 } catch (e) { 383 // If it is not set initially, the browser will throw an error. This will set it if it is not set yet. 384 this.fileProgressWrapper.css('filter', "progid:DXImageTransform.Microsoft.Alpha(opacity=100)"); 385 } 386 } else { 387 this.fileProgressWrapper.css('opacity', 1); 388 } 389 390 this.fileProgressWrapper.css('height', ''); 391 392 this.height = this.fileProgressWrapper.offset().top; 393 this.opacity = 100; 394 this.fileProgressWrapper.show(); 395 396 };