Blame view

online-office/src/controllers/formulaBar.js 11.5 KB
wangtao authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
import menuButton from './menuButton';
import {luckysheetupdateCell} from './updateCell';
import { keycode } from './constant';
import { 
    luckysheetMoveHighlightCell,
} from './sheetMove';

import insertFormula from './insertFormula';
import { 
    rowLocation, 
    colLocation, 
    mouseposition 
} from '../global/location';
import { isEditMode } from '../global/validate';
import formula from '../global/formula';
import tooltip from '../global/tooltip';
import locale from '../locale/locale';
import Store from '../store';

export function formulaBarInitial(){
    //公式栏处理

    const _locale = locale();
    const locale_formula= _locale.formula;

    $("#luckysheet-functionbox-cell").focus(function () {
        if(isEditMode()){//此模式下禁用公式栏
            return;
        }

        if(Store.luckysheet_select_save.length > 0){
            let last = Store.luckysheet_select_save[Store.luckysheet_select_save.length - 1];

            let row_index = last["row_focus"], col_index = last["column_focus"];
            
            // let $input = $("#luckysheet-rich-text-editor"),value = $input.text();
            // if(value) {
            //     formula.updatecell(row_index, col_index);
            // }
            luckysheetupdateCell(row_index, col_index, Store.flowdata, null, true);
            formula.rangeResizeTo = $("#luckysheet-functionbox-cell");
        }
    }).keydown(function (event) {
        if(isEditMode()){//此模式下禁用公式栏
            return;
        }

        let ctrlKey = event.ctrlKey;
        let altKey = event.altKey;
        let shiftKey = event.shiftKey;
        let kcode = event.keyCode;
        let $inputbox = $("#luckysheet-input-box");

        if (kcode == keycode.ENTER && parseInt($inputbox.css("top")) > 0) {
            if ($("#luckysheet-formula-search-c").is(":visible") && formula.searchFunctionCell != null) {
                formula.searchFunctionEnter($("#luckysheet-formula-search-c").find(".luckysheet-formula-search-item-active"));
            }
            else {
                formula.updatecell(Store.luckysheetCellUpdate[0], Store.luckysheetCellUpdate[1]);
                Store.luckysheet_select_save = [{ "row": [Store.luckysheetCellUpdate[0], Store.luckysheetCellUpdate[0]], "column": [Store.luckysheetCellUpdate[1], Store.luckysheetCellUpdate[1]], "row_focus": Store.luckysheetCellUpdate[0], "column_focus": Store.luckysheetCellUpdate[1] }];
                luckysheetMoveHighlightCell("down", 1, "rangeOfSelect");
                //$("#luckysheet-functionbox-cell").blur();
                $("#luckysheet-rich-text-editor").focus();
            }
            event.preventDefault();
        }
        else if (kcode == keycode.ESC && parseInt($inputbox.css("top")) > 0) {
            formula.dontupdate();
            luckysheetMoveHighlightCell("down", 0, "rangeOfSelect");
            //$("#luckysheet-functionbox-cell").blur();
            $("#luckysheet-rich-text-editor").focus();
            event.preventDefault();
        }
        else if (kcode == keycode.F4 && parseInt($inputbox.css("top")) > 0) {
            formula.setfreezonFuc(event);
            event.preventDefault();
        }
        else if (kcode == keycode.UP && parseInt($inputbox.css("top")) > 0) {
            if ($("#luckysheet-formula-search-c").is(":visible")) {
                let $up = $("#luckysheet-formula-search-c").find(".luckysheet-formula-search-item-active").prev();
                if ($up.length == 0) {
                    $up = $("#luckysheet-formula-search-c").find(".luckysheet-formula-search-item").last();
                }
                $("#luckysheet-formula-search-c").find(".luckysheet-formula-search-item").removeClass("luckysheet-formula-search-item-active");
                $up.addClass("luckysheet-formula-search-item-active");
                event.preventDefault();
            }
        }
        else if (kcode == keycode.DOWN && parseInt($inputbox.css("top")) > 0) {
            if ($("#luckysheet-formula-search-c").is(":visible")) {
                let $up = $("#luckysheet-formula-search-c").find(".luckysheet-formula-search-item-active").next();
                if ($up.length == 0) {
                    $up = $("#luckysheet-formula-search-c").find(".luckysheet-formula-search-item").first();
                }
                $("#luckysheet-formula-search-c").find(".luckysheet-formula-search-item").removeClass("luckysheet-formula-search-item-active");
                $up.addClass("luckysheet-formula-search-item-active");
                event.preventDefault();
            }
        }
        else if (kcode == keycode.LEFT && parseInt($inputbox.css("top")) > 0) {
            formula.rangeHightlightselected($("#luckysheet-functionbox-cell"));
        }
        else if (kcode == keycode.RIGHT && parseInt($inputbox.css("top")) > 0) {
            formula.rangeHightlightselected($("#luckysheet-functionbox-cell"));
        }
        else if (!((kcode >= 112 && kcode <= 123) || kcode <= 46 || kcode == 144 || kcode == 108 || event.ctrlKey || event.altKey || (event.shiftKey && (kcode == 37 || kcode == 38 || kcode == 39 || kcode == 40))) || kcode == 8 || kcode == 32 || kcode == 46 || (event.ctrlKey && kcode == 86)) {
            formula.functionInputHanddler($("#luckysheet-rich-text-editor"), $("#luckysheet-functionbox-cell"), kcode);
        }
    }).click(function () {
        if(isEditMode()){//此模式下禁用公式栏
            return;
        }

        formula.rangeHightlightselected($("#luckysheet-functionbox-cell"));
    });

    //公式栏 取消(X)按钮
    $("#luckysheet-wa-functionbox-cancel").click(function () {
        if (!$(this).hasClass("luckysheet-wa-calculate-active")) {
            return;
        }
        //若有参数弹出框,隐藏
        if($("#luckysheet-search-formula-parm").is(":visible")){
            $("#luckysheet-search-formula-parm").hide();
        }
        //若有参数选取范围弹出框,隐藏
        if($("#luckysheet-search-formula-parm-select").is(":visible")){
            $("#luckysheet-search-formula-parm-select").hide();
        }

        formula.dontupdate();
        luckysheetMoveHighlightCell("down", 0, "rangeOfSelect");
    });

    //公式栏 确认()按钮
    $("#luckysheet-wa-functionbox-confirm").click(function () {
        if (!$(this).hasClass("luckysheet-wa-calculate-active")) {
            return;
        }
        //若有参数弹出框,隐藏
        if($("#luckysheet-search-formula-parm").is(":visible")){
            $("#luckysheet-search-formula-parm").hide();
        }
        //若有参数选取范围弹出框,隐藏
        if($("#luckysheet-search-formula-parm-select").is(":visible")){
            $("#luckysheet-search-formula-parm-select").hide();
        }

        formula.updatecell(Store.luckysheetCellUpdate[0], Store.luckysheetCellUpdate[1]);
        luckysheetMoveHighlightCell("down", 0, "rangeOfSelect");
    });

    //公式栏 fx按钮
    $("#luckysheet-wa-functionbox-fx").click(function () {
        //点击函数查找弹出框
        if(Store.luckysheet_select_save.length == 0){
            if(isEditMode()){
                alert(locale_formula.tipSelectCell);
            }
            else{
                tooltip.info(locale_formula.tipSelectCell,"");
            }

            return;
        }

        let last = Store.luckysheet_select_save[Store.luckysheet_select_save.length - 1];

        let row_index = last["row_focus"], col_index = last["column_focus"];

        luckysheetupdateCell(row_index, col_index, Store.flowdata);
        
        let cell = Store.flowdata[row_index][col_index];
        if(cell != null && cell.f != null){
            //单元格有计算
            let functionStr = formula.getfunctionParam(cell.f);
            if(functionStr.fn != null){
                //有函数公式
                insertFormula.formulaParmDialog(functionStr.fn, functionStr.param);
            }
            else{
                //无函数公式
                insertFormula.formulaListDialog();
            }
        }
        else{
            //单元格无计算
            $("#luckysheet-rich-text-editor").html('<span dir="auto" class="luckysheet-formula-text-color">=</span>');
            $("#luckysheet-functionbox-cell").html($("#luckysheet-rich-text-editor").html());
            insertFormula.formulaListDialog();
        }

        insertFormula.init();
    });

    //公式选区操作
    $("#luckysheet-formula-functionrange").on("mousedown", ".luckysheet-copy", function (event) {
        formula.rangeMove = true;
        Store.luckysheet_scroll_status = true;
        formula.rangeMoveObj = $(this).parent();
        formula.rangeMoveIndex = $(this).parent().attr("rangeindex");
        
        let mouse = mouseposition(event.pageX, event.pageY);
        let x = mouse[0] + $("#luckysheet-cell-main").scrollLeft();
        let y = mouse[1] + $("#luckysheet-cell-main").scrollTop();
        $("#luckysheet-formula-functionrange-highlight-" + formula.rangeMoveIndex).find(".luckysheet-selection-copy-hc").css("opacity", 0.13);
        
        let type = $(this).data("type");
        if (type == "top") {
            y += 3;
        }
        else if (type == "right") {
            x -= 3;
        }
        else if (type == "bottom") {
            y -= 3;
        }
        else if (type == "left") {
            x += 3;
        }

        let row_index = rowLocation(y)[2];
        let col_index = colLocation(x)[2];

        formula.rangeMovexy = [row_index, col_index];
        $("#luckysheet-sheettable").css("cursor", "move");
        event.stopPropagation();
    });

    $("#luckysheet-formula-functionrange").on("mousedown", ".luckysheet-highlight", function (event) {
        formula.rangeResize = $(this).data("type");//开始状态resize
        formula.rangeResizeIndex = $(this).parent().attr("rangeindex");
        
        let mouse = mouseposition(event.pageX, event.pageY), 
            scrollLeft = $("#luckysheet-cell-main").scrollLeft(), 
            scrollTop = $("#luckysheet-cell-main").scrollTop();
        let x = mouse[0] + scrollLeft;
        let y = mouse[1] + scrollTop;
        formula.rangeResizeObj = $(this).parent();
        $("#luckysheet-formula-functionrange-highlight-" + formula.rangeResizeIndex).find(".luckysheet-selection-copy-hc").css("opacity", 0.13);
        
        if (formula.rangeResize == "lt") {
            x += 3;
            y += 3;
        }
        else if (formula.rangeResize == "lb") {
            x += 3;
            y -= 3;
        }
        else if (formula.rangeResize == "rt") {
            x -= 3;
            y += 3;
        }
        else if (formula.rangeResize == "rb") {
            x -= 3;
            y -= 3;
        }

        let row_location = rowLocation(y), 
            row = row_location[1], 
            row_pre = row_location[0], 
            row_index = row_location[2];
        let col_location = colLocation(x), 
            col = col_location[1], 
            col_pre = col_location[0], 
            col_index = col_location[2];

        let position = formula.rangeResizeObj.position();
        formula.rangeResizexy = [
            col_pre, 
            row_pre, 
            formula.rangeResizeObj.width(), 
            formula.rangeResizeObj.height(), 
            position.left + scrollLeft, 
            position.top + scrollTop, col, row
        ];
        formula.rangeResizeWinH = $("#luckysheet-cell-main")[0].scrollHeight;
        formula.rangeResizeWinW = $("#luckysheet-cell-main")[0].scrollWidth;
        Store.luckysheet_scroll_status = true;
        event.stopPropagation();
    });
}