web-apps/apps/spreadsheeteditor/main/app/controller/PivotTable.js

487 lines
20 KiB
JavaScript
Raw Normal View History

2017-06-30 14:49:58 +00:00
/*
*
* (c) Copyright Ascensio System SIA 2010-2019
2017-06-30 14:49:58 +00:00
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha
* street, Riga, Latvia, EU, LV-1050.
2017-06-30 14:49:58 +00:00
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
/**
* PivotTable.js
*
* Created by Julia.Radzhabova on 06.27.17
2018-03-01 12:16:38 +00:00
* Copyright (c) 2018 Ascensio System SIA. All rights reserved.
2017-06-30 14:49:58 +00:00
*
*/
define([
'core',
'spreadsheeteditor/main/app/view/PivotTable',
'spreadsheeteditor/main/app/view/CreatePivotDialog'
2017-06-30 14:49:58 +00:00
], function () {
'use strict';
SSE.Controllers.PivotTable = Backbone.Controller.extend(_.extend({
models : [],
views : [
'PivotTable'
],
sdkViewName : '#id_main',
initialize: function () {
this.addListeners({
'PivotTable': {
// comments handlers
'pivottable:rowscolumns': _.bind(this.onCheckTemplateChange, this),
'pivottable:create': _.bind(this.onCreateClick, this),
2017-07-18 08:57:14 +00:00
'pivottable:refresh': _.bind(this.onRefreshClick, this),
'pivottable:select': _.bind(this.onSelectClick, this),
2017-07-17 10:17:30 +00:00
'pivottable:style': _.bind(this.onPivotStyleSelect, this),
'pivottable:layout': _.bind(this.onPivotLayout, this),
'pivottable:blankrows': _.bind(this.onPivotBlankRows, this),
'pivottable:subtotals': _.bind(this.onPivotSubtotals, this),
2020-09-17 13:29:48 +00:00
'pivottable:grandtotals': _.bind(this.onPivotGrandTotals, this),
'pivot:open': _.bind(this.onPivotOpen, this)
},
'TableSettings': {
'pivottable:create': _.bind(this.onCreateClick, this)
2017-06-30 14:49:58 +00:00
}
});
},
onLaunch: function () {
this._state = {
TableName: '',
TemplateName: '',
RowHeader: undefined,
RowBanded: undefined,
ColHeader: undefined,
ColBanded: undefined,
DisabledControls: false
};
this._originalProps = null;
Common.NotificationCenter.on('app:ready', this.onAppReady.bind(this));
Common.NotificationCenter.on('api:disconnect', _.bind(this.SetDisabled, this));
Common.NotificationCenter.on('more:toggle', _.bind(this.onMoreToggle, this));
2017-06-30 14:49:58 +00:00
},
setConfig: function (config) {
this.view = this.createView('PivotTable', {
toolbar: config.toolbar.toolbar
});
2017-06-30 14:49:58 +00:00
},
setApi: function (api) {
if (api) {
this.api = api;
this.api.asc_registerCallback('asc_onCoAuthoringDisconnect',_.bind(this.SetDisabled, this));
Common.NotificationCenter.on('api:disconnect', _.bind(this.SetDisabled, this));
2017-06-30 14:49:58 +00:00
this.api.asc_registerCallback('asc_onSendThemeColors', _.bind(this.onSendThemeColors, this));
this.api.asc_registerCallback('asc_onSelectionChanged', _.bind(this.onSelectionChanged, this));
Common.NotificationCenter.on('cells:range', _.bind(this.onCellsRange, this));
2017-06-30 14:49:58 +00:00
}
return this;
2017-06-30 14:49:58 +00:00
},
setMode: function(mode) {
this.appConfig = mode;
return this;
},
SetDisabled: function() {
this.view && this.view.SetDisabled(true);
},
// helpers
onCheckTemplateChange: function(type, value) {
// this._state[stateName] = undefined;
// if (this.api)
// this.api.asc_changeFormatTableInfo(this._state.TableName, type, value=='checked');
// for test
switch (type) {
case 0:
2017-07-14 08:00:56 +00:00
this._originalProps.asc_getStyleInfo().asc_setShowRowHeaders(this.api, this._originalProps, value=='checked');
2017-06-30 14:49:58 +00:00
break;
case 1:
2017-07-14 08:00:56 +00:00
this._originalProps.asc_getStyleInfo().asc_setShowColHeaders(this.api, this._originalProps, value=='checked');
2017-06-30 14:49:58 +00:00
break;
case 2:
2017-07-14 08:00:56 +00:00
this._originalProps.asc_getStyleInfo().asc_setShowRowStripes(this.api, this._originalProps, value=='checked');
2017-06-30 14:49:58 +00:00
break;
case 3:
2017-07-14 08:00:56 +00:00
this._originalProps.asc_getStyleInfo().asc_setShowColStripes(this.api, this._originalProps, value=='checked');
2017-06-30 14:49:58 +00:00
break;
}
Common.NotificationCenter.trigger('edit:complete', this);
},
createSheetName: function() {
var items = [], wc = this.api.asc_getWorksheetsCount();
while (wc--) {
items.push(this.api.asc_getWorksheetName(wc).toLowerCase());
}
var index = 0, name;
while(++index < 1000) {
name = this.strSheet + index;
if (items.indexOf(name.toLowerCase()) < 0) break;
}
return name;
},
2017-06-30 14:49:58 +00:00
onCreateClick: function(btn, opts){
if (this.api) {
2019-10-03 12:30:43 +00:00
var options = this.api.asc_getAddPivotTableOptions();
if (options) {
var me = this;
(new SSE.Views.CreatePivotDialog(
{
props: options,
api: me.api,
handler: function(result, settings) {
if (result == 'ok' && settings) {
me.view && me.view.fireEvent('insertpivot', me.view);
if (settings.destination)
me.api.asc_insertPivotExistingWorksheet(settings.source, settings.destination);
else
me.api.asc_insertPivotNewWorksheet(settings.source, me.createSheetName());
}
Common.NotificationCenter.trigger('edit:complete', me);
}
})).show();
2019-10-03 12:30:43 +00:00
}
}
2017-06-30 14:49:58 +00:00
Common.NotificationCenter.trigger('edit:complete', this);
},
onRefreshClick: function(type){
if (this.api) {
if(type == 'current') this._originalProps.asc_refresh(this.api);
else if(type == 'all') this.api.asc_refreshAllPivots();
}
2017-07-18 08:57:14 +00:00
Common.NotificationCenter.trigger('edit:complete', this);
},
onSelectClick: function(btn, opts){
2018-02-05 12:41:24 +00:00
if (this.api) {
this._originalProps.asc_select(this.api);
}
Common.NotificationCenter.trigger('edit:complete', this);
},
2017-07-14 12:40:48 +00:00
onPivotStyleSelect: function(record){
2017-07-17 10:17:30 +00:00
if (this.api) {
2017-07-14 12:40:48 +00:00
this._originalProps.asc_getStyleInfo().asc_setName(this.api, this._originalProps, record.get('name'));
2017-06-30 14:49:58 +00:00
}
Common.NotificationCenter.trigger('edit:complete', this);
},
2017-07-17 10:17:30 +00:00
onPivotBlankRows: function(type){
if (this.api) {
var props = new Asc.CT_pivotTableDefinition();
props.asc_setInsertBlankRow(type === 'insert');
this._originalProps.asc_set(this.api, props);
2017-07-17 10:17:30 +00:00
}
Common.NotificationCenter.trigger('edit:complete', this);
},
onPivotLayout: function(type){
if (this.api) {
var props = new Asc.CT_pivotTableDefinition();
2017-07-17 10:17:30 +00:00
switch (type){
case 0:
props.asc_setCompact(true);
props.asc_setOutline(true);
2017-07-17 10:17:30 +00:00
break;
case 1:
props.asc_setCompact(false);
props.asc_setOutline(true);
2017-07-17 10:17:30 +00:00
break;
case 2:
props.asc_setCompact(false);
props.asc_setOutline(false);
2017-07-17 10:17:30 +00:00
break;
case 3:
props.asc_setFillDownLabelsDefault(true);
2017-07-17 10:17:30 +00:00
break;
case 4:
props.asc_setFillDownLabelsDefault(false);
2017-07-17 10:17:30 +00:00
break;
}
this._originalProps.asc_set(this.api, props);
2017-07-17 10:17:30 +00:00
}
Common.NotificationCenter.trigger('edit:complete', this);
},
onPivotGrandTotals: function(type){
if (this.api) {
2017-08-08 14:03:09 +00:00
var props = new Asc.CT_pivotTableDefinition();
2017-08-03 12:28:49 +00:00
props.asc_setColGrandTotals(type == 1 || type == 2);
props.asc_setRowGrandTotals(type == 1 || type == 3);
2017-07-21 07:35:19 +00:00
this._originalProps.asc_set(this.api, props);
2017-07-17 10:17:30 +00:00
}
Common.NotificationCenter.trigger('edit:complete', this);
},
onPivotSubtotals: function(type){
if (this.api) {
var props = new Asc.CT_pivotTableDefinition();
2017-07-17 10:17:30 +00:00
switch (type){
case 0:
props.asc_setDefaultSubtotal(false);
2017-07-17 10:17:30 +00:00
break;
case 1:
props.asc_setDefaultSubtotal(true);
props.asc_setSubtotalTop(false);
2017-07-17 10:17:30 +00:00
break;
case 2:
props.asc_setDefaultSubtotal(true);
props.asc_setSubtotalTop(true);
2017-07-17 10:17:30 +00:00
break;
}
this._originalProps.asc_set(this.api, props);
2017-07-17 10:17:30 +00:00
}
Common.NotificationCenter.trigger('edit:complete', this);
},
2017-06-30 14:49:58 +00:00
ChangeSettings: function(props) {
if (props )
{
this._originalProps = props;
var view = this.view,
needTablePictures = false,
styleInfo = props.asc_getStyleInfo(),
value = styleInfo.asc_getShowRowHeaders();
if (this._state.RowHeader!==value) {
view.chRowHeader.setValue(value, true);
this._state.RowHeader=value;
needTablePictures = true;
}
value = styleInfo.asc_getShowColHeaders();
if (this._state.ColHeader!==value) {
view.chColHeader.setValue(value, true);
this._state.ColHeader=value;
needTablePictures = true;
}
2017-06-30 14:49:58 +00:00
value = styleInfo.asc_getShowColStripes();
if (this._state.ColBanded!==value) {
view.chColBanded.setValue(value, true);
this._state.ColBanded=value;
needTablePictures = true;
}
2017-06-30 14:49:58 +00:00
value = styleInfo.asc_getShowRowStripes();
if (this._state.RowBanded!==value) {
view.chRowBanded.setValue(value, true);
this._state.RowBanded=value;
needTablePictures = true;
}
2017-06-30 14:49:58 +00:00
value = props.asc_getColGrandTotals();
if (this._state.ColGrandTotals!==value) {
this._state.ColGrandTotals=value;
needTablePictures = true;
}
value = props.asc_getRowGrandTotals();
if (this._state.RowGrandTotals!==value) {
this._state.RowGrandTotals=value;
needTablePictures = true;
}
2017-06-30 14:49:58 +00:00
if (needTablePictures)
2017-07-14 11:17:16 +00:00
this.onApiInitPivotStyles(this.api.asc_getTablePictures(this._originalProps, true));
2017-06-30 14:49:58 +00:00
//for table-template
value = styleInfo.asc_getName();
if (this._state.TemplateName!==value || this._isTemplatesChanged) {
view.pivotStyles.suspendEvents();
var rec = view.pivotStyles.menuPicker.store.findWhere({
name: value
});
view.pivotStyles.menuPicker.selectRecord(rec);
view.pivotStyles.resumeEvents();
if (this._isTemplatesChanged) {
if (rec)
2020-09-17 13:29:48 +00:00
view.pivotStyles.fillComboView(view.pivotStyles.menuPicker.getSelectedRec(),true);
2017-06-30 14:49:58 +00:00
else
2020-09-17 13:29:48 +00:00
view.pivotStyles.fillComboView(view.pivotStyles.menuPicker.store.at(0), true);
2017-06-30 14:49:58 +00:00
}
this._state.TemplateName=value;
}
this._isTemplatesChanged = false;
}
},
onSendThemeColors: function() {
// get new table templates
if (this.view.pivotStyles && this._originalProps) {
2017-07-14 11:17:16 +00:00
this.onApiInitPivotStyles(this.api.asc_getTablePictures(this._originalProps, true));
2017-06-30 14:49:58 +00:00
this.view.pivotStyles.menuPicker.scroller.update({alwaysVisibleY: true});
}
},
onApiInitPivotStyles: function(Templates){
var self = this,
styles = this.view.pivotStyles;
this._isTemplatesChanged = true;
var count = styles.menuPicker.store.length;
2022-07-30 11:04:45 +00:00
2017-06-30 14:49:58 +00:00
if (count>0 && count==Templates.length) {
2020-09-16 13:38:40 +00:00
var data = styles.menuPicker.dataViewItems;
data && _.each(Templates, function(template, index){
var img = template.asc_getImage();
data[index].model.set('imageUrl', img, {silent: true});
$(data[index].el).find('img').attr('src', img);
2017-06-30 14:49:58 +00:00
});
2020-09-17 13:29:48 +00:00
styles.fieldPicker.store.reset(styles.fieldPicker.store.models);
2017-06-30 14:49:58 +00:00
} else {
styles.menuPicker.store.reset([]);
2022-07-30 11:04:45 +00:00
var templates = [];
var groups = [
{id: 'menu-table-group-custom', caption: self.view.__proto__.txtGroupPivot_Custom, templates: []},
{id: 'menu-table-group-light', caption: self.view.__proto__.txtGroupPivot_Light, templates: []},
{id: 'menu-table-group-medium', caption: self.view.__proto__.txtGroupPivot_Medium, templates: []},
{id: 'menu-table-group-dark', caption: self.view.__proto__.txtGroupPivot_Dark, templates: []},
{id: 'menu-table-group-no-name', caption: '&nbsp', templates: []},
];
_.each(Templates, function(template, index){
var tip = template.asc_getDisplayName();
var groupItem = '';
if (template.asc_getType()==0) {
var arr = tip.split(' '),
last = arr.pop();
if(tip == 'None'){
groupItem = 'menu-table-group-light';
}
else {
if(arr.length > 0){
groupItem = 'menu-table-group-' + arr[arr.length - 1].toLowerCase();
}
if(groups.some(function(item) {return item.id === groupItem;}) == false) {
groupItem = 'menu-table-group-no-name';
}
}
arr = 'txtTable_' + arr.join('');
tip = self.view.__proto__[arr] ? self.view.__proto__[arr] + ' ' + last : tip;
}
else {
groupItem = 'menu-table-group-custom'
}
groups.filter(function(item){ return item.id == groupItem; })[0].templates.push({
2017-06-30 14:49:58 +00:00
id : Common.UI.getId(),
name : template.asc_getName(),
caption : template.asc_getDisplayName(),
type : template.asc_getType(),
imageUrl : template.asc_getImage(),
2022-07-30 11:04:45 +00:00
group : groupItem,
2017-06-30 14:49:58 +00:00
allowSelected : true,
selected : false,
2022-07-30 11:04:45 +00:00
tip : tip
2017-06-30 14:49:58 +00:00
});
});
2022-07-30 11:04:45 +00:00
groups = groups.filter(function(item, index){
return item.templates.length > 0
});
groups.forEach(function(item){
templates = templates.concat(item.templates);
delete item.templates;
});
styles.groups.reset(groups);
styles.menuPicker.store.reset(templates);
2017-06-30 14:49:58 +00:00
}
},
2020-09-17 13:29:48 +00:00
onPivotOpen: function() {
var styles = this.view.pivotStyles;
if (styles && styles.needFillComboView && styles.menuPicker.store.length > 0 && styles.rendered){
var styleRec;
if (this._state.TemplateName) styleRec = styles.menuPicker.store.findWhere({name: this._state.TemplateName});
styles.fillComboView((styleRec) ? styleRec : styles.menuPicker.store.at(0), true);
}
},
2017-06-30 14:49:58 +00:00
onSelectionChanged: function(info) {
if (this.rangeSelectionMode || !this.appConfig.isEdit || !this.view) return;
2017-06-30 14:49:58 +00:00
var pivotInfo = info.asc_getPivotTableInfo();
2017-06-30 14:49:58 +00:00
2021-12-29 16:41:15 +00:00
Common.Utils.lockControls(Common.enumLock.noPivot, !pivotInfo, {array: this.view.lockedControls});
Common.Utils.lockControls(Common.enumLock.pivotLock, pivotInfo && (info.asc_getLockedPivotTable()===true), {array: this.view.lockedControls});
Common.Utils.lockControls(Common.enumLock.editPivot, !!pivotInfo, {array: this.view.btnsAddPivot});
2017-06-30 14:49:58 +00:00
if (pivotInfo)
this.ChangeSettings(pivotInfo);
},
onCellsRange: function(status) {
this.rangeSelectionMode = (status != Asc.c_oAscSelectionDialogType.None);
},
2017-06-30 14:49:58 +00:00
createToolbarPanel: function() {
return this.view.getPanel();
},
getView: function(name) {
return !name && this.view ?
this.view : Backbone.Controller.prototype.getView.call(this, name);
},
onAppReady: function (config) {
var me = this;
(new Promise(function (resolve) {
resolve();
})).then(function () {
});
},
onMoreToggle: function(btn, state, e) {
if (this.view && this.view.toolbar && this.view.toolbar.isTabActive('pivot') && state) {
var styles = this.view.pivotStyles;
if (styles && styles.needFillComboView && styles.menuPicker.store.length > 0 && styles.rendered){
var styleRec;
if (this._state.TemplateName) styleRec = styles.menuPicker.store.findWhere({name: this._state.TemplateName});
styles.fillComboView((styleRec) ? styleRec : styles.menuPicker.store.at(0), true);
}
}
},
strSheet : 'Sheet'
2017-06-30 14:49:58 +00:00
}, SSE.Controllers.PivotTable || {}));
});