sp_push.js 21 KB

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