std_lib.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. // setting = {
  10. // selector,
  11. // stdType,
  12. // treeSetting,
  13. // spreadSetting,
  14. // cellDoubleClick,
  15. // page,
  16. // };
  17. class stdLib {
  18. constructor(setting) {
  19. const self = this;
  20. this.setting = setting;
  21. this.obj = $(setting.selector + '-spread')[0];
  22. this.stdType = setting.stdType;
  23. this.treeSetting = setting.treeSetting;
  24. this.spreadSetting = setting.spreadSetting;
  25. this.spreadSetting.stdType = setting.stdType;
  26. this.spread = SpreadJsObj.createNewSpread(this.obj);
  27. SpreadJsObj.initSheet(this.spread.getActiveSheet(), this.spreadSetting);
  28. SpreadJsObj.forbiddenSpreadContextMenu(setting.selector, this.spread);
  29. this.spread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, setting.cellDoubleClick);
  30. this.spread.getActiveSheet().bind(spreadNS.Events.SelectionChanged, function (e, info) {
  31. if (!info.oldSelections[0] || info.newSelections[0].row !== info.oldSelections[0].row) {
  32. SpreadJsObj.saveTopAndSelect(info.sheet, self.cacheKey);
  33. }
  34. });
  35. this.spread.getActiveSheet().bind(spreadNS.Events.TopRowChanged, function (e, info) {
  36. SpreadJsObj.saveTopAndSelect(info.sheet, self.cacheKey);
  37. });
  38. this.pathTree = createNewPathTree('base', this.treeSetting);
  39. this.cacheLib = [];
  40. $('select', setting.selector).change(function () {
  41. self.loadLib(parseInt(this.value), true);
  42. });
  43. }
  44. loadLib (listId, reload = false) {
  45. this.cacheKey = this.setting.page + '-' + this.setting.stdType + '-' + listId;
  46. const self = this;
  47. const locateMemory = function () {
  48. if (!reload) {
  49. SpreadJsObj.loadTopAndSelect(self.spread.getActiveSheet(), self.cacheKey);
  50. } else {
  51. removeLocalCache(self.cacheKey);
  52. }
  53. };
  54. const cacheData = this.cacheLib.find(function (lib) {
  55. return lib.id === listId;
  56. });
  57. if (cacheData) {
  58. this.pathTree.loadDatas(cacheData.data);
  59. SpreadJsObj.loadSheetData(this.spread.getActiveSheet(), 'tree', this.pathTree);
  60. locateMemory();
  61. } else {
  62. postData('/std-lib/get-data', {stdType: this.stdType, list_id: listId}, function (data) {
  63. self.cacheLib.push({id: listId, data: data});
  64. self.pathTree.loadDatas(data);
  65. SpreadJsObj.loadSheetData(self.spread.getActiveSheet(), 'tree', self.pathTree);
  66. locateMemory();
  67. });
  68. }
  69. }
  70. }