FileSelector.js 9.1 KB

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