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

262 lines
11 KiB
JavaScript
Raw Normal View History

2019-07-31 12:46:50 +00:00
/*
*
* (c) Copyright Ascensio System SIA 2010-2019
*
* 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.
*
* 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
*
*/
/**
* User: Julia.Radzhabova
* Date: 30.07.19
*/
define([
'core',
'spreadsheeteditor/main/app/view/Spellcheck'
], function () {
'use strict';
SSE.Controllers.Spellcheck = Backbone.Controller.extend(_.extend({
models: [],
collections: [
],
views: [
'Spellcheck'
],
initialize: function() {
var me = this;
this.addListeners({
'Spellcheck': {
'show': function() {
me._initSettings && me.loadLanguages();
me.onClickNext();
2019-07-31 12:46:50 +00:00
},
'hide': function() {
me.api && me.api.asc_cancelSpellCheck();
2019-07-31 12:46:50 +00:00
}
}
});
},
events: function() {
},
onLaunch: function() {
this.panelSpellcheck= this.createView('Spellcheck', {
});
this.panelSpellcheck.on('render:after', _.bind(this.onAfterRender, this));
this._isDisabled = false;
this._initSettings = true;
2019-07-31 12:46:50 +00:00
},
setApi: function(api) {
this.api = api;
2019-08-05 14:08:16 +00:00
this.api.asc_registerCallback('asc_onSpellCheckVariantsFound', _.bind(this.onSpellCheckVariantsFound, this));
this.api.asc_registerCallback('asc_onEditCell', _.bind(this.onApiEditCell, this));
2019-07-31 12:46:50 +00:00
return this;
},
setMode: function(mode) {
this.mode = mode;
2019-08-05 13:37:09 +00:00
if (this.panelSpellcheck) {
this.panelSpellcheck.btnToDictionary.setVisible(mode.isDesktopApp);
}
2019-07-31 12:46:50 +00:00
return this;
},
onAfterRender: function(panelSpellcheck) {
panelSpellcheck.buttonNext.on('click', _.bind(this.onClickNext, this));
2019-08-05 13:37:09 +00:00
panelSpellcheck.btnToDictionary.on('click', _.bind(this.onDictionary, this));
2019-08-05 12:13:29 +00:00
panelSpellcheck.cmbDictionaryLanguage.on('selected', _.bind(this.onSelectLanguage, this));
2019-08-05 14:08:16 +00:00
panelSpellcheck.btnChange.on('click', _.bind(this.onClickChange, this));
panelSpellcheck.btnIgnore.on('click', _.bind(this.onClickIgnore, this));
panelSpellcheck.btnChange.menu.on('item:click', _.bind(this.onClickChangeMenu, this));
panelSpellcheck.btnIgnore.menu.on('item:click', _.bind(this.onClickIgnoreMenu, this));
},
onClickNext: function() {
if (this.api) {
this.api.asc_nextWord();
}
2019-08-28 10:33:29 +00:00
Common.NotificationCenter.trigger('edit:complete', this, {restorefocus:true});
2019-07-31 12:46:50 +00:00
},
2019-08-05 13:37:09 +00:00
onDictionary: function() {
if (this.api) {
2019-08-06 06:33:16 +00:00
var str = this.panelSpellcheck.currentWord.getValue();
str && this.api.asc_spellCheckAddToDictionary(str);
2019-08-05 13:37:09 +00:00
}
},
2019-07-31 12:46:50 +00:00
SetDisabled: function(state) {
this._isDisabled = state;
},
2019-08-05 13:37:09 +00:00
setLanguages: function (array) {
this.languages = array;
this._initSettings = true;
2019-08-05 13:37:09 +00:00
},
loadLanguages: function () {
2019-08-05 15:02:40 +00:00
var value = Common.localStorage.getItem("sse-spellcheck-locale");
2019-08-06 06:33:16 +00:00
if (value)
value = parseInt(value);
else
value = this.mode.lang ? parseInt(Common.util.LanguageInfo.getLocalLanguageCode(this.mode.lang)) : 0x0409;
2019-08-05 15:02:40 +00:00
var combo = this.panelSpellcheck.cmbDictionaryLanguage;
2019-08-05 13:37:09 +00:00
if (this.languages && this.languages.length>0) {
var langs = [], info,
allLangs = Common.util.LanguageInfo.getLanguages();
this.languages.forEach(function (code) {
2019-08-05 21:03:19 +00:00
code = parseInt(code);
if (allLangs.hasOwnProperty(code)) {
info = allLangs[code];
2019-08-05 13:37:09 +00:00
langs.push({
displayValue: info[1],
2019-08-05 15:35:06 +00:00
shortName: info[0],
2019-08-05 21:03:19 +00:00
value: code
2019-08-05 13:37:09 +00:00
});
}
});
2019-08-05 15:35:06 +00:00
langs.sort(function(a, b){
if (a.shortName < b.shortName) return -1;
if (a.shortName > b.shortName) return 1;
return 0;
});
2019-08-05 15:02:40 +00:00
combo.setData(langs);
2019-08-05 21:03:19 +00:00
var item = combo.store.findWhere({value: value});
if (!item && allLangs[value]) {
value = allLangs[value][0].split(/[\-\_]/)[0];
item = combo.store.find(function(model){
return model.get('shortName').indexOf(value)==0;
});
2019-08-05 15:02:40 +00:00
}
2019-08-05 21:03:19 +00:00
combo.setValue(item ? item.get('value') : langs[0].value);
value = combo.getValue();
2019-08-05 15:02:40 +00:00
} else {
combo.setValue(Common.util.LanguageInfo.getLocalLanguageName(value)[1]);
combo.setDisabled(true);
}
this.api.asc_setDefaultLanguage(value);
this._initSettings = false;
2019-08-05 12:13:29 +00:00
},
onSelectLanguage: function (combo, record) {
2019-08-05 15:04:32 +00:00
var lang = record.value;
2019-08-05 12:13:29 +00:00
if (this.api && lang) {
2019-08-05 15:02:40 +00:00
this.api.asc_setDefaultLanguage(lang);
Common.localStorage.setItem("sse-spellcheck-locale", this.panelSpellcheck.cmbDictionaryLanguage.getValue());
2019-08-05 12:13:29 +00:00
}
2019-08-28 10:33:29 +00:00
Common.NotificationCenter.trigger('edit:complete', this, {restorefocus:true});
2019-08-05 14:08:16 +00:00
},
onClickChange: function (btn, e) {
if (this.api) {
2019-08-05 15:02:40 +00:00
var rec = this.panelSpellcheck.suggestionList.getSelectedRec();
rec && this.api.asc_replaceMisspelledWord(rec.get('value'), this._currentSpellObj);
2019-08-05 14:08:16 +00:00
}
2019-08-28 10:33:29 +00:00
Common.NotificationCenter.trigger('edit:complete', this, {restorefocus:true});
2019-08-05 14:08:16 +00:00
},
onClickChangeMenu: function (menu, item) {
2019-08-05 15:02:40 +00:00
if (this.api) {
var rec = this.panelSpellcheck.suggestionList.getSelectedRec();
2019-08-05 14:08:16 +00:00
if (item.value == 0) {
2019-08-05 15:02:40 +00:00
rec && this.api.asc_replaceMisspelledWord(rec.get('value'), this._currentSpellObj);
2019-08-05 14:08:16 +00:00
} else if (item.value == 1) {
2019-08-05 15:02:40 +00:00
rec && this.api.asc_replaceMisspelledWord(rec.get('value'), this._currentSpellObj, true);
2019-08-05 14:08:16 +00:00
}
2019-08-05 15:02:40 +00:00
}
2019-08-28 10:33:29 +00:00
Common.NotificationCenter.trigger('edit:complete', this, {restorefocus:true});
2019-08-05 14:08:16 +00:00
},
onClickIgnore: function () {
if (this.api) {
2019-08-05 15:02:40 +00:00
this.api.asc_ignoreMisspelledWord(this._currentSpellObj, false)
2019-08-05 14:08:16 +00:00
}
2019-08-28 10:33:29 +00:00
Common.NotificationCenter.trigger('edit:complete', this, {restorefocus:true});
2019-08-05 14:08:16 +00:00
},
2019-08-05 15:02:40 +00:00
onClickIgnoreMenu: function (menu, item) {
if (this.api) {
2019-08-05 14:08:16 +00:00
if (item.value == 0) {
2019-08-05 15:02:40 +00:00
this.api.asc_ignoreMisspelledWord(this._currentSpellObj, false);
2019-08-05 14:08:16 +00:00
} else if (item.value == 1) {
2019-08-05 15:02:40 +00:00
this.api.asc_ignoreMisspelledWord(this._currentSpellObj, true);
2019-08-05 14:08:16 +00:00
}
2019-08-05 15:02:40 +00:00
}
2019-08-28 10:33:29 +00:00
Common.NotificationCenter.trigger('edit:complete', this, {restorefocus:true});
2019-08-05 14:08:16 +00:00
},
onSpellCheckVariantsFound: function (property) {
2019-08-05 15:02:40 +00:00
this._currentSpellObj = property;
2019-08-05 21:03:19 +00:00
var arr = [],
word;
if (property) {
word = property.get_Word();
var variants = property.get_Variants();
2019-08-06 06:33:16 +00:00
variants && variants.forEach(function (item) {
2019-08-05 21:03:19 +00:00
var rec = new Common.UI.DataViewModel();
rec.set({
value: item
});
arr.push(rec);
2019-08-05 14:08:16 +00:00
});
2019-08-05 21:03:19 +00:00
}
var disabled = this.api.isCellEdited;
2019-08-05 21:03:19 +00:00
this.panelSpellcheck.currentWord.setValue(word || '');
2019-08-05 14:08:16 +00:00
this.panelSpellcheck.suggestionList.store.reset(arr);
2019-08-06 06:33:16 +00:00
(arr.length>0) && this.panelSpellcheck.suggestionList.selectByIndex(0);
this.panelSpellcheck.currentWord.setDisabled(!word || disabled);
this.panelSpellcheck.btnChange.setDisabled(arr.length<1 || disabled);
this.panelSpellcheck.btnIgnore.setDisabled(!word || disabled);
this.panelSpellcheck.btnToDictionary.setDisabled(!word || disabled);
2019-08-05 21:03:19 +00:00
this.panelSpellcheck.lblComplete.toggleClass('hidden', !property || !!word);
this.panelSpellcheck.buttonNext.setDisabled(!this.panelSpellcheck.lblComplete.hasClass('hidden'));
},
onApiEditCell: function(state) {
if (state == Asc.c_oAscCellEditorState.editEnd) {
this.panelSpellcheck.buttonNext.setDisabled(!this.panelSpellcheck.lblComplete.hasClass('hidden'));
this.panelSpellcheck.cmbDictionaryLanguage.setDisabled(false);
} else {
this.panelSpellcheck.buttonNext.setDisabled(true);
this.panelSpellcheck.currentWord.setDisabled(true);
this.panelSpellcheck.btnChange.setDisabled(true);
this.panelSpellcheck.btnIgnore.setDisabled(true);
this.panelSpellcheck.btnToDictionary.setDisabled(true);
this.panelSpellcheck.cmbDictionaryLanguage.setDisabled(true);
}
2019-07-31 12:46:50 +00:00
}
}, SSE.Controllers.Spellcheck || {}));
});