[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/kcfinder/js/browser/ -> files.js (source)

   1  <?php
   2  
   3  /** This file is part of KCFinder project
   4    *
   5    *      @desc File related functionality
   6    *   @package KCFinder
   7    *   @version 2.21
   8    *    @author Pavel Tzonkov <[email protected]>
   9    * @copyright 2010 KCFinder Project
  10    *   @license http://www.opensource.org/licenses/gpl-2.0.php GPLv2
  11    *   @license http://www.opensource.org/licenses/lgpl-2.1.php LGPLv2
  12    *      @link http://kcfinder.sunhater.com
  13    */?>
  14  
  15  browser.initFiles = function() {
  16      $(document).unbind('keypress');
  17      $(document).keypress(function(e) {
  18          if ((e.which == 65) || (e.which == 97))
  19              browser.selectAll();
  20      });
  21      $('#files').unbind();
  22      $('#files').scroll(function() {
  23          browser.hideDialog();
  24      });
  25      $('.file').unbind();
  26      $('.file').click(function(e) {
  27          _.unselect();
  28          browser.selectFile($(this), e);
  29      });
  30      $('.file').rightClick(function(e) {
  31          _.unselect();
  32          browser.menuFile($(this), e);
  33      });
  34      $('.file').dblclick(function() {
  35          _.unselect();
  36          browser.returnFile($(this));
  37      });
  38      $('.file').mouseup(function() {
  39          _.unselect();
  40      });
  41      $('.file').mouseout(function() {
  42          _.unselect();
  43      });
  44      $.each(this.shows, function(i, val) {
  45          var display = (_.kuki.get('show' + val) == 'off')
  46              ? 'none' : 'block';
  47          $('#files .file div.' + val).css('display', display);
  48      });
  49      this.statusDir();
  50  };
  51  
  52  browser.loadFiles = function(files) {
  53      this.files = [];
  54      $.each(files, function(i, file) {
  55          browser.files[i] = {
  56              name: browser.xmlData(file.getElementsByTagName('name')[0].childNodes),
  57              size: file.getAttribute('size'),
  58              mtime: file.getAttribute('mtime'),
  59              date: file.getAttribute('date'),
  60              readable: file.getAttribute('readable') == 'yes',
  61              writable: file.getAttribute('writable') == 'yes',
  62              bigIcon: file.getAttribute('bigIcon') == 'yes',
  63              smallIcon: file.getAttribute('smallIcon') == 'yes',
  64              thumb: file.getAttribute('thumb') == 'yes',
  65              smallThumb: file.getAttribute('smallThumb') == 'yes'
  66          };
  67      });
  68  };
  69  
  70  browser.showFiles = function(callBack, selected) {
  71      this.fadeFiles();
  72      setTimeout(function() {
  73          var html = '';
  74          $.each(browser.files, function(i, file) {
  75              if (_.kuki.get('view') == 'list') {
  76                  if (!i) html += '<table summary="list">';
  77                  var icon = _.getFileExtension(file.name);
  78                  if (file.thumb)
  79                      icon = ".image";
  80                  else if (!icon.length || !file.smallIcon)
  81                      icon = ".";
  82                  icon = 'themes/' + browser.theme + '/img/files/small/' + icon + '.png';
  83                  html += '<tr class="file">' +
  84                      '<td class="name" style="background-image:url(' + icon + ')">' + _.htmlData(file.name) + '</td>' +
  85                      '<td class="time">' + file.date + '</td>' +
  86                      '<td class="size">' + browser.humanSize(file.size) + '</td>' +
  87                  '</tr>';
  88                  if (i == browser.files.length - 1) html += '</table>';
  89              } else {
  90                  if (file.thumb)
  91                      var icon = browser.baseGetData('thumb') + '&file=' + encodeURIComponent(file.name);
  92                  else if (file.smallThumb) {
  93                      var icon = browser.uploadURL + '/' + browser.dir + '/' + file.name;
  94                      icon = _.escapeDirs(icon).replace(/\'/g, "%27");
  95                  } else {
  96                      var icon = file.bigIcon ? _.getFileExtension(file.name) : ".";
  97                      if (!icon.length) icon = ".";
  98                      icon = 'themes/' + browser.theme + '/img/files/big/' + icon + '.png';
  99                  }
 100                  html += '<div class="file">' +
 101                      '<div class="thumb" style="background-image:url(\'' + icon + '\')" ></div>' +
 102                      '<div class="name">' + _.htmlData(file.name) + '</div>' +
 103                      '<div class="time">' + file.date + '</div>' +
 104                      '<div class="size">' + browser.humanSize(file.size) + '</div>' +
 105                  '</div>';
 106              }
 107          });
 108          $('#files').html('<div>' + html + '<div>');
 109          $.each(browser.files, function(i, file) {
 110              var item = $('#files .file').get(i);
 111              $(item).data(file);
 112              if (file.name == selected)
 113              $(item).addClass('selected');
 114          });
 115          $('#files > div').css({opacity:'', filter:''});
 116          if (callBack) callBack();
 117          browser.initFiles();
 118      }, 200);
 119  };
 120  
 121  browser.selectFile = function(file, e) {
 122      if (e.ctrlKey) {
 123          if (file.hasClass('selected'))
 124              file.removeClass('selected');
 125          else
 126              file.addClass('selected');
 127          var files = $('.file.selected').get();
 128          var size = 0;
 129          if (!files.length)
 130              this.statusDir();
 131          else {
 132              $.each(files, function(i, cfile) {
 133                  size += parseInt($(cfile).data('size'));
 134              });
 135              size = this.humanSize(size);
 136              if (files.length > 1)
 137                  $('#fileinfo').html(files.length + ' ' + this.label("selected files") + ' (' + size + ')');
 138              else {
 139                  var data = $(files[0]).data();
 140                  $('#fileinfo').html(data.name + ' (' + this.humanSize(data.size) + ', ' + data.date + ')');
 141              }
 142          }
 143      } else {
 144          var data = file.data();
 145          $('.file').removeClass('selected');
 146          file.addClass('selected');
 147          $('#fileinfo').html(data.name + ' (' + this.humanSize(data.size) + ', ' + data.date + ')');
 148      }
 149  };
 150  
 151  browser.selectAll = function() {
 152      var files = $('.file').get();
 153      if (files.length) {
 154          var size = 0;
 155          $.each(files, function(i, file) {
 156              if (!$(file).hasClass('selected'))
 157                  $(file).addClass('selected');
 158              size += parseInt($(file).data('size'));
 159          });
 160          size = this.humanSize(size);
 161          $('#fileinfo').html(files.length + ' ' + this.label("selected files") + ' (' + size + ')');
 162      }
 163  };
 164  
 165  browser.returnFile = function(file) {
 166              
 167      var fileURL = file.substr
 168          ? file : browser.uploadURL + '/' + browser.dir + '/' + file.data('name');
 169      fileURL = _.escapeDirs(fileURL);
 170  
 171      if (this.opener.CKEditor) {
 172          this.opener.CKEditor.object.tools.callFunction(this.opener.CKEditor.funcNum, fileURL, '');
 173          window.close();
 174  
 175      } else if (this.opener.FCKeditor) {
 176          window.opener.SetUrl(fileURL) ;
 177          window.close() ;
 178  
 179      } else if (this.opener.TinyMCE) {
 180          var win = tinyMCEPopup.getWindowArg('window');
 181          win.document.getElementById(tinyMCEPopup.getWindowArg('input')).value = fileURL;
 182          if (win.getImageData) win.getImageData();
 183          if (typeof(win.ImageDialog) != "undefined") {
 184              if (win.ImageDialog.getImageData)
 185                  win.ImageDialog.getImageData();
 186              if (win.ImageDialog.showPreviewImage)
 187                  win.ImageDialog.showPreviewImage(fileURL);
 188          }
 189          tinyMCEPopup.close();
 190  
 191      } else if (this.opener.callBack) {
 192  
 193          if (window.opener && window.opener.KCFinder) {
 194              this.opener.callBack(fileURL);
 195              window.close();
 196          }
 197  
 198          if (window.parent && window.parent.KCFinder) {
 199              var button = $('#toolbar a[href="kcact:maximize"]');
 200              if (button.hasClass('selected'))
 201                  this.maximize(button);
 202              this.opener.callBack(fileURL);
 203          }
 204  
 205      } else if (this.opener.callBackMultiple) {
 206          if (window.opener && window.opener.KCFinder) {
 207              this.opener.callBackMultiple([fileURL]);
 208              window.close();
 209          }
 210  
 211          if (window.parent && window.parent.KCFinder) {
 212              var button = $('#toolbar a[href="kcact:maximize"]');
 213              if (button.hasClass('selected'))
 214                  this.maximize(button);
 215              this.opener.callBackMultiple([fileURL]);
 216          }
 217  
 218      } else { // This condition added to handle image select in Email Templates of Email Campaign - sudheer
 219          var imageURL = fileURL.replace(/\s/g,"%20");
 220          window.opener.CKEDITOR.tools.callFunction(0, imageURL,'');
 221          window.close();
 222      }
 223  };
 224  
 225  browser.returnFiles = function(files) {
 226      if (this.opener.callBackMultiple && files.length) {
 227          var rfiles = [];
 228          $.each(files, function(i, file) {
 229              rfiles[i] = browser.uploadURL + '/' + browser.dir + '/' + $(file).data('name');
 230              rfiles[i] = _.escapeDirs(rfiles[i]);
 231          });
 232          this.opener.callBackMultiple(rfiles);
 233          if (window.opener) window.close()
 234      }
 235  };
 236  
 237  browser.returnThumbnails = function(files) {
 238      if (this.opener.callBackMultiple) {
 239          var rfiles = [];
 240          var j = 0;
 241          $.each(files, function(i, file) {
 242              if ($(file).data('thumb')) {
 243                  rfiles[j] = browser.thumbsURL + '/' + browser.dir + '/' + $(file).data('name');
 244                  rfiles[j] = _.escapeDirs(rfiles[j++]);
 245              }
 246          });
 247          this.opener.callBackMultiple(rfiles);
 248          if (window.opener) window.close()
 249      }
 250  };
 251  
 252  browser.menuFile = function(file, e) {
 253      var data = file.data();
 254      var path = this.dir + '/' + data.name;
 255      var files = $('.file.selected').get();
 256      var html = '';
 257  
 258      if (file.hasClass('selected') && files.length && (files.length > 1)) {
 259          var thumb = false;
 260          var notWritable = 0;
 261          var cdata;
 262          $.each(files, function(i, cfile) {
 263              cdata = $(cfile).data();
 264              if (cdata.thumb) thumb = true;
 265              if (!data.writable) notWritable++;
 266          });
 267          if (this.opener.callBackMultiple) {
 268              html += '<a href="kcact:pick">' + this.label("Select") + '</a>';
 269              if (thumb) html +=
 270                  '<a href="kcact:pick_thumb">' + this.label("Select Thumbnails") + '</a>';
 271              html += '<div class="delimiter"></div>';
 272          }
 273          if (this.support.zip) html+=
 274              '<a href="kcact:download">' + this.label("Download") + '</a>';
 275  
 276          if (!this.readonly) html +=
 277              '<div class="delimiter"></div>' +
 278              '<a href="kcact:clpbrdadd">' + this.label("Add to Clipboard") + '</a>' +
 279              '<div class="delimiter"></div>' +
 280              '<a href="kcact:rm"' + ((notWritable == files.length) ? ' class="denied"' : '') + '>' + this.label("Delete") + '</a>';
 281  
 282          if (html.length) {
 283              html = '<div class="menu">' + html + '</div>';
 284              $('#dialog').html(html);
 285              this.showMenu(e);
 286          } else
 287              return;
 288  
 289          $('.menu a[href="kcact:pick"]').click(function() {
 290              browser.returnFiles(files);
 291              browser.hideDialog();
 292              return false;
 293          });
 294  
 295          $('.menu a[href="kcact:pick_thumb"]').click(function() {
 296              browser.returnThumbnails(files);
 297              browser.hideDialog();
 298              return false;
 299          });
 300  
 301          $('.menu a[href="kcact:download"]').click(function() {
 302              browser.hideDialog();
 303              var pfiles = [];
 304              $.each(files, function(i, cfile) {
 305                  pfiles[i] = $(cfile).data('name');
 306              });
 307              browser.post(browser.baseGetData('downloadSelected'), {dir:browser.dir, files:pfiles});
 308              return false;
 309          });
 310  
 311          $('.menu a[href="kcact:clpbrdadd"]').click(function() {
 312              browser.hideDialog();
 313              var msg = '';
 314              $.each(files, function(i, cfile) {
 315                  var cdata = $(cfile).data();
 316                  var failed = false;
 317                  for (i = 0; i < browser.clipboard.length; i++)
 318                      if ((browser.clipboard[i].name == cdata.name) &&
 319                          (browser.clipboard[i].dir == browser.dir)
 320                      ) {
 321                          failed = true
 322                          msg += cdata.name + ": " + browser.label("This file is already added to the Clipboard.") + "\n";
 323                          break;
 324                      }
 325  
 326                  if (!failed) {
 327                      cdata.dir = browser.dir;
 328                      browser.clipboard[browser.clipboard.length] = cdata;
 329                  }
 330              });
 331              browser.initClipboard();
 332              if (msg.length) alert(msg.substr(0, msg.length - 1));
 333              return false;
 334          });
 335  
 336          $('.menu a[href="kcact:rm"]').click(function() {
 337              if ($(this).hasClass('denied')) return false;
 338              browser.hideDialog();
 339              var failed = 0;
 340              var dfiles = [];
 341              $.each(files, function(i, cfile) {
 342                  var cdata = $(cfile).data();
 343                  if (!cdata.writable)
 344                      failed++;
 345                  else
 346                      dfiles[dfiles.length] = browser.dir + "/" + cdata.name;
 347              });
 348              if (failed == files.length) {
 349                  alert(browser.label("The selected files are not removable."))
 350                  return false;
 351              }
 352              if (failed) {
 353                  if (!confirm(browser.label("{count} selected files are not removable. Do you want to delete the rest?", {count:failed})))
 354                      return false;
 355              } else if (!confirm(browser.label("Are you sure you want to delete all selected files?")))
 356                  return false;
 357  
 358              browser.fadeFiles();
 359              $.ajax({
 360                  type: 'POST',
 361                  url: browser.baseGetData('rm_cbd'),
 362                  data: {files:dfiles},
 363                  async: false,
 364                  success: function(xml) {
 365                      browser.errors(xml);
 366                      browser.refresh();
 367                  },
 368                  error: function(request, error) {
 369                      $('#files > div').css('opacity', '');
 370                      $('#files > div').css('filter', '');
 371                      alert(browser.label("Unknown error."));
 372                  }
 373              });
 374              return false;
 375          });
 376  
 377      } else {
 378          html += '<div class="menu">';
 379          $('.file').removeClass('selected');
 380          file.addClass('selected');
 381          $('#fileinfo').html(data.name + ' (' + this.humanSize(data.size) + ', ' + data.date + ')');
 382          if (this.opener.callBack || this.opener.callBackMultiple) {
 383              html += '<a href="kcact:pick">' + this.label("Select") + '</a>';
 384              if (data.thumb) html +=
 385                  '<a href="kcact:pick_thumb">' + this.label("Select Thumbnail") + '</a>';
 386              html += '<div class="delimiter"></div>';
 387          }
 388  
 389          if (data.thumb)
 390              html +='<a href="kcact:view">' + this.label("View") + '</a>';
 391  
 392          html +=
 393              '<a href="kcact:download">' + this.label("Download") + '</a>';
 394  
 395          if (!this.readonly) html +=
 396              '<div class="delimiter"></div>' +
 397              '<a href="kcact:clpbrdadd">' + this.label("Add to Clipboard") + '</a>' +
 398              '<div class="delimiter"></div>' +
 399              '<a href="kcact:mv"' + (!data.writable ? ' class="denied"' : '') + '>' + this.label("Rename...") + '</a>' +
 400              '<a href="kcact:rm"' + (!data.writable ? ' class="denied"' : '') + '>' + this.label("Delete") + '</a>';
 401          html += '</div>';
 402  
 403          $('#dialog').html(html);
 404          this.showMenu(e);
 405  
 406          $('.menu a[href="kcact:pick"]').click(function() {
 407              browser.returnFile(file);
 408              browser.hideDialog();
 409              return false;
 410          });
 411  
 412          $('.menu a[href="kcact:pick_thumb"]').click(function() {
 413              var path = browser.thumbsURL + "/" + browser.dir + '/' + data.name;
 414              browser.returnFile(path);
 415              browser.hideDialog();
 416              return false;
 417          });
 418  
 419          $('.menu a[href="kcact:view"]').click(function() {
 420              browser.hideDialog();
 421              $('#loading').html(browser.label("Loading image..."));
 422              $('#loading').css('display', 'inline');
 423              var img = new Image();
 424              var url = _.escapeDirs(browser.uploadURL + '/' + path);
 425              img.src = url;
 426              img.onload = function() {
 427                  $('#loading').css('display', 'none');
 428                  $('#dialog').html('<img />');
 429                  $('#dialog img').attr('src', url);
 430                  var o_w = $('#dialog').outerWidth();
 431                  var o_h = $('#dialog').outerHeight();
 432                  var f_w = $(window).width() - 30;
 433                  var f_h = $(window).height() - 30;
 434                  if ((o_w > f_w) || (o_h > f_h)) {
 435                      if ((f_w / f_h) > (o_w / o_h))
 436                          f_w = parseInt((o_w * f_h) / o_h);
 437                      else if ((f_w / f_h) < (o_w / o_h))
 438                          f_h = parseInt((o_h * f_w) / o_w);
 439                      $('#dialog img').attr('width', f_w);
 440                      $('#dialog img').attr('height', f_h);
 441                  }
 442                  $('#dialog').click(function() {
 443                      browser.hideDialog();
 444                  });
 445                  browser.showDialog();
 446              }
 447              return false;
 448          });
 449  
 450          $('.menu a[href="kcact:download"]').click(function() {
 451              var html = '<form id="downloadForm" method="post" action="' + browser.baseGetData('download') + '">' +
 452                  '<input type="hidden" name="dir" />' +
 453                  '<input type="hidden" name="file" />' +
 454              '</form>';
 455              $('#dialog').html(html);
 456              $('#downloadForm input').get(0).value = browser.dir;
 457              $('#downloadForm input').get(1).value = data.name;
 458              $('#downloadForm').submit();
 459              return false;
 460          });
 461  
 462          $('.menu a[href="kcact:clpbrdadd"]').click(function() {
 463              for (i = 0; i < browser.clipboard.length; i++)
 464                  if ((browser.clipboard[i].name == data.name) &&
 465                      (browser.clipboard[i].dir == browser.dir)
 466                  ) {
 467                      browser.hideDialog();
 468                      alert(browser.label("This file is already added to the Clipboard."));
 469                      return false;
 470                  }
 471              var cdata = data;
 472              cdata.dir = browser.dir;
 473              browser.clipboard[browser.clipboard.length] = cdata;
 474              browser.initClipboard();
 475              browser.hideDialog();
 476              return false;
 477          });
 478  
 479          $('.menu a[href="kcact:mv"]').click(function(e) {
 480              if (!data.writable) return false;
 481              browser.fileNameDialog(
 482                  e, {dir: browser.dir, file: data.name},
 483                  'newName', data.name, browser.baseGetData('rename'), {
 484                      title: "New file name:",
 485                      errEmpty: "Please enter new file name.",
 486                      errSlash: "Unallowable characters in file name.",
 487                      errDot: "File name shouldn't begins with '.'"
 488                  },
 489                  function() {
 490                      browser.refresh();
 491                  }
 492              );
 493              return false;
 494          });
 495  
 496          $('.menu a[href="kcact:rm"]').click(function() {
 497              if (!data.writable) return false;
 498              browser.hideDialog();
 499              if (confirm(browser.label(
 500                  "Are you sure you want to delete this file?"
 501              )))
 502                  $.ajax({
 503                      type: 'POST',
 504                      url: browser.baseGetData('delete'),
 505                      data: {dir:browser.dir, file:data.name},
 506                      async: false,
 507                      success: function(xml) {
 508                          browser.clearClipboard();
 509                          if (browser.errors(xml)) return;
 510                          browser.refresh();
 511                      },
 512                      error: function(request, error) {
 513                          alert(browser.label("Unknown error."));
 514                      }
 515                  });
 516              return false;
 517          });
 518      }
 519  };


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1