123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- /**
- * Created by Mai on 2017/4/1.
- */
- var TREE_SHEET_CONTROLLER = {
- createNew: function (tree, sheet, setting, loadHeader = true) {
- var controller = function () {
- this.tree = tree;
- this.sheet = sheet;
- this.setting = setting;
- this.event = {
- refreshBaseActn: null,
- treeSelectedChanged: null
- };
- if (loadHeader) {
- TREE_SHEET_HELPER.loadSheetHeader(this.setting, this.sheet);
- }
- };
- controller.prototype.showTreeData = function () {
- var that = this;
- TREE_SHEET_HELPER.showTreeData(this.setting, this.sheet, this.tree);
- this.sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, function (e, info) {
- that.setTreeSelected(that.tree.items[info.newSelections[0].row]);
- });
- };
- controller.prototype.syncDisplayNewNodes = function (newNodes) {
- let me = this;
- TREE_SHEET_HELPER.massOperationSheet(me.sheet, function () {
- var sels = me.sheet.getSelections();
- newNodes.sort(function (a, b) {
- let rst = 0;
- if (a.serialNo() > b.serialNo()) {
- rst = 1;
- }
- else {
- rst = -1;
- }
- return rst;
- });
- for (let newNode of newNodes) {
- me.sheet.addRows(newNode.serialNo(), 1);
- }
- for (let newNode of newNodes) {
- me.setTreeSelected(newNode);
- TREE_SHEET_HELPER.refreshTreeNodeData(me.setting, me.sheet, [newNode], false);
- me.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
- }
- });
- };
- controller.prototype.insert = function () {
- var newNode = null, that = this, sels = this.sheet.getSelections();
- if (this.tree) {
- if (this.tree.selected) {
- newNode = this.tree.insert(this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
- } else {
- newNode = this.tree.insert();
- }
- if (newNode) {
- TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
- that.sheet.addRows(newNode.serialNo(), 1);
- TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
- that.setTreeSelected(newNode);
- that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
- //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
- });
- }
- }
- };
- controller.prototype.m_insert = function (datas) {
- let nodes = [], that = this, sels = this.sheet.getSelections();
- if (this.tree) {
- if (this.tree.selected) {
- nodes = this.tree.m_insert(datas, this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
- } else {
- nodes = this.tree.m_insert(datas);
- }
- }
- let length = nodes.length;
- if (nodes.length > 0) {
- TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
- that.sheet.addRows(nodes[length - 1].serialNo(), length);
- TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, false);
- that.setTreeSelected(nodes[length - 1]);
- that.sheet.setSelection(nodes[length - 1].serialNo(), sels[0].col, 1, 1);
- //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
- });
- }
- return nodes;
- };
- controller.prototype.insertByDatas = function (datas) {
- let nodes = [], that = this, sels = this.sheet.getSelections();
- if (this.tree) {
- nodes = this.tree.insertByDatas(datas);
- }
- let length = nodes.length;
- if (nodes.length > 0) {
- TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
- that.sheet.addRows(nodes[length - 1].serialNo(), length);
- TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, false);
- that.setTreeSelected(nodes[length - 1]);
- that.sheet.setSelection(nodes[length - 1].serialNo(), sels[0].col, 1, 1);
- //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
- });
- }
- return nodes;
- };
- controller.prototype.insertByDatas = function (datas) {
- let nodes = [], that = this, sels = this.sheet.getSelections();
- if (this.tree) {
- nodes = this.tree.insertByDatas(datas);
- }
- let length = nodes.length;
- if (nodes.length > 0) {
- TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
- that.sheet.addRows(nodes[length - 1].serialNo(), length);
- TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, false);
- that.setTreeSelected(nodes[length - 1]);
- that.sheet.setSelection(nodes[length - 1].serialNo(), sels[0].col, 1, 1);
- //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
- });
- }
- return nodes;
- };
- controller.prototype.insertByID = function (ID) {
- var newNode = null, that = this, sels = this.sheet.getSelections();
- if (this.tree) {
- if (this.tree.selected) {
- newNode = this.tree.insertByID(ID, this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
- } else {
- newNode = this.tree.insertByID(ID);
- }
- if (newNode) {
- TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
- that.sheet.addRows(newNode.serialNo(), 1);
- TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
- that.setTreeSelected(newNode);
- that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
- //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
- });
- }
- }
- return newNode;
- };
- controller.prototype.insertByIDS = function (ID, ParentID, NexSiblingID) {
- var newNode = null, that = this, sels = this.sheet.getSelections();
- if (this.tree) {
- if (this.tree.selected) {
- newNode = this.tree.insertByID(ID, ParentID, NexSiblingID);
- } else {
- newNode = this.tree.insertByID(ID);
- }
- if (newNode) {
- TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
- that.sheet.addRows(newNode.serialNo(), 1);
- TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
- that.setTreeSelected(newNode);
- that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
- //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
- });
- }
- }
- return newNode;
- };
- controller.prototype.insertToChild = function (ID, ParentID, NextSiblingID) {
- var newNode = null, that = this, sels = this.sheet.getSelections();
- if (this.tree) {
- if (this.tree.selected) {
- newNode = this.tree.insertByID(ID, ParentID, NextSiblingID);
- } else {
- newNode = this.tree.insertByID(ID);
- }
- if (newNode) {
- TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
- that.sheet.addRows(newNode.serialNo(), 1);
- TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
- that.setTreeSelected(newNode);
- that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
- //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
- });
- }
- }
- return newNode;
- };
- controller.prototype.delete = function () {
- var that = this, sels = this.sheet.getSelections();
- if (this.tree.selected) {
- if (this.tree.delete(this.tree.selected)) {
- TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
- that.sheet.deleteRows(sels[0].row, that.tree.selected.posterityCount() + 1);
- that.setTreeSelected(that.tree.items[sels[0].row] ? that.tree.items[sels[0].row] : that.tree.items[that.tree.items.length - 1]);
- });
- }
- }
- };
- controller.prototype.m_delete = function (nodes) {
- var that = this, sels = this.sheet.getSelections();
- if (nodes.length > 0) {
- if (this.tree.m_delete(nodes)) {
- TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
- let rowCount = 0;
- for (let node of nodes) {
- rowCount = rowCount + node.posterityCount() + 1;
- }
- that.sheet.deleteRows(sels[0].row, rowCount);
- const locateRow = that.tree.items.length <= sels[0].row ? that.tree.items.length - 1 : sels[0].row;
- that.setTreeSelected(that.tree.items[locateRow]);
- that.sheet.setSelection(locateRow, sels[0].col, 1, sels[0].colCount);
- });
- }
- }
- };
- controller.prototype.upLevel = function () {
- var that = this;
- if (this.tree.selected) {
- if (this.tree.selected.upLevel()) {
- TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
- TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
- // that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
- if (that.event.refreshBaseActn) {
- that.event.refreshBaseActn(that.tree);
- }
- });
- }
- }
- };
- controller.prototype.downLevel = function () {
- var that = this;
- if (this.tree.selected) {
- if (this.tree.selected.downLevel()) {
- TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
- TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], that.sheet, true);
- //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
- if (that.event.refreshBaseActn) {
- that.event.refreshBaseActn(that.tree);
- }
- });
- }
- }
- };
- controller.prototype.m_upLevel = function (nodes) { //多选升级
- let that = this;
- if (this.tree.m_upLevel(nodes)) {
- TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
- TREE_SHEET_HELPER.refreshNodesVisible([nodes[0]], that.sheet, true);
- //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
- if (that.event.refreshBaseActn) {
- that.event.refreshBaseActn(that.tree);
- }
- });
- }
- };
- controller.prototype.m_downLevel = function (nodes) { //多选降级
- let that = this;
- if (this.tree.m_downLevel(nodes)) {
- TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
- TREE_SHEET_HELPER.refreshNodesVisible([nodes[0].parent], that.sheet, true);
- //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
- if (that.event.refreshBaseActn) {
- that.event.refreshBaseActn(that.tree);
- }
- });
- }
- };
- controller.prototype.upMove = function () {
- var that = this, sels = this.sheet.getSelections();
- if (this.tree.selected) {
- TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
- TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected, that.tree.selected.serialNo(), true);//为了处理移动前子项是隐藏的情况,先把所有的列设置为显示
- TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected.preSibling, that.tree.selected.preSibling.serialNo(), true);
- if (that.tree.selected.upMove()) {
- TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
- TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected, that.tree.selected.serialNo());
- TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected.nextSibling, that.tree.selected.nextSibling.serialNo());
- that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
- if (that.event.refreshBaseActn) {
- that.event.refreshBaseActn(that.tree);
- }
- }
- });
- }
- };
- controller.prototype.downMove = function () {
- var that = this, sels = this.sheet.getSelections();
- if (this.tree.selected) {
- TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
- TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected, that.tree.selected.serialNo(), true);//为了处理移动前子项是隐藏的情况,先把所有的列设置为显示
- TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected.nextSibling, that.tree.selected.nextSibling.serialNo(), true);
- if (that.tree.selected.downMove()) {
- TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
- TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected, that.tree.selected.serialNo());
- TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected.preSibling, that.tree.selected.preSibling.serialNo());
- that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
- if (that.event.refreshBaseActn) {
- that.event.refreshBaseActn(that.tree);
- }
- }
- });
- }
- };
- controller.prototype.refreshTreeNode = function (nodes, recursive) {
- var that = this;
- TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
- TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive)
- })
- }
- controller.prototype.setTreeSelected = function (node) {
- if (this.event.beforeTreeSelectedChange) {
- this.event.beforeTreeSelectedChange(this.tree.selected);
- }
- this.tree.selected = node;
- if (this.event.refreshBaseActn) {
- this.event.refreshBaseActn(this.tree);
- }
- if (this.event.treeSelectedChanged) {
- this.event.treeSelectedChanged(this.tree.selected);
- }
- }
- controller.prototype.bind = function (eventName, eventFun) {
- this.event[eventName] = eventFun;
- };
- return new controller();
- },
- eventName: {
- refreshBaseActn: 'refreshBaseActn',
- beforeTreeSelectedChange: 'beforeTreeSelectedChange',
- treeSelectedChanged: 'treeSelectedChanged',
- cellDoubleClick: 'cellDoubleClick'
- }
- };
|