std_lib.js 3.2 KB

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