template.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /**
  2. * Created by Mai on 2017/8/22.
  3. */
  4. let TEMPLATE_BILLS_SETTING = {
  5. "emptyRows":1,
  6. "headRows":1,
  7. "headRowHeight":[35],
  8. "treeCol": 1,
  9. "cols":[{
  10. "width":80,
  11. "readOnly":false,
  12. "head":{
  13. "titleNames":["类别"],
  14. "spanCols":[1],
  15. "spanRows":[1],
  16. "vAlign":[1],
  17. "hAlign":[1],
  18. "font":["Arial"]
  19. },
  20. "data":{
  21. "field":"type",
  22. "vAlign":0,
  23. "hAlign":1,
  24. "font":"Arail",
  25. }
  26. }, {
  27. "width":200,
  28. "readOnly":false,
  29. "head":{
  30. "titleNames":["编号"],
  31. "spanCols":[1],
  32. "spanRows":[1],
  33. "vAlign":[1],
  34. "hAlign":[1],
  35. "font":["Arial"]
  36. },
  37. "data":{
  38. "field":"code",
  39. "vAlign":0,
  40. "hAlign":3,
  41. "font":"Arail"
  42. }
  43. }, {
  44. "width":300,
  45. "readOnly":false,
  46. "head":{
  47. "titleNames":["名称"],
  48. "spanCols":[1],
  49. "spanRows":[1],
  50. "vAlign":[1],
  51. "hAlign":[1],
  52. "font":["Arial"]
  53. },
  54. "data":{
  55. "field":"name",
  56. "vAlign":0,
  57. "hAlign":3,
  58. "font":"Arail"
  59. }
  60. }, {
  61. "width":50,
  62. "readOnly":false,
  63. "head":{
  64. "titleNames":["单位"],
  65. "spanCols":[1],
  66. "spanRows":[1],
  67. "vAlign":[1],
  68. "hAlign":[1],
  69. "font":["Arial"]
  70. },
  71. "data":{
  72. "field":"unit",
  73. "vAlign":0,
  74. "hAlign":1,
  75. "font":"Arail"
  76. }
  77. }, {
  78. "width":200,
  79. "readOnly":false,
  80. "head":{
  81. "titleNames":["清单固定类别"],
  82. "spanCols":[1],
  83. "spanRows":[1],
  84. "vAlign":[1],
  85. "hAlign":[1],
  86. "font":["Arial"]
  87. },
  88. "data":{
  89. "field":"flagsIndex.fixed.flag",
  90. "vAlign":0,
  91. "hAlign":3,
  92. "font":"Arail",
  93. }
  94. }, {
  95. "width":50,
  96. "readOnly":true,
  97. "head":{
  98. "titleNames":["ID"],
  99. "spanCols":[1],
  100. "spanRows":[1],
  101. "vAlign":[1],
  102. "hAlign":[1],
  103. "font":["Arial"]
  104. },
  105. "data":{
  106. "field":"ID",
  107. "vAlign":0,
  108. "hAlign":1,
  109. "font":"Arail"
  110. }
  111. }, {
  112. "width":50,
  113. "readOnly":true,
  114. "head":{
  115. "titleNames":["ParentID"],
  116. "spanCols":[1],
  117. "spanRows":[1],
  118. "vAlign":[1],
  119. "hAlign":[1],
  120. "font":["Arial"]
  121. },
  122. "data":{
  123. "field":"ParentID",
  124. "vAlign":0,
  125. "hAlign":1,
  126. "font":"Arail"
  127. }
  128. }, {
  129. "width":50,
  130. "readOnly":true,
  131. "head":{
  132. "titleNames":["NextSiblingID"],
  133. "spanCols":[1],
  134. "spanRows":[1],
  135. "vAlign":[1],
  136. "hAlign":[1],
  137. "font":["Arial"]
  138. },
  139. "data":{
  140. "field":"NextSiblingID",
  141. "vAlign":0,
  142. "hAlign":1,
  143. "font":"Arail"
  144. }
  145. }]
  146. };
  147. $(document).ready(function () {
  148. let RefreshBaseActn = function (tree) {
  149. let showButton = function (show, btn) {
  150. if (show) {
  151. btn.show();
  152. } else {
  153. btn.hide();
  154. }
  155. };
  156. showButton(tree.selected && tree.selected.canUpLevel(), $('#upLevel'));
  157. showButton(tree.selected && tree.selected.canDownLevel(), $('#downLevel'));
  158. showButton(tree.selected && tree.selected.canUpMove(), $('#upMove'));
  159. showButton(tree.selected && tree.selected.canDownMove(), $('#downMove'));
  160. showButton(tree.selected ? true : false, $('#delete'));
  161. }
  162. let RefreshBillsData = function (datas) {
  163. datas.forEach(function (data) {
  164. let node = tree.findNode(data.data.ID);
  165. if (node) {
  166. $.extend(true, node.data, data.data);
  167. }
  168. });
  169. };
  170. let getNameValueComboCellType = function (datas) {
  171. let comboItems = [];
  172. for (let data of datas) {
  173. comboItems.push({text: data.name, value: data.value});
  174. }
  175. let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  176. combo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value)
  177. .items(comboItems);
  178. return combo;
  179. }
  180. let getFixedFlagCellType = function () {
  181. let billsFixedFlagData = JSON.parse(billsFixedFlagList);
  182. return getNameValueComboCellType(billsFixedFlagData);
  183. };
  184. let getTypeFlagCellType = function () {
  185. let billsTypeFlagData = JSON.parse(billsTypeFlagList);
  186. return getNameValueComboCellType(billsTypeFlagData);
  187. };
  188. let setFee = function (data, fullField, value) {
  189. let fields = fullField.split('.'), valueField = data;
  190. for (let i in fields) {
  191. if (valueField[fields[i]]) {
  192. if (i == fields.length - 1) {
  193. valueField[fields[i]] = value;
  194. } else {
  195. valueField = valueField[fields[i]];
  196. }
  197. } else {
  198. if (i == fields.length - 1) {
  199. valueField[fields[i]] = value;
  200. } else {
  201. valueField[fields[i]] = {};
  202. };
  203. valueField = valueField[fields[i]];
  204. }
  205. }
  206. }
  207. billsTemplateData = billsTemplateData.replace(/\n/g, '\\n');
  208. let templateData = JSON.parse(billsTemplateData);
  209. for (let data of templateData) {
  210. if (data.flags) {
  211. data.flagsIndex = {};
  212. for (let flag of data.flags) {
  213. data.flagsIndex[flag.fieldName] = flag;
  214. }
  215. }
  216. }
  217. for (col of TEMPLATE_BILLS_SETTING.cols) {
  218. if (col.data.field === 'type' && TEMPLATE_BILLS_SETTING.cols.indexOf(col) !== TEMPLATE_BILLS_SETTING.treeCol) {
  219. col.data.cellType = getTypeFlagCellType();
  220. } else if (col.data.field === 'flagsIndex.fixed.flag' && TEMPLATE_BILLS_SETTING.cols.indexOf(col) !== TEMPLATE_BILLS_SETTING.treeCol) {
  221. col.data.cellType = getFixedFlagCellType();
  222. }
  223. }
  224. let tree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: true});
  225. let billsSpread = TREE_SHEET_HELPER.createNewSpread($('#billsSpread')[0]);
  226. let controller = TREE_SHEET_CONTROLLER.createNew(tree, billsSpread.getActiveSheet(), TEMPLATE_BILLS_SETTING);
  227. controller.bind('refreshBaseActn', RefreshBaseActn);
  228. billsSpread.bind(GC.Spread.Sheets.Events.EditEnded, function (sender, info) {
  229. var node = controller.tree.items[info.row];
  230. var fieldName = controller.setting.cols[info.col].data.field;
  231. var data = {type: 'update', data: {ID: node.getID()}};
  232. if (/flagsIndex/.test(fieldName)) {
  233. data.data.flags = [];
  234. let flagField = fieldName.split('.');
  235. data.data.flags.push({fieldName: flagField[1], flag: info.editingText});
  236. } else {
  237. setFee(data.data, fieldName, info.editingText);
  238. }
  239. var updateData = [data];
  240. CommonAjax.post(updateUrl, updateData, function (data) {
  241. setFee(node.data, fieldName, info.editingText);
  242. controller.refreshTreeNode([node], false);
  243. }, function () {
  244. controller.refreshTreeNode([node], false);
  245. });
  246. });
  247. billsSpread.bind(GC.Spread.Sheets.Events.ClipboardPasted, function (e, info) {
  248. console.log("ClipboardPasted");
  249. var node, iRow, iCol, curRow, curCol, datas = [], data, fieldName, updateData;
  250. for (iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  251. curRow = info.cellRange.row + iRow;
  252. node = controller.tree.items[curRow];
  253. if (node) {
  254. data = {type: 'update', data: {ID: node.getID()}};
  255. for (iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  256. curCol = info.cellRange.col + iCol;
  257. fieldName = controller.setting.cols[curCol].data.field;
  258. if (/flagsIndex/.test(fieldName)) {
  259. data.data.flags = [];
  260. let flagField = fieldName.split('.');
  261. data.data.flags.push({fieldName: flagField[1],flag: info.sheet.getText(curRow, curCol)});
  262. } else {
  263. setFee(data.data, fieldName, info.sheet.getText(curRow, curCol));
  264. }
  265. }
  266. datas.push(data);
  267. }
  268. };
  269. CommonAjax.post(updateUrl, datas, function (data) {
  270. RefreshBillsData(data);
  271. controller.showTreeData();
  272. }, function () {
  273. controller.showTreeData();
  274. });
  275. });
  276. tree.loadDatas(templateData);
  277. controller.showTreeData();
  278. RefreshBaseActn(tree);
  279. $('#insert').click(function () {
  280. var selected = controller.tree.selected, updateData;
  281. if (selected) {
  282. updateData = controller.tree.getInsertData(selected.getParentID(), selected.getNextSiblingID());
  283. } else {
  284. updateData = controller.tree.getInsertData();
  285. }
  286. if (updateData.length > 0) {
  287. CommonAjax.post(updateUrl, updateData, function (data) {
  288. controller.insert();
  289. controller.showTreeData();
  290. });
  291. } else {
  292. alert('新增节点失败, 请重试.');
  293. }
  294. });
  295. $('#delete').click(function () {
  296. var selected = controller.tree.selected, updateData;
  297. if (selected) {
  298. updateData = controller.tree.getDeleteData(selected);
  299. CommonAjax.post(updateUrl, updateData, function (data) {
  300. controller.delete();
  301. controller.showTreeData();
  302. });
  303. }
  304. });
  305. $('#upLevel').click(function () {
  306. var selected = controller.tree.selected, updateData;
  307. if (selected) {
  308. updateData = selected.getUpLevelData();
  309. CommonAjax.post(updateUrl, updateData, function (data) {
  310. controller.upLevel();
  311. controller.showTreeData();
  312. });
  313. }
  314. });
  315. $('#downLevel').click(function () {
  316. var selected = controller.tree.selected, updateData;
  317. if (selected) {
  318. updateData = selected.getDownLevelData();
  319. CommonAjax.post(updateUrl, updateData, function (data) {
  320. controller.downLevel();
  321. controller.showTreeData();
  322. });
  323. }
  324. });
  325. $('#upMove').click(function () {
  326. var selected = controller.tree.selected, updateData;
  327. if (selected) {
  328. updateData = selected.getUpMoveData();
  329. CommonAjax.post(updateUrl, updateData, function (data) {
  330. controller.upMove();
  331. controller.showTreeData();
  332. });
  333. }
  334. });
  335. $('#downMove').click(function () {
  336. var selected = controller.tree.selected, updateData;
  337. if (selected) {
  338. updateData = selected.getDownMoveData();
  339. CommonAjax.post(updateUrl, updateData, function (data) {
  340. controller.downMove();
  341. controller.showTreeData();
  342. });
  343. }
  344. });
  345. });