123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- 'use strict';
- /**
- *
- *
- * @author Zhong
- * @date 2019/11/14
- * @version
- */
- const lockUtil = (() => {
- // 从地址栏获取是否锁定
- function getLocked() {
- const search = window.location.search;
- const reg = /locked=(false|true)/;
- const match = search.match(reg);
- return match ? JSON.parse(match[1]) : true;
- }
- function lockTools($range, locked) {
- const $btns = $range.find('.lock-btn-control');
- const toolList = [];
- for (const $btn of $btns) {
- toolList.push({ $ref: $($btn), type: 'button' });
- }
- const $texts = $range.find('.lock-text-control');
- for (const $text of $texts) {
- toolList.push({ $ref: $($text), type: 'text' });
- }
- toolList.forEach(item => {
- switch (item.type) {
- case 'button':
- locked ? item.$ref.addClass('disabled') : item.$ref.removeClass('disabled');
- break;
- case 'text':
- item.$ref.prop('readOnly', locked);
- break;
- }
- });
- }
- function lockSpreads(spreads, locked) {
- if (!locked) {
- return;
- }
- spreads.forEach(spread => {
- spread.unbind(GC.Spread.Sheets.Events.ButtonClicked);
- const sheetCount = spread.getSheetCount();
- for (let i = 0; i < sheetCount; i++) {
- const sheet = spread.getSheet(i);
- sheet.unbind(GC.Spread.Sheets.Events.ButtonClicked);
- sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
- sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
- sheet.unbind(GC.Spread.Sheets.Events.RangeChanged);
- sheet.unbind(GC.Spread.Sheets.Events.ClipboardChanging);
- sheet.unbind(GC.Spread.Sheets.Events.ClipboardChanged);
- sheet.unbind(GC.Spread.Sheets.Events.CellDoubleClick);
- sheet.unbind(GC.Spread.Sheets.Events.CellClick);
- sheet.unbind(GC.Spread.Sheets.Events.ValueChanged);
- sheet.suspendPaint();
- sheet.suspendEvent();
- sheet.options.isProtected = true;
- const rowCount = sheet.getRowCount();
- const colCount = sheet.getColumnCount();
- for (let row = 0; row < rowCount; row++) {
- for (let col = 0; col < colCount; col++) {
- sheet.getCell(row, col).locked(true);
- }
- }
- sheet.resumePaint();
- sheet.resumeEvent();
- }
- });
- }
- function unLockSpreads(spreads) {
- spreads.forEach(spread => {
- spread.unbind(GC.Spread.Sheets.Events.ButtonClicked);
- const sheetCount = spread.getSheetCount();
- for (let i = 0; i < sheetCount; i++) {
- const sheet = spread.getSheet(i);
- sheet.suspendPaint();
- sheet.suspendEvent();
- sheet.options.isProtected = false;
- const rowCount = sheet.getRowCount();
- const colCount = sheet.getColumnCount();
- for (let row = 0; row < rowCount; row++) {
- for (let col = 0; col < colCount; col++) {
- sheet.getCell(row, col).locked(false);
- }
- }
- sheet.resumePaint();
- sheet.resumeEvent();
- }
- });
- }
- function lockURL(locked, $url) {
- const originURL = $url.prop('href');
- const originLocked = !locked;
- const reg = new RegExp(`locked=${originLocked}`);
- const curURL = reg.test(originURL) ? originURL.replace(reg, `locked=${locked}`) : `${originURL}&locked=${locked}`;
- $url.prop('href', curURL);
- }
- function displayLock($lock, locked) {
- $lock.data('locked', locked);
- const innerHtml = locked ? '<i class="fa fa-unlock-alt"></i>' : '<i class="fa fa-lock"></i>';
- $lock.html(innerHtml);
- const title = locked ? '解锁' : '锁定';
- $lock.prop('title', title);
- }
- // 库列表页面,锁定按钮点击操作
- function handleLockClick($lock) {
- const curLocked = !$lock.data().locked;
- displayLock($lock, curLocked);
- const $url = $lock.parent().parent().children(':first-child').children(':first-child');
- lockURL(curLocked, $url);
- const $range = $lock.parent().parent();
- lockTools($range, curLocked);
- }
- function lockSpreadsAndTools(spreads, $range, locked) {
- if (!locked) {
- return;
- }
- lockSpreads(spreads, locked);
- lockTools($range, locked);
- }
- return {
- getLocked,
- lockTools,
- lockSpreads,
- unLockSpreads,
- lockURL,
- displayLock,
- handleLockClick,
- lockSpreadsAndTools
- }
- })();
|