core.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import defaultSetting from "./config.js";
  2. import { common_extend } from "./utils/util";
  3. import Store from "./store";
  4. import server from "./controllers/server";
  5. import luckysheetConfigsetting from "./controllers/luckysheetConfigsetting";
  6. import sheetmanage from "./controllers/sheetmanage";
  7. import luckysheetsizeauto from "./controllers/resize";
  8. import luckysheetHandler from "./controllers/handler";
  9. import { initialFilterHandler } from "./controllers/filter";
  10. import { initialMatrixOperation } from "./controllers/matrixOperation";
  11. import { initialSheetBar } from "./controllers/sheetBar";
  12. import { formulaBarInitial } from "./controllers/formulaBar";
  13. import { rowColumnOperationInitial } from "./controllers/rowColumnOperation";
  14. import { keyboardInitial } from "./controllers/keyboard";
  15. import { orderByInitial } from "./controllers/orderBy";
  16. import { initPlugins } from "./controllers/expendPlugins";
  17. import {
  18. getluckysheetfile,
  19. getluckysheet_select_save,
  20. getconfig,
  21. } from "./methods/get";
  22. import { setluckysheet_select_save } from "./methods/set";
  23. import { luckysheetrefreshgrid, jfrefreshgrid } from "./global/refresh";
  24. import functionlist from "./function/functionlist";
  25. import { luckysheetlodingHTML } from "./controllers/constant";
  26. import { getcellvalue, getdatabyselection } from "./global/getdata";
  27. import { setcellvalue } from "./global/setdata";
  28. import { selectHightlightShow } from "./controllers/select";
  29. import { zoomInitial } from "./controllers/zoom";
  30. import { printInitial } from "./controllers/print";
  31. import method from "./global/method";
  32. import * as api from "./global/api";
  33. import flatpickr from "flatpickr";
  34. import Mandarin from "flatpickr/dist/l10n/zh.js";
  35. import { initListener } from "./controllers/listener";
  36. import { hideloading, showloading } from "./global/loading.js";
  37. import { luckysheetextendData } from "./global/extend.js";
  38. let luckysheet = {};
  39. // mount api
  40. // luckysheet.api = api;
  41. // Object.assign(luckysheet, api);
  42. luckysheet = common_extend(api, luckysheet);
  43. //创建luckysheet表格
  44. luckysheet.create = function(setting) {
  45. method.destroy();
  46. server.websocket && server.websocket.close(1000, "Work complete");
  47. // Store original parameters for api: toJson
  48. Store.toJsonOptions = {};
  49. for (let c in setting) {
  50. if (c !== "data") {
  51. Store.toJsonOptions[c] = setting[c];
  52. }
  53. }
  54. let extendsetting = common_extend(defaultSetting, setting);
  55. let loadurl = extendsetting.loadUrl,
  56. authorityUrl = extendsetting.authorityUrl,
  57. templateId = extendsetting.templateId,
  58. menu = extendsetting.menu,
  59. authority = extendsetting.authority,
  60. title = extendsetting.title;
  61. let container = extendsetting.container;
  62. Store.container = container;
  63. Store.luckysheetfile = extendsetting.data;
  64. Store.defaultcolumnNum = extendsetting.column;
  65. Store.defaultrowNum = extendsetting.row;
  66. Store.defaultFontSize = extendsetting.defaultFontSize;
  67. Store.fullscreenmode = extendsetting.fullscreenmode;
  68. Store.lang = extendsetting.lang; //language
  69. Store.allowEdit = extendsetting.allowEdit;
  70. Store.limitSheetNameLength = extendsetting.limitSheetNameLength;
  71. Store.defaultSheetNameMaxLength = extendsetting.defaultSheetNameMaxLength;
  72. Store.fontList = extendsetting.fontList;
  73. server.gridKey = extendsetting.gridKey;
  74. server.loadUrl = extendsetting.loadUrl;
  75. server.updateUrl = extendsetting.updateUrl;
  76. server.updateImageUrl = extendsetting.updateImageUrl;
  77. server.title = extendsetting.title;
  78. server.loadSheetUrl = extendsetting.loadSheetUrl;
  79. server.allowUpdate = extendsetting.allowUpdate;
  80. luckysheetConfigsetting.autoFormatw = extendsetting.autoFormatw;
  81. luckysheetConfigsetting.accuracy = extendsetting.accuracy;
  82. luckysheetConfigsetting.total = extendsetting.data[0].total;
  83. luckysheetConfigsetting.loading = extendsetting.loading;
  84. luckysheetConfigsetting.allowCopy = extendsetting.allowCopy;
  85. luckysheetConfigsetting.showtoolbar = extendsetting.showtoolbar;
  86. luckysheetConfigsetting.showtoolbarConfig = extendsetting.showtoolbarConfig;
  87. luckysheetConfigsetting.showinfobar = extendsetting.showinfobar;
  88. luckysheetConfigsetting.showsheetbar = extendsetting.showsheetbar;
  89. luckysheetConfigsetting.showsheetbarConfig = extendsetting.showsheetbarConfig;
  90. luckysheetConfigsetting.showstatisticBar = extendsetting.showstatisticBar;
  91. luckysheetConfigsetting.showstatisticBarConfig =
  92. extendsetting.showstatisticBarConfig;
  93. luckysheetConfigsetting.sheetFormulaBar = extendsetting.sheetFormulaBar;
  94. luckysheetConfigsetting.cellRightClickConfig =
  95. extendsetting.cellRightClickConfig;
  96. luckysheetConfigsetting.sheetRightClickConfig =
  97. extendsetting.sheetRightClickConfig;
  98. luckysheetConfigsetting.pointEdit = extendsetting.pointEdit;
  99. luckysheetConfigsetting.pointEditUpdate = extendsetting.pointEditUpdate;
  100. luckysheetConfigsetting.pointEditZoom = extendsetting.pointEditZoom;
  101. luckysheetConfigsetting.userInfo = extendsetting.userInfo;
  102. luckysheetConfigsetting.userMenuItem = extendsetting.userMenuItem;
  103. luckysheetConfigsetting.myFolderUrl = extendsetting.myFolderUrl;
  104. luckysheetConfigsetting.functionButton = extendsetting.functionButton;
  105. luckysheetConfigsetting.showConfigWindowResize =
  106. extendsetting.showConfigWindowResize;
  107. luckysheetConfigsetting.enableAddRow = extendsetting.enableAddRow;
  108. luckysheetConfigsetting.enableAddBackTop = extendsetting.enableAddBackTop;
  109. luckysheetConfigsetting.addRowCount = extendsetting.addRowCount;
  110. luckysheetConfigsetting.enablePage = extendsetting.enablePage;
  111. luckysheetConfigsetting.pageInfo = extendsetting.pageInfo;
  112. luckysheetConfigsetting.editMode = extendsetting.editMode;
  113. luckysheetConfigsetting.beforeCreateDom = extendsetting.beforeCreateDom;
  114. luckysheetConfigsetting.workbookCreateBefore =
  115. extendsetting.workbookCreateBefore;
  116. luckysheetConfigsetting.workbookCreateAfter =
  117. extendsetting.workbookCreateAfter;
  118. luckysheetConfigsetting.remoteFunction = extendsetting.remoteFunction;
  119. luckysheetConfigsetting.fireMousedown = extendsetting.fireMousedown;
  120. luckysheetConfigsetting.forceCalculation = extendsetting.forceCalculation;
  121. luckysheetConfigsetting.plugins = extendsetting.plugins;
  122. luckysheetConfigsetting.rowHeaderWidth = extendsetting.rowHeaderWidth;
  123. luckysheetConfigsetting.columnHeaderHeight = extendsetting.columnHeaderHeight;
  124. luckysheetConfigsetting.defaultColWidth = extendsetting.defaultColWidth;
  125. luckysheetConfigsetting.defaultRowHeight = extendsetting.defaultRowHeight;
  126. luckysheetConfigsetting.title = extendsetting.title;
  127. luckysheetConfigsetting.container = extendsetting.container;
  128. luckysheetConfigsetting.hook = extendsetting.hook;
  129. luckysheetConfigsetting.pager = extendsetting.pager;
  130. luckysheetConfigsetting.initShowsheetbarConfig = false;
  131. luckysheetConfigsetting.imageUpdateMethodConfig =
  132. extendsetting.imageUpdateMethodConfig;
  133. if (Store.lang === "zh") flatpickr.localize(Mandarin.zh);
  134. // Store the currently used plugins for monitoring asynchronous loading
  135. Store.asyncLoad.push(...luckysheetConfigsetting.plugins);
  136. // Register plugins
  137. initPlugins(extendsetting.plugins, extendsetting.data);
  138. // Store formula information, including internationalization
  139. functionlist();
  140. let devicePixelRatio = extendsetting.devicePixelRatio;
  141. if (devicePixelRatio == null) {
  142. devicePixelRatio = 1;
  143. }
  144. Store.devicePixelRatio = Math.ceil(devicePixelRatio);
  145. //loading
  146. const loadingObj = luckysheetlodingHTML("#" + container);
  147. Store.loadingObj = loadingObj;
  148. if (loadurl == "") {
  149. sheetmanage.initialjfFile(menu, title);
  150. // luckysheetsizeauto();
  151. initialWorkBook();
  152. } else {
  153. $.get(authorityUrl, function(res) {
  154. let arr = res.data.filter(
  155. (item) => item.template?.template_id == templateId
  156. );
  157. console.log(arr);
  158. $.post(loadurl, { gridKey: server.gridKey }, function(d) {
  159. let data = new Function("return " + d)();
  160. data.forEach((sheet) => {
  161. initSheet(sheet, arr, authority);
  162. });
  163. console.log(data);
  164. Store.luckysheetfile = data;
  165. sheetmanage.initialjfFile(menu, title);
  166. // luckysheetsizeauto();
  167. initialWorkBook();
  168. //需要更新数据给后台时,建立WebSocket连接
  169. if (server.allowUpdate) {
  170. server.getUser = setting.getUser;
  171. server.openWebSocket();
  172. }
  173. });
  174. });
  175. }
  176. };
  177. function initialWorkBook() {
  178. luckysheetHandler(); //Overall dom initialization
  179. initialFilterHandler(); //Filter initialization
  180. initialMatrixOperation(); //Right click matrix initialization
  181. initialSheetBar(); //bottom sheet bar initialization
  182. formulaBarInitial(); //top formula bar initialization
  183. rowColumnOperationInitial(); //row and coloumn operate initialization
  184. keyboardInitial(); //Keyboard operate initialization
  185. orderByInitial(); //menu bar orderby function initialization
  186. zoomInitial(); //zoom method initialization
  187. printInitial(); //print initialization
  188. initListener();
  189. }
  190. //获取所有表格数据
  191. luckysheet.getluckysheetfile = getluckysheetfile;
  192. //获取当前表格 选区
  193. luckysheet.getluckysheet_select_save = getluckysheet_select_save;
  194. //设置当前表格 选区
  195. luckysheet.setluckysheet_select_save = setluckysheet_select_save;
  196. //获取当前表格 config配置
  197. luckysheet.getconfig = getconfig;
  198. //二维数组数据 转化成 {r, c, v}格式 一维数组 (传入参数为二维数据data)
  199. luckysheet.getGridData = sheetmanage.getGridData;
  200. //生成表格所需二维数组 (传入参数为表格数据对象file)
  201. luckysheet.buildGridData = sheetmanage.buildGridData;
  202. // Refresh the canvas display data according to scrollHeight and scrollWidth
  203. luckysheet.luckysheetrefreshgrid = luckysheetrefreshgrid;
  204. // Refresh canvas
  205. luckysheet.jfrefreshgrid = jfrefreshgrid;
  206. // Get the value of the cell
  207. luckysheet.getcellvalue = getcellvalue;
  208. // Set cell value
  209. luckysheet.setcellvalue = setcellvalue;
  210. // Get selection range value
  211. luckysheet.getdatabyselection = getdatabyselection;
  212. luckysheet.sheetmanage = sheetmanage;
  213. // Data of the current table
  214. luckysheet.flowdata = function() {
  215. return Store.flowdata;
  216. };
  217. // Set selection highlight
  218. luckysheet.selectHightlightShow = selectHightlightShow;
  219. // Reset parameters after destroying the table
  220. luckysheet.destroy = method.destroy;
  221. luckysheet.showLoadingProgress = showloading;
  222. luckysheet.hideLoadingProgress = hideloading;
  223. luckysheet.luckysheetextendData = luckysheetextendData;
  224. export { luckysheet };
  225. function initSheet(sheet, cellAuthority, authority) {
  226. let allowRangeList = [],
  227. colhidden = {};
  228. let cache = {};
  229. if (sheet.config) {
  230. delete sheet.config.authority;
  231. }
  232. sheet.celldata.forEach((item) => {
  233. if (!cache[item.c]) cache[item.c] = [];
  234. cache[item.c].push(item);
  235. });
  236. cellAuthority.forEach((item) => {
  237. // 将A B C D....转换为0 1 2 3...
  238. let col_idx = item.template.col_idx;
  239. let col = col_idx.charCodeAt(0) - 65;
  240. if (!item.can_read) {
  241. colhidden[col] = 0;
  242. }
  243. if (item.can_write) {
  244. // 区域保护
  245. allowRangeList.push({
  246. hintText: "",
  247. sqref: `$${col_idx}$0:$${col_idx}$99999`,
  248. });
  249. }
  250. });
  251. // let celldata = []
  252. // Object.values(cache).forEach(colItem => {
  253. // celldata = [...celldata,...colItem]
  254. // })
  255. // sheet.celldata = celldata
  256. sheet.config = sheet.config || {};
  257. // 处于审批时 所有人都无法修改
  258. if (authority) {
  259. sheet.config.authority = authority;
  260. } else if (cellAuthority.length > 0) {
  261. let newAuthority = {
  262. sheet: true,
  263. hintText: "没有编辑权限!",
  264. };
  265. if (allowRangeList.length > 0) {
  266. // 可编辑内容
  267. newAuthority.allowRangeList = allowRangeList;
  268. }
  269. sheet.config.authority = newAuthority;
  270. } else {
  271. let lastCell = sheet.celldata[sheet.celldata.length - 1];
  272. if (lastCell) {
  273. let lastCol = String.fromCharCode(lastCell.c);
  274. // 第一列禁止编辑但是可以增加
  275. let canEditCell = sheet.celldata.filter((item) => {
  276. if (item.r == 0) {
  277. // 判断是否为空
  278. if ((item?.v?.v ?? "") === "" && !item?.v?.ct?.s) return true;
  279. }
  280. });
  281. let canEditRangeList = canEditCell.map((item) => {
  282. let col = String.fromCharCode(item.c + 65);
  283. return {
  284. hintText: "",
  285. sqref: `$${col}$1:$${col}$1`,
  286. };
  287. });
  288. // 设置保护区域,lucksheet不能直接设置不允许编辑的区域
  289. // 只能禁止整体编辑,设置例外区域
  290. sheet.config.authority = {
  291. sheet: 1,
  292. hintText: "表头只能新增,禁止编辑!",
  293. // insertColumns: 0,
  294. allowRangeList: [
  295. // 第一行以外都能编辑
  296. {
  297. hintText: "",
  298. sqref: `$a$2:$z$99999`,
  299. },
  300. // 第一列未加载出来的row也允许编辑
  301. {
  302. hintText: "",
  303. sqref: `$${lastCol}$1:$${lastCol}$1`,
  304. },
  305. ...canEditRangeList,
  306. ],
  307. };
  308. }
  309. }
  310. sheet.config.colhidden = colhidden;
  311. // 公式计算
  312. let calcChain = [];
  313. let index = sheet.index;
  314. sheet.celldata.forEach((item) => {
  315. if (typeof item.v.f !== "undefined") {
  316. calcChain.push({
  317. r: item.r,
  318. c: item.c,
  319. index,
  320. });
  321. }
  322. });
  323. sheet.calcChain = calcChain;
  324. }