/*
 *  Copyright 2013 Anyware Services
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * Abbreviation action singleton.
 * Insert an abbreviation.
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.accessibility.Abbreviation', {
	singleton: true,
	
	/**
	 * Open a dialog box to select an abbreviation for the current selection.
	 * @param {Ametys.cms.editor.EditorButtonController} controller The controller calling this function
	 */
	apply: function (controller)
	{
		Ametys.cms.editor.InsertElementHelper.action(
			controller.getCurrentField(), // field
			Ametys.getPluginResourcesPrefix('cms') + '/img/content/edition/accessibility/abbr_16.png', // icon
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_ABBREVIATION_DIALOG_CAPTION}}", // title
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_ABBREVIATION_DIALOG_FIELD}}", // fieldLabel
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_ABBREVIATION_DIALOG_FIELD_VALUE}}", // defaultValue
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_ABBREVIATION_DIALOG_DESCRIPTION}}", // description
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_ABBREVIATION_DIALOG_POSTDESCRIPTION}}", // postDescription
			"abbr", // tagName
			"title" // attributeName
		);
	},
	
	/**
	 * Enable/disable and toggle/untoggle controller according the current selection
	 * @param {Ametys.cms.editor.EditorButtonController} controller The controller
	 * @param {Ametys.cms.form.widget.RichText} field The current field. Can be null
	 * @param {HTMLElement} node The current selected node. Can be null.
	 */
	selectionListener: function (controller, field, node)
	{
		Ametys.cms.editor.InsertElementHelper.listener(
			controller, // controller
			field, // field
			node, // node
			"abbr", // tagName
			null, // attributeName
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_ABBREVIATION_DIALOG_DESCRIPTION}}", // description
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_ABBREVIATION_DESCRIPTION_DISABLED}}" // disabledDescription
		);
	}
});

/**
 * Acronym action singleton.
 * Insert an acronym.
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.accessibility.Acronym', {
	singleton: true,

	/**
	 * Insert an acronym
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
	 */
	apply: function (controller)
	{
		Ametys.cms.editor.InsertElementHelper.action(
			controller.getCurrentField(), // field
			Ametys.getPluginResourcesPrefix('cms') + '/img/content/edition/accessibility/acronym_16.png', // icon
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_ACRONYM_DIALOG_CAPTION}}", // title
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_ACRONYM_DIALOG_FIELD}}", // fieldLabel
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_ACRONYM_DIALOG_FIELD_VALUE}}", // defaultValue
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_ACRONYM_DIALOG_DESCRIPTION}}", // description
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_ACRONYM_DIALOG_POSTDESCRIPTION}}", // postDescription
			"acronym", // tagName
			"title" // attributeName
		);
	},
	
	/**
	 * Enable/disable and toggle/untoggle controller according if the current selection contains a acronym or not 
	 * @param {Ametys.cms.editor.EditorButtonController} controller The controller
	 * @param {Ametys.cms.form.widget.RichText} field The current field. Can be null
	 * @param {HTMLElement} node The current selected node. Can be null.
	 */
	selectionListener: function (controller, field, node)
	{
		Ametys.cms.editor.InsertElementHelper.listener(
			controller,
			field, 
			node,
			"acronym", 
			null,
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_ACRONYM_DESCRIPTION}}", // description
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_ACRONYM_DESCRIPTION_DISABLED}}" // disabledDescription
		);
	}
});

/**
 * Quote the selection.
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.accessibility.Quote', {
	singleton: true,

	/**
	 * Quote the selection.
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
	 */
	apply: function (controller)
	{
		Ametys.cms.editor.InsertElementHelper.actionDirect({
			tagName: "q",
			field: controller.getCurrentField()
		});
	},
	
	/**
	 * Enable/disable and toggle/untoggle controller according if the current selection is quote or not
	 * @param {Ametys.cms.editor.EditorButtonController} controller The controller
	 * @param {Ametys.cms.form.widget.RichText} field The current field. Can be null
	 * @param {HTMLElement} node The current selected node. Can be null.
	 */
	selectionListener: function (controller, field, node)
	{
		Ametys.cms.editor.InsertElementHelper.listener(
			controller,
			field, 
			node,
			"q",
			null,
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_QUOTE_DESCRIPTION}}", 
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_QUOTE_DESCRIPTION_DISABLED}}"
		);
	}
});

/**
 *  Report a change of language.
 * @private
 */
Ext.define('Ametys.plugins.cms.editor.accessibility.Language', {
	singleton: true,

	/**
	 * Reports a change of language on the current selected text
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling this function
	 */
	apply: function (controller)
	{
		Ametys.cms.editor.InsertElementHelper.action(
			controller.getCurrentField(), // field
			Ametys.getPluginResourcesPrefix('cms') + '/img/content/edition/accessibility/language_16.png', // icon
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_LANGUAGE_DIALOG_CAPTION}}", // title
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_LANGUAGE_DIALOG_FIELD}}", // fieldLabel
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_LANGUAGE_DIALOG_FIELD_VALUE}}", // defaultValue
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_LANGUAGE_DIALOG_DESCRIPTION}}", // description
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_LANGUAGE_DIALOG_POSTDESCRIPTION}}", // postDescription
			"span", // tagName
			"lang", // attributeName
			"language" // className
		);
	},

	/**
	 * Enable/disable and toggle/untoggle controller according the current selection
	 * @param {Ametys.cms.editor.EditorButtonController} controller The controller
	 * @param {Ametys.cms.form.widget.RichText} field The current field. Can be null
	 * @param {HTMLElement} node The current selected node. Can be null.
	 */
	selectionListener: function (controller, field, node)
	{
		Ametys.cms.editor.InsertElementHelper.listener(
			controller, // controller
			field, // field
			node, // node 
			"span", // tagName
			"lang", // attributeName
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_LANGUAGE_DESCRIPTION}}", // description
			"{{i18n CONTENT_EDITION_ACCESSIBILITY_LANGUAGE_DESCRIPTION_DISABLED}}" // disabledDescription
		);
	}
});