Common.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. var text = MWF.xApplication.Profile.LP.pageText.replace("{n}", (this.currentPage + 1) + '/' + (this.page) );
  84. return new Element('span', {
  85. 'text': text,
  86. 'style': "margin:0 3px;"
  87. });
  88. },
  89. click: function (index) {
  90. this.currentPage = index;
  91. this.load();
  92. this.fireEvent('afterLoad', [this.currentPage + 1]);
  93. },
  94. toPage: function (index) {
  95. if( typeOf(index) === "number" )this.currentPage = index;
  96. this.load();
  97. this.fireEvent('afterLoad', [this.currentPage + 1]);
  98. },
  99. load: function () {
  100. this.fireEvent('beforeLoad');
  101. this.page = !this.total?(this.currentPage + Math.ceil((this.length)+1 / this.pageSize)):Math.ceil(this.total / this.pageSize);
  102. this.create(this.pageNode);
  103. },
  104. reload: function (param) {
  105. this.currentPage = 0;
  106. this.load();
  107. },
  108. setpageSize: function (pageSize) {
  109. this.pageSize = pageSize;
  110. this.reload();
  111. }
  112. });
  113. MWF.xApplication.Profile.Common.Content = new Class({
  114. Implements: [Events, Options],
  115. options: {
  116. "pageSize": 10,
  117. "page":1,
  118. "pagination": true,
  119. "search": false,
  120. "columns": [],
  121. "title": "",
  122. "searchItemList": []
  123. },
  124. initialize: function (target, options) {
  125. this.setOptions(options);
  126. this.searchItemList = this.options.searchItemList;
  127. this.opButtonList = this.options.opButtonList;
  128. this.columns = this.options.columns;
  129. this.contentNode = target;
  130. this.contentNode.empty();
  131. },
  132. load: function () {
  133. //this.createNavi();
  134. //this.createSearch();
  135. //this.createOp();
  136. this.createTable();
  137. this.createPagination();
  138. },
  139. reload: function (param) {
  140. this.currentPage = 0;
  141. this.load();
  142. },
  143. createPagination: function () {
  144. if (this.paginationNode) this.paginationNode.remove();
  145. this.paginationNode = new Element("div", {
  146. "class": "pagination ",
  147. "style": "padding: 10px;"
  148. }).inject(this.contentNode);
  149. this.pagination = new MWF.xApplication.Profile.Common.Pagination(this.paginationNode, {
  150. "total": this.total,
  151. "length": this.dataList.length,
  152. "pageSize": this.options.pageSize,
  153. "showNumber": true,
  154. "showText": false,
  155. "onAfterLoad": function (data) {
  156. this.options.page = data;
  157. this.createTbody();
  158. }.bind(this)
  159. });
  160. this.pagination.load();
  161. },
  162. createOp: function () {
  163. this.opNode = new Element("div", {
  164. "class": "container is-fluid buttons",
  165. "style": "margin-left:10px;margin-bottom:0px"
  166. }).inject(this.contentNode);
  167. this.opButtonList.each(function (opButton) {
  168. var opButtonNode = new Element("a", {
  169. "class": "button is-link",
  170. "text": opButton.text
  171. }).inject(this.opNode);
  172. if (opButton.action) {
  173. opButtonNode.addEvent("click", function (ev) {
  174. opButton.action(ev, this);
  175. }.bind(this));
  176. }
  177. }.bind(this));
  178. },
  179. createNavi: function () {
  180. this.subNaviNode = new Element("div", {
  181. "style": "background-color:#3273dc;color:#fff;padding:0.5em 0.5em;margin-bottom:0.5em;display:block",
  182. "html": this.options.title
  183. }).inject(this.contentNode);
  184. },
  185. createSearch: function () {
  186. this.searchItemNode = new Element("div", {
  187. "style": "padding: 0.5rem"
  188. }).inject(this.contentNode);
  189. this.searchItemList.each(function (searchItem) {
  190. var itemNode;
  191. if (searchItem.type === "date" || searchItem.type === "input") {
  192. itemNode = new Element("input", {
  193. "class": "input control",
  194. "name": searchItem.name,
  195. "placeholder": searchItem.title,
  196. "style": "width:200px;margin:5px;"
  197. }).inject(this.searchItemNode);
  198. if (searchItem.type === "date") new MWF.widget.Calendar(itemNode, {
  199. "style": "xform",
  200. "onComplate": function () {
  201. }
  202. });
  203. }
  204. if (searchItem.type === "select") {
  205. var itemParentNode = new Element("div", {"class": "select"}).inject(this.searchItemNode);
  206. itemNode = new Element("select", {
  207. "style": "width:200px;margin:5px;",
  208. "name": searchItem.name
  209. }).inject(itemParentNode);
  210. searchItem.options.each(function (option) {
  211. new Element("option", {
  212. "text": option.split("|")[0],
  213. "value": option.split("|").length > 1 ? option.split("|")[1] : option.split("|")[0]
  214. }).inject(itemNode);
  215. });
  216. }
  217. if (searchItem.click) {
  218. itemNode.addEvent("click", function (ev) {
  219. searchItem.click(ev, itemNode);
  220. });
  221. }
  222. }.bind(this));
  223. this.searchOkButton = new Element("a", {
  224. "class": "button is-link",
  225. "style": "margin:5px;",
  226. "text": MWF.xApplication.Profile.LP.search
  227. }).inject(this.searchItemNode).addEvent("click", function () {
  228. this.options.page = 1;
  229. this.createTbody();
  230. this.createPagination();
  231. }.bind(this));
  232. this.searchCancleButton = new Element("a", {
  233. "class": "button",
  234. "style": "margin:5px;",
  235. "text": MWF.xApplication.Profile.LP.cancel1
  236. }).inject(this.searchItemNode).addEvent("click", function () {
  237. this.searchItemList.each(function (item) {
  238. $$('[name=' + item.name + ']').set("value", "");
  239. }.bind(this));
  240. this.options.page = 1;
  241. this.createTbody();
  242. this.createPagination();
  243. }.bind(this));
  244. },
  245. getMtSelects: function () {
  246. var arr = [];
  247. $$('input[name="mtSelectItem"]').each(function (item) {
  248. if (item.checked) {
  249. arr.push(item.getParent().getParent().retrieve("data"));
  250. }
  251. });
  252. return arr;
  253. },
  254. createTable: function () {
  255. this.tableDivNode = new Element("div.profile_common_tableDiv").inject(this.contentNode);
  256. this.tableNode = new Element("table", {
  257. "class": "table is-fullwidth is-hoverable emPowerTable",
  258. "style": "margin-bottom:0px",
  259. "width":"100%",
  260. "border":"0",
  261. "cellpadding":"0",
  262. "cellspacing":"0"
  263. }).inject(this.tableDivNode);
  264. this.createThead();
  265. this.createTbody();
  266. },
  267. createThead: function () {
  268. this.theadNode = new Element("thead").inject(this.tableNode);
  269. var trNode = new Element("tr.first").inject(this.theadNode);
  270. this.columns.each(function (column) {
  271. var thNode = new Element("th").inject(trNode);
  272. if (column.title) thNode.set("text", column.title);
  273. if (column.width) thNode.setStyle("width", column.width);
  274. if (column.type) {
  275. if (column.type === "checkbox") {
  276. var checkAllNode = new Element("input", {"type": "checkbox", "name": "mtSelectAll"}).inject(thNode);
  277. checkAllNode.addEvent("click", function () {
  278. $$('input[name="mtSelectItem"]').each(function (item) {
  279. if (checkAllNode.checked) {
  280. item.checked = 'checked';
  281. } else {
  282. item.checked = '';
  283. }
  284. });
  285. });
  286. }
  287. }
  288. }.bind(this));
  289. },
  290. createTbody: function () {
  291. if (this.tbodyNode) this.tbodyNode.remove();
  292. this.tbodyNode = new Element("tbody").inject(this.tableNode);
  293. this.fireEvent('beforeLoadData');
  294. this.dataList.each(function (data) {
  295. var trNode = new Element("tr").inject(this.tbodyNode);
  296. trNode.store("data", data);
  297. this.columns.each(function (column) {
  298. var thNode = new Element("td").inject(trNode);
  299. if(column.formatter){
  300. column.formatter.call(this, data, thNode);
  301. }else{
  302. thNode.set("text",data[column.field])
  303. }
  304. if (column.type) {
  305. if (column.type === "operation") {
  306. column.opButtonList.each(function (opButton) {
  307. var opButtonNode = new Element("a", {
  308. "class": "button is-link",
  309. "text": opButton.text
  310. }).inject(thNode);
  311. if (opButton.action) {
  312. opButtonNode.addEvent("click", function (ev) {
  313. opButton.action(data);
  314. }.bind(this));
  315. }
  316. }.bind(this));
  317. }
  318. if (column.type === "checkbox") {
  319. var checkItemNode = new Element("input", {
  320. "type": "checkbox",
  321. "name": "mtSelectItem"
  322. }).inject(thNode);
  323. checkItemNode.addEvent("click", function (event) {
  324. if (event.target.checked) {
  325. var i = 0;
  326. $$('input[name="mtSelectItem"]').each(function (chk) {
  327. if (!chk.checked) {
  328. i = i + 1;
  329. }
  330. });
  331. if (i === 0) {
  332. $$('input[name="mtSelectAll"]')[0].checked = 'checked';
  333. }
  334. } else {
  335. $$('input[name="mtSelectAll"]')[0].checked = '';
  336. }
  337. });
  338. }
  339. }
  340. }.bind(this));
  341. }.bind(this));
  342. }
  343. });