FileSelector.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. MWF.xApplication.File = MWF.xApplication.File || {};
  2. MWF.xDesktop.requireApp("File", "lp.zh-cn", null, false);
  3. MWF.xDesktop.requireApp("File", "Actions.RestActions", null, false);
  4. MWF.xDesktop.requireApp("File", "AttachmentSelector", null, false);
  5. MWF.xDesktop.requireApp("File", "Main", null, false);
  6. MWF.require("MWF.widget.Tree", null, false);
  7. MWF.xApplication.File.FileSelector = new Class({
  8. Extends: MWF.xApplication.File.Main,
  9. Implements: [Options, Events],
  10. options: {
  11. "title" : "",
  12. "style": "default",
  13. "listStyle" : "icon",
  14. "selectType" : "all",
  15. "toBase64" : false,
  16. "base64Width" : 800,
  17. "base64Height" : 0,
  18. "images": ["bmp", "gif", "png", "jpeg", "jpg", "jpe", "ico"],
  19. "audios": ["mp3", "wav", "wma", "wmv"],
  20. "videos": ["avi", "mkv", "mov", "ogg", "mp4", "mpa", "mpe", "mpeg", "mpg", "rmvb"]
  21. },
  22. initialize: function(container, options){
  23. this.setOptions(options);
  24. this.container = $(container);
  25. this.path = "/x_component_File/$FileSelector/";
  26. this.cssPath =this.path+this.options.style+"/css.wcss";
  27. this._loadCss();
  28. this.lp = MWF.xApplication.File.LP;
  29. },
  30. onQueryLoad: function(){
  31. this.lp = MWF.xApplication.File.LP;
  32. },
  33. load : function(){
  34. this.loadApplication();
  35. },
  36. loadApplication: function(callback){
  37. this.history = [];
  38. this.currentHistory = 1;
  39. this.currentFolder = null;
  40. this.restActions = new MWF.xApplication.File.Actions.RestActions();
  41. MWF.getJSON("/x_component_File/$Main/icon.json", function(json){
  42. this.icons = json;
  43. }.bind(this), false, false);
  44. this.createNode();
  45. if (callback) callback();
  46. },
  47. createNode: function(){
  48. this.markNode = new Element("div", {
  49. "styles": this.css.markNode,
  50. "events": {
  51. "mouseover": function(e){e.stopPropagation();},
  52. "mouseout": function(e){e.stopPropagation();}
  53. }
  54. }).inject(this.container);
  55. this.content = new Element("div", {
  56. "styles": this.css.container
  57. });
  58. this.node = new Element("div",{
  59. "styles": this.css.node
  60. }).inject(this.content);
  61. this.createTopNode();
  62. this.loadApplicationContent();
  63. this.createBottomToolbarNode();
  64. this.content.inject(this.markNode, "after");
  65. this.content.fade("in");
  66. this.setNodeSize();
  67. var size = this.container.getSize();
  68. var nodeSize = this.content.getSize();
  69. this.content.makeDraggable({
  70. "handle": this.titleNode,
  71. "limit": {
  72. "x": [0, size.x-nodeSize.x],
  73. "y": [0, size.y-nodeSize.y]
  74. }
  75. });
  76. },
  77. close : function(){
  78. this.content.destroy();
  79. this.markNode.destroy();
  80. delete this;
  81. },
  82. createTopNode: function(){
  83. if (this.options.title){
  84. if (!this.titleNode){
  85. this.titleNode = new Element("div.titleNode", {"styles": this.css.titleNode, "text": this.options.title}).inject(this.node);
  86. this.titleActionNode = new Element("div", {"styles": this.css.titleActionNode}).inject(this.titleNode);
  87. this.titleActionNode.addEvent("click",function(){this.close() }.bind(this))
  88. }
  89. }
  90. },
  91. loadFileContentAreaNode: function(){
  92. this.controller = new MWF.xApplication.File.AttachmentSelector(this.attachmentContentNode, this, {
  93. "resize": false,
  94. "isSizeChange": false,
  95. "style": this.options.style,
  96. "listStyle": this.options.listStyle
  97. })
  98. this.controller.load();
  99. },
  100. createBottomToolbarNode : function( ){
  101. this.bottomToolbarNode = new Element("div.bottomToolbarNode",{ styles : this.css.bottomToolbarNode }).inject(this.node);
  102. this.cancelButton = new Element("div",{
  103. styles : this.css.cancelButton,
  104. text : MWF.LP.widget.cancel
  105. }).inject(this.bottomToolbarNode);
  106. this.cancelButton.addEvent("click",function(){
  107. this.close();
  108. }.bind(this));
  109. this.okButton = new Element("div",{
  110. styles : this.css.okButton,
  111. text : MWF.LP.widget.ok
  112. }).inject(this.bottomToolbarNode);
  113. this.okButton.addEvent("click",function(){
  114. if( this.controller.selectedAttachments.length ){
  115. this.openAttachment(null, null, this.controller.selectedAttachments );
  116. }
  117. this.close();
  118. }.bind(this));
  119. },
  120. fiterByExtension : function( attachments ){
  121. var availableExtensions = this.options[ this.options.selectType ];
  122. if( availableExtensions ){
  123. var atts = [];
  124. while (attachments.length){
  125. var att = attachments.shift();
  126. if( availableExtensions.contains(att.extension) ){
  127. atts.push(att);
  128. }
  129. }
  130. attachments = atts;
  131. }
  132. return attachments;
  133. },
  134. loadShareFile: function(treeNode){
  135. var person = treeNode.data.name;
  136. this.restActions.listShareAttachment(function(json){
  137. //this.fileContentAreaNode.empty();
  138. this.controller.clear();
  139. this.fiterByExtension( json.data ).each(function(file){
  140. this.controller.addAttachment(file);
  141. //new MWF.xApplication.File.ShareAttachment(file, this);
  142. }.bind(this));
  143. }.bind(this), null, person);
  144. },
  145. loadEditorFile: function(treeNode){
  146. var person = treeNode.data.name;
  147. this.restActions.listEditorAttachment(function(json){
  148. //this.fileContentAreaNode.empty();
  149. this.controller.clear();
  150. this.fiterByExtension( json.data ).each(function(file){
  151. this.controller.addAttachment(file);
  152. //new MWF.xApplication.File.ShareAttachment(file, this);
  153. }.bind(this));
  154. }.bind(this), null, person);
  155. },
  156. loadSub: function(treeNode){
  157. this.setPathNode(treeNode);
  158. //this.fileContentAreaNode.empty();
  159. this.controller.clear();
  160. treeNode.children.each(function(node){
  161. var folder = this.controller.addAttachmentFolder(node.data);
  162. folder.treeNode = node;
  163. //var folder = new MWF.xApplication.File.Folder(node.data, this);
  164. //folder.treeNode = node;
  165. }.bind(this));
  166. this.currentFolder = treeNode;
  167. if (treeNode.data){
  168. this.restActions.listAttachment(function(json){
  169. this.fiterByExtension( json.data ).each(function(file){
  170. this.controller.addAttachment(file);
  171. //new MWF.xApplication.File.Attachment(file, this);
  172. }.bind(this));
  173. }.bind(this), null, treeNode.data.id);
  174. }else{
  175. this.restActions.listAttachmentTop(function(json){
  176. this.fiterByExtension( json.data ).each(function(file){
  177. this.controller.addAttachment(file);
  178. //new MWF.xApplication.File.Attachment(file, this);
  179. }.bind(this));
  180. }.bind(this));
  181. }
  182. },
  183. openAttachment: function(e, node, attachment){
  184. var id = attachment[0].data.id;
  185. this.restActions.getFileUrl( id, function(url){
  186. if( this.options.toBase64 ){
  187. this.restActions.getBase64Code(function(json){
  188. var data = json.data ? "data:image/png;base64,"+json.data.value : null;
  189. this.fireEvent("postSelectAttachment",[url, data ]);
  190. }.bind(this), null, id, this.options.base64Width, this.options.base64Height )
  191. }else{
  192. this.fireEvent("postSelectAttachment",[url]);
  193. }
  194. this.close();
  195. }.bind(this));
  196. },
  197. setContentHeight : function(node){
  198. },
  199. setNodeSize: function(node,width, height){
  200. width = width || "50%";
  201. height = height || "50%";
  202. "string" == typeof width && (1 < width.length && "%" == width.substr(width.length - 1, 1)) && (width = parseInt(window.screen.width * parseInt(width, 10) / 100, 10));
  203. "string" == typeof height && (1 < height.length && "%" == height.substr(height.length - 1, 1)) && (height = parseInt(window.screen.height * parseInt(height, 10) / 100, 10));
  204. 700 > width && (width = 700);
  205. 420 > height && (height = 420);
  206. var top = parseInt((window.screen.height - height) / 2, 10);
  207. var left = parseInt((window.screen.width - width) / 2, 10);
  208. //c = window.open("", null, d, !0);
  209. this.content.setStyles({
  210. "width": "" + width + "px",
  211. "height": "" + height + "px",
  212. "top": "" + top + "px",
  213. "left": "" + left + "px"
  214. });
  215. this.node.setStyles({
  216. "width": "" + width + "px",
  217. "height": "" + height + "px"
  218. });
  219. var titleNodeHeight = this.titleNode ? this.titleNode.getSize().y : 0;
  220. var topNodeHeight = this.topNode ? this.topNode.getSize().y : 0;
  221. var bottomToolbarNodeHeight = this.bottomToolbarNode ? this.bottomToolbarNode.getSize().y : 0;
  222. var mtt = this.topNode.getStyle("margin-top").toFloat();
  223. var mbt = this.topNode.getStyle("margin-bottom").toFloat();
  224. var mtc = this.fileContentNode.getStyle("margin-top").toFloat();
  225. var mbc = this.fileContentNode.getStyle("margin-bottom").toFloat();
  226. var h = height - titleNodeHeight - topNodeHeight - bottomToolbarNodeHeight-mtt-mbt-mtc-mbc;
  227. this.fileContentNode.setStyle("height", h);
  228. this.attachmentContentNode.setStyle("height", h);
  229. var attTopSzie = this.controller.topNode.getSize();
  230. var y = h-attTopSzie.y;
  231. this.controller.contentScrollNode.setStyle("height", ""+y+"px");
  232. var tabSize = this.treeTab.tabNodeContainer.getSize();
  233. var h = h-tabSize.y;
  234. this.folderTreeAreaScrollNode.setStyle("height", h);
  235. this.shareTreeAreaScrollNode.setStyle("height", h);
  236. this.editorTreeAreaScrollNode.setStyle("height", h);
  237. }
  238. });