Main.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. o2.require("o2.widget.O2Identity", null, false);
  2. o2.require("o2.widget.ace", null, false);
  3. MWF.xApplication.ANN.Main = new Class({
  4. Extends: MWF.xApplication.Common.Main,
  5. Implements: [Options, Events],
  6. options: {
  7. "style1": "default",
  8. "style": "default",
  9. "name": "ANN",
  10. "icon": "icon.png",
  11. "width": "1100",
  12. "height": "600",
  13. "isResize": true,
  14. "isMax": true,
  15. "title": MWF.xApplication.ANN.LP.title
  16. },
  17. initialize: function(desktop, options){
  18. this.setOptions(options);
  19. this.desktop = desktop;
  20. this.path = "/x_component_"+this.options.name.replace(/\./g, "_")+"/$Main/";
  21. this.options.icon = this.path+this.options.style+"/"+this.options.icon;
  22. //this.cssPath =this.path+this.options.style+"/css.wcss";
  23. this.stylePath = this.path+this.options.style+"/style.css";
  24. this.viewPath = this.path+this.options.style+"/view.html";
  25. },
  26. onQueryLoad: function(){
  27. this.lp = MWF.xApplication.ANN.LP;
  28. this.models = [];
  29. },
  30. loadApplication: function(callback){
  31. this.content.loadAll({css: this.stylePath, "html": this.viewPath}, {"bind": this}, function(){
  32. this.node = this.content.getElement(".o2_ann_content");
  33. this.titleNode = this.content.getElement(".o2_ann_top");
  34. this.contentArea = this.content.getElement(".o2_ann_contentArea");
  35. this.listNode = this.content.getElement(".o2_ann_leftContent");
  36. this.contentNode = this.content.getElement(".o2_ann_rightContent");
  37. this.addAction = this.content.getElement(".o2_ann_topIcon");
  38. if (!o2.AC.isAdministrator()){
  39. this.addAction.destroy();
  40. this.addAction = null;
  41. }
  42. if (this.addAction) this.addAction.addEvent("click", this.createModel.bind(this));
  43. this.resizeFun = this.resize.bind(this);
  44. this.resizeFun();
  45. this.addEvent("resize", this.resizeFun);
  46. this.loadListModel();
  47. if (callback) callback();
  48. }.bind(this));
  49. },
  50. getDefaultData: function(){
  51. return {
  52. "neuralNetworkType": "mlp",
  53. "dataType": "processPlatform",
  54. "name": this.lp.unnamed,
  55. "description": "",
  56. "alias": "",
  57. "inValueScriptText": "",
  58. "outValueScriptText": "",
  59. "attachmentScriptText": "",
  60. "processList": [],
  61. "applicationList": [],
  62. "analyzeType": "default",
  63. "maxResult": 0,
  64. "propertyMap": {}
  65. }
  66. },
  67. checkSave: function(){
  68. if (this.currentModel && this.currentModel.context && (this.currentModel.context.status==="edit" || this.currentModel.context.status==="new")){
  69. this.notice(this.lp.checkSave, "error", this.contentNode, {"x": "left", "y": "top"});
  70. return false;
  71. }else{
  72. if (this.currentModel) this.currentModel.unselected();
  73. return true;
  74. }
  75. },
  76. createModel: function(){
  77. if (this.checkSave()) this.currentModel = new MWF.xApplication.ANN.Model(this.getDefaultData(), this);
  78. },
  79. resize: function(){
  80. var size = this.node.getSize();
  81. var titleSize = this.titleNode.getComputedSize();
  82. var h = size.y-titleSize.totalHeight;
  83. this.contentArea.setStyle("height", ""+h+"px");
  84. },
  85. loadListModel: function(){
  86. o2.Actions.get("x_query_assemble_designer").listModel(function(json){
  87. json.data.each(function(item){
  88. this.models.push(new MWF.xApplication.ANN.Model(item, this));
  89. }.bind(this));
  90. }.bind(this));
  91. }
  92. });
  93. MWF.xApplication.ANN.Model = new Class({
  94. initialize: function(data, app){
  95. this.data = data;
  96. this.app = app;
  97. this.lp = app.lp;
  98. this.listNode = this.app.listNode;
  99. this.load();
  100. },
  101. reload: function(){
  102. o2.Actions.get("x_query_assemble_designer").getModel(this.data.id, function(json){
  103. this.data = json.data;
  104. if (this.context) this.context.data = this.data;
  105. this.node.getElement(".o2_ann_model_nodeContentName").set("text", this.data.name);
  106. this.node.getElement(".o2_ann_model_nodeContentDate").set("text", this.data.updateTime);
  107. var rightIcon = this.node.getElement(".o2_ann_model_nodeRight");
  108. rightIcon.set("title", this.lp.status[this.data.status||'idle']);
  109. rightIcon.setStyle("background-image", "url(/x_component_ANN/$Main/default/icon/"+(this.data.status || 'idle')+".png")
  110. }.bind(this));
  111. },
  112. load: function(){
  113. if (this.data.id){
  114. this.loadList(function(){
  115. this.loadEvent();
  116. }.bind(this));
  117. }else{
  118. this.loadCreate();
  119. }
  120. },
  121. loadList: function(callback){
  122. var viewPath = this.app.path+this.app.options.style+"/model.html";
  123. this.listNode.loadHtml(viewPath, {"bind": {"lp": this.lp, "data": this.data}}, function(){
  124. this.node = this.listNode.getLast();
  125. // var rightIcon = this.node.getElement(".o2_ann_model_nodeRight");
  126. // rightIcon.set("title", this.lp.status[this.data.status||'idle']);
  127. // rightIcon.setStyle("background-image", "url(/x_component_ANN/$Main/default/icon/"+(this.data.status || 'idle')+".png")
  128. if (callback) callback();
  129. }.bind(this));
  130. },
  131. loadCreate: function(){
  132. this.loadList(function(){
  133. this.loadEvent();
  134. this.selected();
  135. }.bind(this));
  136. },
  137. loadEvent: function(){
  138. this.node.addEvents({
  139. "mouseover": function(){if (!this.isSelected) this.node.addClass("o2_ann_model_node_over")}.bind(this),
  140. "mouseout": function(){if (!this.isSelected) this.node.removeClass("o2_ann_model_node_over")}.bind(this),
  141. "click": function(){
  142. if (!this.isSelected){
  143. this.selected();
  144. }else{
  145. if (this.context) this.context.reload();
  146. }
  147. }.bind(this)
  148. });
  149. },
  150. getData: function(callback){
  151. o2.Actions.get("x_query_assemble_designer").getModel(this.data.id, function(json){
  152. this.data = json.data;
  153. if (callback) callback();
  154. }.bind(this));
  155. },
  156. selected: function(){
  157. if (this.app.checkSave()){
  158. this.node.addClass("o2_ann_model_node_selected");
  159. this.isSelected = true;
  160. this.app.currentModel = this;
  161. this.openModel();
  162. }
  163. },
  164. unselected: function(){
  165. this.isSelected = false;
  166. this.app.currentModel = null;
  167. this.node.removeClass("o2_ann_model_node_selected");
  168. this.node.removeClass("o2_ann_model_node_over");
  169. if (this.context) this.context.destroy();
  170. },
  171. openModel: function(){
  172. this.context = new MWF.xApplication.ANN.Model.Context(this);
  173. },
  174. destroy: function(){
  175. this.node.destroy();
  176. o2.release(this);
  177. }
  178. });
  179. MWF.xApplication.ANN.Model.Context = new Class({
  180. initialize: function(model){
  181. this.model = model;
  182. this.data = model.data;
  183. this.app = model.app;
  184. this.lp = model.lp;
  185. this.contentNode = this.app.contentNode;
  186. this.getData(this.load.bind(this));
  187. },
  188. getData: function(callback){
  189. o2.Actions.get("x_query_assemble_designer").getModel(this.data.id, function(json){
  190. this.data = json.data;
  191. if (callback) callback();
  192. }.bind(this));
  193. },
  194. reload: function(){
  195. this.contentNode.empty();
  196. this.getData(this.load.bind(this));
  197. },
  198. load: function(){
  199. this.status = "open";
  200. if (!this.data.id) this.status = "new";
  201. var viewPath = this.app.path+this.app.options.style+"/content.html";
  202. this.contentNode.loadHtml(viewPath, {"bind": {"lp": this.lp, "data": this.data}}, function(){
  203. this.node = this.contentNode.getFirst();
  204. this.toolbarNode = this.node.getFirst();
  205. this.content = this.node.getLast();
  206. this.lines = this.content.getElements(".o2_ann_modelContext_line");
  207. o2.widget.ace.load(function(){
  208. o2.load("/o2_lib/ace/src-min-noconflict/ext-static_highlight.js", function(){
  209. this.loadContent();
  210. this.loadToolbar();
  211. this.resizeFun = this.resize.bind(this);
  212. this.resizeFun();
  213. this.app.addEvent("resize", this.resizeFun);
  214. if (this.status==="new") this.edit();
  215. }.bind(this));
  216. }.bind(this));
  217. }.bind(this));
  218. },
  219. loadContent: function(){
  220. this.loadProcessList();
  221. this.loadApplicationList();
  222. this.loadCode(6, JSON.stringify(this.data.propertyMap,null,2));
  223. this.loadCode(7);
  224. this.loadCode(8);
  225. this.loadCode(9);
  226. this.loadStatus();
  227. },
  228. loadStatus: function(){
  229. this.statusNode = this.node.getElement(".o2_ann_modelContext_status");
  230. },
  231. loadProcessList: function(){
  232. if (this.data.processList){
  233. var node = this.lines[3].getLast().getFirst();
  234. this.data.processList.each(function(process){
  235. new o2.widget.O2Process({id: process}, node)
  236. }.bind(this));
  237. }
  238. },
  239. loadCode: function(i, text){
  240. var node = this.lines[i].getLast();
  241. if (text) node.set("text", text);
  242. var highlight = ace.require("ace/ext/static_highlight");
  243. highlight(node, {mode: "ace/mode/javascript", theme: "ace/theme/tomorrow", "fontSize": 16});
  244. },
  245. loadApplicationList: function(){
  246. if (this.data.applicationList){
  247. var node = this.lines[4].getLast();
  248. this.data.applicationList.each(function(application){
  249. new o2.widget.O2Application({id: application}, node)
  250. }.bind(this));
  251. }
  252. },
  253. destroy: function(){
  254. if (this.resizeFun) this.app.removeEvent("resize", this.resizeFun);
  255. this.contentNode.empty();
  256. o2.release(this);
  257. },
  258. "close": function(el,e){
  259. if (this.status!=="open"){
  260. var _self = this;
  261. this.app.confirm("infor", e, this.lp.closeTitle, this.lp.closeInfor, 380, 100, function(){
  262. if (_self.data.id){
  263. _self.model.unselected();
  264. }else{
  265. var model = _self.model;
  266. _self.model.unselected();
  267. model.destroy();
  268. }
  269. this.close();
  270. }, function(){this.close();});
  271. }else{
  272. this.model.unselected();
  273. }
  274. },
  275. loadToolbar: function(){
  276. MWF.require("MWF.widget.Toolbar", function() {
  277. this.toolbar = new MWF.widget.Toolbar(this.toolbarNode, {"style": "simple"}, this);
  278. this.toolbar.load();
  279. this.setToolbarDisabled();
  280. }.bind(this));
  281. },
  282. setToolbarDisabled: function(){
  283. if (this.toolbar){
  284. if (this.status==="edit"){
  285. this.toolbar.childrenButton[0].node.hide();
  286. this.toolbar.childrenButton[1].node.show();
  287. this.toolbar.childrenButton[6].node.show();
  288. if (this.data.status) {
  289. this.toolbar.childrenButton[2].node.hide();
  290. this.toolbar.childrenButton[3].node.hide();
  291. }else{
  292. this.toolbar.childrenButton[2].node.show();
  293. this.toolbar.childrenButton[3].node.show();
  294. this.toolbar.childrenButton[8].node.show();
  295. }
  296. if (this.data.status && this.data.status==="generating"){
  297. this.toolbar.childrenButton[4].node.show();
  298. this.toolbar.childrenButton[8].node.hide();
  299. }else{
  300. this.toolbar.childrenButton[4].node.hide();
  301. }
  302. if (this.data.status && this.data.status==="learning"){
  303. this.toolbar.childrenButton[5].node.show();
  304. this.toolbar.childrenButton[8].node.hide();
  305. }else{
  306. this.toolbar.childrenButton[5].node.hide();
  307. }
  308. }else if (this.status==="new"){
  309. this.toolbar.childrenButton[0].node.hide();
  310. this.toolbar.childrenButton[1].node.show();
  311. this.toolbar.childrenButton[6].node.hide();
  312. this.toolbar.childrenButton[2].node.hide();
  313. this.toolbar.childrenButton[3].node.hide();
  314. this.toolbar.childrenButton[4].node.hide();
  315. this.toolbar.childrenButton[5].node.hide();
  316. }else{
  317. this.toolbar.childrenButton[0].node.show();
  318. this.toolbar.childrenButton[1].node.hide();
  319. this.toolbar.childrenButton[6].node.show();
  320. if (this.data.status) {
  321. this.toolbar.childrenButton[2].node.hide();
  322. this.toolbar.childrenButton[3].node.hide();
  323. }else{
  324. this.toolbar.childrenButton[2].node.show();
  325. this.toolbar.childrenButton[3].node.show();
  326. this.toolbar.childrenButton[8].node.show();
  327. }
  328. if (this.data.status && this.data.status==="generating"){
  329. this.toolbar.childrenButton[4].node.show();
  330. this.toolbar.childrenButton[8].node.hide();
  331. }else{
  332. this.toolbar.childrenButton[4].node.hide();
  333. }
  334. if (this.data.status && this.data.status==="learning"){
  335. this.toolbar.childrenButton[5].node.show();
  336. this.toolbar.childrenButton[8].node.hide();
  337. }else{
  338. this.toolbar.childrenButton[5].node.hide();
  339. }
  340. }
  341. }
  342. },
  343. resize: function(){
  344. var size = this.contentNode.getSize();
  345. var toolbarSize = this.toolbarNode.getComputedSize();
  346. var h = size.y - toolbarSize.totalHeight;
  347. this.content.setStyle("height", ""+h+"px");
  348. },
  349. edit: function(){
  350. this.status = (!this.data.id) ? "new" : "edit";
  351. this.setToolbarDisabled();
  352. this.nameInput = this.editInput(0, "name");
  353. this.descriptionInput = this.editInput(1, "description");
  354. this.aliasInput = this.editInput(2, "alias");
  355. this.editProcessList(3);
  356. this.editApplicationList(4);
  357. this.analyzeInput = this.editSelect(5, ["default", "full"]);
  358. o2.require("o2.widget.ScriptArea", function(){
  359. this.propertyMapInput = this.editCode(6, "propertyMap");
  360. this.inValueScriptInput = this.editCode(7, "inValueScriptText");
  361. this.outValueScriptInput = this.editCode(8, "outValueScriptText");
  362. this.attachmentScriptInput = this.editCode(9), "attachmentScriptText";
  363. }.bind(this));
  364. },
  365. editInput: function(i, text){
  366. var node = this.lines[i].getLast();
  367. var value = node.get("text");
  368. node.empty();
  369. var input = new Element("input.o2_ann_modelContext_input", {
  370. "type": "text",
  371. "value": value,
  372. "placeholder": this.lp.placeholder[text]
  373. }).inject(node);
  374. return input;
  375. },
  376. editProcessList: function(i){
  377. var node = this.lines[i].getLast();
  378. var action = new Element("button.o2_ann_modelContext_button", {"text": this.lp.selectProcess}).inject(node);
  379. action.addEvent("click", function(e){
  380. MWF.xDesktop.requireApp("Selector", "package", function(){
  381. new MWF.O2Selector(this.app.content, {
  382. "type": "process", "values": this.data.processList,
  383. "onComplete": function(items){
  384. e.target.getPrevious().empty();
  385. this.data.processList = [];
  386. items.each(function(item){
  387. this.data.processList.push(item.data.id);
  388. }.bind(this));
  389. this.loadProcessList();
  390. }.bind(this)
  391. });
  392. }.bind(this));
  393. }.bind(this));
  394. },
  395. editApplicationList: function(i){
  396. var node = this.lines[i].getLast();
  397. var action = new Element("button.o2_ann_modelContext_button", {"text": this.lp.selectApplication}).inject(node);
  398. action.addEvent("click", function(e){
  399. MWF.xDesktop.requireApp("Selector", "package", function(){
  400. new MWF.O2Selector(this.app.content, {
  401. "type": "application", "values": this.data.applicationList,
  402. "onComplete": function(items){
  403. e.target.getPrevious().empty();
  404. this.data.applicationList = [];
  405. items.each(function(item){
  406. this.data.applicationList.push(item.data.id);
  407. }.bind(this));
  408. this.loadApplicationList();
  409. }.bind(this)
  410. });
  411. }.bind(this));
  412. }.bind(this));
  413. },
  414. editSelect: function(i, arr){
  415. var node = this.lines[i].getLast();
  416. var value = node.get("text");
  417. node.empty();
  418. var select = new Element("select").inject(node);
  419. arr.each(function(v){
  420. new Element("option", {"value": v, "text": v, "checked": (value===v)}).inject(select);
  421. });
  422. return select;
  423. },
  424. editCode: function(i, key){
  425. var node = this.lines[i].getLast();
  426. var value = (key==="propertyMap") ? JSON.stringify(this.data.propertyMap,null,2) : this.data[key];
  427. node.empty();
  428. var editor = new o2.widget.ScriptArea(node, {"title": "","maxObj": this.app.content, "isbind": false, "isload": true});
  429. editor.load({"code": value});
  430. return editor;
  431. },
  432. getModelData: function(){
  433. this.data.name = this.nameInput.get("value");
  434. this.data.description = this.descriptionInput.get("value");
  435. this.data.alias = this.aliasInput.get("value");
  436. this.data.analyzeType = this.analyzeInput.options[this.analyzeInput.selectedIndex].get("value");
  437. this.data.propertyMap = JSON.parse(this.propertyMapInput.editor.getValue());
  438. this.data.inValueScriptText = this.inValueScriptInput.editor.getValue();
  439. this.data.outValueScriptText = this.outValueScriptInput.editor.getValue();
  440. this.data.attachmentScriptText = this.attachmentScriptInput.editor.getValue();
  441. if (this.data.propertyMap) this.data.propertyMap = {};
  442. },
  443. save: function(){
  444. debugger;
  445. this.getModelData();
  446. if (!this.data.id){
  447. o2.Actions.get("x_query_assemble_designer").createModel(this.data, function(json){
  448. this.data.id = json.data.id;
  449. this.app.notice(this.lp.generate, "success");
  450. this.model.reload();
  451. this.reload();
  452. }.bind(this));
  453. }else{
  454. o2.Actions.get("x_query_assemble_designer").updateModel(this.data.id, this.data, function(){
  455. this.app.notice(this.lp.saveSuccess, "success");
  456. this.model.reload();
  457. this.reload();
  458. }.bind(this));
  459. }
  460. },
  461. generate: function(){
  462. o2.Actions.get("x_query_assemble_designer").generate(this.data.id,function(){
  463. this.app.notice(this.lp.generate, "success");
  464. this.model.reload();
  465. this.reload();
  466. }.bind(this));
  467. },
  468. learn: function(){
  469. o2.Actions.get("x_query_assemble_designer").learn(this.data.id, function(){
  470. this.app.notice(this.lp.learn, "success");
  471. this.model.reload();
  472. this.reload();
  473. }.bind(this));
  474. },
  475. stopGenerate: function(el, e){
  476. var _self = this;
  477. this.app.confirm("infor", e, this.lp.stopGenerateTitle, this.lp.stopGenerateInfor, 380, 100, function(){
  478. o2.Actions.get("x_query_assemble_designer").stopGenerating(_self.data.id, function(){
  479. this.app.notice(this.lp.stopGenerate, "success");
  480. this.model.reload();
  481. this.reload();
  482. }.bind(_self));
  483. this.close();
  484. }, function(){this.close();});
  485. },
  486. stopLearn: function(el, e){
  487. var _self = this;
  488. this.app.confirm("infor", e, this.lp.stopLearningTitle, this.lp.stopLearningInfor, 380, 100, function(){
  489. o2.Actions.get("x_query_assemble_designer").stopLearning(_self.data.id, function(){
  490. this.app.notice(this.lp.stopLearn, "success");
  491. this.model.reload();
  492. this.reload();
  493. }.bind(_self));
  494. this.close();
  495. }, function(){this.close();});
  496. },
  497. reset: function(el, e){
  498. var _self = this;
  499. this.app.confirm("infor", e, this.lp.resetStatusTitle, this.lp.resetStatusInfor, 380, 100, function(){
  500. o2.Actions.get("x_query_assemble_designer").resetStatus(_self.data.id, function(){
  501. this.app.notice(this.lp.resetStatus, "success");
  502. this.model.reload();
  503. this.reload();
  504. }.bind(_self));
  505. this.close();
  506. }, function(){this.close();});
  507. },
  508. remove: function(el, e){
  509. var _self = this;
  510. this.app.confirm("infor", e, this.lp.removeTitle, this.lp.removeInfor, 380, 100, function(){
  511. o2.Actions.get("x_query_assemble_designer").deleteModel(_self.data.id, function(){
  512. var text = this.lp.deleteModel.replace("{name}", this.data.name);
  513. this.app.notice(text, "success");
  514. var model = this.model;
  515. model.unselected();
  516. model.destroy();
  517. }.bind(_self));
  518. this.close();
  519. }, function(){this.close();});
  520. }
  521. });