import Store from '../store'; import locale from '../locale/locale'; import { modelHTML } from './constant'; import { getSheetIndex } from '../methods/get'; import { setluckysheet_scroll_status } from '../methods/set'; import sheetmanage from './sheetmanage'; import luckysheetsizeauto from './resize'; import dataVerificationCtrl from './dataVerificationCtrl'; import { replaceHtml,transformRangeToAbsolute,openSelfModel } from '../utils/util'; import { selectionCopyShow } from './select'; import tooltip from '../global/tooltip'; import cleargridelement from '../global/cleargridelement'; let isInitialProtection = false, isInitialProtectionAddRang = false, rangeItemListCache=[], isAddRangeItemState=true, updateRangeItemIndex = null, validationAuthority=null, updatingSheetFile=null, firstInputSheetProtectionPassword = true; let sqrefMapCache = {}, inputRangeProtectionPassword = {}, initialRangePasswordHtml=false; const authorityItemArr = [ "selectLockedCells", "selectunLockedCells", "formatCells", "formatColumns", "formatRows", "insertColumns", "insertRows", "insertHyperlinks", "deleteColumns", "deleteRows", "sort", "filter", "usePivotTablereports", "editObjects", "editScenarios" ] function addRangeItem(item){ const _locale = locale(); const local_protection = _locale.protection; const locale_button = _locale.button; let title = item.name, sqref = item.sqref, password = item.password; let passwordTxt = ""; if(password!=null && password.length>0){ passwordTxt = ''; } let rangeItemTemplate = `
${title}${passwordTxt}
${sqref}
`; $("#luckysheet-protection-rangeItem-container").append(rangeItemTemplate); } function initialEvent(file){ const _locale = locale(); const local_protection = _locale.protection; const locale_button = _locale.button; //confirm protection $("#luckysheet-slider-protection-ok").click(function(){ let password = $("#protection-password").val(); let sheet = $("#protection-swichProtectionState").is(":checked"); let hint = $("#protection-hint").val(); let file = updatingSheetFile, aut = {}; if(file!=null && file.config!=null && file.config.authority!=null){ aut = file.config.authority; } let authorityData = { } let algorithmName = "None"; if(password!="••••••••"){ authorityData.password = password; authorityData.algorithmName = "None"; authorityData.saltValue = null; } else if(aut!=null){ authorityData.algorithmName = aut.algorithmName; authorityData.saltValue = aut.saltValue; authorityData.password = aut.password; } else { authorityData.algorithmName = "None"; authorityData.saltValue = null; authorityData.password = ""; } authorityData.hintText = hint; authorityData.sheet = sheet==true?1:0; for(let i=0;i div.luckysheet-protection-rangeItem").index($rangeItem); let item = rangeItemListCache[index]; updateRangeItemIndex = index; $("#protection-allowRangeAdd-title").val(item.name); $("#protection-allowRangeAdd-range input").val(item.sqref); if(item.algorithmName=="None"){ $("#protection-allowRangeAdd-password").val(item.password); } else{ $("#protection-allowRangeAdd-password").val("••••••••"); } $("#protection-allowRangeAdd-hint").val(item.hintText); }); //delete allow edit range $(document).off("click.luckysheetProtection.rangeItemDelete").on("click.luckysheetProtection.rangeItemDelete","#luckysheet-protection-rangeItem-container .luckysheet-protection-rangeItem-del", function(e){ let $rangeItem = $(e.target).closest(".luckysheet-protection-rangeItem"); let $rangeItemContainer = $("#luckysheet-protection-rangeItem-container"); let index = $rangeItemContainer.find("> div.luckysheet-protection-rangeItem").index($rangeItem); let item = rangeItemListCache[index]; rangeItemListCache.splice(index, 1); $rangeItem.remove(); }); //confirm allow edit range $(document).off("click.luckysheetProtection.rangeItemConfirm").on("click.luckysheetProtection.rangeItemConfirm","#luckysheet-protection-rangeItem-confirm", function(){ let name = $("#protection-allowRangeAdd-title").val(), rangeText = $("#protection-allowRangeAdd-range input").val(), password = $("#protection-allowRangeAdd-password").val(), hint = $("#protection-allowRangeAdd-hint").val(); if(name.length==0){ alert(local_protection.rangeItemErrorTitleNull); return; } let range = dataVerificationCtrl.getRangeByTxt(rangeText); if(rangeText.length==0){ alert(local_protection.rangeItemErrorRangeNull); return; } if(range.length==0){ alert(local_protection.rangeItemErrorRange); return; } rangeText = transformRangeToAbsolute(rangeText); if(isAddRangeItemState){ let item = { name:name, password:password, hintText:hint, algorithmName:"None",//MD2,MD4,MD5,RIPEMD-128,RIPEMD-160,SHA-1,SHA-256,SHA-384,SHA-512,WHIRLPOOL saltValue:null, checkRangePasswordUrl:null, sqref:rangeText } addRangeItem(item); rangeItemListCache.push(item); } else{ let index = updateRangeItemIndex; let item = rangeItemListCache[index]; item.name = name; item.sqref = rangeText; item.hintText = hint; if(password!="••••••••"){ item.password = password; item.algorithmName = "None"; } let $rangeItemContainer = $("#luckysheet-protection-rangeItem-container"); let $rangeitem = $rangeItemContainer.find("> div.luckysheet-protection-rangeItem").eq(index); let $name = $rangeitem.find(".luckysheet-protection-rangeItem-name"); let passwordTxt = ""; if(password!=null && password.length>0){ passwordTxt = ''; } $name.html(name+passwordTxt).attr("title",name); let $range = $rangeitem.find(".luckysheet-protection-rangeItem-range"); $range.html(rangeText).attr("title",rangeText); } $("#luckysheet-protection-rangeItem-dialog").hide(); $("#luckysheet-modal-dialog-mask").hide(); }); //sheet validation check passWord $(document).off("click.luckysheetProtection.validationConfirm").on("click.luckysheetProtection.validationConfirm","#luckysheet-protection-sheet-validation-confirm", function(e){ let $validation = $("#luckysheet-protection-sheet-validation"); let aut = validationAuthority; if(aut==null){ restoreProtectionConfig(validationAuthority); $validation.hide(); $("#luckysheet-modal-dialog-mask").hide(); $("#luckysheet-modal-dialog-slider-protection").show(); luckysheetsizeauto(); return; } let $input = $validation.find("input"); let password = $input.val(); if(password==null || password.length==0){ alert(local_protection.checkPasswordNullalert); return; } if(aut.algorithmName!=null && aut.algorithmName!="None"){ if(aut.saltValue!=null && aut.saltValue.length>0){ var hasher = CryptoApi.getHasher(aut.algorithmName); password =CryptoApi.hmac(aut.saltValue, password, hasher); } else{ password = CryptoApi.hash(aut.algorithmName, password); } } if(password==aut.password){ restoreProtectionConfig(validationAuthority); $validation.hide(); $("#luckysheet-modal-dialog-mask").hide(); $("#luckysheet-modal-dialog-slider-protection").show(); luckysheetsizeauto(); firstInputSheetProtectionPassword = false; } else{ alert(local_protection.checkPasswordWrongalert); } }); $("#luckysheet-protection-check-selectLockedCells").change(function() { let $selectLockedCells = $("#luckysheet-protection-check-selectLockedCells"), $selectunLockedCells = $("#luckysheet-protection-check-selectunLockedCells"); let selectLockedCellsChecked = $selectLockedCells.is(":checked"), selectunLockedCellsChecked = $selectunLockedCells.is(":checked"); if(selectLockedCellsChecked){ $selectunLockedCells.prop('checked', true); } }); $("#luckysheet-protection-check-selectunLockedCells").change(function() { let $selectLockedCells = $("#luckysheet-protection-check-selectLockedCells"), $selectunLockedCells = $("#luckysheet-protection-check-selectunLockedCells"); let selectLockedCellsChecked = $selectLockedCells.is(":checked"), selectunLockedCellsChecked = $selectunLockedCells.is(":checked"); if(!selectunLockedCellsChecked){ $selectLockedCells.prop('checked', false); } }); //Cell range select controll $(document).off("click.luckysheetProtection.dvRange").on("click.luckysheetProtection.dvRange", "#protection-allowRangeAdd-range .fa-table", function(e) { $("#luckysheet-protection-rangeItem-dialog").hide(); let dataSource = "0"; let txt = $(this).siblings("input").val().trim(); dataVerificationCtrl.rangeDialog(dataSource, txt); dataVerificationCtrl.selectRange = []; let range = dataVerificationCtrl.getRangeByTxt(txt); if(range.length > 0){ for(let s = 0; s < range.length; s++){ let r1 = range[s].row[0], r2 = range[s].row[1]; let c1 = range[s].column[0], c2 = range[s].column[1]; let row = Store.visibledatarow[r2], row_pre = r1 - 1 == -1 ? 0 : Store.visibledatarow[r1 - 1]; let col = Store.visibledatacolumn[c2], col_pre = c1 - 1 == -1 ? 0 : Store.visibledatacolumn[c1 - 1]; dataVerificationCtrl.selectRange.push({ "left": col_pre, "width": col - col_pre - 1, "top": row_pre, "height": row - row_pre - 1, "left_move": col_pre, "width_move": col - col_pre - 1, "top_move": row_pre, "height_move": row - row_pre - 1, "row": [r1, r2], "column": [c1, c2], "row_focus": r1, "column_focus": c1 }); } } selectionCopyShow(dataVerificationCtrl.selectRange); }); $(document).off("click.luckysheetProtection.dvRange2").on("click.luckysheetProtection.dvRange2", "#luckysheet-protection-rangeItem-dialog .show-box-item-dropdown .range .fa-table", function(e) { $("#luckysheet-protection-rangeItem-dialog").hide(); let dataSource = "1"; let txt = $(this).siblings("input").val().trim(); dataVerificationCtrl.rangeDialog(dataSource, txt); dataVerificationCtrl.selectRange = []; let range = dataVerificationCtrl.getRangeByTxt(txt); if(range.length > 0){ for(let s = 0; s < range.length; s++){ let r1 = range[s].row[0], r2 = range[s].row[1]; let c1 = range[s].column[0], c2 = range[s].column[1]; let row = Store.visibledatarow[r2], row_pre = r1 - 1 == -1 ? 0 : Store.visibledatarow[r1 - 1]; let col = Store.visibledatacolumn[c2], col_pre = c1 - 1 == -1 ? 0 : Store.visibledatacolumn[c1 - 1]; dataVerificationCtrl.selectRange.push({ "left": col_pre, "width": col - col_pre - 1, "top": row_pre, "height": row - row_pre - 1, "left_move": col_pre, "width_move": col - col_pre - 1, "top_move": row_pre, "height_move": row - row_pre - 1, "row": [r1, r2], "column": [c1, c2], "row_focus": r1, "column_focus": c1 }); } } selectionCopyShow(dataVerificationCtrl.selectRange); }); $(document).off("click.luckysheetProtection.dvRangeConfirm").on("click.luckysheetProtection.dvRangeConfirm", "#luckysheet-dataVerificationRange-dialog-confirm", function(e) { let txt = $(this).parents("#luckysheet-dataVerificationRange-dialog").find("input").val(); let $input = $("#protection-allowRangeAdd-range input"), inputValue = $input.val(); if(inputValue.substr(inputValue.length-1, 1)==","){ $input.val(inputValue + txt); } else{ $input.val(txt); } $("#luckysheet-dataVerificationRange-dialog").hide(); $("#luckysheet-modal-dialog-mask").show(); $("#luckysheet-protection-rangeItem-dialog").show(); let range = []; selectionCopyShow(range); }); $(document).off("click.luckysheetProtection.dvRangeClose").on("click.dvRangeClose", "#luckysheet-dataVerificationRange-dialog-close", function(e) { $("#luckysheet-dataVerificationRange-dialog").hide(); $("#luckysheet-modal-dialog-mask").show(); $("#luckysheet-protection-rangeItem-dialog").show(); let range = []; selectionCopyShow(range); }); $(document).on("click.luckysheetProtection.luckysheetProtection", "#luckysheet-dataVerificationRange-dialog .luckysheet-modal-dialog-title-close", function(e) { $("#luckysheet-dataVerificationRange-dialog").hide(); $("#luckysheet-modal-dialog-mask").show(); $("#luckysheet-protection-rangeItem-dialog").show(); let range = []; selectionCopyShow(range); }); } //protect range config function initialProtectionRangeModal(file){ if(isInitialProtectionAddRang){ return; } isInitialProtectionAddRang = true; let _locale = locale(); let local_protection = _locale.protection; const locale_button = _locale.button; $("body").append(replaceHtml(modelHTML, { "id": "luckysheet-protection-rangeItem-dialog", "addclass": "luckysheet-protection-rangeItem-dialog", "title": local_protection.allowRangeTitle, "content": `
${local_protection.allowRangeAddTitle}
${local_protection.allowRangeAddSqrf}
${local_protection.allowRangeAddTitlePassword}
${local_protection.allowRangeAddTitleHint}
`, "botton": ` `, "style": "z-index:100003" })); } //Protect sheet initial function initialProtectionRIghtBar(file){ const _locale = locale(); const local_protection = _locale.protection; const locale_button = _locale.button; let authorityItemHtml = ""; for(let i=0;i
`; } const protectionModalHtml = ` `; $("body").append(protectionModalHtml); //Password input initial for sheet Protection $("body").append(replaceHtml(modelHTML, { "id": "luckysheet-protection-sheet-validation", "addclass": "luckysheet-protection-sheet-validation", "title": local_protection.validationTitle, "content": `
${local_protection.validationTips}
`, "botton": ` `, "style": "z-index:100003" })); } function restoreProtectionConfig(aut){ if(aut==null){ aut = {}; } for(let i=0;i0){ if(aut.algorithmName=="None" || aut.algorithmName==null){ $("#protection-password").val(aut.password); } else{ $("#protection-password").val("••••••••"); } } else{ $("#protection-password").val(""); } let sheet = aut.sheet; if(aut.sheet==null){ sheet = 0; } $("#protection-swichProtectionState").prop('checked',sheet==1?true:false); let hintText = aut.hintText; if(hintText==null){ hintText = ""; } $("#protection-hint").val(hintText); rangeItemListCache = []; $("#luckysheet-protection-rangeItem-container").empty(); let allowRangeList = aut.allowRangeList; if(allowRangeList!=null && allowRangeList.length>0){ for(let i=0;i0){ validationAuthority = aut; $("#luckysheet-protection-sheet-validation input").val(""); openSelfModel("luckysheet-protection-sheet-validation"); return; } else{//retore protection config restoreProtectionConfig(aut); } } else{//protection initial config $("#luckysheet-protection-check-selectLockedCells").prop('checked',true); $("#luckysheet-protection-check-selectunLockedCells").prop('checked',true); } $("#luckysheet-modal-dialog-slider-protection").show(); luckysheetsizeauto(); } export function closeProtectionModal(){ $("#luckysheet-protection-rangeItem-dialog").hide(); $("#luckysheet-modal-dialog-slider-protection").hide(); luckysheetsizeauto(); } function checkProtectionLockedSqref(r, c, aut, local_protection, isOpenAlert=true, isLock=true){ let isPass = false; let rangeAut = aut.allowRangeList; if(rangeAut!=null && rangeAut.length>0){ let isExists = false; for(let i=0;i 0){ for(let s = 0; s < range.length; s++){ let r1 = range[s].row[0], r2 = range[s].row[1]; let c1 = range[s].column[0], c2 = range[s].column[1]; if(r>=r1 && r<=r2 && c>=c1 && c<=c2){ isExists = true; break; } } } if(isExists){ let password = ra.password; if(password!=null && password.length>0 && !(sqref in inputRangeProtectionPassword)){ if(isOpenAlert){ openRangePasswordModal(ra); $("#luckysheet-selection-copy .luckysheet-selection-copy").hide(); } return false; } else{ isPass = true; } break; } } } if (!isPass && !isLock) isPass = true if(!isPass && isOpenAlert){ let ht; if(aut.hintText != null && aut.hintText.length>0){ ht = aut.hintText; } else{ ht = local_protection.defaultSheetHintText; } tooltip.info("", ht); $("#luckysheet-selection-copy .luckysheet-selection-copy").hide(); } return isPass; } function openRangePasswordModal(rangeAut) { const _locale = locale(); const local_protection = _locale.protection; const locale_button = _locale.button; if(!initialRangePasswordHtml){ //Password input initial for range $("body").append(replaceHtml(modelHTML, { "id": "luckysheet-protection-range-validation", "addclass": "luckysheet-protection-sheet-validation", "title": local_protection.validationTitle, "content": `
`, "botton": ` `, "style": "z-index:100003" })); } initialRangePasswordHtml = true; openSelfModel("luckysheet-protection-range-validation"); let $hint = $("#luckysheet-protection-range-validation-hint"); if(rangeAut.hintText != null && rangeAut.hintText.length>0){ $hint.html(rangeAut.hintText); } else{ $hint.html(local_protection.defaultRangeHintText); } let $rangeV = $("#luckysheet-protection-range-validation"); let $input = $rangeV.find("input"); $input.val(""); $("#luckysheet-protection-range-validation-confirm").off("click").on("click", function(){ let password = $input.val(); if(password==null || password.length==0){ alert(local_protection.checkPasswordNullalert); return; } if(rangeAut.algorithmName!=null && rangeAut.algorithmName!="None"){ // password = CryptoApi.hash(rangeAut.algorithmName, password); if(rangeAut.saltValue!=null && rangeAut.saltValue.length>0){ var hasher = CryptoApi.getHasher(rangeAut.algorithmName); password =CryptoApi.hmac(rangeAut.saltValue, password, hasher); } else{ password = CryptoApi.hash(rangeAut.algorithmName, password); } } if(password==rangeAut.password){ inputRangeProtectionPassword[rangeAut.sqref] = 1; $rangeV.hide(); $("#luckysheet-modal-dialog-mask").hide(); alert(local_protection.checkPasswordSucceedalert); } else{ alert(local_protection.checkPasswordWrongalert); } }); } //protection state export function checkProtectionNotEnable(sheetIndex){ let sheetFile = sheetmanage.getSheetByIndex(sheetIndex); if(sheetFile==null){ return true; } if(sheetFile.config==null || sheetFile.config.authority==null){ return true; } let aut = sheetFile.config.authority; if(aut==null || aut.sheet==null || aut.sheet==0 ){ return true; } const _locale = locale(); const local_protection = _locale.protection; let ht; if(aut.hintText != null && aut.hintText.length>0){ ht = aut.hintText; } else{ ht = local_protection.defaultSheetHintText; } tooltip.info("", ht); return false; } //cell locked state export function checkProtectionLocked(r, c, sheetIndex, isOpenAlert=true, isLock=true){ let sheetFile = sheetmanage.getSheetByIndex(sheetIndex); if(sheetFile==null){ return true; } if(sheetFile.config==null || sheetFile.config.authority==null){ return true; } let data=sheetFile.data, cell=data[r][c], aut = sheetFile.config.authority; if(aut==null || aut.sheet==null || aut.sheet==0 ){ return true; } if(cell && cell.lo === 0){ // lo为0的时候才是可编辑 return true; } const _locale = locale(); const local_protection = _locale.protection; return checkProtectionLockedSqref(r, c , aut, local_protection, isOpenAlert, isLock); } //cell hidden state export function checkProtectionCellHidden(r, c, sheetIndex){ let sheetFile = sheetmanage.getSheetByIndex(sheetIndex); if(!sheetFile || (sheetFile.data && !sheetFile.data[r]) || (sheetFile.data && !sheetFile.data[r][c])){ return true; } if(sheetFile.config==null || sheetFile.config.authority==null){ return true; } let data=sheetFile.data, cell=data[r][c], aut = sheetFile.config.authority; if(aut==null || aut.sheet==null || aut.sheet==0 ){ return true; } if(cell==null || cell.hi==null || cell.hi==0){ return true; } return false; } //cell range locked state export function checkProtectionLockedRangeList(rangeList, sheetIndex){ let sheetFile = sheetmanage.getSheetByIndex(sheetIndex); if(sheetFile==null){ return true; } if(sheetFile.config==null || sheetFile.config.authority==null){ return true; } let aut = sheetFile.config.authority; if(aut==null || aut.sheet==null || aut.sheet==0 ){ return true; } if(rangeList==null || rangeList.length==0){ return true; } const _locale = locale(); const local_protection = _locale.protection; for(let s = 0; s < rangeList.length; s++){ let r1 = rangeList[s].row[0], r2 = rangeList[s].row[1]; let c1 = rangeList[s].column[0], c2 = rangeList[s].column[1]; for(let r=r1;r<=r2;r++){ for(let c=c1;c<=c2;c++){ const cell = sheetFile.data[r][c] || {} let isLock = cell.lo === undefined || cell.lo === 1, // 单元格是否锁定 isPass = checkProtectionLockedSqref(r, c , aut, local_protection, true, isLock); if(!isPass){ return false; } } } } return true; } //selectLockedCells , selectunLockedCells and cell state export function checkProtectionSelectLockedOrUnLockedCells(r, c, sheetIndex){ const _locale = locale(); const local_protection = _locale.protection; let sheetFile = sheetmanage.getSheetByIndex(sheetIndex); if(sheetFile==null){ return true; } if(sheetFile.config==null || sheetFile.config.authority==null){ return true; } let data=sheetFile.data, cell=data[r][c], aut = sheetFile.config.authority; if(aut==null || aut.sheet==null || aut.sheet==0 ){ return true; } if(cell && cell.lo === 0){ // lo为0的时候才是可编辑 if(aut.selectunLockedCells==1 || aut.selectunLockedCells==null){ return true; } else{ return false; } } else{//locked?? let isAllEdit = checkProtectionLockedSqref(r, c , aut, local_protection, false);//dont alert password model if(isAllEdit){//unlocked if(aut.selectunLockedCells==1 || aut.selectunLockedCells==null){ return true; } else{ return false; } } else{//locked if(aut.selectLockedCells==1 || aut.selectLockedCells==null){ return true; } else{ return false; } } } } //selectLockedCells or selectunLockedCells authority, highlight cell export function checkProtectionAllSelected(sheetIndex){ const _locale = locale(); const local_protection = _locale.protection; let sheetFile = sheetmanage.getSheetByIndex(sheetIndex); if(sheetFile==null){ return true; } if(sheetFile.config==null || sheetFile.config.authority==null){ return true; } let aut = sheetFile.config.authority; if(aut==null || aut.sheet==null || aut.sheet==0 ){ return true; } let selectunLockedCells = false; if(aut.selectunLockedCells==1 || aut.selectunLockedCells==null){ selectunLockedCells = true; } let selectLockedCells = false; if(aut.selectLockedCells==1 || aut.selectLockedCells==null){ selectLockedCells = true; } if(selectunLockedCells && selectLockedCells){ return true; } return false; } //formatCells authority, bl cl fc fz ff ct border etc. export function checkProtectionFormatCells(sheetIndex){ let sheetFile = sheetmanage.getSheetByIndex(sheetIndex); if(sheetFile==null){ return true; } if(sheetFile.config==null || sheetFile.config.authority==null){ return true; } let aut = sheetFile.config.authority; if(aut==null || aut.sheet==null || aut.sheet==0 ){ return true; } if(aut.formatCells==1 || aut.formatCells==null){ return true; } const _locale = locale(); const local_protection = _locale.protection; let ht; if(aut.hintText != null && aut.hintText.length>0){ ht = aut.hintText; } else{ ht = local_protection.defaultSheetHintText; } tooltip.info("", ht); return false; } //formatColumns authority: controll column hidden and width //formatRows authority: controll row hidden and height //insertColumns authority //insertRows authority //insertHyperlinks authority:Hyperlinks is not incomplete //deleteColumns authority //deleteRows authority //sort authority //filter authority //usePivotTablereports authority //editObjects authority: insert,delete,update for image, chart, comment,shape etc. //editScenarios authority: Scenarios features is uncompleted export function checkProtectionAuthorityNormal(sheetIndex, type="formatColumns", isAlert=true){ let sheetFile = sheetmanage.getSheetByIndex(sheetIndex); if(sheetFile==null){ return true; } if(sheetFile.config==null || sheetFile.config.authority==null){ return true; } let aut = sheetFile.config.authority; if(aut==null || aut.sheet==null || aut.sheet==0 ){ return true; } if(aut[type]==1 || aut[type]==null){ return true; } if(isAlert){ const _locale = locale(); const local_protection = _locale.protection; let ht; if(aut.hintText != null && aut.hintText.length>0){ ht = aut.hintText; } else{ ht = local_protection.defaultSheetHintText; } tooltip.info("", ht); } return false; }