FileExplorer.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. MWF.xDesktop.requireApp("cms.ColumnManager", "Explorer", null, false);
  2. MWF.xApplication.cms.ColumnManager.FileExplorer = new Class({
  3. Extends: MWF.xApplication.cms.ColumnManager.Explorer,
  4. Implements: [Options, Events],
  5. options: {
  6. "create": MWF.CMSCM.LP.file.create,
  7. "search": MWF.CMSCM.LP.file.search,
  8. "searchText": MWF.CMSCM.LP.file.searchText,
  9. "noElement": MWF.CMSCM.LP.file.noDictionaryNoticeText
  10. },
  11. // createSearchElementNode: function(){
  12. // this.titleActionNodeNode = new Element("div", {
  13. // "styles": this.css.titleActionNode,
  14. // "text": MWF.CMSCM.LP.file.loadFiles
  15. // }).inject(this.toolbarNode);
  16. // this.titleActionNodeNode.addEvent("click", function(){
  17. // this.implodeFiles();
  18. // }.bind(this));
  19. // },
  20. getNewData: function(){
  21. return {
  22. "id": "",
  23. "name": "",
  24. "alias": "",
  25. "description": "",
  26. //"application": (this.app.options.application || this.app.application).id,
  27. "appId" : (this.app.options.application || this.app.application).id,
  28. "fileName": ""
  29. }
  30. },
  31. getSizeText: function(s){
  32. var o = [
  33. {"t": "K", "i": 1024},
  34. {"t": "M", "i": 1024*1024},
  35. {"t": "G", "i": 1024*1024*1024}
  36. ];
  37. var i = 0;
  38. var n = s/o[i].i;
  39. while (n>1000 && i<2){
  40. i++;
  41. n = s/o[i].i;
  42. }
  43. n = Math.round(n*100)/100;
  44. return ""+n+" "+o[i].t;
  45. },
  46. implodeFiles: function(){
  47. MWF.require("MWF.widget.Upload", function(){
  48. new MWF.widget.Upload(this.app.content, {
  49. "action": MWF.Actions.get("x_cms_assemble_control").action,
  50. "multiple": true,
  51. "method": "uploadFile",
  52. "parameter": {"id": ""},
  53. "onBeforeUploadEntry": function(file, up){
  54. var data = this.getNewData();
  55. data.name = file.name;
  56. data.fileName = file.name;
  57. data.description = file.name+" "+this.getSizeText(file.size);
  58. data.updateTime = (new Date()).format("db");
  59. MWF.Actions.get("x_cms_assemble_control").saveFile(data, function(json){
  60. up.options.parameter = {"id": json.data.id};
  61. var node = this.elementContentListNode.getFirst();
  62. if (node) if (node.hasClass("noElementNode")){
  63. node.destroy();
  64. }
  65. var itemObj = this._getItemObject(data);
  66. itemObj.load();
  67. }.bind(this), null, false);
  68. }.bind(this)
  69. }).load();
  70. }.bind(this));
  71. },
  72. loadContentNode: function(){
  73. this.cssPath = this.path+this.options.style+"/file.css";
  74. this.elementContentNode = new Element("div", {
  75. "styles": this.css.elementContentNode
  76. }).inject(this.node);
  77. this.elementContentNode.addEvent("click", function(){
  78. while (this.selectMarkItems.length){
  79. this.selectMarkItems[0].unSelected();
  80. }
  81. }.bind(this));
  82. this.elementContentListNode = new Element("div", {
  83. "styles": this.css.elementContentListNode
  84. }).inject(this.elementContentNode);
  85. this.elementContentListNode.loadCss(this.cssPath);
  86. this.setContentSize();
  87. this.app.addEvent("resize", function(){this.setContentSize();}.bind(this));
  88. },
  89. _createElement: function(e){
  90. this.implodeFiles();
  91. },
  92. _loadItemDataList: function(callback){
  93. var id = "";
  94. if (this.app.application) id = this.app.application.id;
  95. if (this.app.options.application) id = this.app.options.application.id;
  96. this.actions.listFile(id,callback);
  97. },
  98. _getItemObject: function(item){
  99. return new MWF.xApplication.cms.ColumnManager.FileExplorer.File(this, item)
  100. },
  101. setTooltip: function(){
  102. this.options.tooltip = {
  103. "create": MWF.CMSCM.LP.file.create,
  104. "search": MWF.CMSCM.LP.file.search,
  105. "searchText": MWF.CMSCM.LP.file.searchText,
  106. "noElement": MWF.CMSCM.LP.file.noScriptNoticeText
  107. };
  108. },
  109. deleteItems: function(){
  110. this.hideDeleteAction();
  111. while (this.deleteMarkItems.length){
  112. var item = this.deleteMarkItems.shift();
  113. if (this.deleteMarkItems.length){
  114. item.deleteFile();
  115. }else{
  116. item.deleteFile(function(){
  117. }.bind(this));
  118. }
  119. }
  120. },
  121. destroy: function(){
  122. this.node.destroy();
  123. o2.removeCss(this.cssPath);
  124. o2.release(this);
  125. },
  126. getIconJson: function(callback){
  127. if (!this.icons){
  128. MWF.getJSON("/x_component_File/$Main/icon.json", function(json){
  129. this.icons = json;
  130. if (callback) callback();
  131. }.bind(this), false, false);
  132. }else{
  133. if (callback) callback();
  134. }
  135. },
  136. getIcon: function(fileName){
  137. this.getIconJson();
  138. var ext = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length);
  139. if (!ext) ext="unkonw";
  140. var iconName = this.icons[ext.toLowerCase()] || this.icons.unknow;
  141. return "/x_component_File/$Main/default/file/"+iconName;
  142. }
  143. });
  144. MWF.xApplication.cms.ColumnManager.FileExplorer.File = new Class({
  145. Extends: MWF.xApplication.cms.ColumnManager.Explorer.Item,
  146. load: function(){
  147. var view = this.explorer.path+this.explorer.options.style+"/file.html";
  148. this.node = new Element("div.o2_fileItemNode", {
  149. "events": {
  150. "mouseover": function(){
  151. if (this.deleteActionNode) this.deleteActionNode.fade("in");
  152. if (this.saveasActionNode) this.saveasActionNode.fade("in");
  153. }.bind(this),
  154. "mouseout": function(){
  155. if (this.deleteActionNode) this.deleteActionNode.fade("out");
  156. if (this.saveasActionNode) this.saveasActionNode.fade("out");
  157. }.bind(this)
  158. }
  159. }).inject(this.container);
  160. if (this.data.name.icon) this.icon = this.data.name.icon;
  161. var ext = this.data.fileName.substring(this.data.fileName.lastIndexOf(".")+1, this.data.fileName.length);
  162. this.data.fileUrl = this._getUrl();
  163. if (["png","jpg","bmp","gif","jpeg","jpe"].indexOf(ext)!==-1){
  164. //new Image()
  165. this.data.iconUrl = this.data.fileUrl;
  166. }else{
  167. this.data.iconUrl = this.explorer.getIcon(this.data.fileName);
  168. }
  169. //this.data.iconUrl = this.explorer.path+""+this.explorer.options.style+"/processIcon/"+this.icon;
  170. this.node.loadHtml(view, {"bind": this.data}, function(){
  171. var itemIconNode = this.node.getElement(".o2_fileItemIconNode");
  172. var itemImgNode = this.node.getElement(".o2_fileItemImgNode");
  173. var itemUrlNode = this.node.getElement(".o2_fileItemUrlNode");
  174. itemUrlNode.setStyle("-webkit-user-select", "text");
  175. var s = itemIconNode.getSize();
  176. // if (!s.x) s.x = itemIconNode.getStyle("width").toFloat();
  177. // if (!s.y) s.y = itemIconNode.getStyle("height").toFloat();
  178. itemImgNode.setStyles({
  179. "max-width": ""+s.x+"px",
  180. "max-height": ""+s.y+"px"
  181. });
  182. this.deleteActionNode = this.node.getElement(".o2_fileDeleteActionNode");
  183. var itemTextTitleNode = this.node.getElement(".o2_fileItemTextTitleNode");
  184. itemIconNode.addEvent("click", function(e){
  185. this.toggleSelected();
  186. e.stopPropagation();
  187. }.bind(this));
  188. itemIconNode.makeLnk({
  189. "par": this._getLnkPar()
  190. });
  191. if (!this.explorer.options.noDelete) this._createActions();
  192. itemTextTitleNode.addEvent("click", function(e){
  193. this._open(e);
  194. e.stopPropagation();
  195. }.bind(this));
  196. this._customNodes();
  197. this._isNew();
  198. }.bind(this));
  199. },
  200. _createActions: function(){
  201. if (this.deleteActionNode) this.deleteActionNode.addEvent("click", function(e){
  202. this.deleteItem(e);
  203. }.bind(this));
  204. },
  205. _customNodes: function(){},
  206. _open: function(e){
  207. var _self = this;
  208. MWF.Actions.get("x_cms_assemble_control").getFile(this.data.id, function(json){
  209. this.data = json.data;
  210. new MWF.xApplication.cms.ColumnManager.FileDesigner(this.explorer, this.data);
  211. }.bind(this));
  212. },
  213. _getIcon: function(){
  214. return "file.png";
  215. },
  216. _getUrl: function(){
  217. var url = MWF.Actions.get("x_cms_assemble_control").action.actions.readFile.uri;
  218. url = url.replace(/{flag}/, this.data.id);
  219. url = url.replace(/{applicationFlag}/, this.data.application || this.data.appId);
  220. url = "/x_cms_assemble_control"+url;
  221. return MWF.Actions.getHost("x_cms_assemble_control")+url;
  222. },
  223. _getLnkPar: function(){
  224. return {
  225. "icon": this.data.iconUrl,
  226. "title": this.data.name,
  227. "par": "@url#"+this._getUrl()
  228. };
  229. },
  230. deleteFile: function(callback){
  231. this.explorer.app.restActions.deleteFile(this.data.id, function(){
  232. this.node.destroy();
  233. if (callback) callback();
  234. }.bind(this));
  235. }
  236. });
  237. MWF.xApplication.cms.ColumnManager.FileDesigner = new Class({
  238. initialize: function(explorer, item){
  239. this.explorer = explorer;
  240. this.app = this.explorer.app;
  241. this.data = item;
  242. this.container = this.explorer.container;
  243. this.css = this.explorer.css;
  244. this.lp = MWF.CMSCM.LP;
  245. this.load();
  246. },
  247. getNewData: function(){
  248. return {
  249. "id": "",
  250. "name": "",
  251. "alias": "",
  252. "description": "",
  253. //"application": (this.explorer.app.options.application || this.explorer.app.application).id,
  254. "appId": (this.explorer.app.options.application || this.explorer.app.application).id,
  255. "fileName": ""
  256. }
  257. },
  258. resize: function(){
  259. var size = this.app.content.getSize();
  260. var nodeSize = this.fileAreaNode.getSize();
  261. var x = (size.x-nodeSize.x)/2;
  262. var y = (size.y-nodeSize.y)/2;
  263. if (y<0) y=0;
  264. if (x<0) x=0;
  265. this.fileAreaNode.setStyles({
  266. "top": ""+y+"px",
  267. "left": ""+x+"px"
  268. });
  269. var titleSize = this.titleNode.getSize();
  270. var buttonSize = this.buttonNode.getSize();
  271. var h = nodeSize.y-titleSize.y-buttonSize.y;
  272. this.contentNode.setStyle("height", ""+h+"px");
  273. },
  274. load: function(){
  275. if (!this.data) this.data = this.getNewData();
  276. this.fileMaskNode = new Element("div", {"styles": this.css.createTemplateMaskNode}).inject(this.app.content);
  277. this.fileAreaNode = new Element("div", {"styles": this.css.createFormTemplateAreaNode}).inject(this.app.content);
  278. this.fileAreaNode.fade("in");
  279. this.titleNode = new Element("div", {"styles": this.css.fileDesignerTitleNode}).inject(this.fileAreaNode);
  280. this.titleIconNode = new Element("div", {"styles": this.css.fileDesignerTitleIconNode}).inject(this.titleNode);
  281. if (!this.data.id) this.titleIconNode.setStyle("background-image", "url("+this.explorer.path+this.app.options.style+"/icon/new.png)");
  282. this.titleTextNode = new Element("div", {"styles": this.css.fileDesignerTitleTextNode}).inject(this.titleNode);
  283. var title = (this.data.name) ? this.data.name : this.explorer.options.tooltip.create;
  284. this.titleTextNode.set("text", title);
  285. this.contentNode = new Element("div", {"styles": this.css.fileDesignerContentNode}).inject(this.fileAreaNode);
  286. this.createContent();
  287. this.buttonNode = new Element("div", {"styles": this.css.fileDesignerButtonNode}).inject(this.fileAreaNode);
  288. this.createButton();
  289. this.resizeFun = this.resize.bind(this);
  290. this.resizeFun();
  291. this.app.addEvent("resize", this.resizeFun);
  292. this.setEvent();
  293. },
  294. createContent: function(){
  295. this.contentAreaNode = new Element("div", {"styles": this.css.fileDesignerContentAreaNode}).inject(this.contentNode);
  296. this.nameInput = this.createContentLine(this.lp.name, this.data.name);
  297. this.aliasInput = this.createContentLine(this.lp.alias, this.data.alias);
  298. this.descriptionInput = this.createContentLine(this.lp.file.description, this.data.description, true);
  299. this.createContentFile();
  300. this.createContentFileUrl();
  301. },
  302. createContentFileUrl: function(){
  303. if (this.data.fileName){
  304. var div = new Element("div", {"styles": this.css.fileDesignerContentLineNode}).inject(this.contentAreaNode);
  305. var lineTitleNode = new Element("div", {"styles": this.css.fileDesignerContentLineTitleNode, "text": "URL"}).inject(div);
  306. this.fileUrlNode = new Element("div", {"styles": this.css.fileDesignerContentLineContentNode}).inject(div);
  307. div.setStyle("height", "80px");
  308. var url = MWF.Actions.get("x_cms_assemble_control").action.actions.readFile.uri;
  309. url = url.replace(/{flag}/, this.data.id);
  310. url = url.replace(/{applicationFlag}/, this.data.application || this.data.appId);
  311. url = "/x_cms_assemble_control"+url;
  312. this.fileUrlNode.setStyle("line-height", "18px");
  313. var href = MWF.Actions.getHost("x_cms_assemble_control")+url;
  314. //this.fileUrlNode.set("html", "<a target='_blank' href='"+href+"'>"+url+"</a>");
  315. this.fileUrlNode.set("text", url);
  316. var a = new Element("div", {
  317. "styles": {"height": "30px"},
  318. "html": "<a target='_blank' href='"+href+"'>open</a>"
  319. }).inject(this.fileUrlNode, "bottom");
  320. }
  321. },
  322. modifyContentFileUrl: function(){
  323. if (!this.fileUrlNode){
  324. this.createContentFileUrl();
  325. }else{
  326. var url = MWF.Actions.get("x_cms_assemble_control").action.actions.readFile.uri;
  327. url = url.replace(/{flag}/, this.data.id);
  328. url = url.replace(/{applicationFlag}/, this.data.application || this.data.appId);
  329. //this.fileUrlNode.set("text", "/x_cms_assemble_control"+url);
  330. url = "/x_cms_assemble_control"+url;
  331. this.fileUrlNode.setStyle("line-height", "18px");
  332. var href = MWF.Actions.getHost("x_cms_assemble_control")+url;
  333. //this.fileUrlNode.set("html", "<a target='_blank' href='"+href+"'>"+url+"</a>");
  334. this.fileUrlNode.set("text", url);
  335. var a = new Element("div", {
  336. "styles": {"height": "30px"},
  337. "html": "<a target='_blank' href='"+href+"'>open</a>"
  338. }).inject(this.fileUrlNode, "bottom");
  339. }
  340. },
  341. createContentFile: function(){
  342. var div = new Element("div", {"styles": this.css.fileDesignerContentFileLineNode}).inject(this.contentAreaNode);
  343. var lineTitleNode = new Element("div", {"styles": this.css.fileDesignerContentFileLineTitleNode, "text": this.lp.attachment}).inject(div);
  344. var lineRightNode = new Element("div", {"styles": this.css.fileDesignerContentFileLineRightNode}).inject(div);
  345. this.fileContentNode = new Element("div", {"styles": this.css.fileDesignerContentFileLineContentNode}).inject(div);
  346. this.uploadFileButton = new Element("div", {"styles": this.css.fileDesignerUploadButtonNode, "text": this.lp.upload}).inject(lineRightNode);
  347. if (this.data.fileName){
  348. this.loadFileIcon();
  349. }
  350. },
  351. // getIconJson: function(callback){
  352. // if (!this.icons){
  353. // MWF.getJSON("/x_component_File/$Main/icon.json", function(json){
  354. // this.icons = json;
  355. // if (callback) callback();
  356. // }.bind(this), false, false);
  357. // }else{
  358. // if (callback) callback();
  359. // }
  360. // },
  361. // getIcon: function(ext){
  362. // if (!ext) ext="unkonw";
  363. // var iconName = this.icons[ext.toLowerCase()] || this.icons.unknow;
  364. // return "/x_component_File/$Main/default/file/"+iconName;
  365. // },
  366. loadFileIcon: function(){
  367. debugger;
  368. this.fileContentNode.empty();
  369. //var ext = this.data.fileName.substr(this.data.fileName.lastIndexOf(".")+1, this.data.fileName.length);
  370. //this.explorer.getIconJson(function(){
  371. var url = this.explorer.getIcon(this.data.fileName);
  372. var fileIconNode = new Element("div", {"styles": this.css.fileDesignerContentFileLineFileIconNode}).inject(this.fileContentNode);
  373. fileIconNode.setStyle("background-image", "url('"+url+"')");
  374. var fileTextNode = new Element("div", {"styles": this.css.fileDesignerContentFileLineFileNameNode, "text": this.data.fileName}).inject(this.fileContentNode);
  375. var fileSizeNode = new Element("div", {"styles": this.css.fileDesignerContentFileLineFileSizeNode, "text": this.data.description}).inject(this.fileContentNode);
  376. //}.bind(this));
  377. },
  378. createContentLine: function(text, value, readonly){
  379. var div = new Element("div", {"styles": this.css.fileDesignerContentLineNode}).inject(this.contentAreaNode);
  380. var lineTitleNode = new Element("div", {"styles": this.css.fileDesignerContentLineTitleNode, "text": text}).inject(div);
  381. var lineContentNode = new Element("div", {"styles": this.css.fileDesignerContentLineContentNode}).inject(div);
  382. return new Element("input", {"styles": this.css.fileDesignerContentLineInputNode, "value": value, "readonly": readonly}).inject(lineContentNode);
  383. },
  384. createButton: function(){
  385. this.cancelButton = new Element("div", {"styles": this.css.fileDesignerCancelButtonNode, "text": this.lp.cancel}).inject(this.buttonNode);
  386. this.okButton = new Element("div", {"styles": this.css.fileDesignerOkButtonNode, "text": this.lp.ok}).inject(this.buttonNode);
  387. },
  388. setEvent: function(){
  389. this.cancelButton.addEvent("click", function(e){ this.close(e); }.bind(this));
  390. this.okButton.addEvent("click", function(){ this.save(); }.bind(this));
  391. this.uploadFileButton.addEvent("click", function(){ this.upload(); }.bind(this));
  392. },
  393. upload: function(){
  394. if (!this.data.id){
  395. //MWF.Actions.get("x_cms_assemble_control").saveFile(this.data, function(){
  396. // this.explorer.reload();
  397. this.uploadFile(function(){
  398. this.app.notice(this.lp.file.uploadSuccess, "success");
  399. }.bind(this));
  400. //}.bind(this));
  401. }else{
  402. this.uploadFile(function(){
  403. this.app.notice(this.lp.file.uploadSuccess, "success");
  404. }.bind(this));
  405. }
  406. },
  407. uploadFile: function(callback){
  408. MWF.require("MWF.widget.Upload", function(){
  409. new MWF.widget.Upload(this.app.content, {
  410. "action": MWF.Actions.get("x_cms_assemble_control").action,
  411. "multiple": false,
  412. "method": "uploadFile",
  413. "parameter": {"id": this.data.id},
  414. "onCompleted": function(){
  415. this.loadFileIcon();
  416. this.modifyContentFileUrl();
  417. this.explorer.reload();
  418. if (callback) callback();
  419. }.bind(this),
  420. "onBeforeUpload": function(files, up){
  421. var name = files[0].name;
  422. this.nameInput.set("value", name);
  423. var data = this.getData();
  424. this.data = Object.merge(this.data, data);
  425. MWF.Actions.get("x_cms_assemble_control").saveFile(this.data, function(json){
  426. this.explorer.reload();
  427. up.options.parameter = {"id": json.data.id};
  428. }.bind(this), null, false);
  429. }.bind(this),
  430. "onEvery": function(json, current, count, file){
  431. //this.data.description = file.name+" "+this.getSizeText(file.size);
  432. //this.data.id = json.data.id;
  433. this.data.fileName = file.name;
  434. this.data.description = file.name+" "+this.getSizeText(file.size);
  435. this.descriptionInput.set("value", this.data.description);
  436. MWF.Actions.get("x_cms_assemble_control").saveFile(this.data);
  437. }.bind(this)
  438. }).load();
  439. }.bind(this));
  440. },
  441. getSizeText: function(s){
  442. var o = [
  443. {"t": "K", "i": 1024},
  444. {"t": "M", "i": 1024*1024},
  445. {"t": "G", "i": 1024*1024*1024}
  446. ];
  447. var i = 0;
  448. var n = s/o[i].i;
  449. while (n>1000 && i<2){
  450. i++;
  451. n = s/o[i].i;
  452. }
  453. n = Math.round(n*100)/100;
  454. return ""+n+" "+o[i].t;
  455. },
  456. getData: function(){
  457. return {
  458. "name": this.nameInput.get("value"),
  459. "alias": this.aliasInput.get("value"),
  460. "description": this.descriptionInput.get("value")
  461. }
  462. },
  463. close: function(e){
  464. var data = this.getData();
  465. var _self = this;
  466. if (data.name!==this.data.name || data.alias!==this.data.alias || data.description!== this.data.description){
  467. this.app.confirm("infor", e, this.lp.file.saveConfirm, this.lp.file.saveConfirmText, 350, 120, function(){
  468. this.close();
  469. _self.save();
  470. }, function(){
  471. this.close();
  472. _self.destroy();
  473. })
  474. }else{
  475. this.destroy();
  476. }
  477. },
  478. save: function(){
  479. var data = this.getData();
  480. this.data = Object.merge(this.data, data);
  481. MWF.Actions.get("x_cms_assemble_control").saveFile(this.data, function(){
  482. this.explorer.reload();
  483. this.app.notice(this.lp.file.saveSuccess, "success");
  484. this.destroy();
  485. }.bind(this));
  486. },
  487. destroy: function(){
  488. this.fileMaskNode.destroy();
  489. this.fileAreaNode.destroy();
  490. if (this.resizeFun) this.app.removeEvent("resize", this.resizeFun);
  491. MWF.release(this);
  492. }
  493. });