Ext.namespace("Zarafa.plugins.spellchecker");
Zarafa.plugins.spellchecker.SpellcheckerPlugin=Ext.extend(Zarafa.core.Plugin,{initPlugin:function(){Zarafa.plugins.spellchecker.SpellcheckerPlugin.superclass.initPlugin.apply(this,arguments);this.registerInsertionPoint("context.settings.category.mail.aftercomposesettings",this.createSettingsWidget,this);this.registerInsertionPoint("common.htmleditor.tinymceconfig",this.createTinyMCEConfig,this)},getSupportedLanguages:function(){var a={};container.populateInsertionPoint("plugins.spellchecker.supportedlanguages",{scope:this,
languages:a});return a},getDefaultLanguage:function(){var a=container.getSettingsModel().get("zarafa/v1/plugins/spellchecker/spellchecker_language"),b=this.getUserSelectedLanguages(true);if(!Ext.isEmpty(a)&&b.indexOf(a)>=0)return a;switch(container.getSettingsModel().get("zarafa/v1/main/language").substr(0,2)){case "de":a="de_DE";break;case "fr":a="fr_FR";break;case "it":a="it_IT";break;case "es":a="es_ES";break;case "nl":a="nl_NL";break;case "pt":a="pt_PT";break;case "ru":a="ru_RU";break;default:a=
"en_US"}if(b.indexOf(a)<0)a=b.length>0?b[0]:"";container.getSettingsModel().set("zarafa/v1/plugins/spellchecker/spellchecker_language",a);return a},getUserSelectedLanguages:function(a){var b=this.getSupportedLanguages(),c=container.getSettingsModel().get("zarafa/v1/plugins/spellchecker/spellchecker_languages").split(", "),e=[];Ext.each(c,function(d){if(b.hasOwnProperty(d))a?e.push(d):e.push(b[d]+"="+d)});e.sort();return e},createTinyMCEConfig:function(a,b){var c=this.getUserSelectedLanguages();if(c.length!==
0){b.config.spellchecker_rpc_url="spellchecker.php";b.config.spellchecker_language=this.getDefaultLanguage();b.config.spellchecker_languages=c.join(", ");b.config.browser_spellcheck=false;if(!Ext.isDefined(b.config.external_plugins))b.config.external_plugins={};b.config.external_plugins.spellchecker="../../plugins/spellchecker/tinymce-plugin/spellchecker/plugin.min.js";b.config.toolbar1+=" | spellchecker"}},createSettingsWidget:function(){return{xtype:"zarafa.settingshtmleditorwidget"}}});
Zarafa.onReady(function(){container.registerPlugin(new Zarafa.core.PluginMetaData({name:"spellchecker",displayName:_("Spellchecker"),pluginConstructor:Zarafa.plugins.spellchecker.SpellcheckerPlugin}))});Ext.namespace("Zarafa.plugins.spellchecker.settings");
Zarafa.plugins.spellchecker.settings.SettingsHtmlEditorWidget=Ext.extend(Zarafa.settings.ui.SettingsWidget,{settingsLanguagesName:"zarafa/v1/plugins/spellchecker/spellchecker_languages",settingsDefaultLanguageName:"zarafa/v1/plugins/spellchecker/spellchecker_language",constructor:function(a){a=a||{};var b=container.getPluginByName("spellchecker"),c=b.getSupportedLanguages();Ext.applyIf(a,{title:_("HTML editor settings"),layout:"form",items:[]});if(Object.keys(c).length>0){c=b.getUserSelectedLanguages();
b=b.getDefaultLanguage();var e=[];Ext.each(c,function(f){f=f.split("=");e.push([f[1],f[0]])});var d=new Ext.data.ArrayStore({fields:["code","display"],data:e});a.items=[{xtype:"displayfield",hideLabel:true,value:_("Enable the following languages for the spell checker")+':<br><span style="opacity:0.5;">('+_("Note: When no language is selected the browsers spell checking features will be enabled if available")+")</span>"},{xtype:"checkboxgroup",name:this.settingsLanguagesName,ref:"spellCheckerLanguagesGroup",
columns:1,hideLabel:true,items:this.createSpellCheckerLanguageOptions(),listeners:{change:this.onSpellCheckerLanguagesChange,scope:this}},{xtype:"combo",name:this.settingsDefaultLanguageName,ref:"spellCheckerDefaultLanguageCombo",fieldLabel:_("Default selected language"),store:d,displayField:"display",valueField:"code",editable:false,mode:"local",triggerAction:"all",allowBlank:true,autoSelect:true,typeAhead:false,value:b,disabled:c.length===0?true:false,validateOnBlur:false,invalidClass:"",listeners:{change:this.onSpellCheckerLanguageChange,
scope:this}}]}else a.items=[{xtype:"displayfield",hideLabel:true,value:_("No language pack found. Ask your administrator to install language pack plugins to use the spellchecker in the HTML editor.")}];Zarafa.plugins.spellchecker.settings.SettingsHtmlEditorWidget.superclass.constructor.call(this,a)},createSpellCheckerLanguageOptions:function(){var a=container.getPluginByName("spellchecker"),b=a.getSupportedLanguages(),c=a.getUserSelectedLanguages(true),e=[];Ext.iterate(b,function(d,f){e.push({xtype:"checkbox",
name:"spellCheckerLanguages",inputValue:d,boxLabel:f,checked:c.indexOf(d)>=0?true:false})});e.sort(function(d,f){return d.boxLabel<f.boxLabel?-1:1});return e},update:function(a){if(Ext.isDefined(this.spellCheckerLanguagesGroup)){this.model=a;var b=a.get(this.settingsName);Ext.isDefined(b)||(b="");b=b.split(", ");Ext.each(this.spellCheckerLanguagesGroup.items.items,function(c){b.indexOf(c.getRawValue())>-1&&c.setValue(true)},this)}},onSpellCheckerLanguagesChange:function(a,b){if(this.model){var c=
[];Ext.each(b,function(g){c.push(g.getRawValue())});c.sort();c=c.join(", ");this.model.get(a.name)!==c&&this.model.set(a.name,c)}var e=this.spellCheckerDefaultLanguageCombo.getValue(),d=false,f=[];Ext.each(b,function(g){f.push([g.getRawValue(),g.boxLabel]);if(g.getRawValue()===e)d=true});this.spellCheckerDefaultLanguageCombo.getStore().loadData(f);if(b.length===0){this.spellCheckerDefaultLanguageCombo.setValue("");this.spellCheckerDefaultLanguageCombo.disable()}else{this.spellCheckerDefaultLanguageCombo.enable();
d||this.spellCheckerDefaultLanguageCombo.setValue(b[0].getRawValue())}},onSpellCheckerLanguageChange:function(a,b){this.model&&this.model.get(a.name)!==b&&this.model.set(a.name,b)}});Ext.reg("zarafa.settingshtmleditorwidget",Zarafa.plugins.spellchecker.settings.SettingsHtmlEditorWidget);
