Main.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. if (this.data.id){
  190. o2.Actions.get("x_query_assemble_designer").getModel(this.data.id, function(json){
  191. this.data = json.data;
  192. if (callback) callback();
  193. }.bind(this));
  194. }else{
  195. if (callback) callback();
  196. }
  197. },
  198. reload: function(){
  199. this.contentNode.empty();
  200. this.getData(this.load.bind(this));
  201. },
  202. load: function(){
  203. this.status = "open";
  204. if (!this.data.id) this.status = "new";
  205. var viewPath = this.app.path+this.app.options.style+"/content.html";
  206. this.contentNode.loadHtml(viewPath, {"bind": {"lp": this.lp, "data": this.data}}, function(){
  207. this.node = this.contentNode.getFirst();
  208. this.toolbarNode = this.node.getFirst();
  209. this.content = this.node.getLast();
  210. this.lines = this.content.getElements(".o2_ann_modelContext_line");
  211. o2.widget.ace.load(function(){
  212. o2.load("../o2_lib/ace/src-min-noconflict/ext-static_highlight.js", function(){
  213. this.loadContent();
  214. this.loadToolbar();
  215. this.resizeFun = this.resize.bind(this);
  216. this.resizeFun();
  217. this.app.addEvent("resize", this.resizeFun);
  218. if (this.status==="new") this.edit();
  219. }.bind(this));
  220. }.bind(this));
  221. }.bind(this));
  222. },
  223. loadContent: function(){
  224. this.loadProcessList();
  225. this.loadApplicationList();
  226. this.loadCode(6, JSON.stringify(this.data.propertyMap,null,2));
  227. this.loadCode(7);
  228. this.loadCode(8);
  229. this.loadCode(9);
  230. this.loadStatus();
  231. },
  232. loadStatus: function(){
  233. this.statusNode = this.node.getElement(".o2_ann_modelContext_status");
  234. },
  235. loadProcessList: function(){
  236. if (this.data.processList){
  237. var node = this.lines[3].getLast().getFirst();
  238. this.data.processList.each(function(process){
  239. new o2.widget.O2Process({id: process}, node)
  240. }.bind(this));
  241. }
  242. },
  243. loadCode: function(i, text){
  244. var node = this.lines[i].getLast();
  245. if (text) node.set("text", text);
  246. var highlight = ace.require("ace/ext/static_highlight");
  247. highlight(node, {mode: "ace/mode/javascript", theme: "ace/theme/tomorrow", "fontSize": 16});
  248. },
  249. loadApplicationList: function(){
  250. if (this.data.applicationList){
  251. var node = this.lines[4].getLast();
  252. this.data.applicationList.each(function(application){
  253. new o2.widget.O2Application({id: application}, node)
  254. }.bind(this));
  255. }
  256. },
  257. destroy: function(){
  258. if (this.resizeFun) this.app.removeEvent("resize", this.resizeFun);
  259. this.contentNode.empty();
  260. o2.release(this);
  261. },
  262. "close": function(el,e){
  263. if (this.status!=="open"){
  264. var _self = this;
  265. this.app.confirm("infor", e, this.lp.closeTitle, this.lp.closeInfor, 380, 100, function(){
  266. if (_self.data.id){
  267. _self.model.unselected();
  268. }else{
  269. var model = _self.model;
  270. _self.model.unselected();
  271. model.destroy();
  272. }
  273. this.close();
  274. }, function(){this.close();});
  275. }else{
  276. this.model.unselected();
  277. }
  278. },
  279. loadToolbar: function(){
  280. MWF.require("MWF.widget.Toolbar", function() {
  281. this.toolbar = new MWF.widget.Toolbar(this.toolbarNode, {"style": "simple"}, this);
  282. this.toolbar.load();
  283. this.setToolbarDisabled();
  284. }.bind(this));
  285. },
  286. setToolbarDisabled: function(){
  287. if (this.toolbar){
  288. if (this.status==="edit"){
  289. this.toolbar.childrenButton[0].node.hide();
  290. this.toolbar.childrenButton[1].node.show();
  291. this.toolbar.childrenButton[6].node.show();
  292. if (this.data.status) {
  293. this.toolbar.childrenButton[2].node.hide();
  294. this.toolbar.childrenButton[3].node.hide();
  295. }else{
  296. this.toolbar.childrenButton[2].node.show();
  297. this.toolbar.childrenButton[3].node.show();
  298. this.toolbar.childrenButton[8].node.show();
  299. }
  300. if (this.data.status && this.data.status==="generating"){
  301. this.toolbar.childrenButton[4].node.show();
  302. this.toolbar.childrenButton[8].node.hide();
  303. }else{
  304. this.toolbar.childrenButton[4].node.hide();
  305. }
  306. if (this.data.status && this.data.status==="learning"){
  307. this.toolbar.childrenButton[5].node.show();
  308. this.toolbar.childrenButton[8].node.hide();
  309. }else{
  310. this.toolbar.childrenButton[5].node.hide();
  311. }
  312. }else if (this.status==="new"){
  313. this.toolbar.childrenButton[0].node.hide();
  314. this.toolbar.childrenButton[1].node.show();
  315. this.toolbar.childrenButton[6].node.hide();
  316. this.toolbar.childrenButton[2].node.hide();
  317. this.toolbar.childrenButton[3].node.hide();
  318. this.toolbar.childrenButton[4].node.hide();
  319. this.toolbar.childrenButton[5].node.hide();
  320. }else{
  321. this.toolbar.childrenButton[0].node.show();
  322. this.toolbar.childrenButton[1].node.hide();
  323. this.toolbar.childrenButton[6].node.show();
  324. if (this.data.status) {
  325. this.toolbar.childrenButton[2].node.hide();
  326. this.toolbar.childrenButton[3].node.hide();
  327. }else{
  328. this.toolbar.childrenButton[2].node.show();
  329. this.toolbar.childrenButton[3].node.show();
  330. this.toolbar.childrenButton[8].node.show();
  331. }
  332. if (this.data.status && this.data.status==="generating"){
  333. this.toolbar.childrenButton[4].node.show();
  334. this.toolbar.childrenButton[8].node.hide();
  335. }else{
  336. this.toolbar.childrenButton[4].node.hide();
  337. }
  338. if (this.data.status && this.data.status==="learning"){
  339. this.toolbar.childrenButton[5].node.show();
  340. this.toolbar.childrenButton[8].node.hide();
  341. }else{
  342. this.toolbar.childrenButton[5].node.hide();
  343. }
  344. }
  345. }
  346. },
  347. resize: function(){
  348. var size = this.contentNode.getSize();
  349. var toolbarSize = this.toolbarNode.getComputedSize();
  350. var h = size.y - toolbarSize.totalHeight;
  351. this.content.setStyle("height", ""+h+"px");
  352. },
  353. edit: function(){
  354. this.status = (!this.data.id) ? "new" : "edit";
  355. this.setToolbarDisabled();
  356. this.nameInput = this.editInput(0, "name");
  357. this.descriptionInput = this.editInput(1, "description");
  358. this.aliasInput = this.editInput(2, "alias");
  359. this.editProcessList(3);
  360. this.editApplicationList(4);
  361. this.analyzeInput = this.editSelect(5, ["default", "full"]);
  362. o2.require("o2.widget.ScriptArea", function(){
  363. this.propertyMapInput = this.editCode(6, "propertyMap");
  364. this.inValueScriptInput = this.editCode(7, "inValueScriptText");
  365. this.outValueScriptInput = this.editCode(8, "outValueScriptText");
  366. this.attachmentScriptInput = this.editCode(9), "attachmentScriptText";
  367. }.bind(this));
  368. },
  369. editInput: function(i, text){
  370. var node = this.lines[i].getLast();
  371. var value = node.get("text");
  372. node.empty();
  373. var input = new Element("input.o2_ann_modelContext_input", {
  374. "type": "text",
  375. "value": value,
  376. "placeholder": this.lp.placeholder[text]
  377. }).inject(node);
  378. return input;
  379. },
  380. editProcessList: function(i){
  381. var node = this.lines[i].getLast();
  382. var action = new Element("button.o2_ann_modelContext_button", {"text": this.lp.selectProcess}).inject(node);
  383. action.addEvent("click", function(e){
  384. MWF.xDesktop.requireApp("Selector", "package", function(){
  385. new MWF.O2Selector(this.app.content, {
  386. "type": "process", "values": this.data.processList,
  387. "onComplete": function(items){
  388. e.target.getPrevious().empty();
  389. this.data.processList = [];
  390. items.each(function(item){
  391. this.data.processList.push(item.data.id);
  392. }.bind(this));
  393. this.loadProcessList();
  394. }.bind(this)
  395. });
  396. }.bind(this));
  397. }.bind(this));
  398. },
  399. editApplicationList: function(i){
  400. var node = this.lines[i].getLast();
  401. var action = new Element("button.o2_ann_modelContext_button", {"text": this.lp.selectApplication}).inject(node);
  402. action.addEvent("click", function(e){
  403. MWF.xDesktop.requireApp("Selector", "package", function(){
  404. new MWF.O2Selector(this.app.content, {
  405. "type": "application", "values": this.data.applicationList,
  406. "onComplete": function(items){
  407. e.target.getPrevious().empty();
  408. this.data.applicationList = [];
  409. items.each(function(item){
  410. this.data.applicationList.push(item.data.id);
  411. }.bind(this));
  412. this.loadApplicationList();
  413. }.bind(this)
  414. });
  415. }.bind(this));
  416. }.bind(this));
  417. },
  418. editSelect: function(i, arr){
  419. var node = this.lines[i].getLast();
  420. var value = node.get("text");
  421. node.empty();
  422. var select = new Element("select").inject(node);
  423. arr.each(function(v){
  424. new Element("option", {"value": v, "text": v, "checked": (value===v)}).inject(select);
  425. });
  426. return select;
  427. },
  428. editCode: function(i, key){
  429. var node = this.lines[i].getLast();
  430. var value = (key==="propertyMap") ? JSON.stringify(this.data.propertyMap,null,2) : this.data[key];
  431. node.empty();
  432. var editor = new o2.widget.ScriptArea(node, {"title": "","maxObj": this.app.content, "isbind": false, "isload": true});
  433. editor.load({"code": value});
  434. return editor;
  435. },
  436. getModelData: function(){
  437. this.data.name = this.nameInput.get("value");
  438. this.data.description = this.descriptionInput.get("value");
  439. this.data.alias = this.aliasInput.get("value");
  440. this.data.analyzeType = this.analyzeInput.options[this.analyzeInput.selectedIndex].get("value");
  441. this.data.propertyMap = JSON.parse(this.propertyMapInput.editor.getValue());
  442. this.data.inValueScriptText = this.inValueScriptInput.editor.getValue();
  443. this.data.outValueScriptText = this.outValueScriptInput.editor.getValue();
  444. this.data.attachmentScriptText = this.attachmentScriptInput.editor.getValue();
  445. if (this.data.propertyMap) this.data.propertyMap = {};
  446. },
  447. save: function(){
  448. debugger;
  449. this.getModelData();
  450. if (!this.data.id){
  451. o2.Actions.get("x_query_assemble_designer").createModel(this.data, function(json){
  452. this.data.id = json.data.id;
  453. this.app.notice(this.lp.generate, "success");
  454. this.model.reload();
  455. this.reload();
  456. }.bind(this));
  457. }else{
  458. o2.Actions.get("x_query_assemble_designer").updateModel(this.data.id, this.data, function(){
  459. this.app.notice(this.lp.saveSuccess, "success");
  460. this.model.reload();
  461. this.reload();
  462. }.bind(this));
  463. }
  464. },
  465. generate: function(){
  466. o2.Actions.get("x_query_assemble_designer").generate(this.data.id,function(){
  467. this.app.notice(this.lp.generate, "success");
  468. this.model.reload();
  469. this.reload();
  470. }.bind(this));
  471. },
  472. learn: function(){
  473. o2.Actions.get("x_query_assemble_designer").learn(this.data.id, function(){
  474. this.app.notice(this.lp.learn, "success");
  475. this.model.reload();
  476. this.reload();
  477. }.bind(this));
  478. },
  479. stopGenerate: function(el, e){
  480. var _self = this;
  481. this.app.confirm("infor", e, this.lp.stopGenerateTitle, this.lp.stopGenerateInfor, 380, 100, function(){
  482. o2.Actions.get("x_query_assemble_designer").stopGenerating(_self.data.id, function(){
  483. this.app.notice(this.lp.stopGenerate, "success");
  484. this.model.reload();
  485. this.reload();
  486. }.bind(_self));
  487. this.close();
  488. }, function(){this.close();});
  489. },
  490. stopLearn: function(el, e){
  491. var _self = this;
  492. this.app.confirm("infor", e, this.lp.stopLearningTitle, this.lp.stopLearningInfor, 380, 100, function(){
  493. o2.Actions.get("x_query_assemble_designer").stopLearning(_self.data.id, function(){
  494. this.app.notice(this.lp.stopLearn, "success");
  495. this.model.reload();
  496. this.reload();
  497. }.bind(_self));
  498. this.close();
  499. }, function(){this.close();});
  500. },
  501. reset: function(el, e){
  502. var _self = this;
  503. this.app.confirm("infor", e, this.lp.resetStatusTitle, this.lp.resetStatusInfor, 380, 100, function(){
  504. o2.Actions.get("x_query_assemble_designer").resetStatus(_self.data.id, function(){
  505. this.app.notice(this.lp.resetStatus, "success");
  506. this.model.reload();
  507. this.reload();
  508. }.bind(_self));
  509. this.close();
  510. }, function(){this.close();});
  511. },
  512. remove: function(el, e){
  513. var _self = this;
  514. this.app.confirm("infor", e, this.lp.removeTitle, this.lp.removeInfor, 380, 100, function(){
  515. o2.Actions.get("x_query_assemble_designer").deleteModel(_self.data.id, function(){
  516. var text = this.lp.deleteModel.replace("{name}", this.data.name);
  517. this.app.notice(text, "success");
  518. var model = this.model;
  519. model.unselected();
  520. model.destroy();
  521. }.bind(_self));
  522. this.close();
  523. }, function(){this.close();});
  524. }
  525. });