Common.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. MWF.xApplication.Profile.Common = {};
  2. MWF.xApplication.Profile.Common.Pagination = new Class({
  3. Implements: [Events, Options],
  4. options: {
  5. "page": 1,
  6. "pageSize": 10,
  7. "showNumber": true,
  8. "showText": false
  9. },
  10. initialize: function (target, options) {
  11. this.setOptions(options);
  12. if (typeof (target) == "string") {
  13. this.target = $(target);
  14. } else {
  15. this.target = target;
  16. }
  17. this.total = this.options.total;
  18. this.length = this.options.length;
  19. this.currentPage = 0;
  20. this.pageSize = this.options.pageSize;
  21. this.pageNode = new Element('div');
  22. this.target.empty();
  23. this.pageNode.inject(this.target);
  24. },
  25. create: function (panel) {
  26. panel.empty();
  27. if(this.page==1){
  28. return;
  29. }
  30. if (this.currentPage > 0) {
  31. var prev = new Element('div', {
  32. 'text': '',
  33. 'class': "pagination-previous page",
  34. //'href': 'javascript:void(null)',
  35. 'events': {
  36. 'click': this.click.bind(this, this.currentPage - 1)
  37. }
  38. });
  39. panel.grab(prev);
  40. }
  41. if (this.options.showNumber) {
  42. var beginInx = this.currentPage - 2 < 0 ? 0 : this.currentPage - 2;
  43. var endIdx = this.currentPage + 2 > this.page ? this.page : this.currentPage + 2;
  44. if (beginInx > 0) panel.grab(this.createNumber(0));
  45. if (beginInx > 1) panel.grab(this.createNumber(1));
  46. if (beginInx > 2) panel.grab(this.createSplit());
  47. for (var i = beginInx; i < endIdx; i++) {
  48. panel.grab(this.createNumber(i));
  49. }
  50. if (endIdx < this.page - 2) panel.grab(this.createSplit());
  51. if (endIdx < this.page - 1) panel.grab(this.createNumber(this.page - 2));
  52. if (endIdx < this.page) panel.grab(this.createNumber(this.page - 1));
  53. }
  54. if (this.currentPage < this.page - 1) {
  55. var next = new Element('div', {
  56. 'text': '',
  57. 'class': 'pagination-next page',
  58. //'href': 'javascript:void(null)',
  59. 'events': {
  60. 'click': this.click.bind(this, this.currentPage + 1)
  61. }
  62. });
  63. panel.grab(next);
  64. }
  65. if (this.options.showText) panel.grab(this.createText());
  66. },
  67. createNumber: function (i) {
  68. var a = new Element('div', {
  69. 'text': i + 1,
  70. "class": "pagination-link page",
  71. //s'href': 'javascript:void(null)',
  72. 'events': {'click': this.click.bind(this, i)}
  73. });
  74. if (i === this.currentPage) {
  75. a.set('class', "pagination-link page is-current mainColor_bg");
  76. }
  77. return a;
  78. },
  79. createSplit: function () {
  80. return new Element('div.page', {'text': '...'});
  81. },
  82. createText: function () {
  83. return new Element('span', {
  84. 'text': '第' + (this.currentPage + 1) + '/' + (this.page) + '页',
  85. 'style': "margin:0 3px;"
  86. });
  87. },
  88. click: function (index) {
  89. this.currentPage = index;
  90. this.load();
  91. this.fireEvent('afterLoad', [this.currentPage + 1]);
  92. },
  93. load: function () {
  94. this.fireEvent('beforeLoad');
  95. this.page = !this.total?(this.currentPage + Math.ceil((this.length)+1 / this.pageSize)):Math.ceil(this.total / this.pageSize);
  96. this.create(this.pageNode);
  97. },
  98. reload: function (param) {
  99. this.currentPage = 0;
  100. this.load();
  101. },
  102. setpageSize: function (pageSize) {
  103. this.pageSize = pageSize;
  104. this.reload();
  105. }
  106. });
  107. MWF.xApplication.Profile.Common.Content = new Class({
  108. Implements: [Events, Options],
  109. options: {
  110. "pageSize": 10,
  111. "page":1,
  112. "pagination": true,
  113. "search": false,
  114. "columns": [],
  115. "title": "",
  116. "searchItemList": []
  117. },
  118. initialize: function (target, options) {
  119. this.setOptions(options);
  120. this.searchItemList = this.options.searchItemList;
  121. this.opButtonList = this.options.opButtonList;
  122. this.columns = this.options.columns;
  123. this.contentNode = target;
  124. this.contentNode.empty();
  125. },
  126. load: function () {
  127. //this.createNavi();
  128. //this.createSearch();
  129. //this.createOp();
  130. this.createTable();
  131. this.createPagination();
  132. },
  133. reload: function (param) {
  134. this.currentPage = 0;
  135. this.load();
  136. },
  137. createPagination: function () {
  138. if (this.paginationNode) this.paginationNode.remove();
  139. this.paginationNode = new Element("div", {
  140. "class": "pagination ",
  141. "style": "padding: 10px;"
  142. }).inject(this.contentNode);
  143. new MWF.xApplication.Profile.Common.Pagination(this.paginationNode, {
  144. "total": this.total,
  145. "length": this.dataList.length,
  146. "pageSize": this.options.pageSize,
  147. "showNumber": true,
  148. "showText": false,
  149. "onAfterLoad": function (data) {
  150. this.options.page = data;
  151. this.createTbody();
  152. }.bind(this)
  153. }).load();
  154. },
  155. createOp: function () {
  156. this.opNode = new Element("div", {
  157. "class": "container is-fluid buttons",
  158. "style": "margin-left:10px;margin-bottom:0px"
  159. }).inject(this.contentNode);
  160. this.opButtonList.each(function (opButton) {
  161. var opButtonNode = new Element("a", {
  162. "class": "button is-link",
  163. "text": opButton.text
  164. }).inject(this.opNode);
  165. if (opButton.action) {
  166. opButtonNode.addEvent("click", function (ev) {
  167. opButton.action(ev, this);
  168. }.bind(this));
  169. }
  170. }.bind(this));
  171. },
  172. createNavi: function () {
  173. this.subNaviNode = new Element("div", {
  174. "style": "background-color:#3273dc;color:#fff;padding:0.5em 0.5em;margin-bottom:0.5em;display:block",
  175. "html": this.options.title
  176. }).inject(this.contentNode);
  177. },
  178. createSearch: function () {
  179. this.searchItemNode = new Element("div", {
  180. "style": "padding: 0.5rem"
  181. }).inject(this.contentNode);
  182. this.searchItemList.each(function (searchItem) {
  183. var itemNode;
  184. if (searchItem.type === "date" || searchItem.type === "input") {
  185. itemNode = new Element("input", {
  186. "class": "input control",
  187. "name": searchItem.name,
  188. "placeholder": searchItem.title,
  189. "style": "width:200px;margin:5px;"
  190. }).inject(this.searchItemNode);
  191. if (searchItem.type === "date") new MWF.widget.Calendar(itemNode, {
  192. "style": "xform",
  193. "onComplate": function () {
  194. }
  195. });
  196. }
  197. if (searchItem.type === "select") {
  198. var itemParentNode = new Element("div", {"class": "select"}).inject(this.searchItemNode);
  199. itemNode = new Element("select", {
  200. "style": "width:200px;margin:5px;",
  201. "name": searchItem.name
  202. }).inject(itemParentNode);
  203. searchItem.options.each(function (option) {
  204. new Element("option", {
  205. "text": option.split("|")[0],
  206. "value": option.split("|").length > 1 ? option.split("|")[1] : option.split("|")[0]
  207. }).inject(itemNode);
  208. });
  209. }
  210. if (searchItem.click) {
  211. itemNode.addEvent("click", function (ev) {
  212. searchItem.click(ev, itemNode);
  213. });
  214. }
  215. }.bind(this));
  216. this.searchOkButton = new Element("a", {
  217. "class": "button is-link",
  218. "style": "margin:5px;",
  219. "text": "搜索"
  220. }).inject(this.searchItemNode).addEvent("click", function () {
  221. this.options.page = 1;
  222. this.createTbody();
  223. this.createPagination();
  224. }.bind(this));
  225. this.searchCancleButton = new Element("a", {
  226. "class": "button",
  227. "style": "margin:5px;",
  228. "text": "取消"
  229. }).inject(this.searchItemNode).addEvent("click", function () {
  230. this.searchItemList.each(function (item) {
  231. $$('[name=' + item.name + ']').set("value", "");
  232. }.bind(this));
  233. this.options.page = 1;
  234. this.createTbody();
  235. this.createPagination();
  236. }.bind(this));
  237. },
  238. getMtSelects: function () {
  239. var arr = [];
  240. $$('input[name="mtSelectItem"]').each(function (item) {
  241. if (item.checked) {
  242. arr.push(item.getParent().getParent().retrieve("data"));
  243. }
  244. });
  245. return arr;
  246. },
  247. createTable: function () {
  248. this.tableDivNode = new Element("div.profile_common_tableDiv").inject(this.contentNode);
  249. this.tableNode = new Element("table", {
  250. "class": "table is-fullwidth is-hoverable emPowerTable",
  251. "style": "margin-bottom:0px",
  252. "width":"100%",
  253. "border":"0",
  254. "cellpadding":"0",
  255. "cellspacing":"0"
  256. }).inject(this.tableDivNode);
  257. this.createThead();
  258. this.createTbody();
  259. },
  260. createThead: function () {
  261. this.theadNode = new Element("thead").inject(this.tableNode);
  262. var trNode = new Element("tr.first").inject(this.theadNode);
  263. this.columns.each(function (column) {
  264. var thNode = new Element("th").inject(trNode);
  265. if (column.title) thNode.set("text", column.title);
  266. if (column.width) thNode.setStyle("width", column.width);
  267. if (column.type) {
  268. if (column.type === "checkbox") {
  269. var checkAllNode = new Element("input", {"type": "checkbox", "name": "mtSelectAll"}).inject(thNode);
  270. checkAllNode.addEvent("click", function () {
  271. $$('input[name="mtSelectItem"]').each(function (item) {
  272. if (checkAllNode.checked) {
  273. item.checked = 'checked';
  274. } else {
  275. item.checked = '';
  276. }
  277. });
  278. });
  279. }
  280. }
  281. });
  282. },
  283. createTbody: function () {
  284. if (this.tbodyNode) this.tbodyNode.remove();
  285. this.tbodyNode = new Element("tbody").inject(this.tableNode);
  286. this.fireEvent('beforeLoadData');
  287. this.dataList.each(function (data) {
  288. var trNode = new Element("tr").inject(this.tbodyNode);
  289. trNode.store("data", data);
  290. this.columns.each(function (column) {
  291. var thNode = new Element("td").inject(trNode);
  292. if(column.formatter){
  293. column.formatter(data,thNode);
  294. }else{
  295. thNode.set("text",data[column.field])
  296. }
  297. if (column.type) {
  298. if (column.type === "operation") {
  299. column.opButtonList.each(function (opButton) {
  300. var opButtonNode = new Element("a", {
  301. "class": "button is-link",
  302. "text": opButton.text
  303. }).inject(thNode);
  304. if (opButton.action) {
  305. opButtonNode.addEvent("click", function (ev) {
  306. opButton.action(data);
  307. }.bind(this));
  308. }
  309. }.bind(this));
  310. }
  311. if (column.type === "checkbox") {
  312. var checkItemNode = new Element("input", {
  313. "type": "checkbox",
  314. "name": "mtSelectItem"
  315. }).inject(thNode);
  316. checkItemNode.addEvent("click", function (event) {
  317. if (event.target.checked) {
  318. var i = 0;
  319. $$('input[name="mtSelectItem"]').each(function (chk) {
  320. if (!chk.checked) {
  321. i = i + 1;
  322. }
  323. });
  324. if (i === 0) {
  325. $$('input[name="mtSelectAll"]')[0].checked = 'checked';
  326. }
  327. } else {
  328. $$('input[name="mtSelectAll"]')[0].checked = '';
  329. }
  330. });
  331. }
  332. }
  333. }.bind(this));
  334. }.bind(this));
  335. }
  336. });