se_other.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. 'use strict';
  2. /**
  3. * 期 - 其他
  4. *
  5. * @author Mai
  6. * @date 2020/2/12
  7. * @version
  8. */
  9. $(document).ready(() => {
  10. autoFlashHeight();
  11. let datepicker;
  12. const otherSpreadSetting = {
  13. cols: [
  14. {title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 235, formatter: '@'},
  15. {title: '金额', colSpan: '1', rowSpan: '1', field: 'total_price', hAlign: 2, width: 100, type: 'Number'},
  16. {title: '本期金额', colSpan: '1', rowSpan: '1', field: 'tp', hAlign: 2, width: 100, type: 'Number'},
  17. {title: '截止本期金额', colSpan: '1', rowSpan: '1', field: 'end_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true},
  18. {
  19. title: '时间', colSpan: '1', rowSpan: '1', field: 'real_time', hAlign: 2, width: 120, readOnly: true,
  20. formatter: 'yyyy-MM-dd', cellType: 'activeImageBtn', normalImg: '#ellipsis-icon', indent: 5,
  21. },
  22. {title: '备注', colSpan: '1', rowSpan: '1', field: 'memo', hAlign: 0, width: 180, formatter: '@', cellType: 'ellipsisAutoTip'}
  23. ],
  24. emptyRows: 3,
  25. headRows: 1,
  26. headRowHeight: [32],
  27. defaultRowHeight: 21,
  28. headerFont: '12px 微软雅黑',
  29. font: '12px 微软雅黑',
  30. getColor: function (sheet, data, row, col, defaultColor) {
  31. if (data) {
  32. if (data.total_price) {
  33. return data.total_price >= 0
  34. ? data.end_tp > data.total_price ? '#f8d7da' : defaultColor
  35. : data.end_tp < data.total_price ? '#f8d7da' : defaultColor;
  36. } else {
  37. return defaultColor;
  38. }
  39. } else {
  40. return defaultColor;
  41. }
  42. },
  43. imageClick: function (data, hitinfo) {
  44. if (!data) return;
  45. const setting = hitinfo.sheet.zh_setting;
  46. if (!setting) return;
  47. const col = setting.cols[hitinfo.col];
  48. if (!col || col.field !== 'real_time') return;
  49. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  50. if (!datepicker) {
  51. datepicker = $('.datepicker-here').datepicker({
  52. language: 'zh',
  53. dateFormat: 'yyyy-MM-dd',
  54. autoClose: true,
  55. onSelect: function (formattedDate, date, inst) {
  56. if (!inst.visible) return;
  57. const sels = hitinfo.sheet.getSelections();
  58. if (!sels || !sels[0]) return;
  59. const node = SpreadJsObj.getSelectObject(hitinfo.sheet);
  60. const uData = { update: {id: node.id, real_time: date} };
  61. postData(window.location.pathname + '/update', uData, function (result) {
  62. seOtherObj.loadUpdateData(result);
  63. SpreadJsObj.reLoadRowData(hitinfo.sheet, sels[0].row);
  64. }, function () {
  65. SpreadJsObj.reLoadRowData(hitinfo.sheet, sels[0].row);
  66. });
  67. }
  68. }).data('datepicker');
  69. }
  70. const value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  71. if (value) {
  72. datepicker.selectDate(value);
  73. } else {
  74. datepicker.clear();
  75. }
  76. datepicker.show();
  77. $('#datepickers-container').css('top', hitinfo.cellRect.y + pos.y).css('left', hitinfo.cellRect.x + pos.x);
  78. }
  79. };
  80. const otherSpread = SpreadJsObj.createNewSpread($('#other-spread')[0]);
  81. const otherSheet = otherSpread.getActiveSheet();
  82. SpreadJsObj.initSheet(otherSheet, otherSpreadSetting);
  83. $.subMenu({
  84. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  85. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  86. key: 'menu.1.0.0',
  87. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  88. callback: function (info) {
  89. if (info.mini) {
  90. $('.panel-title').addClass('fluid');
  91. $('#sub-menu').removeClass('panel-sidebar');
  92. } else {
  93. $('.panel-title').removeClass('fluid');
  94. $('#sub-menu').addClass('panel-sidebar');
  95. }
  96. autoFlashHeight();
  97. otherSpread.refresh();
  98. }
  99. });
  100. class SeOther {
  101. constructor () {
  102. this.data = [];
  103. }
  104. resortData() {
  105. this.data.sort(function (a, b) {
  106. return a.order - b.order;
  107. });
  108. }
  109. calculateAll() {
  110. for (const d of this.data) {
  111. d.end_tp = ZhCalc.add(d.pre_tp, d.tp);
  112. }
  113. }
  114. loadDatas(datas) {
  115. this.data = datas;
  116. this.calculateAll();
  117. this.resortData();
  118. }
  119. loadUpdateData(updateData) {
  120. if (updateData.add) {
  121. for (const a of updateData.add) {
  122. this.data.push(a);
  123. }
  124. }
  125. if (updateData.update) {
  126. for (const u of updateData.update) {
  127. const d = this.data.find(function (x) {
  128. return u.id === x.id;
  129. });
  130. if (d) {
  131. _.assign(d, u);
  132. } else {
  133. this.data.push(d);
  134. }
  135. }
  136. }
  137. if (updateData.del) {
  138. _.remove(this.data, function (d) {
  139. return updateData.del.indexOf(d.id) >= 0;
  140. });
  141. }
  142. this.calculateAll();
  143. this.resortData();
  144. }
  145. }
  146. const seOtherObj = new SeOther();
  147. postData(window.location.pathname + '/load', null, function (result) {
  148. seOtherObj.loadDatas(result);
  149. SpreadJsObj.loadSheetData(otherSheet, SpreadJsObj.DataType.Data, seOtherObj.data);
  150. });
  151. if (!readOnly) {
  152. const seOtherOprObj = {
  153. /**
  154. * 删除按钮响应事件
  155. * @param sheet
  156. */
  157. deletePress: function (sheet) {
  158. if (!sheet.zh_setting || readOnly) return;
  159. const sortData = sheet.zh_data;
  160. const datas = [];
  161. const sels = sheet.getSelections();
  162. if (!sels || !sels[0]) return;
  163. for (let iRow = sels[0].row; iRow < sels[0].row + sels[0].rowCount; iRow++) {
  164. let bDel = false;
  165. const node = sortData[iRow];
  166. if (node) {
  167. const data = {id: node.id};
  168. for (let iCol = sels[0].col; iCol < sels[0].col + sels[0].colCount; iCol++) {
  169. const colSetting = sheet.zh_setting.cols[iCol];
  170. if (colSetting.field === 'name') {
  171. toastr.error('名称不能为空,如需删除请使用右键删除');
  172. return;
  173. }
  174. const style = sheet.getStyle(iRow, iCol);
  175. if (!style.locked) {
  176. const colSetting = sheet.zh_setting.cols[iCol];
  177. data[colSetting.field] = null;
  178. bDel = true;
  179. }
  180. }
  181. if (bDel) {
  182. datas.push(data);
  183. }
  184. }
  185. }
  186. if (datas.length > 0) {
  187. postData(window.location.pathname + '/update', {updateType: 'update', updateData: datas}, function (result) {
  188. seOtherObj.loadUpdateData(result);
  189. SpreadJsObj.reLoadSheetData(otherSheet);
  190. }, function () {
  191. SpreadJsObj.reLoadSheetData(otherSheet);
  192. });
  193. }
  194. },
  195. delete: function (sheet) {
  196. if (!sheet.zh_setting || readOnly) return;
  197. const sortData = sheet.zh_data;
  198. const datas = [];
  199. const sels = sheet.getSelections();
  200. if (!sels || !sels[0]) return;
  201. const hint = {
  202. isOld: {type: 'warning', msg: '该数据已计量,不可删除'},
  203. invalidDel: {type: 'warning', msg: '该数据不是您新增的,只有原报和新增人可删除'},
  204. };
  205. for (let iRow = sels[0].row, iLen = sels[0].row + sels[0].rowCount; iRow < iLen; iRow++) {
  206. const node = sortData[iRow];
  207. if (node.pre_used) {
  208. toastMessageUniq(hint.isOld);
  209. continue;
  210. } else {
  211. if (node.add_uid !== userID && stageUserId !== userID) {
  212. toastMessageUniq(hint.invalidDel);
  213. continue;
  214. }
  215. datas.push(node.id);
  216. }
  217. }
  218. if (datas.length > 0) {
  219. postData(window.location.pathname + '/update', {del: datas}, function (result) {
  220. seOtherObj.loadUpdateData(result);
  221. SpreadJsObj.reLoadSheetData(otherSheet);
  222. }, function () {
  223. SpreadJsObj.reLoadSheetData(otherSheet);
  224. });
  225. }
  226. },
  227. editEnded: function (e, info) {
  228. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  229. const node = info.sheet.zh_data[info.row];
  230. const col = info.sheet.zh_setting.cols[info.col];
  231. const data = {};
  232. if (node) {
  233. data.update = {};
  234. data.update.id = node.id;
  235. const oldValue = node ? node[col.field] : null;
  236. const newValue = trimInvalidChar(info.editingText);
  237. if (oldValue == info.editingText || ((!oldValue || oldValue === '') && (newValue === ''))) {
  238. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  239. return;
  240. }
  241. data.update[col.field] = newValue;
  242. } else {
  243. if (col.field !== 'name') {
  244. toastr.warning('请先输入名称');
  245. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  246. return;
  247. }
  248. data.add = {};
  249. data.add.order = info.row + 1;
  250. data.add.name = trimInvalidChar(info.editingText);
  251. }
  252. postData(window.location.pathname + '/update', data, function (result) {
  253. seOtherObj.loadUpdateData(result);
  254. SpreadJsObj.reLoadSheetData(info.sheet);
  255. }, function () {
  256. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  257. });
  258. },
  259. editStarting(e, info) {
  260. if (!info.sheet.zh_setting || !info.sheet.zh_data) {
  261. info.cancel = true;
  262. return;
  263. }
  264. const col = info.sheet.zh_setting.cols[info.col];
  265. const node = info.sheet.zh_data[info.row];
  266. if (!node) return;
  267. switch (col.field) {
  268. case 'name':
  269. info.cancel = readOnly || node.pre_used;
  270. break;
  271. }
  272. },
  273. clipboardPasting(e, info) {
  274. const setting = info.sheet.zh_setting, sortData = info.sheet.zh_data;
  275. info.cancel = true;
  276. if (!setting || !sortData) return;
  277. const pasteData = info.pasteData.html
  278. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  279. : (info.pasteData.text === ''
  280. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  281. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  282. const hint = {
  283. name: {type: 'warning', msg: '名称不可为空,已过滤'},
  284. tp: {type: 'warning', msg: '输入的 金额 非法,已过滤'},
  285. };
  286. const uDatas = [], iDatas = [];
  287. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  288. const curRow = info.cellRange.row + iRow;
  289. const node = sortData[curRow];
  290. let bPaste = false;
  291. const data = {};
  292. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  293. const curCol = info.cellRange.col + iCol;
  294. const colSetting = setting.cols[curCol];
  295. const value = trimInvalidChar(pasteData[iRow][iCol]);
  296. if (colSetting.field === 'name' && (!value || value === '')) {
  297. toastMessageUniq(hint.name);
  298. break;
  299. }
  300. if (colSetting.type === 'Number') {
  301. const num = _.toNumber(value);
  302. if (num) {
  303. data[colSetting.field] = num;
  304. bPaste = true;
  305. }
  306. } else {
  307. data[colSetting.field] = value;
  308. bPaste = true;
  309. }
  310. }
  311. if (bPaste) {
  312. if (node) {
  313. data.id = node.id;
  314. uDatas.push(data);
  315. } else {
  316. data.order = curRow + 1;
  317. iDatas.push(data);
  318. }
  319. }
  320. }
  321. const updateData = {};
  322. if (uDatas.length > 0) updateData.update = uDatas;
  323. if (iDatas.length > 0) updateData.add = iDatas;
  324. if (uDatas.length > 0 || iDatas.length > 0) {
  325. postData(window.location.pathname + '/update', updateData, function (result) {
  326. seOtherObj.loadUpdateData(result);
  327. SpreadJsObj.reLoadSheetData(info.sheet);
  328. });
  329. } else {
  330. SpreadJsObj.reLoadSheetData(info.sheet);
  331. }
  332. },
  333. upMove: function () {
  334. const sels = otherSheet.getSelections(), sortData = otherSheet.zh_data;
  335. const node = sortData[sels[0].row];
  336. const preNode = sortData[sels[0].row - 1];
  337. const data = [
  338. {id: node.id, order: preNode.order},
  339. {id: preNode.id, order: node.order}
  340. ];
  341. postData(window.location.pathname + '/update', {update: data}, function (result) {
  342. seOtherObj.loadUpdateData(result);
  343. SpreadJsObj.reLoadRowsData(otherSheet, [sels[0].row, sels[0].row - 1]);
  344. otherSheet.setSelection(sels[0].row - 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  345. });
  346. },
  347. downMove: function () {
  348. const sels = otherSheet.getSelections(), sortData = otherSheet.zh_data;
  349. const node = sortData[sels[0].row];
  350. const nextNode = sortData[sels[0].row + 1];
  351. const data = [
  352. {id: node.id, order: nextNode.order},
  353. {id: nextNode.id, order: node.order}
  354. ];
  355. postData(window.location.pathname + '/update', {update: data}, function (result) {
  356. seOtherObj.loadUpdateData(result);
  357. SpreadJsObj.reLoadRowsData(otherSheet, [sels[0].row, sels[0].row + 1]);
  358. otherSheet.setSelection(sels[0].row + 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  359. });
  360. }
  361. };
  362. otherSheet.bind(spreadNS.Events.EditEnded, seOtherOprObj.editEnded);
  363. otherSheet.bind(spreadNS.Events.EditStarting, seOtherOprObj.editStarting);
  364. otherSheet.bind(spreadNS.Events.ClipboardPasting, seOtherOprObj.clipboardPasting);
  365. SpreadJsObj.addDeleteBind(otherSpread, seOtherOprObj.deletePress);
  366. $.contextMenu({
  367. selector: '#other-spread',
  368. build: function ($trigger, e) {
  369. const target = SpreadJsObj.safeRightClickSelection($trigger, e, otherSpread);
  370. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  371. },
  372. items: {
  373. del: {
  374. name: '删除',
  375. icon: 'fa-remove',
  376. callback: function (key, opt) {
  377. seOtherOprObj.delete(otherSheet);
  378. },
  379. disabled: function (key, opt) {
  380. const sels = otherSheet.getSelections();
  381. if (!sels || !sels[0]) return true;
  382. const row = sels[0].row;
  383. const node = seOtherObj.data[row];
  384. return node === undefined || node === null;
  385. },
  386. visible: function (key, opt) {
  387. return !readOnly;
  388. }
  389. },
  390. sprDel: '------------',
  391. upMove: {
  392. name: '上移',
  393. icon: 'fa-arrow-up',
  394. callback: function (key, opt) {
  395. seOtherOprObj.upMove();
  396. },
  397. disabled: function (key, opt) {
  398. const sels = otherSheet.getSelections();
  399. if (!sels || !sels[0] || sels[0].row === 0) return true;
  400. const row = sels[0].row;
  401. const node = seOtherObj.data[row];
  402. return node === undefined || node === null;
  403. },
  404. visible: function (key, opt) {
  405. return !readOnly;
  406. }
  407. },
  408. downMove: {
  409. name: '下移',
  410. icon: 'fa-arrow-down',
  411. callback: function (key, opt) {
  412. seOtherOprObj.downMove();
  413. },
  414. disabled: function (key, opt) {
  415. const sels = otherSheet.getSelections();
  416. if (!sels || !sels[0] || sels[0].row >= seOtherObj.data.length - 1) return true;
  417. const row = sels[0].row;
  418. const node = seOtherObj.data[row];
  419. return node === undefined || node === null;
  420. },
  421. visible: function (key, opt) {
  422. return !readOnly;
  423. }
  424. }
  425. },
  426. })
  427. }
  428. });