lock_util.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2019/11/14
  7. * @version
  8. */
  9. const lockUtil = (() => {
  10. // 从地址栏获取是否锁定
  11. function getLocked() {
  12. const search = window.location.search;
  13. const reg = /locked=(false|true)/;
  14. const match = search.match(reg);
  15. return match ? JSON.parse(match[1]) : true;
  16. }
  17. function lockTools($range, locked) {
  18. const $btns = $range.find('.lock-btn-control');
  19. const toolList = [];
  20. for (const $btn of $btns) {
  21. toolList.push({ $ref: $($btn), type: 'button' });
  22. }
  23. const $texts = $range.find('.lock-text-control');
  24. for (const $text of $texts) {
  25. toolList.push({ $ref: $($text), type: 'text' });
  26. }
  27. toolList.forEach(item => {
  28. switch (item.type) {
  29. case 'button':
  30. locked ? item.$ref.addClass('disabled') : item.$ref.removeClass('disabled');
  31. break;
  32. case 'text':
  33. item.$ref.prop('readOnly', locked);
  34. break;
  35. }
  36. });
  37. }
  38. function lockSpreads(spreads, locked) {
  39. if (!locked) {
  40. return;
  41. }
  42. spreads.forEach(spread => {
  43. spread.unbind(GC.Spread.Sheets.Events.ButtonClicked);
  44. const sheetCount = spread.getSheetCount();
  45. for (let i = 0; i < sheetCount; i++) {
  46. const sheet = spread.getSheet(i);
  47. sheet.unbind(GC.Spread.Sheets.Events.ButtonClicked);
  48. sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
  49. sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
  50. sheet.unbind(GC.Spread.Sheets.Events.RangeChanged);
  51. sheet.unbind(GC.Spread.Sheets.Events.ClipboardChanging);
  52. sheet.unbind(GC.Spread.Sheets.Events.ClipboardChanged);
  53. sheet.unbind(GC.Spread.Sheets.Events.CellDoubleClick);
  54. sheet.unbind(GC.Spread.Sheets.Events.CellClick);
  55. sheet.unbind(GC.Spread.Sheets.Events.ValueChanged);
  56. sheet.suspendPaint();
  57. sheet.suspendEvent();
  58. sheet.options.isProtected = true;
  59. const rowCount = sheet.getRowCount();
  60. const colCount = sheet.getColumnCount();
  61. for (let row = 0; row < rowCount; row++) {
  62. for (let col = 0; col < colCount; col++) {
  63. sheet.getCell(row, col).locked(true);
  64. }
  65. }
  66. sheet.resumePaint();
  67. sheet.resumeEvent();
  68. }
  69. });
  70. }
  71. function unLockSpreads(spreads) {
  72. spreads.forEach(spread => {
  73. spread.unbind(GC.Spread.Sheets.Events.ButtonClicked);
  74. const sheetCount = spread.getSheetCount();
  75. for (let i = 0; i < sheetCount; i++) {
  76. const sheet = spread.getSheet(i);
  77. sheet.suspendPaint();
  78. sheet.suspendEvent();
  79. sheet.options.isProtected = false;
  80. const rowCount = sheet.getRowCount();
  81. const colCount = sheet.getColumnCount();
  82. for (let row = 0; row < rowCount; row++) {
  83. for (let col = 0; col < colCount; col++) {
  84. sheet.getCell(row, col).locked(false);
  85. }
  86. }
  87. sheet.resumePaint();
  88. sheet.resumeEvent();
  89. }
  90. });
  91. }
  92. function lockURL(locked, $url) {
  93. const originURL = $url.prop('href');
  94. const originLocked = !locked;
  95. const reg = new RegExp(`locked=${originLocked}`);
  96. const curURL = reg.test(originURL) ? originURL.replace(reg, `locked=${locked}`) : `${originURL}&locked=${locked}`;
  97. $url.prop('href', curURL);
  98. }
  99. function displayLock($lock, locked) {
  100. $lock.data('locked', locked);
  101. const innerHtml = locked ? '<i class="fa fa-unlock-alt"></i>' : '<i class="fa fa-lock"></i>';
  102. $lock.html(innerHtml);
  103. const title = locked ? '解锁' : '锁定';
  104. $lock.prop('title', title);
  105. }
  106. // 库列表页面,锁定按钮点击操作
  107. function handleLockClick($lock) {
  108. const curLocked = !$lock.data().locked;
  109. displayLock($lock, curLocked);
  110. const $url = $lock.parent().parent().children(':first-child').children(':first-child');
  111. lockURL(curLocked, $url);
  112. const $range = $lock.parent().parent();
  113. lockTools($range, curLocked);
  114. }
  115. function lockSpreadsAndTools(spreads, $range, locked) {
  116. if (!locked) {
  117. return;
  118. }
  119. lockSpreads(spreads, locked);
  120. lockTools($range, locked);
  121. }
  122. return {
  123. getLocked,
  124. lockTools,
  125. lockSpreads,
  126. unLockSpreads,
  127. lockURL,
  128. displayLock,
  129. handleLockClick,
  130. lockSpreadsAndTools
  131. }
  132. })();