std_lib.js 3.3 KB

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