Skip to content

Commit

Permalink
Merge pull request #14 from RaymondChao/ZSS-678
Browse files Browse the repository at this point in the history
Feature ZSS-678: Support creating reference between two sheets
  • Loading branch information
henrichen committed Jun 11, 2014
2 parents c5469cf + e5994a3 commit aefd78a
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 51 deletions.
1 change: 1 addition & 0 deletions zkdoc/release-note
Expand Up @@ -7,6 +7,7 @@ ZK Spreadsheet 3.5.0
ZSS-565 Support input with Swedish locale into formula
ZSS-685 Do not import duplicate cell style
ZSS-684 Auto expand editing area's width when editing cell
ZSS-678 Support creating reference between two sheets

*Bugs:
ZSS-652 Cut Formula shouldn't be shifted when it is not in cut range
Expand Down
15 changes: 13 additions & 2 deletions zss/src/archive/web/js/zss/DataPanel.js
Expand Up @@ -379,12 +379,21 @@ zss.DataPanel = zk.$extends(zk.Object, {

var data = this._wgt._cacheCtrl.getSelectedSheet(),
editor = inlineEditing ? this.sheet.inlineEditor : this.sheet.formulabarEditor,
value = editor.getValue(),
value = editor.getValue(),
sheetId = editor.sheetId,
row = editor.row,
col = editor.col,
cell = sheet.getCell(row, col),
edit = cell ? cell.edit : null;

if (sheetId && sheetId != sheet.serverSheetId) {
var sheetBar = sheet._wgt._sheetBar,
sheetSelector = sheetBar ? sheetBar.getSheetSelector(): null;
if (sheetSelector) {
sheetSelector.doSelectSheet(sheetId);
}
}

if (edit != value) { //has to send back to server
var token = "";
if (type == "movedown") {//move down after aysnchronized call back
Expand Down Expand Up @@ -555,7 +564,9 @@ zss.DataPanel = zk.$extends(zk.Object, {
* @param trigger should fire focustag.focus (if genFocus is call by a focustag listener for focus, then it is false
*/
gainFocus: function (trigger) {
this.stopEditing("refocus");
if (!this.sheet.isSwitchingSheet) {
this.stopEditing("refocus");
}
this._gainFocus(trigger);
},
/* gain focus to the focus cell*/
Expand Down
119 changes: 91 additions & 28 deletions zss/src/archive/web/js/zss/Editbox.js
Expand Up @@ -239,22 +239,29 @@ Copyright (C) 2007 Potix Corporation. All Rights Reserved.
if (sheet) {
// ZSS-674: stay focused if formulabar's ok/cancel btn was pressed down.
if (sheet.shallIgnoreBlur) {
if (!zk.gecko) {
wgt.focus();
} else {
// 20140603, RaymondChao: firefox needs setTimeout to refocus when blur.
setTimeout(function () {wgt.focus()});
}
focusWhenBlur(wgt);
sheet.shallIgnoreBlur = false;
} else if (sheet.isSwitchingFocus) {
// 20140519, RaymondChao: when change focus between editors, spreadsheet could have no focus as below
// formulabar editor blur -> ( no focus ) -> editbox focus
// need to add isSwitchingFocus flag to determine the situation
sheet.isSwitchingFocus = false;
wgt.isFocused = false;
} else if (sheet.isSwitchingSheet) {
focusWhenBlur(wgt);
} else if (!sheet._wgt.hasFocus()) {
sheet.dp.stopEditing(sheet.innerClicking > 0 ? "refocus" : "lostfocus");
wgt.isFocused = false;
}
wgt.isFocused = false;
}
}

function focusWhenBlur (wgt) {
if (!zk.gecko) {
wgt.focus();
} else {
// 20140603, RaymondChao: firefox needs setTimeout to refocus when blur.
setTimeout(function () {wgt.focus()});
}
}

Expand Down Expand Up @@ -318,15 +325,23 @@ Copyright (C) 2007 Potix Corporation. All Rights Reserved.
v = $n.text(),
c1 = sheet.getCell(tRow, lCol),
c2 = tRow == bRow && lCol == rCol ? null : sheet.getCell(bRow, rCol),
ref = c1.ref;
ref = c1.ref,
editor = sheet.inlineEditor,
editorSheetId = editor.sheetId,
currentSheetId = sheet.serverSheetId;
if (c2) {
ref += (':' + c2.ref);
}

if (editorSheetId && editorSheetId != currentSheetId) {
ref = getSheetName(sheet, currentSheetId) + '!' + ref;
}

if (!start) {
ref = '=' + ref;
}
$n.text(v.substring(0, start) + ref + v.substring(end, v.length));
sheet.inlineEditor.setValue($n.text());
editor.setValue($n.text());
end = start + ref.length;
info.end = end;
if (zk.ie && zk.ie < 11) {
Expand All @@ -338,6 +353,30 @@ Copyright (C) 2007 Potix Corporation. All Rights Reserved.
}
}

/**
* Get sheet name
*
* @param Object sheet the ({@link zss.SSheetCtrl})
* @param string sheetId
*/
function getSheetName (sheet, sheetId) {
var labels = sheet._wgt.getSheetLabels();
for (var prop in labels) {
if (labels[prop]['id'] == sheetId) {
return quoteName(labels[prop]['name']);
}
}
}

// returns sheet name or surrounds name with single-quote if nessarery
function quoteName (name) {
if (/^[a-z_]+[a-z0-9_.]*$/i.test(name)) {
return name;
} else {
return "'" + name + "'";
}
}

zss.FormulabarEditor = zk.$extends(zul.inp.InputWidget, {
widgetName: 'FormulabarEditor',
row: -1,
Expand Down Expand Up @@ -493,7 +532,7 @@ zss.FormulabarEditor = zk.$extends(zul.inp.InputWidget, {
},
_onContentsChanged: function (evt) {
var sheet = this.sheet;
if (sheet && this.isRealVisible()) {
if (sheet && this.isRealVisible() && !sheet.editingFormulaInfo) {
var p = sheet.getLastFocus(),
c = sheet.getCell(p.row, p.column);
if (c) {
Expand Down Expand Up @@ -635,9 +674,6 @@ zss.FormulabarEditor = zk.$extends(zul.inp.InputWidget, {
},
getZclass: function () {
return 'zsformulabar-editor';
},
getInputNode: function () {
return this.$n('real');
}
});

Expand All @@ -659,7 +695,7 @@ zss.Editbox = zk.$extends(zul.inp.InputWidget, {
},
bind_: function () {
this.$supers(zss.Editbox, 'bind_', arguments);
this.comp = this.$n();
this.comp = this.getInputNode();
this.comp.ctrl = this;

this.sheet.listen({'onStartEditing': this.proxy(this._onStartEditing)})
Expand All @@ -683,9 +719,13 @@ zss.Editbox = zk.$extends(zul.inp.InputWidget, {
return this._editing;
},
_onStartEditing: function (evt) {
var d = evt.data;
var d = evt.data,
sheet = this.sheet;
this.row = d.row;
this.col = d.col;
this.sheetId = sheet.serverSheetId;
this.sheetName = getSheetName(sheet, this.sheetId);
this.cellRef = sheet.getCell(this.row, this.col).ref;
},
_onCellSelection: function (evt) {
var sheet = this.sheet;
Expand All @@ -695,7 +735,7 @@ zss.Editbox = zk.$extends(zul.inp.InputWidget, {
if (info && 'inlineEditing' == info.type && !sheet._skipInsertCellRef) {
var d = evt.data,
formulabarEditor = sheet.formulabarEditor;
insertCellRef(sheet, this.comp, d.top, d.left, d.bottom, d.right);
insertCellRef(sheet, this.getInputNode(), d.top, d.left, d.bottom, d.right);
if (formulabarEditor) {
formulabarEditor.setValue(this.getValue());
}
Expand All @@ -721,7 +761,7 @@ zss.Editbox = zk.$extends(zul.inp.InputWidget, {
}
var info = sheet.editingFormulaInfo;
if (info && info.moveCell) {//re-eval editing formula info
sheet.editingFormulaInfo = getEditingFormulaInfo(this._type, this.$n());
sheet.editingFormulaInfo = getEditingFormulaInfo(this._type, this.getInputNode());
}
}
}
Expand Down Expand Up @@ -885,13 +925,16 @@ zss.Editbox = zk.$extends(zul.inp.InputWidget, {
t -= 1;//position adjust
w -= 1;
h -= 1;
l -= 1;

if ((zk.ie /*&& zk.ie < 11*/) || zk.safari || zk.opera)
if (zk.safari || zk.opera)
//the display in different browser.
w -= 2;

this.editingWidth = w;
this.editingHeight = h;
this.editingTop = t;
this.editingLeft = l;

//issue 228: firefox need set display block, but IE can not set this.
$edit.css({'min-width': jq.px0(w), 'min-height': jq.px0(h), 'width': 'auto', 'height': 'auto',
Expand Down Expand Up @@ -927,21 +970,25 @@ zss.Editbox = zk.$extends(zul.inp.InputWidget, {
this.autoAdjust(true);
},
cancel: function () {
this._editing = false;
this.disable(true);
jq(this.comp).text('').css('display', 'none');
this.row = this.col = -1;
this.clearStatus();
},
stop: function () {
if (this.sheet)
var str = this.getValue();
this.clearStatus();
return str;
},
clearStatus: function () {
if (this.sheet) {
this.sheet.editingFormulaInfo = null;
}
this._editing = false;
this.disable(true);
var editorcmp = this.comp,
str = this.getValue();
jq(editorcmp).text('').css('display', 'none');
jq(this.comp).text('').css('display', 'none');
jq(this.$n('info')).text('').css('display', 'none');
this.row = this.col = -1;
return str;
this.sheetId = null;
this.sheetName = null;
this.cellRef = null;
},
getValue: function () {
return br2nl(this.comp);
Expand Down Expand Up @@ -990,8 +1037,24 @@ zss.Editbox = zk.$extends(zul.inp.InputWidget, {
}
}, 0);
},
updateInfo: function () {
var sheet = this.sheet,
info = this.$n('info'),
$info = jq(info),
label = '',
sheetId = this.sheetId;

if (sheetId && sheetId != sheet.serverSheetId) {
label = this.sheetName + '!' + this.cellRef;
$info.css('display', 'block');
} else {
$info.css('display', 'none');
}
$info.text(label);
$info.css({'left': this.editingLeft, 'top': jq.px(this.editingTop - info.offsetHeight)});
},
redrawHTML_: function () {
return '<div id="' + this.uuid + '" class="zsedit" zs.t="SEditbox" contentEditable="true"></div>';
return '<div id="' + this.uuid + '" class="zsedit"><div id="' + this.uuid + '-info" class="zsedit-info"></div><div id="' + this.uuid + '-real" class="zsedit-real" zs.t="SEditbox" contentEditable="true"></div></div>';
}
});
})();
7 changes: 6 additions & 1 deletion zss/src/archive/web/js/zss/SSheetCtrl.js
Expand Up @@ -86,7 +86,12 @@ Copyright (C) 2007 Potix Corporation. All Rights Reserved.
if (wgt.isSheetCSSReady()) {
sheet.activeBlock.setVisible(true); //show cells
sheet._doSSInitLater(); //may show focus, process wrap height
wgt.focus();
if (!sheet.isSwitchingSheet) {
wgt.focus();
} else {
sheet.inlineEditor.updateInfo();
sheet.isSwitchingSheet = null;
}
} else {
setTimeout(function () {
doAfterCSSReady(sheet);
Expand Down
34 changes: 20 additions & 14 deletions zss/src/archive/web/js/zss/Sheetbar.js
Expand Up @@ -208,9 +208,13 @@ zss.SheetTab = zk.$extends(zul.tab.Tab, {
this.startEditing();
}
},
doMouseDown_: function () {
doMouseDown_: function (event) {
//TODO: spreadsheet shall remain focus when mouse down on SheetTab
//eat event
var sheet = this._wgt.sheetCtrl;
if (sheet.editingFormulaInfo) {
sheet.isSwitchingSheet = true;
}
},
doMouseUp_: function () {
//eat event
Expand Down Expand Up @@ -285,12 +289,14 @@ zss.SheetSelector = zk.$extends(zul.tab.Tabbox, {
if (!tab.$instanceof(zss.SheetTab)) {
return;
}
this.doSelectSheet(tab.getSheetUuid(), this._wgt.sheetCtrl.isSwitchingSheet);
},
doSelectSheet: function (sheetId, ignoreStatus) {

var wgt = this._wgt,
sheet = wgt.sheetCtrl,
currSheetId = wgt.getSheetId(),
targetSheetId = tab.getSheetUuid();
if (targetSheetId != currSheetId) {
currSheetId = wgt.getSheetId();
if (sheetId != currSheetId) {
var useCache = false,
row = -1, col = -1,
left = -1, top = -1, right = -1, bottom = -1,
Expand All @@ -309,10 +315,10 @@ zss.SheetSelector = zk.$extends(zul.tab.Tabbox, {
var cacheCtrl = wgt._cacheCtrl;
if (cacheCtrl) {
cacheCtrl.snap(currSheetId);//snapshot current sheet status
if (cacheCtrl.isCached(targetSheetId)) {
if (cacheCtrl.isCached(sheetId)) {

//restore target sheet status: focus, selection etc..
var snapshop = cacheCtrl.getSnapshot(targetSheetId),
var snapshop = cacheCtrl.getSnapshot(sheetId),
visRng = snapshop.getVisibleRange(),
focus = snapshop.getFocus(),
sel = snapshop.getSelection(),
Expand All @@ -322,36 +328,36 @@ zss.SheetSelector = zk.$extends(zul.tab.Tabbox, {
frow = snapshop.getRowFreeze(),
fcol = snapshop.getColumnFreeze();

if (focus) {
if (focus && !ignoreStatus) {
row = focus.row;
col = focus.column;
}
if (sel) {
if (sel && !ignoreStatus) {
left = sel.left;
top = sel.top;
right = sel.right;
bottom = sel.bottom;
}
if (hsel) { //highlight
if (hsel && !ignoreStatus) { //highlight
hleft = hsel.left;
htop = hsel.top;
hright = hsel.right;
hbottom = hsel.bottom;
}
if (dv) {
if (dv && !ignoreStatus) {
wgt.setDataValidations(dv);
} else if (wgt.setDataValidations) {
wgt.setDataValidations(null);
}
if (af) {
if (af && !ignoreStatus) {
wgt.setAutoFilter(af);
} else if (wgt.setAutoFilter) {
wgt.setAutoFilter(null);
}
wgt.setSheetId(targetSheetId, false, visRng);//replace widgets: cells, headers etc..
wgt.setSheetId(sheetId, false, visRng);//replace widgets: cells, headers etc..

//restore sheet last focus/selection
if (row >= 0 && col >= 0) {
if (row >= 0 && col >= 0 && !ignoreStatus) {
sheet.moveCellFocus(row, col);
sheet.moveCellSelection(left, top, right, bottom);
}
Expand All @@ -366,7 +372,7 @@ zss.SheetSelector = zk.$extends(zul.tab.Tabbox, {
}

wgt.fire('onSheetSelect',
{sheetId: targetSheetId, cache: useCache,
{sheetId: sheetId, cache: useCache,
row: row, col: col,
left: left, top: top, right: right, bottom: bottom,
hleft: hleft, htop: htop, hright: hright, hbottom: hbottom,
Expand Down

0 comments on commit aefd78a

Please sign in to comment.