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

468 lines
20 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
/**
*
* NameManagerDlg.js
*
* Created by Julia.Radzhabova on 01.06.15
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([ 'text!spreadsheeteditor/main/app/template/NameManagerDlg.template',
'common/main/lib/view/AdvancedSettingsWindow',
'common/main/lib/component/ComboBox',
'common/main/lib/component/ListView',
'common/main/lib/component/InputField'
], function (contentTemplate) {
'use strict';
SSE.Views = SSE.Views || {};
SSE.Views.NameManagerDlg = Common.Views.AdvancedSettingsWindow.extend(_.extend({
options: {
alias: 'NameManagerDlg',
2020-03-26 10:03:25 +00:00
contentWidth: 525,
2019-09-11 08:38:08 +00:00
height: 353,
buttons: null
2016-03-11 00:48:53 +00:00
},
initialize: function (options) {
var me = this;
_.extend(this.options, {
title: this.txtTitle,
template: [
'<div class="box" style="height:' + (this.options.height-85) + 'px;">',
'<div class="content-panel" style="padding: 0;">' + _.template(contentTemplate)({scope: this}) + '</div>',
'</div>',
2020-06-05 13:10:48 +00:00
'<div class="separator horizontal"></div>',
2016-03-11 00:48:53 +00:00
'<div class="footer center">',
'<button class="btn normal dlg-btn" result="cancel" style="width: 86px;">' + this.closeButtonText + '</button>',
'</div>'
].join('')
}, options);
this.api = options.api;
this.handler = options.handler;
this.sheets = options.sheets || [];
this.sheetNames = options.sheetNames || [];
this.ranges = options.ranges || [];
this.props = options.props;
this.sort = options.sort || {type: 'name', direction: 1}; // ascend
this.locked = options.locked || false;
this.userTooltip = true;
this.currentNamedRange = undefined;
this.rangesStore = new Common.UI.DataViewStore();
this.wrapEvents = {
onRefreshDefNameList: _.bind(this.onRefreshDefNameList, this),
onLockDefNameManager: _.bind(this.onLockDefNameManager, this)
};
Common.Views.AdvancedSettingsWindow.prototype.initialize.call(this, this.options);
},
render: function () {
Common.Views.AdvancedSettingsWindow.prototype.render.call(this);
var me = this;
this.cmbFilter = new Common.UI.ComboBox({
el : $('#name-manager-combo-filter'),
menuStyle : 'min-width: 100%;',
editable : false,
cls : 'input-group-nr',
data : [
{ value: 0, displayValue: this.textFilterAll },
{ value: 1, displayValue: this.textFilterDefNames },
{ value: 2, displayValue: this.textFilterTableNames },
{ value: 3, displayValue: this.textFilterSheet },
{ value: 4, displayValue: this.textFilterWorkbook }
],
takeFocusOnClose: true
2016-03-11 00:48:53 +00:00
}).on('selected', function(combo, record) {
me.refreshRangeList(null, 0);
});
this.cmbFilter.setValue(0);
this.rangeList = new Common.UI.ListView({
el: $('#name-manager-range-list', this.$window),
store: new Common.UI.DataViewStore(),
simpleAddMode: true,
emptyText: this.textEmpty,
itemTemplate: _.template([
'<div id="<%= id %>" class="list-item" style="width: 100%;display:inline-block;<% if (!lock) { %>pointer-events:none;<% } %>">',
2020-06-10 10:38:50 +00:00
'<div class="listitem-icon toolbar__icon <% print(isTable?"btn-menu-table":(isSlicer ? "btn-slicer" : "btn-named-range")) %>"></div>',
2021-04-26 11:54:55 +00:00
'<div style="width:141px;padding-right: 5px;"><%= Common.Utils.String.htmlEncode(name) %></div>',
2020-03-26 10:03:25 +00:00
'<div style="width:117px;padding-right: 5px;"><%= scopeName %></div>',
'<div style="width:204px;"><%= range %></div>',
2016-03-11 00:48:53 +00:00
'<% if (lock) { %>',
'<div class="lock-user"><%=lockuser%></div>',
'<% } %>',
'</div>'
].join('')),
tabindex: 1
2016-03-11 00:48:53 +00:00
});
this.rangeList.store.comparator = function(item1, item2) {
var n1 = item1.get(me.sort.type).toLowerCase(),
n2 = item2.get(me.sort.type).toLowerCase();
if (n1==n2) return 0;
return (n1<n2) ? -me.sort.direction : me.sort.direction;
};
this.rangeList.on('item:select', _.bind(this.onSelectRangeItem, this))
.on('item:keydown', _.bind(this.onKeyDown, this))
2016-03-16 08:41:59 +00:00
.on('item:dblclick', _.bind(this.onDblClickItem, this))
.on('entervalue', _.bind(this.onDblClickItem, this));
2016-03-11 00:48:53 +00:00
this.btnNewRange = new Common.UI.Button({
el: $('#name-manager-btn-new')
});
this.btnNewRange.on('click', _.bind(this.onEditRange, this, false));
this.btnEditRange = new Common.UI.Button({
el: $('#name-manager-btn-edit')
});
this.btnEditRange.on('click', _.bind(this.onEditRange, this, true));
this.btnDeleteRange = new Common.UI.Button({
el: $('#name-manager-btn-delete')
});
this.btnDeleteRange.on('click', _.bind(this.onDeleteRange, this));
$('#name-manager-sort-name').on('click', _.bind(this.onSortNames, this, 'name'));
$('#name-manager-sort-scope').on('click', _.bind(this.onSortNames, this, 'scopeName'));
this.spanSortName = $('#name-manager-sort-name-span');
this.spanSortScope = $('#name-manager-sort-scope-span');
(this.sort.type=='name') ? this.spanSortScope.addClass('hidden') : this.spanSortName.addClass('hidden');
if (this.sort.direction<0) {
(this.sort.type=='name') ? this.spanSortName.addClass('sort-desc') : this.spanSortScope.addClass('sort-desc');
}
this.afterRender();
},
getFocusedComponents: function() {
2021-04-14 22:02:11 +00:00
return [ this.cmbFilter, this.rangeList, this.btnNewRange, this.btnEditRange, this.btnDeleteRange ];
},
2020-10-22 10:53:58 +00:00
getDefaultFocusableComponent: function () {
return this.rangeList;
},
2016-03-11 00:48:53 +00:00
afterRender: function() {
this._setDefaults(this.props);
},
_setDefaults: function (props) {
this.refreshRangeList(this.ranges, 0);
this.api.asc_registerCallback('asc_onLockDefNameManager', this.wrapEvents.onLockDefNameManager);
this.api.asc_registerCallback('asc_onRefreshDefNameList', this.wrapEvents.onRefreshDefNameList);
},
onRefreshDefNameList: function() {
this.refreshRangeList(this.api.asc_getDefinedNames(Asc.c_oAscGetDefinedNamesList.All), this.currentNamedRange);
2016-03-11 00:48:53 +00:00
},
refreshRangeList: function(ranges, selectedItem) {
if (ranges) {
this.ranges = ranges;
var arr = [];
for (var i=0; i<this.ranges.length; i++) {
var scope = this.ranges[i].asc_getScope(),
2020-06-10 10:38:50 +00:00
id = this.ranges[i].asc_getIsLock(),
type = this.ranges[i].asc_getType();
2016-03-11 00:48:53 +00:00
arr.push({
name: this.ranges[i].asc_getName(true),
2016-03-11 00:48:53 +00:00
scope: scope,
scopeName: (scope===null) ? this.textWorkbook: this.sheetNames[scope],
2020-06-10 12:32:23 +00:00
range: (type===Asc.c_oAscDefNameType.slicer) ? '' : this.ranges[i].asc_getRef(),
2016-03-11 00:48:53 +00:00
lock: (id!==null && id!==undefined),
lockuser: (id) ? (this.isUserVisible(id) ? this.getUserName(id) : this.lockText) : this.guestText,
2020-06-10 10:38:50 +00:00
type: type,
isTable: type===Asc.c_oAscDefNameType.table,
isSlicer: type===Asc.c_oAscDefNameType.slicer
2016-03-11 00:48:53 +00:00
});
}
this.rangesStore.reset(arr);
this.rangeList.setEmptyText((this.rangesStore.length>0) ? this.textnoNames : this.textEmpty);
}
var me = this,
store = this.rangeList.store,
models = this.rangesStore.models,
val = this.cmbFilter.getValue(),
isTableFilter = (val<3) ? (val==2) : -1,
isWorkbook = (val>2) ? (val==4) : -1;
if (val>0)
models = this.rangesStore.filter(function(item) {
if (isTableFilter!==-1)
return (isTableFilter===item.get('isTable'));
if (isWorkbook!==-1)
return (isWorkbook===(item.get('scope')===null));
return false;
});
store.reset(models, {silent: false});
val = store.length;
this.btnEditRange.setDisabled(!val);
this.btnDeleteRange.setDisabled(!val);
if (val>0) {
if (selectedItem===undefined || selectedItem===null) selectedItem = 0;
if (_.isNumber(selectedItem)) {
if (selectedItem>val-1) selectedItem = val-1;
this.rangeList.selectByIndex(selectedItem);
setTimeout(function() {
me.rangeList.scrollToRecord(store.at(selectedItem));
}, 50);
} else if (selectedItem){ // object
var rec = store.findWhere({name: selectedItem.asc_getName(true), scope: selectedItem.asc_getScope()});
2016-03-11 00:48:53 +00:00
if (rec) {
this.rangeList.selectRecord(rec);
setTimeout(function() {
me.rangeList.scrollToRecord(rec);
}, 50);
}
}
if (this.userTooltip===true && this.rangeList.cmpEl.find('.lock-user').length>0)
this.rangeList.cmpEl.on('mouseover', _.bind(me.onMouseOverLock, me)).on('mouseout', _.bind(me.onMouseOutLock, me));
}
_.delay(function () {
2016-03-16 08:41:59 +00:00
me.rangeList.scroller.update({alwaysVisibleY: true});
2016-03-11 00:48:53 +00:00
}, 100, this);
},
onMouseOverLock: function (evt, el, opt) {
if (this.userTooltip===true && $(evt.target).hasClass('lock-user')) {
var me = this,
tipdata = $(evt.target).tooltip({title: this.tipIsLocked,trigger:'manual'}).data('bs.tooltip');
this.userTooltip = tipdata.tip();
this.userTooltip.css('z-index', parseInt(this.$window.css('z-index')) + 10);
tipdata.show();
setTimeout(function() { me.userTipHide(); }, 5000);
}
},
userTipHide: function () {
if (typeof this.userTooltip == 'object') {
this.userTooltip.remove();
this.userTooltip = undefined;
this.rangeList.cmpEl.off('mouseover').off('mouseout');
}
},
onMouseOutLock: function (evt, el, opt) {
if (typeof this.userTooltip == 'object') this.userTipHide();
},
onEditRange: function (isEdit) {
if (this._isWarningVisible) return;
2016-03-11 00:48:53 +00:00
if (this.locked) {
Common.NotificationCenter.trigger('namedrange:locked');
return;
}
var me = this,
xy = me.$window.offset(),
rec = this.rangeList.getSelectedRec(),
2019-06-27 11:18:01 +00:00
idx = _.indexOf(this.rangeList.store.models, rec),
2020-06-10 10:38:50 +00:00
oldname = (isEdit && rec) ? new Asc.asc_CDefName(rec.get('name'), rec.get('range'), rec.get('scope'), rec.get('type'), undefined, undefined, undefined, true) : null;
2016-03-11 00:48:53 +00:00
var win = new SSE.Views.NamedRangeEditDlg({
api: me.api,
sheets : this.sheets,
props : (isEdit) ? oldname : this.props,
isEdit : isEdit,
handler : function(result, settings) {
if (result == 'ok' && settings) {
if (isEdit) {
me.currentNamedRange = settings;
me.api.asc_editDefinedNames(oldname, settings);
} else {
me.cmbFilter.setValue(0);
me.currentNamedRange = settings;
me.api.asc_setDefinedNames(settings);
}
}
}
}).on('close', function() {
me.show();
2021-04-14 22:02:11 +00:00
setTimeout(function(){ me.getDefaultFocusableComponent().focus(); }, 100);
2016-03-11 00:48:53 +00:00
});
me.hide();
win.show(xy.left + 65, xy.top + 77);
},
onDeleteRange: function () {
var rec = this.rangeList.getSelectedRec();
2019-06-27 11:18:01 +00:00
if (rec) {
var me = this;
me._isWarningVisible = true;
Common.UI.warning({
msg: Common.Utils.String.format(me.warnDelete, rec.get('name')),
buttons: ['ok', 'cancel'],
callback: function(btn) {
if (btn == 'ok') {
me.currentNamedRange = _.indexOf(me.rangeList.store.models, rec);
me.api.asc_delDefinedNames(new Asc.asc_CDefName(rec.get('name'), rec.get('range'), rec.get('scope'), rec.get('type'), undefined, undefined, undefined, true));
}
setTimeout(function(){ me.getDefaultFocusableComponent().focus(); }, 100);
me._isWarningVisible = false;
}
});
2016-03-11 00:48:53 +00:00
}
},
getSettings: function() {
return this.sort;
},
onPrimary: function() {
return true;
},
onDlgBtnClick: function(event) {
this.handler && this.handler.call(this, event.currentTarget.attributes['result'].value);
this.close();
},
onSortNames: function(type) {
if (type !== this.sort.type) {
this.sort = {type: type, direction: 1};
this.spanSortName.toggleClass('hidden');
this.spanSortScope.toggleClass('hidden');
} else {
this.sort.direction = -this.sort.direction;
}
var sorted = (type=='name') ? this.spanSortName : this.spanSortScope;
(this.sort.direction>0) ? sorted.removeClass('sort-desc') : sorted.addClass('sort-desc');
this.rangeList.store.sort();
this.rangeList.onResetItems();
2016-03-16 08:41:59 +00:00
this.rangeList.scroller.update({alwaysVisibleY: true});
2016-03-11 00:48:53 +00:00
},
getUserName: function(id){
var usersStore = SSE.getCollection('Common.Collections.Users');
if (usersStore){
var rec = usersStore.findUser(id);
if (rec)
2021-03-26 11:29:16 +00:00
return AscCommon.UserInfoParser.getParsedName(rec.get('username'));
2016-03-11 00:48:53 +00:00
}
return this.guestText;
},
isUserVisible: function(id){
var usersStore = SSE.getCollection('Common.Collections.Users');
if (usersStore){
var rec = usersStore.findUser(id);
if (rec)
return !rec.get('hidden');
}
return true;
},
2016-03-11 00:48:53 +00:00
onSelectRangeItem: function(lisvView, itemView, record) {
2020-11-16 16:00:40 +00:00
if (!record) return;
2016-03-11 00:48:53 +00:00
this.userTipHide();
var rawData = {},
isViewSelect = _.isFunction(record.toJSON);
if (isViewSelect){
if (record.get('selected')) {
rawData = record.toJSON();
} else {// record deselected
return;
}
this.currentNamedRange = _.indexOf(this.rangeList.store.models, record);
this.btnEditRange.setDisabled(rawData.lock);
2020-06-10 10:38:50 +00:00
this.btnDeleteRange.setDisabled(rawData.lock || rawData.isTable || rawData.isSlicer);
2016-03-11 00:48:53 +00:00
}
},
hide: function () {
this.userTipHide();
Common.UI.Window.prototype.hide.call(this);
},
close: function () {
this.userTipHide();
this.api.asc_unregisterCallback('asc_onLockDefNameManager', this.wrapEvents.onLockDefNameManager);
this.api.asc_unregisterCallback('asc_onRefreshDefNameList', this.wrapEvents.onRefreshDefNameList);
Common.UI.Window.prototype.close.call(this);
},
onKeyDown: function (lisvView, record, e) {
if (e.keyCode==Common.UI.Keys.DELETE && !this.btnDeleteRange.isDisabled())
this.onDeleteRange();
},
onDblClickItem: function (lisvView, record, e) {
if (!this.btnEditRange.isDisabled())
this.onEditRange(true);
},
onLockDefNameManager: function(state) {
this.locked = (state == Asc.c_oAscDefinedNameReason.LockDefNameManager);
2016-03-11 00:48:53 +00:00
},
txtTitle: 'Name Manager',
closeButtonText : 'Close',
textDataRange: 'Data Range',
textNew: 'New',
textEdit: 'Edit',
textDelete: 'Delete',
textRanges: 'Named Ranges',
textScope: 'Scope',
textFilter: 'Filter',
textEmpty: 'No named ranges have been created yet.<br>Create at least one named range and it will appear in this field.',
textnoNames: 'No named ranges matching your filter could be found.',
textFilterAll: 'All',
textFilterDefNames: 'Defined names',
textFilterTableNames: 'Table names',
textFilterSheet: 'Names Scoped to Sheet',
textFilterWorkbook: 'Names Scoped to Workbook',
textWorkbook: 'Workbook',
guestText: 'Guest',
tipIsLocked: 'This element is being edited by another user.',
warnDelete: 'Are you sure you want to delete the name {0}?',
lockText: 'Locked'
2016-03-11 00:48:53 +00:00
}, SSE.Views.NameManagerDlg || {}));
});