section_tree.js 17 KB

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