Skip to content

Commit

Permalink
Merge pull request #3001 from ONLYOFFICE/feature/move-sheet
Browse files Browse the repository at this point in the history
Feature/move sheet
  • Loading branch information
JuliaRadzhabova authored May 21, 2024
2 parents 2ce4db2 + 8a89835 commit 7e167ec
Show file tree
Hide file tree
Showing 3 changed files with 295 additions and 163 deletions.
188 changes: 156 additions & 32 deletions apps/spreadsheeteditor/main/app/controller/Statusbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,87 @@ define([
this.bindViewEvents(this.statusbar, this.events);

$('#id-tab-menu-new-color', this.statusbar.$el).on('click', _.bind(this.onNewBorderColor, this));

this.statusbar.tabbar.on({
'tab:dragstart': _.bind(function (dataTransfer, selectTabs) {
Common.Utils.isIE && (this.isDrop = false);
Common.UI.Menu.Manager.hideAll();
this.api.asc_closeCellEditor();
var arrTabs = [],
arrName = [],
me = this;
var wc = me.api.asc_getWorksheetsCount(), items = [], i = -1;
while (++i < wc) {
if (!this.api.asc_isWorksheetHidden(i)) {
items.push({
value: me.api.asc_getWorksheetName(i),
inindex: i
});
}
}
var arrSelectIndex = [];
selectTabs.forEach(function (item) {
arrSelectIndex.push(item.sheetindex);
});
items.forEach(function (item) {
if (arrSelectIndex.indexOf(item.inindex) !== -1) {
arrTabs.push(item.inindex);
arrName.push(item.value);
}
});
var stringSheet, arr = [];
stringSheet = this.api.asc_StartMoveSheet(_.clone(arrTabs));
arr.push({type: 'onlyoffice', value: stringSheet});
arr.push({type: 'indexes', value: arrTabs});
arr.push({type: 'names', value: arrName});
arr.push({type: 'key', value: Common.Utils.InternalSettings.get("sse-doc-info-key")});
var json = JSON.stringify(arr);
if (!Common.Utils.isIE) {
dataTransfer.setData('onlyoffice', json);
} else {
dataTransfer.setData('text', 'sheet');
this.dataTransfer = json;
}
this.dropTabs = selectTabs;
}, this),
'tab:drop': _.bind(function (dataTransfer, index, copy) {
if (this.isEditFormula || (Common.Utils.isIE && this.dataTransfer === undefined)) return;
Common.Utils.isIE && (this.isDrop = true);
var data = !Common.Utils.isIE ? dataTransfer.getData('onlyoffice') : this.dataTransfer;
if (data) {
var arrData = JSON.parse(data);
if (arrData) {
var key = _.findWhere(arrData, {type: 'key'}).value;
if (Common.Utils.InternalSettings.get("sse-doc-info-key") === key) {
this.statusbar.fireEvent('sheet:move', [_.findWhere(arrData, {type: 'indexes'}).value, !copy, true, _.isNumber(index) ? index : this.api.asc_getWorksheetsCount()]);
Common.NotificationCenter.trigger('tabs:dragend', this);
} else {
var arrNames = _.findWhere(arrData, {type: 'names'}).value;
var newNames = this.generateSheetNames(false, undefined, arrNames);
var index = _.isNumber(index) ? index : this.api.asc_getWorksheetsCount();
this.api.asc_EndMoveSheet(index, newNames, _.findWhere(arrData, {type: 'onlyoffice'}).value);
}
}
}
}, this),
'tab:dragend': _.bind(function (cut) {
if (cut && !(Common.Utils.isIE && this.isDrop === false)) {
if (this.dropTabs.length > 0) {
var arr = [];
this.dropTabs.forEach(function (tab) {
arr.push(tab.sheetindex);
});
this.api.asc_deleteWorksheet(arr);
}
}
this.dropTabs = undefined;
if (Common.Utils.isIE) {
this.isDrop = undefined;
this.dataTransfer = undefined;
}
Common.NotificationCenter.trigger('tabs:dragend', this);
}, this)
});
},

setApi: function(api) {
Expand All @@ -110,6 +191,9 @@ define([
this.api.asc_registerCallback('asc_onFilterInfo', _.bind(this.onApiFilterInfo , this));
this.api.asc_registerCallback('asc_onActiveSheetChanged', _.bind(this.onApiActiveSheetChanged, this));
this.api.asc_registerCallback('asc_onRefreshNamedSheetViewList', _.bind(this.onRefreshNamedSheetViewList, this));
this.api.asc_registerCallback('asc_generateNewSheetNames', _.bind(function (arrNames, callback) {
callback(this.generateSheetNames(false, undefined, arrNames));
}, this));

this.statusbar.setApi(api);
},
Expand Down Expand Up @@ -314,11 +398,8 @@ define([
case 'ren':
this.renameWorksheet();
break;
case 'copy':
this.moveWorksheet(arrIndex, false);
break;
case 'move':
this.moveWorksheet(arrIndex, true);
case 'move-copy':
this.moveWorksheet(arrIndex);
break;
case 'hide':
setTimeout(function () {
Expand Down Expand Up @@ -358,25 +439,23 @@ define([
return name;
},

createCopyName: function(orig, curArrNames) {
var wc = this.api.asc_getWorksheetsCount(), names = [];
while (wc--) {
names.push(this.api.asc_getWorksheetName(wc).toLowerCase());
}

createCopyName: function(copy, orig, curNames, names) {
var re = /^(.*)\((\d)\)$/.exec(orig);
var first = re ? re[1] : orig + ' ';

var index = 1, name;
while(true) {
var index = 1, name = orig;
if (copy) {
index++;
name = first + '(' + index + ')';
}
while(names.indexOf(name.toLowerCase()) !== -1) {
index++;
name = first + '(' + index + ')';
if (names.indexOf(name.toLowerCase()) < 0) break;
}

if (curArrNames && curArrNames.length > 0) {
if (curNames && curNames.length > 0) {
var arr = [];
curArrNames.forEach(function (item) {
curNames.forEach(function (item) {
arr.push(item.toLowerCase());
});
while(arr.indexOf(name.toLowerCase()) !== -1) {
Expand All @@ -388,6 +467,27 @@ define([
return name;
},

generateSheetNames: function (copy, arrIndexes, arrNames) {
var me = this,
names = [];
var wc = this.api.asc_getWorksheetsCount();
while (wc--) {
names.push(this.api.asc_getWorksheetName(wc).toLowerCase());
}

var newNames = [];
if (arrIndexes) {
arrIndexes.forEach(function (item) {
newNames.push(me.createCopyName(copy, me.api.asc_getWorksheetName(item), newNames, names));
});
} else if (arrNames) {
arrNames.forEach(function (item) {
newNames.push(me.createCopyName(copy, item, newNames, names));
});
}
return newNames;
},

deleteWorksheet: function(selectTabs) {
var me = this;

Expand Down Expand Up @@ -491,34 +591,58 @@ define([
me.api.asc_moveWorksheet(indTo, arrIndex);
me.api.asc_enableKeyEvents(true);
} else {
var arrNames = [];
arrIndex.forEach(function (item) {
arrNames.push(me.createCopyName(me.api.asc_getWorksheetName(item), arrNames));
});
var arrNames = me.generateSheetNames(!cut, arrIndex);
me.api.asc_copyWorksheet(indTo, arrNames, arrIndex);
}
return;
}

(new SSE.Views.Statusbar.CopyDialog({
title : cut ? me.statusbar.itemMove : me.statusbar.itemCopy,
ismove : cut,
names : items,
handler : function(btn, i) {
var btn;
me.copyDialog = new SSE.Views.Statusbar.CopyDialog({
title : me.statusbar.itemMoveOrCopy,
sheets : items,
spreadsheetName: me.api.asc_getDocumentName(),
isDesktopApp: me.statusbar.mode.isDesktopApp,
handler : function(result, i, copy, workbook) {
btn = result;
if (btn == 'ok') {
if (cut) {
me.api.asc_moveWorksheet(i == -255 ? wc : i, arrIndex);
} else {
var arrNames = [];
var arrBooks,
arrNames;
if (workbook === 'new')
arrBooks = [];
else if (workbook !== 'current')
arrBooks = [workbook];
if (workbook !== 'current') {
arrNames = [];
arrIndex.forEach(function (item) {
arrNames.push(me.createCopyName(me.api.asc_getWorksheetName(item), arrNames));
arrNames.push(me.api.asc_getWorksheetName(item));
});
me.api.asc_copyWorksheet(i == -255 ? wc : i, arrNames, arrIndex);
}
if (!copy) {
me.api.asc_moveWorksheet(i == -255 ? wc : i, arrIndex, arrNames, arrBooks);
} else {
if (!arrNames)
arrNames = me.generateSheetNames(copy, arrIndex);
me.api.asc_copyWorksheet(i == -255 ? wc : i, arrNames, arrIndex, arrBooks);
}
} else {
me.api.asc_cancelMoveCopyWorksheet();
}
me.api.asc_enableKeyEvents(true);
}
})).show();
});
me.copyDialog.on('close', function () {
if (!btn) me.api.asc_cancelMoveCopyWorksheet();
me.copyDialog = undefined;
});
me.copyDialog.show();

var callback = function (workbooks) {
if (workbooks) {
me.copyDialog.changeSpreadsheets(workbooks);
}
};
me.api.asc_getOpeningDocumentsList(callback);
},

onAddWorksheetClick: function(o, index, opts) {
Expand Down
Loading

0 comments on commit 7e167ec

Please sign in to comment.