github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/qiniu-js-sdk-master/demo/js/main.js (about)

     1  /*global Qiniu */
     2  /*global plupload */
     3  /*global FileProgress */
     4  /*global hljs */
     5  
     6  
     7  $(function() {
     8      var uploader = Qiniu.uploader({
     9          runtimes: 'html5,flash,html4',
    10          browse_button: 'pickfiles',
    11          container: 'container',
    12          drop_element: 'container',
    13          max_file_size: '100mb',
    14          flash_swf_url: 'js/plupload/Moxie.swf',
    15          dragdrop: true,
    16          chunk_size: '4mb',
    17          uptoken_url: $('#uptoken_url').val(),
    18          domain: $('#domain').val(),
    19          // downtoken_url: '/downtoken',
    20          // unique_names: true,
    21          // save_key: true,
    22          // x_vars: {
    23          //     'id': '1234',
    24          //     'time': function(up, file) {
    25          //         var time = (new Date()).getTime();
    26          //         // do something with 'time'
    27          //         return time;
    28          //     },
    29          // },
    30          auto_start: true,
    31          init: {
    32              'FilesAdded': function(up, files) {
    33                  $('table').show();
    34                  $('#success').hide();
    35                  plupload.each(files, function(file) {
    36                      var progress = new FileProgress(file, 'fsUploadProgress');
    37                      progress.setStatus("等待...");
    38                  });
    39              },
    40              'BeforeUpload': function(up, file) {
    41                  var progress = new FileProgress(file, 'fsUploadProgress');
    42                  var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
    43                  if (up.runtime === 'html5' && chunk_size) {
    44                      progress.setChunkProgess(chunk_size);
    45                  }
    46              },
    47              'UploadProgress': function(up, file) {
    48                  var progress = new FileProgress(file, 'fsUploadProgress');
    49                  var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
    50  
    51                  progress.setProgress(file.percent + "%", file.speed, chunk_size);
    52              },
    53              'UploadComplete': function() {
    54                  $('#success').show();
    55              },
    56              'FileUploaded': function(up, file, info) {
    57                  var progress = new FileProgress(file, 'fsUploadProgress');
    58                  progress.setComplete(up, info);
    59              },
    60              'Error': function(up, err, errTip) {
    61                  $('table').show();
    62                  var progress = new FileProgress(err.file, 'fsUploadProgress');
    63                  progress.setError();
    64                  progress.setStatus(errTip);
    65              }
    66              // ,
    67              // 'Key': function(up, file) {
    68              //     var key = "";
    69              //     // do something with key
    70              //     return key
    71              // }
    72          }
    73      });
    74  
    75      uploader.bind('FileUploaded', function() {
    76          console.log('hello man,a file is uploaded');
    77      });
    78      $('#container').on(
    79          'dragenter',
    80          function(e) {
    81              e.preventDefault();
    82              $('#container').addClass('draging');
    83              e.stopPropagation();
    84          }
    85      ).on('drop', function(e) {
    86          e.preventDefault();
    87          $('#container').removeClass('draging');
    88          e.stopPropagation();
    89      }).on('dragleave', function(e) {
    90          e.preventDefault();
    91          $('#container').removeClass('draging');
    92          e.stopPropagation();
    93      }).on('dragover', function(e) {
    94          e.preventDefault();
    95          $('#container').addClass('draging');
    96          e.stopPropagation();
    97      });
    98  
    99  
   100  
   101      $('#show_code').on('click', function() {
   102          $('#myModal-code').modal();
   103          $('pre code').each(function(i, e) {
   104              hljs.highlightBlock(e);
   105          });
   106      });
   107  
   108  
   109      $('body').on('click', 'table button.btn', function() {
   110          $(this).parents('tr').next().toggle();
   111      });
   112  
   113  
   114      var getRotate = function(url) {
   115          if (!url) {
   116              return 0;
   117          }
   118          var arr = url.split('/');
   119          for (var i = 0, len = arr.length; i < len; i++) {
   120              if (arr[i] === 'rotate') {
   121                  return parseInt(arr[i + 1], 10);
   122              }
   123          }
   124          return 0;
   125      };
   126  
   127      $('#myModal-img .modal-body-footer').find('a').on('click', function() {
   128          var img = $('#myModal-img').find('.modal-body img');
   129          var key = img.data('key');
   130          var oldUrl = img.attr('src');
   131          var originHeight = parseInt(img.data('h'), 10);
   132          var fopArr = [];
   133          var rotate = getRotate(oldUrl);
   134          if (!$(this).hasClass('no-disable-click')) {
   135              $(this).addClass('disabled').siblings().removeClass('disabled');
   136              if ($(this).data('imagemogr') !== 'no-rotate') {
   137                  fopArr.push({
   138                      'fop': 'imageMogr2',
   139                      'auto-orient': true,
   140                      'strip': true,
   141                      'rotate': rotate,
   142                      'format': 'png'
   143                  });
   144              }
   145          } else {
   146              $(this).siblings().removeClass('disabled');
   147              var imageMogr = $(this).data('imagemogr');
   148              if (imageMogr === 'left') {
   149                  rotate = rotate - 90 < 0 ? rotate + 270 : rotate - 90;
   150              } else if (imageMogr === 'right') {
   151                  rotate = rotate + 90 > 360 ? rotate - 270 : rotate + 90;
   152              }
   153              fopArr.push({
   154                  'fop': 'imageMogr2',
   155                  'auto-orient': true,
   156                  'strip': true,
   157                  'rotate': rotate,
   158                  'format': 'png'
   159              });
   160          }
   161  
   162          $('#myModal-img .modal-body-footer').find('a.disabled').each(function() {
   163  
   164              var watermark = $(this).data('watermark');
   165              var imageView = $(this).data('imageview');
   166              var imageMogr = $(this).data('imagemogr');
   167  
   168              if (watermark) {
   169                  fopArr.push({
   170                      fop: 'watermark',
   171                      mode: 1,
   172                      image: 'http://www.b1.qiniudn.com/images/logo-2.png',
   173                      dissolve: 100,
   174                      gravity: watermark,
   175                      dx: 100,
   176                      dy: 100
   177                  });
   178              }
   179  
   180              if (imageView) {
   181                  var height;
   182                  switch (imageView) {
   183                      case 'large':
   184                          height = originHeight;
   185                          break;
   186                      case 'middle':
   187                          height = originHeight * 0.5;
   188                          break;
   189                      case 'small':
   190                          height = originHeight * 0.1;
   191                          break;
   192                      default:
   193                          height = originHeight;
   194                          break;
   195                  }
   196                  fopArr.push({
   197                      fop: 'imageView2',
   198                      mode: 3,
   199                      h: parseInt(height, 10),
   200                      q: 100,
   201                      format: 'png'
   202                  });
   203              }
   204  
   205              if (imageMogr === 'no-rotate') {
   206                  fopArr.push({
   207                      'fop': 'imageMogr2',
   208                      'auto-orient': true,
   209                      'strip': true,
   210                      'rotate': 0,
   211                      'format': 'png'
   212                  });
   213              }
   214          });
   215  
   216          var newUrl = Qiniu.pipeline(fopArr, key);
   217  
   218          var newImg = new Image();
   219          img.attr('src', 'loading.gif');
   220          newImg.onload = function() {
   221              img.attr('src', newUrl);
   222              img.parent('a').attr('href', newUrl);
   223          };
   224          newImg.src = newUrl;
   225          return false;
   226      });
   227  
   228  });