List.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. MWF.xApplication.Org.List = new Class({
  2. Extends: MWF.widget.Common,
  3. Implements: [Options, Events],
  4. options: {
  5. "style": "default",
  6. "action": false,
  7. "canEdit": true,
  8. "data": {
  9. "name": "",
  10. "attributeList": [""]
  11. },
  12. "attr": [],
  13. "saveAction": "savePersonAttribute",
  14. "deleteAction": "removePersonAttribute",
  15. "deleteItemTitle": MWF.xApplication.Org.LP.deleteAttributeTitle,
  16. "deleteItemText": MWF.xApplication.Org.LP.deleteAttribute
  17. },
  18. _loadPath: function(){
  19. this.path = "../x_component_Org/$List/";
  20. this.cssPath = "../x_component_Org/$List/"+this.options.style+"/css.wcss";
  21. },
  22. initialize: function(node, content, options){
  23. this.setOptions(options);
  24. this._loadPath();
  25. this._loadCss();
  26. this.content = content;
  27. this.contentNode = $(node);
  28. this.items = [];
  29. this.selectedItems = [];
  30. },
  31. load: function(headers){
  32. this.headers = headers;
  33. //this.node = new Element("div", {"styles": this.css.node}).inject(this.contentNode);
  34. var html = "<table cellspacing='0' cellpadding='5' border='0' width='80%' align='center' style='line-height:normal; clear: both;'>";
  35. html += "<tr><th style='width:20px'></th>";
  36. headers.each(function(title){
  37. html += "<th style='"+title.style+"'>"+o2.txt(title.text)+"</th>";
  38. }.bind(this));
  39. html += "</table>";
  40. this.contentNode.set("html", html);
  41. this.loadAction();
  42. this.table = new HtmlTable(this.contentNode.getFirst("table"));
  43. this.contentNode.getElements("th").setStyles(this.css.listTitle);
  44. },
  45. // reLoad: function(){
  46. // var html = "<table cellspacing='0' cellpadding='5' border='0' width='80%' align='center' style='line-height:normal; clear: both;'>";
  47. // html += "<tr><th style='width:20px'></th>";
  48. // this.headers.each(function(title){
  49. // html += "<th style='"+title.style+"'>"+title.text+"</th>";
  50. // }.bind(this));
  51. // html += "</table>";
  52. // this.contentNode.set("html", html);
  53. //
  54. // this.table = new HtmlTable(this.contentNode.getFirst("table"));
  55. // this.contentNode.getElements("th").setStyles(this.css.listTitle);
  56. // },
  57. loadAction: function(){
  58. this.actionAreaNode = new Element("div", {"styles": this.css.actionAreaNode}).inject(this.contentNode, "top");
  59. if (this.options.action){
  60. this.actionNode = new Element("div", {"styles": this.css.actionNode}).inject(this.actionAreaNode);
  61. this.deleteAction = new Element("div", {"styles": this.css.deleteActionNode_disabled, "text": this.content.explorer.app.lp.delete}).inject(this.actionNode);
  62. this.addAction = new Element("div", {"styles": this.css.addActionNode, "text": this.content.explorer.app.lp.add}).inject(this.actionNode);
  63. this.addAction.addEvent("click", function(){
  64. this.addItem();
  65. }.bind(this));
  66. this.deleteAction.addEvent("click", function(e){
  67. this.deleteItem(e);
  68. }.bind(this));
  69. this.fireEvent("postLoadAction")
  70. }
  71. },
  72. addItem: function(){
  73. var data = Object.clone(this.options.data);
  74. var tr = new MWF.xApplication.Org.List.Item(data, this.options.attr, this);
  75. this.items.push(tr);
  76. tr.edit(tr.tr.tds[1]);
  77. },
  78. deleteItem: function(e){
  79. if (this.selectedItems.length){
  80. this.fireEvent("queryDelete");
  81. var _self = this;
  82. this.content.explorer.app.confirm("infor", e, this.options.deleteItemTitle, this.options.deleteItemText, 350, 120, function(){
  83. this.close();
  84. var delCount = 0;
  85. var deleteCompleted = function(){
  86. if (delCount === _self.selectedItems.length){
  87. var continueDelete = true;
  88. _self.fireEvent("delete", continueDelete);
  89. if (continueDelete){
  90. while (_self.selectedItems.length){
  91. var tr = _self.selectedItems[0];
  92. tr.destroy();
  93. }
  94. }
  95. _self.fireEvent("postDelete", delCount);
  96. }
  97. };
  98. _self.selectedItems.each(function(item){
  99. item["delete"](function(){
  100. delCount++;
  101. deleteCompleted();
  102. });
  103. }.bind(this));
  104. }, function(){this.close();});
  105. }
  106. },
  107. push: function(data){
  108. // var rows = [""];
  109. // attr.each(function(n){
  110. // if (typeOf(n)==="function"){
  111. // rows.push(n.apply(data));
  112. // }else if(typeOf(n)==="string"){
  113. // rows.push(data[n]);
  114. // }else{
  115. // rows.push("");
  116. // }
  117. // }.bind(this));
  118. // var tr = this.table.push(rows, {"styles": this.css.contentTrNode});
  119. // tr.tds.each(function(td){td.setStyles(this.css.contentTdNode);}.bind(this));
  120. // tr.tds[0].setStyles(this.css.selectTdNode);
  121. var i = this.items.push(new MWF.xApplication.Org.List.Item(data, this.options.attr, this));
  122. return this.items[i-1];
  123. },
  124. setAction: function(){
  125. if (this.selectedItems.length){
  126. this.deleteAction.setStyles(this.css.deleteActionNode);
  127. }else{
  128. this.deleteAction.setStyles(this.css.deleteActionNode_disabled);
  129. }
  130. },
  131. clear: function(){
  132. this.items = [];
  133. this.selectedItems = [];
  134. var table = this.contentNode.getFirst("table");
  135. while (table.rows.length>1){
  136. table.rows[table.rows.length-1].destroy();
  137. }
  138. //this.reLoad();
  139. }
  140. });
  141. MWF.xApplication.Org.List.Item = new Class({
  142. initialize: function(data, attr, list){
  143. this.data = data;
  144. this.attr = attr;
  145. this.list = list;
  146. this.css = this.list.css;
  147. this.load();
  148. },
  149. reload: function(data){
  150. this.data = data;
  151. this.tr.tds.each(function(td, i){
  152. if (i===0) this.selectTd = td;
  153. if (i>0){
  154. var at = this.attr[i-1];
  155. if (typeOf(at)==="object"){
  156. if (at.get){
  157. td.set("text", at.get.apply(this.data));
  158. }else{
  159. td.set("text", "");
  160. }
  161. }else if(typeOf(at)==="string"){
  162. if (at==="icon"){
  163. td.set("html", "<div></div>");
  164. }else{
  165. var v = this.data[at];
  166. if (typeOf(v)==="array") v = v.join(",");
  167. td.set("text", o2.txt(v) );
  168. }
  169. }else{
  170. td.set("text", "");
  171. }
  172. if (at.events){
  173. if (at.events["init"]) at.events["init"].apply({"item": this, "data": this.data, "td": td, "item": this});
  174. }
  175. }
  176. td.setStyles(this.css.contentTdNode);
  177. td.set("title", td.get("text"));
  178. }.bind(this));
  179. },
  180. load: function(){
  181. var rows = [""];
  182. this.attr.each(function(n){
  183. if (typeOf(n)==="object"){
  184. if (n.get){
  185. var v = n.get.apply(this.data) || "";
  186. v = v.replace(/\</g, "&lt;");
  187. v = v.replace(/\</g, "&gt;");
  188. rows.push(v);
  189. }else if(n.getHtml){
  190. var v = n.getHtml.apply(this.data);
  191. rows.push(v);
  192. }else{
  193. rows.push("");
  194. }
  195. }else if(typeOf(n)==="string"){
  196. if (n==="icon"){
  197. rows.push("<div>cc</div>");
  198. }else{
  199. rows.push(typeOf(this.data[n])==='string' ? o2.txt(this.data[n]) : this.data[n]);
  200. }
  201. }else{
  202. rows.push("");
  203. }
  204. }.bind(this));
  205. this.tr = this.list.table.push(rows, {"styles": this.css.contentTrNode});
  206. this.tr.tr.store("data", this.data);
  207. var _self = this;
  208. this.tr.tds.each(function(td, i){
  209. td.setStyles(this.css.contentTdNode);
  210. td.set("title", td.get("text"));
  211. if (i===0) this.selectTd = td;
  212. if (i>0){
  213. if (this.list.options.action || this.list.options.canEdit){
  214. td.store("attr", this.attr[i-1]);
  215. if (this.list.options.canEdit){
  216. td.addEvent("click", function(){
  217. _self.edit(this);
  218. });
  219. }
  220. }
  221. var at = this.attr[i-1];
  222. if (at.events){
  223. td.removeEvents("click");
  224. Object.each(at.events, function(v, k){
  225. if (k.toLowerCase!=="init") td.addEvent(k, v.bind({"data": this.data, "td": td, "item": this}));
  226. }.bind(this));
  227. if (at.events["init"]) at.events["init"].apply({"item": this, "data": this.data, "td": td, "item": this});
  228. }
  229. }
  230. }.bind(this));
  231. if (this.list.options.action){
  232. this.selectTd.setStyles(this.css.selectTdNode);
  233. this.selectTd.addEvent("click", function(){
  234. if (!this.isSelected){
  235. this.selected();
  236. }else{
  237. this.unSelected();
  238. }
  239. }.bind(this));
  240. }else{
  241. this.selectTd.setStyles(this.css.blankTdNode);
  242. }
  243. },
  244. edit: function(td){
  245. var attr = td.retrieve("attr");
  246. var text = td.get("text");
  247. td.empty();
  248. var input = new Element("input", {"styles": this.css.inputNode, "value": text}).inject(td);
  249. td.removeEvents("click");
  250. var _self = this;
  251. input.focus();
  252. input.addEvents({
  253. "blur": function(){
  254. var value = this.get("value");
  255. if (value){
  256. if (typeOf(attr)==="object"){
  257. if (attr.set){
  258. attr.set.apply(_self.data, [value]);
  259. }
  260. }else if(typeOf(attr)==="string") {
  261. _self.data[attr] = value
  262. }
  263. }
  264. _self.editCompleted(td, value, text);
  265. }
  266. });
  267. },
  268. editCompleted: function(td, value, text){
  269. td.empty();
  270. if (!value && !text){
  271. if (td.cellIndex===1){
  272. this.destroy();
  273. }
  274. }else if (!value){
  275. if (td.cellIndex===1){
  276. td.set("text", text);
  277. }else{
  278. td.set("text", value);
  279. if (value!==text) this.save(td);
  280. }
  281. }else{
  282. td.set("text", value);
  283. if (value!==text) this.save(td);
  284. }
  285. var _self = this;
  286. if (this.list.options.canEdit){
  287. td.addEvent("click", function(){
  288. _self.edit(this);
  289. });
  290. }
  291. },
  292. "delete": function(callback){
  293. this.list.content.explorer.actions[this.list.options.deleteAction](this.data.id, function(){
  294. if (callback) callback();
  295. //this.destroy();
  296. }.bind(this));
  297. },
  298. destroy: function(){
  299. this.list.items.erase(this);
  300. if (this.isSelected) this.unSelected();
  301. this.list.setAction();
  302. this.tr.tr.destroy();
  303. MWF.release(this);
  304. },
  305. save: function(td){
  306. this.list.content.explorer.actions[this.list.options.saveAction](this.data, function(json){
  307. this.list.fireEvent("postSave", [this, json.data.id]);
  308. this.data.id = json.data.id;
  309. }.bind(this), function(xhr, text, error){
  310. td.set("text", "");
  311. this.edit(td);
  312. this.list.content.explorer.app.notice((JSON.decode(xhr.responseText).message.trim() || "request json error"), "error");
  313. }.bind(this));
  314. },
  315. selected: function(){
  316. this.selectTd.setStyles(this.css.selectTdNode_selected);
  317. this.tr.tr.setStyles(this.css.contentTrNode_selected);
  318. this.list.selectedItems.push(this);
  319. this.isSelected = true;
  320. this.list.setAction();
  321. },
  322. unSelected: function(){
  323. this.selectTd.setStyles(this.css.selectTdNode);
  324. this.tr.tr.setStyles(this.css.contentTrNode);
  325. this.list.selectedItems.erase(this);
  326. this.isSelected = false;
  327. this.list.setAction();
  328. }
  329. });