sp_push.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. const showSideTools = function (show) {
  2. const left = $('#left-view'), right = $('#right-view'), parent = left.parent();
  3. if (show) {
  4. right.show();
  5. autoFlashHeight();
  6. /**
  7. * right.show()后, parent被撑开成2倍left.height, 导致parent.width减少了10px
  8. * 第一次left.width调整后,parent的缩回left.height, 此时parent.width又增加了10px
  9. * 故需要通过最终的parent.width再计算一次left.width
  10. *
  11. * Q: 为什么不通过先计算left.width的宽度,以避免计算两次left.width?
  12. * A: 右侧工具栏不一定显示,当右侧工具栏显示过一次后,就必须使用parent和right来计算left.width
  13. *
  14. */
  15. //left.css('width', parent.width() - right.outerWidth());
  16. //left.css('width', parent.width() - right.outerWidth());
  17. const percent = 100 - right.outerWidth() /parent.width() * 100;
  18. left.css('width', percent + '%');
  19. } else {
  20. left.width(parent.width());
  21. right.hide();
  22. }
  23. };
  24. $(document).ready(() => {
  25. autoFlashHeight();
  26. let datepicker;
  27. class PushObj {
  28. constructor() {
  29. this.spread = SpreadJsObj.createNewSpread($('#push-spread')[0]);
  30. this.sheet = this.spread.getActiveSheet();
  31. this.data = [];
  32. this.spreadSetting = {
  33. cols: [
  34. { title: '序号', colSpan: '1', rowSpan: '1', field: 'push_order', hAlign: 1, width: 80, readOnly: true },
  35. {
  36. title: '日期', colSpan: '1', rowSpan: '1', field: 'push_date', hAlign: 2, width: 100, formatter: '@',
  37. formatter: 'yyyy-MM-dd', cellType: 'activeImageBtn', normalImg: '#ellipsis-icon', indent: 5
  38. },
  39. {title: '记录内容', colSpan: '1', rowSpan: '1', field: 'push_content', hAlign: 0, width: 450, formatter: '@', wordWrap: true},
  40. {title: '备注', colSpan: '1', rowSpan: '2', field: 'memo', hAlign: 0, width: 200, formatter: '@', cellType: 'ellipsisAutoTip'},
  41. ],
  42. emptyRows: 3,
  43. headRows: 1,
  44. headRowHeight: [32],
  45. defaultRowHeight: 21,
  46. headerFont: '12px 微软雅黑',
  47. font: '12px 微软雅黑',
  48. localCache: {
  49. key: 'sub-proj-push',
  50. colWidth: true,
  51. },
  52. forceLoadEmpty: true,
  53. imageClick: function (data, hitinfo) {
  54. const setting = hitinfo.sheet.zh_setting;
  55. if (!setting) return;
  56. const col = setting.cols[hitinfo.col];
  57. if (!col || col.field.indexOf('_date') < 0) return;
  58. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  59. if (!datepicker) {
  60. datepicker = $('.datepicker-here').datepicker({
  61. language: 'zh',
  62. dateFormat: 'yyyy-MM-dd',
  63. autoClose: true,
  64. onSelect: function (formattedDate, date, inst) {
  65. if (!inst.visible) return;
  66. const sels = hitinfo.sheet.getSelections();
  67. if (!sels || !sels[0]) return;
  68. const node = SpreadJsObj.getSelectObject(hitinfo.sheet);
  69. const relaCol = hitinfo.sheet.zh_setting.cols[sels[0].col];
  70. const updateData = {};
  71. if (node) {
  72. updateData.update = { id: node.id };
  73. updateData.update[relaCol.field] = formattedDate;
  74. } else {
  75. updateData.add = { push_order: hitinfo.sheet.zh_data.length + 1 };
  76. updateData.add[relaCol.field] = formattedDate;
  77. }
  78. postData('push/update', updateData, function (result) {
  79. pushObj.refreshData(result);
  80. SpreadJsObj.reLoadSheetData(pushObj.sheet);
  81. }, function () {
  82. SpreadJsObj.reLoadRowData(hitinfo.sheet, sels[0].row, 1);
  83. });
  84. }
  85. }).data('datepicker');
  86. }
  87. const value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  88. if (value) {
  89. datepicker.selectDate(value);
  90. } else {
  91. datepicker.clear();
  92. }
  93. datepicker.show();
  94. $('#datepickers-container').css('top', hitinfo.cellRect.y + pos.y).css('left', hitinfo.cellRect.x + pos.x);
  95. }
  96. };
  97. this.ckSpread = window.location.pathname + '-pushSelect';
  98. this.initSpread();
  99. this.initOtherEvent();
  100. }
  101. initSpread() {
  102. SpreadJsObj.initSheet(this.sheet, this.spreadSetting);
  103. this.spread.bind(spreadNS.Events.SelectionChanged, this.selectionChanged);
  104. this.spread.bind(spreadNS.Events.topRowChanged, this.topRowChanged);
  105. this.spread.bind(spreadNS.Events.ClipboardChanging, function (e, info) {
  106. const copyText = SpreadJsObj.getFilterCopyText(info.sheet);
  107. SpreadJsObj.Clipboard.setCopyData(copyText);
  108. });
  109. this.spread.bind(spreadNS.Events.EditEnded, this.editEnded);
  110. this.spread.bind(spreadNS.Events.ClipboardPasting, this.clipboardPasting);
  111. SpreadJsObj.addDeleteBind(this.spread, this.deletePress);
  112. }
  113. initOtherEvent() {
  114. const self = this;
  115. // 增删上下移升降级
  116. $('a[name="base-opr"]').click(function () {
  117. self.baseOpr(this.getAttribute('type'));
  118. });
  119. $.contextMenu({
  120. selector: '#push-spread',
  121. build: function ($trigger, e) {
  122. const target = SpreadJsObj.safeRightClickSelection($trigger, e, self.spread);
  123. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  124. },
  125. items: {
  126. add: {
  127. name: '插入',
  128. icon: 'fa-plus',
  129. callback: function (key, opt) {
  130. pushObj.insert();
  131. },
  132. },
  133. del: {
  134. name: '删除',
  135. icon: 'fa-remove',
  136. callback: function (key, opt) {
  137. pushObj.delete();
  138. },
  139. disabled: function (key, opt) {
  140. const node = SpreadJsObj.getSelectObject(pushObj.sheet);
  141. return !node;
  142. },
  143. },
  144. sprDel: '------------',
  145. upMove: {
  146. name: '上移',
  147. icon: 'fa-arrow-up',
  148. callback: function (key, opt) {
  149. pushObj.upMove();
  150. },
  151. disabled: function (key, opt) {
  152. const sels = pushObj.sheet.getSelections();
  153. if (!sels || !sels[0] || sels[0].row === 0) return true;
  154. },
  155. },
  156. downMove: {
  157. name: '下移',
  158. icon: 'fa-arrow-down',
  159. callback: function (key, opt) {
  160. pushObj.downMove();
  161. },
  162. disabled: function (key, opt) {
  163. const sels = pushObj.sheet.getSelections();
  164. if (!sels || !sels[0] || sels[0].row >= pushObj.data.length - 1) return true;
  165. },
  166. }
  167. },
  168. })
  169. }
  170. loadRelaData() {
  171. SpreadJsObj.saveTopAndSelect(this.sheet, this.ckSpread);
  172. pushFile.getCurAttHtml(SpreadJsObj.getSelectObject(this.sheet));
  173. }
  174. refreshData(data) {
  175. if (data.add) {
  176. for (const a of data.add) {
  177. this.data.push(a);
  178. }
  179. }
  180. if (data.update) {
  181. for (const u of data.update) {
  182. const d = this.data.find(function (x) {
  183. return u.id === x.id;
  184. });
  185. if (d) {
  186. _.assign(d, u);
  187. } else {
  188. this.data.push(d);
  189. }
  190. }
  191. }
  192. if (data.del) {
  193. _.remove(this.data, function (d) {
  194. return data.del.indexOf(d.id) >= 0;
  195. });
  196. }
  197. this.resortData();
  198. }
  199. resortData() {
  200. this.data.sort(function (a, b) {
  201. return a.push_order - b.push_order;
  202. });
  203. }
  204. loadData(datas) {
  205. this.data.length = 0;
  206. this.data.push(...datas);
  207. this.resortData();
  208. SpreadJsObj.loadSheetData(this.sheet, SpreadJsObj.DataType.Data, this.data);
  209. SpreadJsObj.loadTopAndSelect(this.sheet, this.ckSpread);
  210. }
  211. // 事件
  212. selectionChanged(e, info) {
  213. if (info.newSelections) {
  214. if (!info.oldSelections || info.newSelections[0].row !== info.oldSelections[0].row) {
  215. pushObj.loadRelaData();
  216. }
  217. }
  218. }
  219. topRowChanged(e, info) {
  220. SpreadJsObj.saveTopAndSelect(info.sheet, pushObj.ckBillsSpread);
  221. }
  222. editEnded(e, info) {
  223. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  224. const node = info.sheet.zh_data[info.row];
  225. const col = info.sheet.zh_setting.cols[info.col];
  226. const data = {};
  227. if (node) {
  228. data.update = {};
  229. data.update.id = node.id;
  230. const oldValue = node ? node[col.field] : null;
  231. const newValue = col.wordWrap ? info.editingText : trimInvalidChar(info.editingText);
  232. if (oldValue == info.editingText || ((!oldValue || oldValue === '') && (newValue === ''))) {
  233. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  234. return;
  235. }
  236. data.update[col.field] = newValue;
  237. } else {
  238. data.add = {};
  239. data.add.push_order = info.sheet.zh_data.length + 1;
  240. data.add[col.field] = col.wordWrap ? info.editingText : trimInvalidChar(info.editingText);
  241. }
  242. // 更新至服务器
  243. postData('push/update', data, function (result) {
  244. pushObj.refreshData(result);
  245. SpreadJsObj.reLoadSheetData(pushObj.sheet);
  246. if (data.add) pushObj.loadRelaData();
  247. }, function () {
  248. SpreadJsObj.reLoadRowData(info.sheet, info.row, 1);
  249. });
  250. }
  251. deletePress (sheet) {
  252. if (!sheet.zh_setting) return;
  253. const sel = sheet.getSelections()[0], datas = [];
  254. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  255. let bDel = false;
  256. const node = sheet.zh_tree.nodes[iRow];
  257. const data = sheet.zh_tree.getNodeKeyData(node);
  258. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  259. const col = sheet.zh_setting.cols[iCol];
  260. const style = sheet.getStyle(iRow, iCol);
  261. if (style.locked) continue;
  262. data[col.field] = col.type === 'Number' ? 0 : '';
  263. bDel = true;
  264. }
  265. if (bDel) datas.push(data);
  266. }
  267. if (datas.length > 0) {
  268. postData('push/update', {update: datas}, function (result) {
  269. pushObj.refreshData(result);
  270. SpreadJsObj.reLoadSheetData(pushObj.sheet);
  271. }, function () {
  272. SpreadJsObj.reLoadRowData(info.sheet, sel.row, sel.rowCount);
  273. });
  274. }
  275. }
  276. clipboardPasting(e, info) {
  277. info.cancel = true;
  278. const setting = info.sheet.zh_setting, sortData = info.sheet.zh_data;
  279. if (!setting || !sortData) return;
  280. const pasteData = info.pasteData.html
  281. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  282. : (info.pasteData.text === ''
  283. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  284. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  285. const uDatas = [], iDatas = [];
  286. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  287. const curRow = info.cellRange.row + iRow;
  288. const node = sortData[curRow];
  289. let bPaste = false;
  290. const data = {};
  291. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  292. const curCol = info.cellRange.col + iCol;
  293. const colSetting = setting.cols[curCol];
  294. const value = colSetting.wordWrap ? pasteData[iRow][iCol] : trimInvalidChar(pasteData[iRow][iCol]);
  295. if (colSetting.type === 'Number') {
  296. const num = _.toNumber(value);
  297. if (num) {
  298. data[colSetting.field] = num;
  299. bPaste = true;
  300. }
  301. } else {
  302. data[colSetting.field] = value || '';
  303. bPaste = true;
  304. }
  305. }
  306. if (bPaste) {
  307. if (node) {
  308. data.id = node.id;
  309. uDatas.push(data);
  310. } else {
  311. data.push_order = curRow + 1;
  312. iDatas.push(data);
  313. }
  314. }
  315. }
  316. const updateData = {};
  317. if (uDatas.length > 0) updateData.update = uDatas;
  318. if (iDatas.length > 0) updateData.add = iDatas;
  319. if (uDatas.length > 0 || iDatas.length > 0) {
  320. postData('push/update', updateData, function (result) {
  321. pushObj.refreshData(result);
  322. SpreadJsObj.reLoadSheetData(info.sheet);
  323. if (updateData.add && !updateData.update) pushObj.loadRelaData();
  324. });
  325. } else {
  326. SpreadJsObj.reLoadSheetData(info.sheet);
  327. }
  328. }
  329. // 操作
  330. insert () {
  331. const sheet = this.sheet;
  332. const node = SpreadJsObj.getSelectObject(sheet);
  333. const push_order = node ? node.push_order : sheet.getRowCount() - 2;
  334. const add = { push_order };
  335. // 更新至服务器
  336. postData('push/update', { add }, function (result) {
  337. pushObj.refreshData(result);
  338. SpreadJsObj.reLoadSheetData(pushObj.sheet);
  339. }, function () {
  340. SpreadJsObj.reLoadRowData(info.sheet, info.row, 1);
  341. });
  342. }
  343. delete () {
  344. const sortData = this.sheet.zh_data;
  345. const datas = [];
  346. const sels = this.sheet.getSelections();
  347. if (!sels || !sels[0]) return;
  348. for (let iRow = sels[0].row, iLen = sels[0].row + sels[0].rowCount; iRow < iLen; iRow++) {
  349. const node = sortData[iRow];
  350. datas.push(node.id);
  351. }
  352. if (datas.length > 0) {
  353. postData('push/update', {del: datas}, function (result) {
  354. pushObj.refreshData(result);
  355. SpreadJsObj.reLoadSheetData(pushObj.sheet);
  356. pushFile.deleteFileByNodeId(result.del);
  357. pushObj.loadRelaData();
  358. }, function () {
  359. SpreadJsObj.reLoadSheetData(pushObj.sheet);
  360. });
  361. }
  362. }
  363. upMove () {
  364. const sels = this.sheet.getSelections(), sortData = this.sheet.zh_data;
  365. const node = sortData[sels[0].row];
  366. const preNode = sortData[sels[0].row - 1];
  367. const data = [
  368. {id: node.id, push_order: preNode.push_order},
  369. {id: preNode.id, push_order: node.push_order}
  370. ];
  371. postData('push/update', {update: data}, function (result) {
  372. pushObj.refreshData(result);
  373. SpreadJsObj.reLoadRowsData(pushObj.sheet, [sels[0].row, sels[0].row - 1]);
  374. pushObj.sheet.setSelection(sels[0].row - 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  375. });
  376. }
  377. downMove () {
  378. const sels = this.sheet.getSelections(), sortData = this.sheet.zh_data;
  379. const node = sortData[sels[0].row];
  380. const nextNode = sortData[sels[0].row + 1];
  381. const data = [
  382. {id: node.id, push_order: nextNode.push_order},
  383. {id: nextNode.id, push_order: node.push_order}
  384. ];
  385. postData('push/update', {update: data}, function (result) {
  386. pushObj.refreshData(result);
  387. SpreadJsObj.reLoadRowsData(pushObj.sheet, [sels[0].row, sels[0].row + 1]);
  388. pushObj.sheet.setSelection(sels[0].row + 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  389. });
  390. }
  391. }
  392. const pushObj = new PushObj();
  393. const pushFile = $.ledger_att({
  394. selector: '#fujian',
  395. key: 'id',
  396. masterKey: 'rela_id',
  397. uploadUrl: 'push/file/upload',
  398. deleteUrl: 'push/file/delete',
  399. checked: false,
  400. zipName: `推进记录-附件.zip`,
  401. readOnly: false,
  402. fileIdType: 'string',
  403. fileInfo: {
  404. user_name: 'user_name',
  405. user_id: 'user_id',
  406. create_time: 'create_time',
  407. },
  408. getCurHint: function(node) { return ''; },
  409. locate: function (att) {
  410. if (!att) return;
  411. SpreadJsObj.locateDataBy(pushObj.sheet, function(x) { return x.id === att.node.id; });
  412. pushFile.getCurAttHtml(att.node);
  413. }
  414. });
  415. // 展开收起标准清单
  416. $('a', '#side-menu').bind('click', function (e) {
  417. e.preventDefault();
  418. const tab = $(this), tabPanel = $(tab.attr('content'));
  419. // 展开工具栏、切换标签
  420. if (!tab.hasClass('active')) {
  421. // const close = $('.active', '#side-menu').length === 0;
  422. $('a', '#side-menu').removeClass('active');
  423. $('.tab-content .tab-select-show.tab-pane.active').removeClass('active');
  424. tab.addClass('active');
  425. tabPanel.addClass('active');
  426. // $('.tab-content .tab-pane').removeClass('active');
  427. showSideTools(tab.hasClass('active'));
  428. } else { // 收起工具栏
  429. tab.removeClass('active');
  430. tabPanel.removeClass('active');
  431. showSideTools(tab.hasClass('active'));
  432. }
  433. pushObj.spread.refresh();
  434. });
  435. postData('load', { filter: 'push;push_file'}, function(result) {
  436. pushObj.loadData(result.push);
  437. for (const f of result.push_file) {
  438. f.node = pushObj.data.find(x => { return x.id === f.rela_id; });
  439. }
  440. pushFile.loadDatas(result.push_file);
  441. pushFile.getCurAttHtml(SpreadJsObj.getSelectObject(pushObj.sheet));
  442. });
  443. // 工具栏spr
  444. $.divResizer({
  445. select: '#right-spr',
  446. callback: function () {
  447. pushObj.spread.refresh();
  448. }
  449. });
  450. // 导航Menu
  451. $.subMenu({
  452. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  453. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  454. key: 'menu.1.0.0',
  455. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  456. callback: function (info) {
  457. if (info.mini) {
  458. $('.panel-title').addClass('fluid');
  459. $('#sub-menu').removeClass('panel-sidebar');
  460. } else {
  461. $('.panel-title').removeClass('fluid');
  462. $('#sub-menu').addClass('panel-sidebar');
  463. }
  464. autoFlashHeight();
  465. pushObj.spread.refresh();
  466. }
  467. });
  468. });