66eaa9e5b49d7df3a8f39a432ca654897b05899a.svn-base 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. (function(){
  2. //作用域定义
  3. var hidoger = typeof this.hidoger == 'function' ? this.hidoger : function(){};
  4. this.hidoger = hidoger;
  5. var x = hidoger;
  6. x.fileManager = (function(){
  7. var $layer, $frame, $title,
  8. //所有类型
  9. types = {'Files' : 'Files', 'Images' : 'Images', 'Flash' : 'Flash'},
  10. typesSets = {},
  11. //默认类型
  12. defType = 'Files',
  13. //链接配置项
  14. url = {
  15. type : defType
  16. };
  17. typesSets.Files = {name : '文件', exp : /.+\.*/i, type : '*'};
  18. typesSets.Images = {name : '图片', exp : /.+\.gif|jpg|png|jpeg|bmp/i, type : 'gif,jpg,png,jpeg,bmp'};
  19. typesSets.Flash = {name : 'flash', exp : /.+\.swf/i, type : 'swf'};
  20. //初始化文件管理器
  21. function _init(){
  22. var str = '<div class="file-manager" style="width:850px;height:570px;">'
  23. + '<div class="top"><h2 class="title">文件选择</h2><a class="close">×</a></div>'
  24. + '<ul class="tabs"><li class="tab">本地</li><li class="tab">网络</li></ul>'
  25. + '<div class="files-area location-files"><iframe width="100%" height="500px" frameborder="0" scrolling="yes"></iframe></div>'
  26. + '<div class="files-area net-files"><p class="tips"></p><input class="url-text" type="text" /><a class="btn">确定</a></div>'
  27. + '</div>';
  28. $layer = $(str).appendTo('body');
  29. if(x.DragBox) new x.DragBox({$box : $layer, $drag : $layer.find('div.top')});
  30. $frame = $layer.find('iframe');
  31. $title = $layer.find('h2.title');
  32. $layer.find('div.top a.close').click(function(){
  33. fm.hide();
  34. });
  35. $layer.find('ul.tabs > li.tab').each(function(i){
  36. var $this = $(this), cls = 'tab-checked',
  37. $area = i ? $layer.find('div.net-files') : $layer.find('div.location-files');
  38. $this.click(function(){
  39. if($this.hasClass(cls)) return;
  40. $this.parent().find('li.' + cls).removeClass(cls);
  41. $this.addClass(cls);
  42. $this.parents().find('div.files-area').hide();
  43. $area.show();
  44. });
  45. });
  46. $layer.find('div.net-files a.btn').click(function(){
  47. var val = $layer.find('div.net-files input.url-text').val();
  48. if(typesSets[url.type].exp.test(val)){
  49. fm.fileActive(val);
  50. }else alert('请填写正确的' + typesSets[url.type].name + '地址');
  51. });
  52. _change();
  53. }
  54. //改变链接状态
  55. function _change(){
  56. $frame.attr('src', 'frame.php?type=' + url.type);
  57. }
  58. var fm = {
  59. //设置文件显示的类型
  60. setType : function(type){
  61. if(!types[type]) type = defType;
  62. url.type = type;
  63. if($frame) _change();
  64. },
  65. setTitle : function(str){
  66. if($title) $title.text(str);
  67. },
  68. //显示
  69. show : function(o){
  70. if(!$layer) _init();
  71. o = o || {};
  72. if(o.title) this.setTitle(o.title);
  73. this.setConfig(o);
  74. var left = (parseInt(screen.availWidth) - parseInt($layer.width())) / 2,
  75. top = (parseInt(screen.availHeight) - parseInt($layer.height())) / 2;
  76. $layer.find('li.tab:eq(0)').click();
  77. $layer.find('p.tips').text('请输入外部文件的URL地址(文件类型: ' + typesSets[url.type].type + ')');
  78. $layer.css({left : left + 'px', top : top + 'px'}).fadeIn('fast');
  79. },
  80. setConfig : function(o){
  81. if(o.type) this.setType(o.type);
  82. if(o.callback) this.callback = o.callback;
  83. if(o.additionalData) this.additionalData = o.additionalData;
  84. if(o.complete) this.selectActionFunction = o.complete;
  85. },
  86. //隐藏
  87. hide : function(){
  88. if($layer) $layer.fadeOut('fast');
  89. },
  90. //文件的回调
  91. fileActive : function(fileUrl, data, allFiles){
  92. if(this.callback instanceof Function) this.callback(fileUrl, data, allFiles);
  93. },
  94. //上传完成
  95. complete : function(file){
  96. if(this.selectActionFunction instanceof Function) this.selectActionFunction(file);
  97. },
  98. //销毁
  99. destroy : function(){
  100. if($frame){
  101. $frame.remove();
  102. $layer.remove();
  103. $layer = null;
  104. $frame = null;
  105. }
  106. }
  107. };
  108. return fm;
  109. })();
  110. })();