ApplicationServers.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. MWF.xApplication.Setting.servers = MWF.xApplication.Setting.servers || {};
  2. MWF.xApplication.Setting.servers.ApplicationServers = new Class({
  3. Extends: MWF.widget.Common,
  4. Implements: [Options, Events],
  5. initialize: function(explorer){
  6. this.explorer = explorer;
  7. this.app = this.explorer.app;
  8. this.actions = this.app.actions;
  9. this.css = this.app.css;
  10. this.applicationServers = [];
  11. this.content = this.explorer.applicationServerContent;
  12. this.page = this.app.serverPage;
  13. this.currentDoc = null;
  14. this.load();
  15. },
  16. load: function(){
  17. this.actions.listApplicationServer(function(json){
  18. json.data.each(function(serverJson){
  19. this.applicationServers.push(new MWF.xApplication.Setting.servers.ApplicationServer(this, serverJson));
  20. }.bind(this));
  21. this.createAddAction();
  22. this.setApplicationServerAreaWidth();
  23. this.setApplicationServerAreaWidthFun = this.setApplicationServerAreaWidth.bind(this);
  24. this.app.addEvent("resize", this.setApplicationServerAreaWidthFun);
  25. }.bind(this));
  26. },
  27. createAddAction: function(){
  28. this.createServerAction = new Element("div", {"styles": this.css.applicationServerCreateAction}).inject(this.content);
  29. this.createServerAction.addEvents({
  30. "mouseover": function(){this.createServerAction.setStyles( this.css.applicationServerCreateAction_over);}.bind(this),
  31. "mouseout": function(){this.createServerAction.setStyles( this.css.applicationServerCreateAction);}.bind(this),
  32. "click": function(){this.createServer();}.bind(this),
  33. });
  34. },
  35. createServer: function(){
  36. var server = {
  37. "list": this,
  38. "app": this.app,
  39. "json": {
  40. "contextList": [],
  41. "planList": []
  42. },
  43. "node": this.createServerAction,
  44. "name": "",
  45. "reload": function(){
  46. this.list.applicationServers.push(new MWF.xApplication.Setting.servers.ApplicationServer(this.list, this.json));
  47. }
  48. }
  49. this.currentDoc = new MWF.xApplication.Setting.servers.ApplicationServer.Document(server);
  50. },
  51. setApplicationServerAreaWidth: function(){
  52. var count = this.applicationServers.length;
  53. var width = this.explorer.contentAreaNode.getSize().x;
  54. var nodeWidth = this.applicationServers[0].node.getSize().x;
  55. var x1 = this.applicationServers[0].node.getStyle("margin-left").toInt();
  56. var x2 = this.applicationServers[0].node.getStyle("margin-right").toInt();
  57. nodeWidth = nodeWidth+x1+x2;
  58. var n = (width/nodeWidth).toInt();
  59. var w = nodeWidth*n;
  60. this.content.setStyle("width", ""+w+"px");
  61. },
  62. getDepolyableList: function(callback){
  63. this.actions.listDepolyable(function(json){
  64. this.depolyableList = json.data;
  65. if (callback) callback(json.data);
  66. }.bind(this));
  67. },
  68. destroy: function(){
  69. if (this.setApplicationServerAreaWidthFun) this.app.removeEvent("resize", this.setApplicationServerAreaWidthFun);
  70. this.applicationServers.each(function(server){
  71. server.destroy();
  72. }.bind(this));
  73. if (this.currentDoc) this.currentDoc.destroy();
  74. this.content.destroy();
  75. MWF.release(this);
  76. }
  77. });
  78. MWF.xApplication.Setting.servers.ApplicationServer = new Class({
  79. Implements: [Events],
  80. initialize: function(list, json){
  81. this.list = list
  82. this.explorer = this.list.explorer;
  83. this.app = this.list.explorer.app;
  84. this.json = json;
  85. this.container = this.list.content;
  86. this.css = this.app.css;
  87. this.name = this.json.name;
  88. this.load();
  89. },
  90. load: function(){
  91. this.node = new Element("div", {"styles": this.css.applicationServerNode}).inject(this.container);
  92. if (this.list.createServerAction) this.node.inject(this.list.createServerAction, "before");
  93. this.iconNode = new Element("div", {"styles": this.css.applicationServerIconNode}).inject(this.node);
  94. this.statusNode = new Element("div", {"styles": this.css.applicationServerStatusNode}).inject(this.node);
  95. if (this.json.status=="connected"){
  96. this.statusNode.setStyle("background-color", "#23b107");
  97. }else{
  98. this.statusNode.setStyle("background-color", "#999");
  99. }
  100. this.textNode = new Element("div", {"styles": this.css.applicationServerTextNode}).inject(this.node);
  101. this.nameNode = new Element("div", {"styles": this.css.applicationServerNameNode}).inject(this.textNode);
  102. this.hostNode = new Element("div", {"styles": this.css.applicationServerHostNode}).inject(this.textNode);
  103. this.adminNode = new Element("div", {"styles": this.css.applicationServerAdminNode}).inject(this.textNode);
  104. this.messageNode = new Element("div", {"styles": this.css.applicationServerMessageNode}).inject(this.textNode);
  105. this.setServerText();
  106. this.node.addEvent("click", this.open.bind(this));
  107. //this.checkWidthFun = this.checkWidth.bind(this);
  108. //this.app.addEvent("resize", this.checkWidthFun);
  109. },
  110. setServerText: function(){
  111. this.nameNode.set("text", (this.json.name || ""));
  112. this.hostNode.set("text", (this.json.host || "")+" : "+(this.json.port || ""));
  113. this.adminNode.set("text", (this.json.username || ""));
  114. this.messageNode.set("text", (this.json.message || ""));
  115. },
  116. open: function(){
  117. this.list.currentDoc = new MWF.xApplication.Setting.servers.ApplicationServer.Document(this);
  118. },
  119. reload: function(){
  120. this.app.actions.getAppServer(this.json.name, function(json){
  121. this.name = this.json.name;
  122. this.json = json.data;
  123. this.nameNode.set("text", this.json.name);
  124. this.hostNode.set("text", this.json.host+" : "+this.json.port);
  125. this.adminNode.set("text", this.json.username);
  126. this.messageNode.set("text", this.json.message);
  127. }.bind(this));
  128. },
  129. destroy: function(){
  130. this.node.destroy();
  131. MWF.release(this);
  132. }
  133. });
  134. MWF.xApplication.Setting.servers.ApplicationServer.Document = new Class({
  135. Implements: [Events],
  136. initialize: function(server){
  137. this.server = server;
  138. this.list = this.server.list
  139. this.app = this.server.app;
  140. this.json = this.server.json;
  141. this.container = this.list.explorer.container;
  142. this.css = this.app.css;
  143. this.apps = [];
  144. this.load();
  145. },
  146. load: function(){
  147. this.node = new Element("div", {"styles": this.css.applicationServerDocumentNode}).inject(this.container);
  148. this.setNodeSize();
  149. this.show();
  150. //this.createForm();
  151. },
  152. setNodeSize: function(){
  153. var size = this.server.node.getSize();
  154. var position = this.server.node.getPosition(this.server.node.getOffsetParent());
  155. this.node.setStyles({
  156. "width": ""+size.x+"px",
  157. "height": ""+size.y+"px",
  158. "top": ""+position.y+"px",
  159. "left": ""+position.x+"px"
  160. });
  161. },
  162. show: function(){
  163. var size = this.list.page.contentNodeArea.getSize();
  164. var position = this.list.page.contentNodeArea.getPosition(this.list.page.contentNodeArea.getOffsetParent());
  165. var css = {
  166. "width": ""+size.x+"px",
  167. "height": ""+size.y+"px",
  168. "top": ""+position.y+"px",
  169. "left": ""+position.x+"px"
  170. }
  171. this.morph = new Fx.Morph(this.node,{
  172. "duration": 100,
  173. "transition": Fx.Transitions.Sine.easeOut
  174. });
  175. this.morph.start(css).chain(function(){
  176. this.list.content.setStyle("display", "none");
  177. this.createForm();
  178. this.setDocumentSizeFun = this.setDocumentSize.bind(this);
  179. this.setDocumentSize();
  180. this.app.addEvent("resize", this.setDocumentSizeFun);
  181. }.bind(this));
  182. },
  183. setDocumentSize: function(){
  184. var size = this.list.page.contentNodeArea.getSize();
  185. var actionSize = this.actionNode.getSize();
  186. var h = size.y-actionSize.y;
  187. this.inforNode.setStyle("height", ""+h+"px");
  188. this.applicationAreaNode.setStyle("height", ""+h+"px");
  189. //this.inforNode.setStyle("min-height", ""+h+"px");
  190. this.node.setStyles({
  191. "width": ""+size.x+"px",
  192. "height": ""+size.y+"px",
  193. });
  194. },
  195. createForm: function(){
  196. this.createActions();
  197. this.createBaseInfo();
  198. },
  199. createActions: function(){
  200. this.actionNode = new Element("div", {"styles": this.css.applicationServerDocumentActionNode}).inject(this.node);
  201. this.saveAction = new Element("div", {"styles": this.css.applicationServerDocumentSaveNode}).inject(this.actionNode);
  202. if (this.server.name) this.deleteAction = new Element("div", {"styles": this.css.applicationServerDocumentDeleteNode}).inject(this.actionNode);
  203. if (this.server.name) this.deployAction = new Element("div", {"title": this.app.lp.deploy, "styles": this.css.applicationServerDocumentDeployNode}).inject(this.actionNode);
  204. this.closeAction = new Element("div", {"styles": this.css.applicationServerDocumentCloseNode}).inject(this.actionNode);
  205. this.saveAction.addEvents({
  206. "mouseover": function(){this.saveAction.setStyles(this.css.applicationServerDocumentSaveNode_over);}.bind(this),
  207. "mouseout": function(){this.saveAction.setStyles(this.css.applicationServerDocumentSaveNode);}.bind(this),
  208. "mousedown": function(){this.saveAction.setStyles(this.css.applicationServerDocumentSaveNode_down);}.bind(this),
  209. "mouseup": function(){this.saveAction.setStyles(this.css.applicationServerDocumentSaveNode_over);}.bind(this),
  210. "click": function(e){this.saveDocument();}.bind(this)
  211. });
  212. if (this.deleteAction){
  213. this.deleteAction.addEvents({
  214. "mouseover": function(){this.deleteAction.setStyles(this.css.applicationServerDocumentDeleteNode_over);}.bind(this),
  215. "mouseout": function(){this.deleteAction.setStyles(this.css.applicationServerDocumentDeleteNode);}.bind(this),
  216. "mousedown": function(){this.deleteAction.setStyles(this.css.applicationServerDocumentDeleteNode_down);}.bind(this),
  217. "mouseup": function(){this.deleteAction.setStyles(this.css.applicationServerDocumentDeleteNode_over);}.bind(this),
  218. "click": function(e){this.deleteDocument(e);}.bind(this)
  219. });
  220. }
  221. this.closeAction.addEvents({
  222. "mouseover": function(){this.closeAction.setStyles(this.css.applicationServerDocumentCloseNode_over);}.bind(this),
  223. "mouseout": function(){this.closeAction.setStyles(this.css.applicationServerDocumentCloseNode);}.bind(this),
  224. "mousedown": function(){this.closeAction.setStyles(this.css.applicationServerDocumentCloseNode_down);}.bind(this),
  225. "mouseup": function(){this.closeAction.setStyles(this.css.applicationServerDocumentCloseNode_over);}.bind(this),
  226. "click": function(e){this.closeDocument();}.bind(this)
  227. });
  228. if (this.deployAction){
  229. this.deployAction.addEvents({
  230. "mouseover": function(){this.deployAction.setStyles(this.css.applicationServerDocumentDeployNode_over);}.bind(this),
  231. "mouseout": function(){this.deployAction.setStyles(this.css.applicationServerDocumentDeployNode);}.bind(this),
  232. "mousedown": function(){this.deployAction.setStyles(this.css.applicationServerDocumentDeployNode_down);}.bind(this),
  233. "mouseup": function(){this.deployAction.setStyles(this.css.applicationServerDocumentDeployNode_over);}.bind(this),
  234. "click": function(e){this.deploy(e);}.bind(this)
  235. });
  236. }
  237. },
  238. createBaseInfo: function(){
  239. this.inforAreaNode = new Element("div", {"styles": this.css.applicationServerDocumentInforAreaNode}).inject(this.node);
  240. this.inforNode = new Element("div", {"styles": this.css.applicationServerDocumentInforNode}).inject(this.inforAreaNode);
  241. var html = "<table cellSpacing='8px' width='90%' align='center'>" +
  242. "<tr><td>name<input value='"+(this.json.name || "")+"'/></td></tr>" +
  243. "<tr><td>order<input value='"+(this.json.order || "0")+"'/></td></tr>" +
  244. "<tr><td>containerType<input value='"+(this.json.containerType || "")+"'/></td></tr>" +
  245. "<tr><td>host<input value='"+(this.json.host || "")+"'/></td></tr>" +
  246. "<tr><td>port<input value='"+(this.json.port || "")+"'/></td></tr>" +
  247. "<tr><td>proxyHost<input value='"+(this.json.proxyHost || "")+"'/></td></tr>" +
  248. "<tr><td>proxyPort<input value='"+(this.json.proxyPort || "")+"'/></td></tr>" +
  249. "<tr><td>username<input value='"+(this.json.username || "")+"'/></td></tr>" +
  250. "<tr><td>password<input type='password' value='"+(this.json.password || "")+"'/></td></tr>" +
  251. "</table>";
  252. this.inforNode.set("html", html);
  253. var tds = this.inforNode.getElements("td");
  254. var inputs = this.inforNode.getElements("input");
  255. tds.setStyles(this.css.applicationServerDocumentTdNode);
  256. inputs.setStyles(this.css.applicationServerDocumentInputNode);
  257. this.applicationAreaNode = new Element("div", {"styles": this.css.applicationServerDocumentApplicationAreaNode}).inject(this.inforAreaNode);
  258. this.applicationNode = new Element("div", {"styles": this.css.applicationServerDocumentApplicationNode}).inject(this.applicationAreaNode);
  259. this.listApplications();
  260. },
  261. listApplications: function(){
  262. var html = "<table cellPadding='5px' cellSpacing='0' width='100%' align='center'>" +
  263. "<tr><th align='left'>name</th><th align='left'>description</th><th align='left'>weight</th><th align='left'>deployed</th><th align='left'>plan</th></tr>" +
  264. "</table>";
  265. this.applicationNode.set("html", html);
  266. this.applicationTable = this.applicationNode.getElement("table");
  267. this.list.getDepolyableList(function(){
  268. this.list.depolyableList.each(function(json, i){
  269. var color = "#FFF";
  270. if (i%2==0) color = "#f3f4ff";
  271. this.apps.push(new MWF.xApplication.Setting.servers.ApplicationServer.Document.App(this, json.name, color));
  272. }.bind(this));
  273. }.bind(this));
  274. },
  275. closeDocument: function(){
  276. this.node.empty();
  277. this.list.content.setStyle("display", "block");
  278. var size = this.server.node.getSize();
  279. var position = this.server.node.getPosition(this.server.node.getOffsetParent());
  280. var css = {
  281. "width": ""+size.x+"px",
  282. "height": ""+size.y+"px",
  283. "top": ""+position.y+"px",
  284. "left": ""+position.x+"px"
  285. }
  286. //this.morph = new Fx.Morph(this.node,{
  287. // "duration": 100,
  288. // "transition": Fx.Transitions.Sine.easeIn
  289. //});
  290. this.morph.start(css).chain(function(){
  291. this.destroy();
  292. }.bind(this));
  293. },
  294. destroy: function(){
  295. this.app.removeEvent("resize", this.setDocumentSizeFun);
  296. this.apps.each(function(app){
  297. app.destroy();
  298. }.bind(this));
  299. this.node.destroy();
  300. this.list.currentDoc = null;
  301. MWF.release(this);
  302. },
  303. saveDocument: function(){
  304. debugger;
  305. var inputs = this.inforNode.getElements("input");
  306. this.json.name = inputs[0].get("value");
  307. this.json.order = inputs[1].get("value");
  308. this.json.host = inputs[3].get("value");
  309. this.json.port = inputs[4].get("value");
  310. this.json.proxyHost = inputs[5].get("value");
  311. this.json.proxyPort = inputs[6].get("value");
  312. this.json.username = inputs[7].get("value");
  313. this.json.password = inputs[8].get("value");
  314. this.apps.each(function(app){
  315. if (app.weightInput){
  316. if (app.planAppArr.length){
  317. var weight = app.weightInput.get("value").toFloat();
  318. if (!isNaN(weight)){
  319. app.planAppArr[0].weight = weight;
  320. }
  321. }
  322. }
  323. }.bind(this));
  324. if (this.server.name){
  325. this.app.actions.updateAppServer(this.server.name, this.json, function(){
  326. this.closeDocument();
  327. this.server.reload();
  328. }.bind(this));
  329. }else{
  330. this.app.actions.addAppServer(this.json, function(){
  331. this.closeDocument();
  332. this.server.reload();
  333. }.bind(this));
  334. }
  335. },
  336. deleteDocument: function(e){
  337. var _self = this;
  338. this.app.confirm("warn", e, this.app.lp.deleteAppServer_title, this.app.lp.deleteAppServer, "350", "120", function(){
  339. _self.app.actions.removeAppServer(_self.server.name, function(){
  340. this.closeDocument();
  341. this.server.destroy();
  342. }.bind(_self));
  343. this.close();
  344. }, function(){
  345. this.close();
  346. });
  347. },
  348. deploy: function(e){
  349. var _self = this;
  350. this.app.confirm("warn", e, this.app.lp.deployAppServer_title, {"html": this.app.lp.deployAppServer}, "350", "150", function(){
  351. var input = this.content.getElement("input");
  352. var force = "false";
  353. if (input.checked) force = "true";
  354. var inputs = _self.inforNode.getElements("input");
  355. _self.json.name = inputs[0].get("value");
  356. _self.json.order = inputs[1].get("value");
  357. _self.json.host = inputs[3].get("value");
  358. _self.json.port = inputs[4].get("value");
  359. _self.json.username = inputs[5].get("value");
  360. _self.json.password = inputs[6].get("value");
  361. _self.json.weight = inputs[7].get("value");
  362. if (_self.server.name){
  363. _self.app.actions.updateAppServer(_self.server.name, _self.json, function(){
  364. this.app.actions.deploy(this.server.name, force, function(){
  365. this.closeDocument();
  366. this.server.reload();
  367. }.bind(this));
  368. }.bind(_self));
  369. }
  370. this.close();
  371. }, function(){
  372. this.close();
  373. });
  374. }
  375. });
  376. MWF.xApplication.Setting.servers.ApplicationServer.Document.App = new Class({
  377. Implements: [Events],
  378. initialize: function(doc, appName, color){
  379. this.document = doc;
  380. this.server = this.document.server;
  381. this.app = this.server.app;
  382. this.json = this.server.json;
  383. this.css = this.app.css;
  384. this.appName = appName;
  385. this.table = this.document.applicationTable;
  386. this.status = "";
  387. this.load(color);
  388. },
  389. load: function(color){
  390. this.checkbox = new Element("input", {"type": "checkbox", "name": this.appName});
  391. this.tr = new Element("tr", {"styles": {"background-color": color}}).inject(this.table);
  392. var td = new Element("td", {"text": this.appName}).inject(this.tr);
  393. td = new Element("td").inject(this.tr);
  394. this.weightTd = new Element("td").inject(this.tr);
  395. this.weightTd.setStyle("width", "80px");
  396. td = new Element("td").inject(this.tr);
  397. var idx1 = this.json.contextList.indexOf(this.appName);
  398. this.planAppArr = this.json.planList.filter(function(item, index){
  399. return (item.name==this.appName);
  400. }.bind(this));
  401. if ((idx1!=-1) && (this.planAppArr.length)){
  402. new Element("div", {"styles": this.css.applicationServerDocumentApplicationDeployedNode}).inject(td);
  403. this.checkbox.set("checked", true);
  404. this.status = "depolyed";
  405. }else if ((idx1==-1) && (this.planAppArr.length)){
  406. new Element("div", {"styles": this.css.applicationServerDocumentApplicationReadyDeployNode}).inject(td);
  407. this.checkbox.set("checked", true);
  408. this.status = "readyDepoly";
  409. }else if ((idx1!=-1) && (!this.planAppArr.length)){
  410. new Element("div", {"styles": this.css.applicationServerDocumentApplicationReadyDeleteNode}).inject(td);
  411. this.status = "readyDelete";
  412. }else{
  413. new Element("div", {"styles": this.css.applicationServerDocumentApplicationNotDepolyNode}).inject(td);
  414. this.status = "";
  415. }
  416. if (this.checkbox.get("checked")){
  417. this.createWeightInput();
  418. }
  419. td = new Element("td").inject(this.tr);
  420. this.checkbox.inject(td);
  421. var _self = this;
  422. this.checkbox.addEvent("click", function(){
  423. var td = this.getParent().getPrevious("td");
  424. var div = td.getElement("div");
  425. var name = this.get("name");
  426. var tmpArr = _self.json.planList.filter(function(item, index){
  427. return (item.name==name);
  428. });
  429. if (this.checked){
  430. _self.createWeightInput();
  431. if (!tmpArr.length) _self.json.planList.push({"name": name, "weight": 100});
  432. if (_self.status == "depolyed"){
  433. div.setStyle("background", "url(/x_component_Setting/$Main/default/icon/deployed.png) no-repeat center center");
  434. }else{
  435. div.setStyle("background", "url(/x_component_Setting/$Main/default/icon/readyDeploy.png) no-repeat center center");
  436. }
  437. }else{
  438. _self.removeWeightInput();
  439. if (tmpArr.length) _self.json.planList.erase(tmpArr[0]);
  440. if (_self.status == "depolyed"){
  441. div.setStyle("background", "url(/x_component_Setting/$Main/default/icon/readyDelete.png) no-repeat center center");
  442. }else{
  443. div.setStyle("background", "");
  444. }
  445. }
  446. });
  447. },
  448. createWeightInput: function(){
  449. if (!this.weightInput){
  450. this.weightInput = new Element("input", {
  451. "styles": {
  452. "border": "1px solid #BBB",
  453. "width": "80px"
  454. },
  455. "type": "text",
  456. "value": (this.planAppArr.length) ? this.planAppArr[0].weight : ""
  457. }).inject(this.weightTd);
  458. }
  459. },
  460. removeWeightInput: function(){
  461. if (this.weightInput){
  462. this.weightInput.destroy();
  463. this.weightInput = null;
  464. }
  465. },
  466. destroy: function(){
  467. this.tr.destroy();
  468. MWF.release(this);
  469. }
  470. });