{"version":3,"sources":["../../../lib/doc/defined-names.js"],"names":["_","require","colCache","CellMatrix","Range","rangeRegexp","DefinedNames","matrixMap","name","matrix","locStr","location","decodeEx","addEx","getMatrix","top","col","left","right","row","bottom","address","sheetName","n2l","addCellEx","removeEx","removeCellEx","each","callback","forEach","cell","addressStr","getNamesEx","map","findCellEx","filter","Boolean","mark","range","x","y","vGrow","yy","edge","c","findCellAt","hGrow","xx","cells","push","i","length","ranges","_explore","$shortRange","forEachInSheet","start","numDelete","numInsert","spliceRows","normaliseMatrix","spliceColumns","getRanges","definedName","value","rangeStr","test","split","pop","addCell","module","exports"],"mappings":"AAAA;;;;;;;;AAEA,IAAMA,CAAC,GAAGC,OAAO,CAAC,qBAAD,CAAjB;;AACA,IAAMC,QAAQ,GAAGD,OAAO,CAAC,oBAAD,CAAxB;;AACA,IAAME,UAAU,GAAGF,OAAO,CAAC,sBAAD,CAA1B;;AACA,IAAMG,KAAK,GAAGH,OAAO,CAAC,SAAD,CAArB;;AAEA,IAAMI,WAAW,GAAG,sCAApB;;IAEMC,Y;AACJ,0BAAc;AAAA;;AACZ,SAAKC,SAAL,GAAiB,EAAjB;AACD;;;;8BAESC,I,EAAM;AACd,UAAMC,MAAM,GAAG,KAAKF,SAAL,CAAeC,IAAf,MAAyB,KAAKD,SAAL,CAAeC,IAAf,IAAuB,IAAIL,UAAJ,EAAhD,CAAf;AACA,aAAOM,MAAP;AACD,K,CAED;;;;wBACIC,M,EAAQF,I,EAAM;AAChB,UAAMG,QAAQ,GAAGT,QAAQ,CAACU,QAAT,CAAkBF,MAAlB,CAAjB;AACA,WAAKG,KAAL,CAAWF,QAAX,EAAqBH,IAArB;AACD;;;0BAEKG,Q,EAAUH,I,EAAM;AACpB,UAAMC,MAAM,GAAG,KAAKK,SAAL,CAAeN,IAAf,CAAf;;AACA,UAAIG,QAAQ,CAACI,GAAb,EAAkB;AAChB,aAAK,IAAIC,GAAG,GAAGL,QAAQ,CAACM,IAAxB,EAA8BD,GAAG,IA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EAA9C,CAAJ,EAAuD;AACrD3D,YAAAA,MAAM,CAAC4D,OAAP,CAAeJ,QAAf;AACD;AACF,SAJD;AAKD,OAPD;AAQD;;;;;;AAGHK,MAAM,CAACC,OAAP,GAAiBjE,YAAjB","sourcesContent":["'use strict';\r\n\r\nconst _ = require('../utils/under-dash');\r\nconst colCache = require('../utils/col-cache');\r\nconst CellMatrix = require('../utils/cell-matrix');\r\nconst Range = require('./range');\r\n\r\nconst rangeRegexp = /[$](\\w+)[$](\\d+)(:[$](\\w+)[$](\\d+))?/;\r\n\r\nclass DefinedNames {\r\n  constructor() {\r\n    this.matrixMap = {};\r\n  }\r\n\r\n  getMatrix(name) {\r\n    const matrix = this.matrixMap[name] || (this.matrixMap[name] = new CellMatrix());\r\n    return matrix;\r\n  }\r\n\r\n  // add a name to a cell. locStr in the form SheetName!$col$row or SheetName!$c1$r1:$c2:$r2\r\n  add(locStr, name) {\r\n    const location = colCache.decodeEx(locStr);\r\n    this.addEx(location, name);\r\n  }\r\n\r\n  addEx(location, name) {\r\n    const matrix = this.getMatrix(name);\r\n    if (location.top) {\r\n      for (let col = location.left; col <= location.right; col++) {\r\n        for (let row = location.top; row <= location.bottom; row++) {\r\n          const address = {\r\n            sheetName: location.sheetName,\r\n            address: colCache.n2l(col) + row,\r\n            row,\r\n            col,\r\n          };\r\n\r\n          matrix.addCellEx(address);\r\n        }\r\n      }\r\n    } else {\r\n      matrix.addCellEx(location);\r\n    }\r\n  }\r\n\r\n  remove(locStr, name) {\r\n    const location = colCache.decodeEx(locStr);\r\n    this.removeEx(location, name);\r\n  }\r\n\r\n  removeEx(location, name) {\r\n    const matrix = this.getMatrix(name);\r\n    matrix.removeCellEx(location);\r\n  }\r\n\r\n  removeAllNames(location) {\r\n    _.each(this.matrixMap, matrix => {\r\n      matrix.removeCellEx(location);\r\n    });\r\n  }\r\n\r\n  forEach(callback) {\r\n    _.each(this.matrixMap, (matrix, name) => {\r\n      matrix.forEach(cell => {\r\n        callback(name, cell);\r\n      });\r\n    });\r\n  }\r\n\r\n  // get all the names of a cell\r\n  getNames(addressStr) {\r\n    return this.getNamesEx(colCache.decodeEx(addressStr));\r\n  }\r\n\r\n  getNamesEx(address) {\r\n    return _.map(this.matrixMap, (matrix, name) => matrix.findCellEx(address) && name).filter(\r\n      Boolean\r\n    );\r\n  }\r\n\r\n  _explore(matrix, cell) {\r\n    cell.mark = false;\r\n    const {sheetName} = cell;\r\n\r\n    const range = new Range(cell.row, cell.col, cell.row, cell.col, sheetName);\r\n    let x;\r\n    let y;\r\n\r\n    // grow vertical - only one col to worry about\r\n    function vGrow(yy, edge) {\r\n      const c = matrix.findCellAt(sheetName, yy, cell.col);\r\n      if (!c || !c.mark) {\r\n        return false;\r\n      }\r\n      range[edge] = yy;\r\n      c.mark = false;\r\n      return true;\r\n    }\r\n    for (y = cell.row - 1; vGrow(y, 'top'); y--);\r\n    for (y = cell.row + 1; vGrow(y, 'bottom'); y++);\r\n\r\n    // grow horizontal - ensure all rows can grow\r\n    function hGrow(xx, edge) {\r\n      const cells = [];\r\n      for (y = range.top; y <= range.bottom; y++) {\r\n        const c = matrix.findCellAt(sheetName, y, xx);\r\n        if (c && c.mark) {\r\n          cells.push(c);\r\n        } else {\r\n          return false;\r\n        }\r\n      }\r\n      range[edge] = xx;\r\n      for (let i = 0; i < cells.length; i++) {\r\n        cells[i].mark = false;\r\n      }\r\n      return true;\r\n    }\r\n    for (x = cell.col - 1; hGrow(x, 'left'); x--);\r\n    for (x = cell.col + 1; hGrow(x, 'right'); x++);\r\n\r\n    return range;\r\n  }\r\n\r\n  getRanges(name, matrix) {\r\n    matrix = matrix || this.matrixMap[name];\r\n\r\n    if (!matrix) {\r\n      return {name, ranges: []};\r\n    }\r\n\r\n    // mark and sweep!\r\n    matrix.forEach(cell => {\r\n      cell.mark = true;\r\n    });\r\n    const ranges = matrix\r\n      .map(cell => cell.mark && this._explore(matrix, cell))\r\n      .filter(Boolean)\r\n      .map(range => range.$shortRange);\r\n\r\n    return {\r\n      name,\r\n      ranges,\r\n    };\r\n  }\r\n\r\n  normaliseMatrix(matrix, sheetName) {\r\n    // some of the cells might have shifted on specified sheet\r\n    // need to reassign rows, cols\r\n    matrix.forEachInSheet(sheetName, (cell, row, col) => {\r\n      if (cell) {\r\n        if (cell.row !== row || cell.col !== col) {\r\n          cell.row = row;\r\n          cell.col = col;\r\n          cell.address = colCache.n2l(col) + row;\r\n        }\r\n      }\r\n    });\r\n  }\r\n\r\n  spliceRows(sheetName, start, numDelete, numInsert) {\r\n    _.each(this.matrixMap, matrix => {\r\n      matrix.spliceRows(sheetName, start, numDelete, numInsert);\r\n      this.normaliseMatrix(matrix, sheetName);\r\n    });\r\n  }\r\n\r\n  spliceColumns(sheetName, start, numDelete, numInsert) {\r\n    _.each(this.matrixMap, matrix => {\r\n      matrix.spliceColumns(sheetName, start, numDelete, numInsert);\r\n      this.normaliseMatrix(matrix, sheetName);\r\n    });\r\n  }\r\n\r\n  get model() {\r\n    // To get names per cell - just iterate over all names finding cells if they exist\r\n    return _.map(this.matrixMap, (matrix, name) => this.getRanges(name, matrix)).filter(\r\n      definedName => definedName.ranges.length\r\n    );\r\n  }\r\n\r\n  set model(value) {\r\n    // value is [ { name, ranges }, ... ]\r\n    const matrixMap = (this.matrixMap = {});\r\n    value.forEach(definedName => {\r\n      const matrix = (matrixMap[definedName.name] = new CellMatrix());\r\n      definedName.ranges.forEach(rangeStr => {\r\n        if (rangeRegexp.test(rangeStr.split('!').pop() || '')) {\r\n          matrix.addCell(rangeStr);\r\n        }\r\n      });\r\n    });\r\n  }\r\n}\r\n\r\nmodule.exports = DefinedNames;\r\n"],"file":"defined-names.js"}