section_tree.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /**
  2. * Created by Zhong on 2017/12/18.
  3. */
  4. let pageOprObj = {
  5. rationLibName : null,
  6. rationLibId : null,
  7. initPage : function() {
  8. var me = this, rationLibId = getQueryString("repository"),//获取定额库参数
  9. rationLibName = storageUtil.getSessionCache("RationGrp","repositoryID_" + rationLibId);
  10. if (rationLibName) {
  11. var html = $("#rationname")[0].outerHTML;
  12. html = html.replace("XXX定额库", rationLibName);
  13. $("#rationname")[0].outerHTML = html;
  14. me.rationLibName = rationLibName;
  15. me.rationLibId = rationLibId;
  16. sectionTreeObj.getSectionTree(rationLibId);
  17. //job
  18. jobContentOprObj.radiosChange(jobContentOprObj.radios, jobContentOprObj.tableAll, jobContentOprObj.tablePartial);
  19. $('#addConBtn').click(jobContentOprObj.bindAddConBtn());
  20. $('#updateConBtn').click(jobContentOprObj.bindUpdateConBtn());
  21. jobContentOprObj.bindAllEvents($('#txtareaAll'));
  22. //fz
  23. annotationOprObj.radiosChange(annotationOprObj.radios, annotationOprObj.fzTableAll, annotationOprObj.fzTablePartial);
  24. $('#fzAddConBtn').click(annotationOprObj.bindAddConBtn());
  25. $('#fzUpdateConBtn').click(annotationOprObj.bindUpdateConBtn());
  26. annotationOprObj.bindAllEvents($('#fzTxtareaAll'));
  27. }
  28. }
  29. }
  30. let sectionTreeObj = {
  31. cache: null,//ref to tree.items
  32. tree: null,
  33. controller: null,
  34. workBook: null,
  35. sheet: null,
  36. updateType: {new: 'new', update: 'update'},
  37. insertBtn: $('#tree_Insert'),
  38. removeBtn: $('#tree_remove'),
  39. upLevelBtn: $('#tree_upLevel'),
  40. downLevelBtn: $('#tree_downLevel'),
  41. downMoveBtn: $('#tree_downMove'),
  42. upMoveBtn: $('#tree_upMove'),
  43. setting: {
  44. sheet: {
  45. cols:[
  46. {
  47. head: {
  48. titleNames: ['名称'],
  49. spanCols: [1],
  50. spanRows: [2],
  51. vAlign: [1, 1],
  52. hAlign: [1, 1],
  53. font: 'Arial'
  54. },
  55. data: {
  56. field: 'name',
  57. vAlign: 1,
  58. hAlign: 0,
  59. font: 'Arial'
  60. },
  61. width: 400
  62. }
  63. ],
  64. headRows: 1,
  65. headRowHeight: [47],
  66. emptyRows: 0,
  67. treeCol: 0
  68. },
  69. tree: {
  70. id: 'ID',
  71. pid: 'ParentID',
  72. nid: 'NextSiblingID',
  73. rootId: -1
  74. },
  75. options: {
  76. tabStripVisible: false,
  77. allowCopyPasteExcelStyle : false,
  78. allowExtendPasteRange: false,
  79. allowUserDragDrop : false,
  80. allowUserDragFill: false,
  81. scrollbarMaxAlign : true
  82. }
  83. },
  84. isDef: function (v) {
  85. return v !== undefined && v !== null;
  86. },
  87. isFunc: function (v) {
  88. return this.isDef(v) && typeof v === 'function';
  89. },
  90. //sheet things
  91. setOptions: function (workbook, opts) {
  92. for(let opt in opts){
  93. workbook.options[opt] = opts[opt];
  94. }
  95. },
  96. renderFunc: function (sheet, func) {
  97. sheet.suspendPaint();
  98. sheet.suspendEvent();
  99. if(this.isFunc(func)){
  100. func();
  101. }
  102. sheet.resumePaint();
  103. sheet.resumeEvent();
  104. },
  105. buildSheet: function () {
  106. if(!this.isDef(this.workBook)){
  107. this.workBook = new GC.Spread.Sheets.Workbook($('#sectionSpread')[0], {sheetCount: 1});
  108. this.sheet = this.workBook.getActiveSheet();
  109. this.setOptions(this.workBook, this.setting.options);
  110. this.sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  111. this.bindEvents(this.sheet);
  112. }
  113. },
  114. bindEvents: function (sheet) {
  115. let me = sectionTreeObj;
  116. const Events = GC.Spread.Sheets.Events;
  117. sheet.bind(Events.SelectionChanged, me.onSelectionChanged);
  118. sheet.bind(Events.EditEnded, me.onEditEnded);
  119. sheet.bind(Events.ClipboardPasted, me.onClipboardPasted);
  120. },
  121. onSelectionChanged: function (sender, info) {
  122. let me = sectionTreeObj;
  123. if(info.oldSelections.length === 0 && info.newSelections.length > 0 || info.oldSelections[0].row !== info.newSelections[0].row){
  124. let row = info.newSelections[0].row;
  125. let section = me.cache[row];
  126. me.initSelection(section);
  127. }
  128. else {
  129. me.refreshBtn(null);
  130. }
  131. },
  132. onEditEnded: function (sender, args) {
  133. let me = sectionTreeObj;
  134. let postData = [];
  135. let v = me.isDef(args.editingText) ? args.editingText.toString().trim() : '';
  136. let node = me.cache[args.row];
  137. if(me.isDef(node) && node.data.name !== v){
  138. let updateObj = me.getUpdateObj(me.updateType.update, node.getID(), null, null, v, null);
  139. postData.push(updateObj);
  140. //ajax
  141. //update
  142. me.sectionTreeAjax(postData, function (rstData) {
  143. node.data.name = v;
  144. }, function () {
  145. args.sheet.setValue(args.row, args.col, node.data.name ? node.data.name : '');
  146. });
  147. }
  148. },
  149. onClipboardPasted: function (sender, info) {
  150. let me = sectionTreeObj;
  151. let items = sheetCommonObj.analyzePasteData({header: [{dataCode: 'name'}]}, info);
  152. let postData = [];
  153. let frontData = [];
  154. for(let i = 0, len = items.length; i < len; i++){
  155. let row = info.cellRange.row + i;
  156. let node = me.cache[row];
  157. if(me.isDef(node) && me.isDef(items[i].name) && node.data.name !== items[i].name){
  158. let updateObj = me.getUpdateObj(me.updateType.update, node.getID(), null, null, items[i].name, null);
  159. postData.push(updateObj);
  160. frontData.push({row: row, name: items[i].name});
  161. node.data.name = items[i].name;
  162. }
  163. }
  164. if(postData.length > 0){
  165. //ajax
  166. me.sectionTreeAjax(postData, function (rstData) {
  167. for(let i = 0, len = frontData.length; i < len; i++){
  168. let node = me.cache[frontData[i]['row']];
  169. if(me.isDef(node)){
  170. node.data.name = frontData[i]['name'];
  171. }
  172. }
  173. }, function () {
  174. for(let i = 0, len = frontData.length; i < len; i++){
  175. let node = me.cache[frontData[i]['row']];
  176. me.sheet.setValue(frontData[i]['row'], 0, me.isDef(node) ? node.data.name : '');
  177. }
  178. });
  179. }
  180. },
  181. getSectionTree: function (repId) {
  182. let me = sectionTreeObj;
  183. let url = 'api/getRationTree';
  184. let postData = {rationLibId: repId};
  185. let sucFunc = function (rstData) {
  186. if(rstData.length > 0){
  187. storageUtil.setSessionCache("RationGrp","repositoryID",rstData[0].rationRepId);
  188. }
  189. //init
  190. me.buildSheet();
  191. me.initTree(rstData);
  192. me.cache = me.tree.items;
  193. me.bindBtn();
  194. me.initController(me.tree, me.sheet, me.setting.sheet);
  195. me.controller.showTreeData();
  196. me.sheet.setFormatter(-1, 0, '@');
  197. me.initSelection(me.tree.selected);
  198. explanatoryOprObj.bindEvents($('#explanationShow'), $('#ruleTextShow'));
  199. };
  200. let errFunc = function () {
  201. };
  202. CommonAjax.post(url, postData, sucFunc, errFunc);
  203. },
  204. initTree: function (datas) {
  205. this.tree = idTree.createNew(this.setting.tree);
  206. this.tree.loadDatas(datas);
  207. this.tree.selected = this.tree.items.length > 0 ? this.tree.items[0] : null;
  208. },
  209. initController: function (tree, sheet, setting) {
  210. this.controller = TREE_SHEET_CONTROLLER.createNew(tree, sheet, setting);
  211. },
  212. refreshBtn: function (selected) {
  213. let me = this;
  214. me.insertBtn.removeClass('disabled');
  215. me.removeBtn.removeClass('disabled');
  216. me.upLevelBtn.removeClass('disabled');
  217. me.downLevelBtn.removeClass('disabled');
  218. me.downMoveBtn.removeClass('disabled');
  219. me.upMoveBtn.removeClass('disabled');
  220. if(!me.isDef(selected)){
  221. me.removeBtn.addClass('disabled');
  222. me.upLevelBtn.addClass('disabled');
  223. me.downLevelBtn.addClass('disabled');
  224. me.downMoveBtn.addClass('disabled');
  225. me.upMoveBtn.addClass('disabled');
  226. }
  227. else {
  228. if(!me.isDef(selected.preSibling)){
  229. me.downLevelBtn.addClass('disabled');
  230. me.upMoveBtn.addClass('disabled');
  231. }
  232. if(!me.isDef(selected.nextSibling)){
  233. me.downMoveBtn.addClass('disabled');
  234. }
  235. if(!me.isDef(selected.parent)){
  236. me.upLevelBtn.addClass('disabled');
  237. }
  238. }
  239. },
  240. bindBtn: function () {
  241. let me = this;
  242. me.insertBtn.click(function () {
  243. me.insert();
  244. });
  245. me.removeBtn.click(function () {
  246. me.remove(me.tree.selected);
  247. });
  248. me.upLevelBtn.click(function () {
  249. me.upLevel(me.tree.selected);
  250. });
  251. me.downLevelBtn.click(function () {
  252. me.downLevel(me.tree.selected);
  253. });
  254. me.downMoveBtn.click(function () {
  255. me.downMove(me.tree.selected);
  256. });
  257. me.upMoveBtn.click(function () {
  258. me.upMove(me.tree.selected);
  259. });
  260. },
  261. insert: function () {
  262. let me = this;
  263. me.insertBtn.addClass('disabled');
  264. let postData = [];
  265. CommonAjax.post('api/getNewRationTreeID', {}, function (newID) {
  266. if(!me.isDef(newID)){
  267. return;
  268. }
  269. me.tree.maxNodeID(newID - 1);
  270. let selected = me.tree.selected;
  271. let insertObj = me.getUpdateObj(me.updateType.new, newID, -1, -1, '', null);
  272. if(me.isDef(selected)) {
  273. let updateObj = me.getUpdateObj(me.updateType.update, selected.getID(), newID, null, null, null);
  274. postData.push(updateObj);
  275. insertObj.updateData.ParentID = selected.getParentID();
  276. if(me.isDef(selected.nextSibling)){
  277. insertObj.updateData.NextSiblingID = selected.getNextSiblingID();
  278. }
  279. }
  280. postData.push(insertObj);
  281. if(postData.length > 0){
  282. //ajax
  283. me.sectionTreeAjax(postData, function (rstData) {
  284. me.controller.insert();
  285. me.refreshBtn(me.tree.selected);
  286. //fresh tools
  287. me.initTools(me.tree.selected);
  288. });
  289. }
  290. });
  291. },
  292. remove: function (selected) {
  293. let me = this;
  294. me.removeBtn.addClass('disabled');
  295. let postData = [], IDs = [];
  296. if(!selected){
  297. return;
  298. }
  299. getDelIds(selected);
  300. function getDelIds(node){
  301. if(me.isDef(node)){
  302. IDs.push(node.getID());
  303. if(node.children.length > 0){
  304. for(let i = 0, len = node.children.length; i < len; i++){
  305. getDelIds(node.children[i]);
  306. }
  307. }
  308. }
  309. }
  310. if(me.isDef(selected.preSibling)){
  311. let updateObj = me.getUpdateObj(me.updateType.update, selected.preSibling.getID(), selected.getNextSiblingID(), null, null, null);
  312. postData.push(updateObj);
  313. }
  314. if(IDs.length > 0){
  315. for(let i = 0, len = IDs.length; i < len; i++){
  316. let delObj = me.getUpdateObj(me.updateType.update, IDs[i], null, null, null, true);
  317. postData.push(delObj);
  318. }
  319. }
  320. if(postData.length > 0){
  321. //ajax
  322. me.sectionTreeAjax(postData, function (rstData) {
  323. me.controller.delete();
  324. me.refreshBtn(me.tree.selected);
  325. me.initTools(me.tree.selected);
  326. });
  327. }
  328. },
  329. upLevel: function (selected) {
  330. let me = this;
  331. me.upLevelBtn.addClass('disabled');
  332. let postData = [];
  333. if(!me.isDef(selected)){
  334. return;
  335. }
  336. if(!me.isDef(selected.parent)){
  337. return;
  338. }
  339. if(me.isDef(selected.preSibling)){
  340. let updateObj = me.getUpdateObj(me.updateType.update, selected.preSibling.getID(), -1, null, null, null);
  341. postData.push(updateObj);
  342. }
  343. let updateObj = me.getUpdateObj(me.updateType.update, selected.getID(), selected.parent.getNextSiblingID(), selected.parent.getParentID(), null, null);
  344. postData.push(updateObj);
  345. let updateParent = me.getUpdateObj(me.updateType.update, selected.getParentID(), selected.getID(), null, null, null);
  346. postData.push(updateParent);
  347. let nextIDs = [];
  348. getNext(selected);
  349. function getNext(node){
  350. if(me.isDef(node.nextSibling)){
  351. nextIDs.push(node.getNextSiblingID());
  352. getNext(node.nextSibling);
  353. }
  354. }
  355. for(let i = 0, len = nextIDs.length; i < len; i++){
  356. postData.push(me.getUpdateObj(me.updateType.update, nextIDs[i], null, selected.getID(), null, null));
  357. }
  358. if(postData.length > 0){
  359. //ajax
  360. me.sectionTreeAjax(postData, function (rstData) {
  361. me.controller.upLevel();
  362. me.refreshBtn(me.tree.selected);
  363. });
  364. }
  365. },
  366. downLevel: function (selected) {
  367. let me = this;
  368. me.downLevelBtn.addClass('disabled');
  369. let postData = [];
  370. if(!me.isDef(selected)){
  371. return;
  372. }
  373. if(!me.isDef(selected.preSibling)){
  374. return;
  375. }
  376. let updatePre = me.getUpdateObj(me.updateType.update, selected.preSibling.getID(), selected.getNextSiblingID(), null, null, null);
  377. postData.push(updatePre);
  378. if(selected.preSibling.children.length > 0){
  379. let updateObj = me.getUpdateObj(me.updateType.update, selected.preSibling.children[selected.preSibling.children.length - 1].getID(), selected.getID(), null, null, null);
  380. postData.push(updateObj);
  381. }
  382. let updateObj = me.getUpdateObj(me.updateType.update, selected.getID(), -1, selected.preSibling.getID(), null, null);
  383. postData.push(updateObj);
  384. if(postData.length > 0){
  385. //ajax
  386. me.sectionTreeAjax(postData, function (rstData) {
  387. me.controller.downLevel();
  388. me.refreshBtn(me.tree.selected);
  389. });
  390. }
  391. },
  392. upMove: function (selected) {
  393. let me = this;
  394. me.upMoveBtn.addClass('disabled');
  395. let postData = [];
  396. if(!me.isDef(selected)){
  397. return;
  398. }
  399. if(!me.isDef(selected.preSibling)){
  400. return;
  401. }
  402. let updateObj = me.getUpdateObj(me.updateType.update, selected.getID(), selected.preSibling.getID(), null, null, null);
  403. postData.push(updateObj);
  404. let updatePre = me.getUpdateObj(me.updateType.update, selected.preSibling.getID(), selected.getNextSiblingID(), null, null, null);
  405. postData.push(updatePre);
  406. if(me.isDef(selected.preSibling.preSibling)){
  407. let updatePrepre = me.getUpdateObj(me.updateType.update, selected.preSibling.preSibling.getID(), selected.getID(), null, null, null);
  408. postData.push(updatePrepre);
  409. }
  410. if(postData.length > 0){
  411. //ajax
  412. me.sectionTreeAjax(postData, function (rstData) {
  413. me.controller.upMove();
  414. me.refreshBtn(me.tree.selected);
  415. });
  416. }
  417. },
  418. downMove: function (selected) {
  419. let me = this;
  420. me.downMoveBtn.addClass('disabled');
  421. let postData = [];
  422. if(!me.isDef(selected)){
  423. return;
  424. }
  425. if(!me.isDef(selected.nextSibling)){
  426. return;
  427. }
  428. if(me.isDef(selected.preSibling)){
  429. let updatePre = me.getUpdateObj(me.updateType.update, selected.preSibling.getID(), selected.getNextSiblingID(), null, null, null);
  430. postData.push(updatePre);
  431. }
  432. let updateObj = me.getUpdateObj(me.updateType.update, selected.getID(), selected.nextSibling.getNextSiblingID(), null, null, null);
  433. postData.push(updateObj);
  434. let updateNext = me.getUpdateObj(me.updateType.update, selected.getNextSiblingID(), selected.getID(), null, null, null);
  435. postData.push(updateNext);
  436. if(postData.length > 0){
  437. //ajax
  438. me.sectionTreeAjax(postData, function (rstData) {
  439. me.controller.downMove();
  440. me.refreshBtn(me.tree.selected);
  441. });
  442. }
  443. },
  444. getUpdateObj: function (updateType, id, nid, pid, name, deleted) {
  445. let updateObj = Object.create(null);
  446. updateObj.updateType = '';
  447. updateObj.updateData = Object.create(null);
  448. updateObj.updateData.rationRepId = pageOprObj.rationLibId;
  449. if(this.isDef(updateType)){
  450. updateObj.updateType = updateType;
  451. }
  452. if(this.isDef(id)){
  453. updateObj.updateData.ID = id;
  454. }
  455. if(this.isDef(nid)){
  456. updateObj.updateData.NextSiblingID = nid;
  457. }
  458. if(this.isDef(pid)){
  459. updateObj.updateData.ParentID = pid;
  460. }
  461. if(this.isDef(name)){
  462. updateObj.updateData.name = name;
  463. }
  464. if(this.isDef(deleted)){
  465. updateObj.updateData.isDeleted = true;
  466. }
  467. return updateObj;
  468. },
  469. sectionTreeAjax: function (postData, scFunc, errFunc) {
  470. CommonAjax.post('api/updateNodes', {updateData: postData, lastOpr: userAccount}, scFunc, errFunc);
  471. },
  472. initTools: function (node) {
  473. if(this.isDef(node)){
  474. explanatoryOprObj.setAttribute(explanatoryOprObj.currentTreeNode ? explanatoryOprObj.currentTreeNode : node, node, node.data.explanation, node.data.ruleText);
  475. explanatoryOprObj.clickUpdate($('#explanationShow'), $('#ruleTextShow'));
  476. explanatoryOprObj.showText($('#explanationShow'), $('#ruleTextShow'), node.data.explanation, node.data.ruleText);
  477. //job
  478. jobContentOprObj.currentSituation = typeof node.data.jobContentSituation !== 'undefined'? node.data.jobContentSituation : jobContentOprObj.situations.NONE;
  479. jobContentOprObj.setAttribute(jobContentOprObj.currentTreeNode ? jobContentOprObj.currentTreeNode : node, node);
  480. jobContentOprObj.clickUpdate($('#txtareaAll'));
  481. //fz
  482. annotationOprObj.currentSituation = typeof node.data.annotationSituation !== 'undefined'? node.data.annotationSituation : annotationOprObj.situations.NONE;
  483. annotationOprObj.clickUpdate($('#fzTxtareaAll'));
  484. }
  485. },
  486. //模仿默认点击
  487. initSelection: function (node) {
  488. let me = this;
  489. if(!me.isDef(node)){
  490. return;
  491. }
  492. me.initTools(node);
  493. me.refreshBtn(node);
  494. if(!me.isDef(node.children) || node.children.length === 0){
  495. rationOprObj.canRations = true;
  496. rationOprObj.workBook.getSheet(0).clearSelection();
  497. rationOprObj.getRationItems(node.data.ID);
  498. rationOprObj.setCombo(rationOprObj.workBook.getSheet(0), 'dynamic');
  499. }
  500. else {
  501. rationOprObj.canRations = false;
  502. rationOprObj.currentSectionId = node.data.ID;
  503. rationOprObj.workBook.getSheet(0).setRowCount(30);
  504. rationOprObj.setCombo(rationOprObj.workBook.getSheet(0), null);
  505. jobContentOprObj.setRadiosDisabled(true, jobContentOprObj.radios);
  506. jobContentOprObj.hideTable($('#tableAll'), $('#tablePartial'));
  507. annotationOprObj.setRadiosDisabled(true, annotationOprObj.radios);
  508. annotationOprObj.hideTable($('#fzTableAll'), $('#fzTablePartial'));
  509. sheetCommonObj.cleanSheet(rationOprObj.workBook.getSheet(0), rationOprObj.setting, -1);
  510. }
  511. sheetCommonObj.cleanSheet(rationGLJOprObj.sheet, rationGLJOprObj.setting, -1);
  512. rationGLJOprObj.sheet.getParent().focus(false);
  513. me.workBook.focus(true);
  514. }
  515. };