web-apps/apps/spreadsheeteditor/main/app/view/FormulaDialog.js

430 lines
17 KiB
JavaScript
Raw Normal View History

2016-04-01 13:17:09 +00:00
/*
*
* (c) Copyright Ascensio System SIA 2010-2019
2016-04-01 13:17:09 +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.
2016-04-01 13:17:09 +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
*
*/
2016-03-11 00:48:53 +00:00
/**
* FormulaDialog.js
*
* Add formula to cell dialog
*
* Created by Alexey.Musinov on 11/04/14
2018-03-01 12:16:38 +00:00
* Copyright (c) 2018 Ascensio System SIA. All rights reserved.
2016-03-11 00:48:53 +00:00
*
*/
define([
'common/main/lib/component/Window',
'spreadsheeteditor/main/app/collection/FormulaGroups'
], function () {
'use strict';
SSE.Views = SSE.Views || {};
SSE.Views.FormulaDialog = Common.UI.Window.extend(_.extend({
applyFunction: undefined,
initialize : function (options) {
var t = this,
_options = {};
_.extend(_options, {
2019-03-26 11:23:42 +00:00
width : 375,
2016-03-11 00:48:53 +00:00
height : 490,
header : true,
cls : 'formula-dlg',
contentTemplate : '',
title : t.txtTitle,
2019-09-11 08:38:08 +00:00
items : [],
2020-05-07 14:08:59 +00:00
buttons: null
2016-03-11 00:48:53 +00:00
}, options);
this.template = options.template || [
'<div class="box" style="height:' + (_options.height - 85) + 'px;">',
'<div class="content-panel" >',
2020-05-07 14:08:59 +00:00
'<div id="formula-dlg-search" style="height:22px; margin-bottom:10px;"></div>',
2016-03-11 00:48:53 +00:00
'<label class="header">' + t.textGroupDescription + '</label>',
2020-05-07 14:08:59 +00:00
'<div id="formula-dlg-combo-group" class="input-group-nr" style=""/>',
'<label class="header" style="margin-top: 10px">' + t.textListDescription + '</label>',
2016-03-11 00:48:53 +00:00
'<div id="formula-dlg-combo-functions" class="combo-functions"/>',
'<label id="formula-dlg-args" style="margin-top: 7px">' + '</label>',
2019-03-26 11:23:42 +00:00
'<label id="formula-dlg-desc" style="margin-top: 4px; display: block;">' + '</label>',
2016-03-11 00:48:53 +00:00
'</div>',
'</div>',
2020-05-07 14:08:59 +00:00
'<div class="separator horizontal"/>',
'<div class="footer center">',
'<button id="formula-dlg-btn-ok" class="btn normal dlg-btn primary" result="ok" style="width: 86px;">' + this.okButtonText + '</button>',
'<button class="btn normal dlg-btn" result="cancel" style="width: 86px;">' + this.cancelButtonText + '</button>',
'</div>'
2016-03-11 00:48:53 +00:00
].join('');
this.api = options.api;
this.formulasGroups = options.formulasGroups;
this.handler = options.handler;
2017-04-21 08:10:07 +00:00
_options.tpl = _.template(this.template)(_options);
2016-03-11 00:48:53 +00:00
Common.UI.Window.prototype.initialize.call(this, _options);
},
render: function () {
Common.UI.Window.prototype.render.call(this);
this.$window.find('.dlg-btn').on('click', _.bind(this.onBtnClick, this));
2020-05-07 14:08:59 +00:00
var me = this;
this.inputSearch = new Common.UI.InputField({
el : $('#formula-dlg-search', this.$window),
allowBlank : true,
placeHolder : this.txtSearch,
validateOnChange : true,
validation : function () { return true; }
}).on ('changing', function (input, value) {
if (value.length) {
value = value.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
me.filter = new RegExp(value, 'ig');
} else {
me.filter = undefined;
}
me.filterFormulas();
});
this.btnOk = new Common.UI.Button({
el: $('#formula-dlg-btn-ok')
});
2016-03-11 00:48:53 +00:00
this.syntaxLabel = $('#formula-dlg-args');
this.descLabel = $('#formula-dlg-desc');
2016-03-11 00:48:53 +00:00
this.fillFormulasGroups();
},
2019-06-25 07:39:09 +00:00
show: function (group) {
2016-03-11 00:48:53 +00:00
if (this.$window) {
var main_width, main_height, top, left, win_height = this.initConfig.height;
if (window.innerHeight === undefined) {
main_width = document.documentElement.offsetWidth;
main_height = document.documentElement.offsetHeight;
} else {
main_width = Common.Utils.innerWidth();
main_height = Common.Utils.innerHeight();
2016-03-11 00:48:53 +00:00
}
top = (parseInt(main_height, 10) - parseInt(win_height, 10)) / 2;
2016-03-11 00:48:53 +00:00
left = (parseInt(main_width, 10) - parseInt(this.initConfig.width, 10)) / 2;
this.$window.css('left', Math.floor(left));
this.$window.css('top', Math.floor(top));
}
Common.UI.Window.prototype.show.call(this);
2019-06-25 07:39:09 +00:00
group && this.cmbFuncGroup.setValue(group);
(group || this.cmbFuncGroup.getValue()=='Last10') && this.fillFunctions(this.cmbFuncGroup.getValue());
2016-03-11 00:48:53 +00:00
if (this.cmbListFunctions) {
2020-05-07 14:08:59 +00:00
this.inputSearch.setValue('');
2016-03-11 00:48:53 +00:00
_.delay(function (me) {
2020-05-07 14:08:59 +00:00
me.inputSearch.$el.find('input').focus();
2016-03-11 00:48:53 +00:00
}, 100, this);
}
2020-04-17 17:12:45 +00:00
this._preventCloseCellEditor = false;
2016-03-11 00:48:53 +00:00
},
hide: function () {
2020-05-07 14:08:59 +00:00
var val = this.cmbFuncGroup.getValue();
(val=='Recommended') && (val = 'Last10');
if (this.cmbFuncGroup.store.at(0).get('value')=='Recommended') {
this.cmbFuncGroup.store.shift();
this.cmbFuncGroup.onResetItems();
}
this.cmbFuncGroup.setValue(val);
this.recommended = this.filter = undefined;
2016-03-11 00:48:53 +00:00
Common.UI.Window.prototype.hide.call(this);
2020-04-17 17:12:45 +00:00
2020-04-20 09:43:04 +00:00
!this._preventCloseCellEditor && this.api.asc_closeCellEditor(true);
2016-03-11 00:48:53 +00:00
},
onBtnClick: function (event) {
2020-05-07 14:08:59 +00:00
this._handleInput(event.currentTarget.attributes['result'].value);
2016-03-11 00:48:53 +00:00
},
onDblClickFunction: function () {
2020-05-07 14:08:59 +00:00
this._handleInput('ok');
2016-03-11 00:48:53 +00:00
},
onSelectGroup: function (combo, record) {
if (!_.isUndefined(record) && !_.isUndefined(record.value)) {
2019-06-25 07:39:09 +00:00
record.value && this.fillFunctions(record.value);
2016-03-11 00:48:53 +00:00
}
this.onUpdateFocus();
},
onSelectFunction: function (listView, itemView, record) {
var funcId, functions, func;
2020-05-07 14:08:59 +00:00
if (this.formulasGroups && record) {
this.applyFunction = {name: record.get('value'), origin: record.get('origin')};
this.syntaxLabel.text(this.applyFunction.name + record.get('args'));
this.descLabel.text(record.get('desc'));
2016-03-11 00:48:53 +00:00
}
},
onPrimary: function(list, record, event) {
2020-05-07 14:08:59 +00:00
this._handleInput('ok');
},
_handleInput: function(state) {
if (this.handler && state == 'ok') {
if (this.btnOk.isDisabled())
return;
2020-04-17 17:12:45 +00:00
this._preventCloseCellEditor = true;
2016-03-11 00:48:53 +00:00
this.handler.call(this, this.applyFunction);
}
this.hide();
},
onUpdateFocus: function () {
2020-05-07 14:08:59 +00:00
if (this.cmbListFunctions) {
_.delay(function (me) {
me.cmbListFunctions.$el.find('.listview').focus();
}, 100, this);
}
2016-03-11 00:48:53 +00:00
},
//
fillFormulasGroups: function () {
if (this.formulasGroups) {
2017-10-04 15:44:01 +00:00
var lang = Common.Utils.InternalSettings.get("sse-settings-func-locale");
2016-03-11 00:48:53 +00:00
if (_.isEmpty(lang)) lang = 'en';
var i, groupsListItems = [], length = this.formulasGroups.length;
for (i = 0; i < length; ++i) {
var group = this.formulasGroups.at(i);
if (group.get('functions').length) {
2016-03-11 00:48:53 +00:00
groupsListItems.push({
2019-06-25 07:39:09 +00:00
value : group.get('name'),
2019-06-20 09:51:27 +00:00
displayValue : group.get('caption')
2016-03-11 00:48:53 +00:00
});
}
}
if (!this.cmbFuncGroup) {
this.cmbFuncGroup = new Common.UI.ComboBox({
el : $('#formula-dlg-combo-group'),
2019-05-31 08:43:36 +00:00
menuStyle : 'min-width: 100%;',
2016-03-11 00:48:53 +00:00
cls : 'input-group-nr',
data : groupsListItems,
editable : false
});
this.cmbFuncGroup.on('selected', _.bind(this.onSelectGroup, this));
} else {
this.cmbFuncGroup.setData(groupsListItems);
}
2019-06-25 07:39:09 +00:00
this.cmbFuncGroup.setValue('Last10');
this.fillFunctions('Last10');
2020-05-07 14:08:59 +00:00
this.allFunctions = new Common.UI.DataViewStore();
var group = this.formulasGroups.findWhere({name : 'All'});
group && this.allFunctions.reset(group.get('functions'));
2016-03-11 00:48:53 +00:00
}
},
fillFunctions: function (name) {
if (this.formulasGroups) {
if (!this.cmbListFunctions && !this.functions) {
this.functions = new Common.UI.DataViewStore();
this.cmbListFunctions = new Common.UI.ListView({
el: $('#formula-dlg-combo-functions'),
store: this.functions,
itemTemplate: _.template('<div id="<%= id %>" class="list-item" style="pointer-events:none;"><%= value %></div>')
});
this.cmbListFunctions.on('item:select', _.bind(this.onSelectFunction, this));
this.cmbListFunctions.on('item:dblclick', _.bind(this.onDblClickFunction, this));
this.cmbListFunctions.on('entervalue', _.bind(this.onPrimary, this));
this.cmbListFunctions.onKeyDown = _.bind(this.onKeyDown, this.cmbListFunctions);
this.cmbListFunctions.scrollToRecord = _.bind(this.onScrollToRecordCustom, this.cmbListFunctions);
2020-05-07 14:08:59 +00:00
this.onUpdateFocus();
2016-03-11 00:48:53 +00:00
}
if (this.functions) {
this.functions.reset();
2020-05-07 14:08:59 +00:00
var functions = null;
if (name=='Recommended') {
functions = this.recommended;
} else {
var group = this.formulasGroups.findWhere({name : name});
group && (functions = group.get('functions'));
}
var length = functions ? functions.length : 0;
for (var i = 0; i < length; ++i) {
this.functions.push(new Common.UI.DataViewModel({
id : functions[i].get('index'),
selected : i < 1,
allowSelected : true,
value : functions[i].get('name'),
args : functions[i].get('args'),
desc : functions[i].get('desc'),
origin : functions[i].get('origin')
}));
}
2016-03-11 00:48:53 +00:00
2020-05-07 14:08:59 +00:00
this.applyFunction = length ? {name: functions[0].get('name'), origin: functions[0].get('origin')} : undefined;
2016-03-11 00:48:53 +00:00
2020-05-07 14:08:59 +00:00
this.syntaxLabel.text(length ? this.applyFunction.name + functions[0].get('args') : '');
this.descLabel.text(length ? functions[0].get('desc') : '');
this.cmbListFunctions.scroller.update({
minScrollbarLength : 40,
alwaysVisibleY : true
});
this.btnOk.setDisabled(!length);
2016-03-11 00:48:53 +00:00
}
}
},
2020-05-07 14:08:59 +00:00
filterFormulas: function() {
if (!this.filter) {
this.cmbFuncGroup.setValue('Last10');
this.fillFunctions('Last10');
return;
}
var me = this,
arr = this.allFunctions.filter(function(item) {
return !!item.get('name').match(me.filter);
});
if (arr.length>0 && this.cmbFuncGroup.store.at(0).get('value')!='Recommended') {
this.cmbFuncGroup.store.unshift({value: 'Recommended', displayValue: this.txtRecommended});
this.cmbFuncGroup.onResetItems();
} else if (arr.length==0 && this.cmbFuncGroup.store.at(0).get('value')=='Recommended') {
this.cmbFuncGroup.store.shift();
this.cmbFuncGroup.onResetItems();
}
this.cmbFuncGroup.setValue('Recommended');
this.recommended = arr;
this.fillFunctions('Recommended');
},
2016-03-11 00:48:53 +00:00
onKeyDown: function (e, event) {
var i = 0, record = null,
me = this,
charVal = '',
value = '',
firstRecord = null,
recSelect = false,
innerEl = null,
isEqualSelectRecord = false,
selectRecord = null,
needNextRecord = false;
if (this.disabled) return;
if (_.isUndefined(undefined)) event = e;
function selectItem (item) {
me.selectRecord(item);
me.scrollToRecord(item);
innerEl = $(me.el).find('.inner');
me.scroller.scrollTop(innerEl.scrollTop(), 0);
event.preventDefault();
event.stopPropagation();
}
2019-07-12 07:20:08 +00:00
charVal = e.key;
2016-03-11 00:48:53 +00:00
if (e.keyCode > 64 && e.keyCode < 91 && charVal && charVal.length) {
2019-07-12 07:20:08 +00:00
charVal = charVal.toLocaleLowerCase();
2016-03-11 00:48:53 +00:00
selectRecord = this.store.findWhere({selected: true});
if (selectRecord) {
value = selectRecord.get('value');
2019-07-12 07:20:08 +00:00
isEqualSelectRecord = (value && value.length && value[0].toLocaleLowerCase() === charVal)
2016-03-11 00:48:53 +00:00
}
for (i = 0; i < this.store.length; ++i) {
record = this.store.at(i);
value = record.get('value');
2019-07-12 07:20:08 +00:00
if (value[0].toLocaleLowerCase() === charVal) {
2016-03-11 00:48:53 +00:00
if (null === firstRecord) firstRecord = record;
if (isEqualSelectRecord) {
if (selectRecord === record) {
isEqualSelectRecord = false;
}
continue;
}
if (record.get('selected')) continue;
selectItem(record);
return;
}
}
if (firstRecord) {
selectItem(firstRecord);
return;
}
}
Common.UI.DataView.prototype.onKeyDown.call(this, e, event);
},
onScrollToRecordCustom: function (record) {
var innerEl = $(this.el).find('.inner');
var inner_top = innerEl.offset().top;
var div = innerEl.find('#' + record.get('id')).parent();
var div_top = div.offset().top;
if (div_top < inner_top || div_top+div.height() > inner_top + innerEl.height()) {
if (this.scroller) {
this.scroller.scrollTop(innerEl.scrollTop() + div_top - inner_top, 0);
} else {
innerEl.scrollTop(innerEl.scrollTop() + div_top - inner_top);
}
}
},
textGroupDescription: 'Select Function Group',
textListDescription: 'Select Function',
sDescription: 'Description',
2020-05-07 14:08:59 +00:00
txtTitle: 'Insert Function',
txtSearch: 'Search',
txtRecommended: 'Recommended'
2016-03-11 00:48:53 +00:00
}, SSE.Views.FormulaDialog || {}));
});