List.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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+"'>"+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. }
  70. },
  71. addItem: function(){
  72. var data = Object.clone(this.options.data);
  73. var tr = new MWF.xApplication.Org.List.Item(data, this.options.attr, this);
  74. this.items.push(tr);
  75. tr.edit(tr.tr.tds[1]);
  76. },
  77. deleteItem: function(e){
  78. if (this.selectedItems.length){
  79. this.fireEvent("queryDelete");
  80. var _self = this;
  81. this.content.explorer.app.confirm("infor", e, this.options.deleteItemTitle, this.options.deleteItemText, 350, 120, function(){
  82. this.close();
  83. var delCount = 0;
  84. var deleteCompleted = function(){
  85. if (delCount === _self.selectedItems.length){
  86. var continueDelete = true;
  87. _self.fireEvent("delete", continueDelete);
  88. if (continueDelete){
  89. while (_self.selectedItems.length){
  90. var tr = _self.selectedItems[0];
  91. tr.destroy();
  92. }
  93. }
  94. _self.fireEvent("postDelete", delCount);
  95. }
  96. };
  97. _self.selectedItems.each(function(item){
  98. item["delete"](function(){
  99. delCount++;
  100. deleteCompleted();
  101. });
  102. }.bind(this));
  103. }, function(){this.close();});
  104. }
  105. },
  106. push: function(data){
  107. // var rows = [""];
  108. // attr.each(function(n){
  109. // if (typeOf(n)==="function"){
  110. // rows.push(n.apply(data));
  111. // }else if(typeOf(n)==="string"){
  112. // rows.push(data[n]);
  113. // }else{
  114. // rows.push("");
  115. // }
  116. // }.bind(this));
  117. // var tr = this.table.push(rows, {"styles": this.css.contentTrNode});
  118. // tr.tds.each(function(td){td.setStyles(this.css.contentTdNode);}.bind(this));
  119. // tr.tds[0].setStyles(this.css.selectTdNode);
  120. var i = this.items.push(new MWF.xApplication.Org.List.Item(data, this.options.attr, this));
  121. return this.items[i-1];
  122. },
  123. setAction: function(){
  124. if (this.selectedItems.length){
  125. this.deleteAction.setStyles(this.css.deleteActionNode);
  126. }else{
  127. this.deleteAction.setStyles(this.css.deleteActionNode_disabled);
  128. }
  129. },
  130. clear: function(){
  131. this.items = [];
  132. this.selectedItems = [];
  133. var table = this.contentNode.getFirst("table");
  134. while (table.rows.length>1){
  135. table.rows[table.rows.length-1].destroy();
  136. }
  137. //this.reLoad();
  138. }
  139. });
  140. MWF.xApplication.Org.List.Item = new Class({
  141. initialize: function(data, attr, list){
  142. this.data = data;
  143. this.attr = attr;
  144. this.list = list;
  145. this.css = this.list.css;
  146. this.load();
  147. },
  148. reload: function(data){
  149. this.data = data;
  150. this.tr.tds.each(function(td, i){
  151. if (i===0) this.selectTd = td;
  152. if (i>0){
  153. var at = this.attr[i-1];
  154. if (typeOf(at)==="object"){
  155. if (at.get){
  156. td.set("text", at.get.apply(this.data));
  157. }else{
  158. td.set("text", "");
  159. }
  160. }else if(typeOf(at)==="string"){
  161. if (at==="icon"){
  162. td.set("html", "<div></div>");
  163. }else{
  164. var v = this.data[at];
  165. if (typeOf(v)==="array") v = v.join(",");
  166. td.set("text", v);
  167. }
  168. }else{
  169. td.set("text", "");
  170. }
  171. if (at.events){
  172. if (at.events["init"]) at.events["init"].apply({"item": this, "data": this.data, "td": td, "item": this});
  173. }
  174. }
  175. td.setStyles(this.css.contentTdNode);
  176. td.set("title", td.get("text"));
  177. }.bind(this));
  178. },
  179. load: function(){
  180. var rows = [""];
  181. this.attr.each(function(n){
  182. if (typeOf(n)==="object"){
  183. if (n.get){
  184. var v = n.get.apply(this.data) || "";
  185. v = v.replace(/\</g, "&lt;");
  186. v = v.replace(/\</g, "&gt;");
  187. rows.push(v);
  188. }else if(n.getHtml){
  189. var v = n.getHtml.apply(this.data);
  190. rows.push(v);
  191. }else{
  192. rows.push("");
  193. }
  194. }else if(typeOf(n)==="string"){
  195. if (n==="icon"){
  196. rows.push("<div>cc</div>");
  197. }else{
  198. rows.push(this.data[n]);
  199. }
  200. }else{
  201. rows.push("");
  202. }
  203. }.bind(this));
  204. this.tr = this.list.table.push(rows, {"styles": this.css.contentTrNode});
  205. this.tr.tr.store("data", this.data);
  206. var _self = this;
  207. this.tr.tds.each(function(td, i){
  208. td.setStyles(this.css.contentTdNode);
  209. td.set("title", td.get("text"));
  210. if (i===0) this.selectTd = td;
  211. if (i>0){
  212. if (this.list.options.action || this.list.options.canEdit){
  213. td.store("attr", this.attr[i-1]);
  214. if (this.list.options.canEdit){
  215. td.addEvent("click", function(){
  216. _self.edit(this);
  217. });
  218. }
  219. }
  220. var at = this.attr[i-1];
  221. if (at.events){
  222. td.removeEvents("click");
  223. Object.each(at.events, function(v, k){
  224. if (k.toLowerCase!=="init") td.addEvent(k, v.bind({"data": this.data, "td": td, "item": this}));
  225. }.bind(this));
  226. if (at.events["init"]) at.events["init"].apply({"item": this, "data": this.data, "td": td, "item": this});
  227. }
  228. }
  229. }.bind(this));
  230. if (this.list.options.action){
  231. this.selectTd.setStyles(this.css.selectTdNode);
  232. this.selectTd.addEvent("click", function(){
  233. if (!this.isSelected){
  234. this.selected();
  235. }else{
  236. this.unSelected();
  237. }
  238. }.bind(this));
  239. }else{
  240. this.selectTd.setStyles(this.css.blankTdNode);
  241. }
  242. },
  243. edit: function(td){
  244. var attr = td.retrieve("attr");
  245. var text = td.get("text");
  246. td.empty();
  247. var input = new Element("input", {"styles": this.css.inputNode, "value": text}).inject(td);
  248. td.removeEvents("click");
  249. var _self = this;
  250. input.focus();
  251. input.addEvents({
  252. "blur": function(){
  253. var value = this.get("value");
  254. if (value){
  255. if (typeOf(attr)==="object"){
  256. if (attr.set){
  257. attr.set.apply(_self.data, [value]);
  258. }
  259. }else if(typeOf(attr)==="string") {
  260. _self.data[attr] = value
  261. }
  262. }
  263. _self.editCompleted(td, value, text);
  264. }
  265. });
  266. },
  267. editCompleted: function(td, value, text){
  268. td.empty();
  269. if (!value && !text){
  270. if (td.cellIndex===1){
  271. this.destroy();
  272. }
  273. }else if (!value){
  274. if (td.cellIndex===1){
  275. td.set("text", text);
  276. }else{
  277. td.set("text", value);
  278. if (value!==text) this.save(td);
  279. }
  280. }else{
  281. td.set("text", value);
  282. if (value!==text) this.save(td);
  283. }
  284. var _self = this;
  285. if (this.list.options.canEdit){
  286. td.addEvent("click", function(){
  287. _self.edit(this);
  288. });
  289. }
  290. },
  291. "delete": function(callback){
  292. this.list.content.explorer.actions[this.list.options.deleteAction](this.data.id, function(){
  293. if (callback) callback();
  294. //this.destroy();
  295. }.bind(this));
  296. },
  297. destroy: function(){
  298. this.list.items.erase(this);
  299. if (this.isSelected) this.unSelected();
  300. this.list.setAction();
  301. this.tr.tr.destroy();
  302. MWF.release(this);
  303. },
  304. save: function(td){
  305. this.list.content.explorer.actions[this.list.options.saveAction](this.data, function(json){
  306. this.list.fireEvent("postSave", [this, json.data.id]);
  307. this.data.id = json.data.id;
  308. }.bind(this), function(xhr, text, error){
  309. td.set("text", "");
  310. this.edit(td);
  311. this.list.content.explorer.app.notice((JSON.decode(xhr.responseText).message.trim() || "request json error"), "error");
  312. }.bind(this));
  313. },
  314. selected: function(){
  315. this.selectTd.setStyles(this.css.selectTdNode_selected);
  316. this.tr.tr.setStyles(this.css.contentTrNode_selected);
  317. this.list.selectedItems.push(this);
  318. this.isSelected = true;
  319. this.list.setAction();
  320. },
  321. unSelected: function(){
  322. this.selectTd.setStyles(this.css.selectTdNode);
  323. this.tr.tr.setStyles(this.css.contentTrNode);
  324. this.list.selectedItems.erase(this);
  325. this.isSelected = false;
  326. this.list.setAction();
  327. }
  328. });