/* global autosize */ ( function () { 'use strict'; /** * Dictionary of classes that will be used by different types of notices * TODO: Should probably review and rename these classes in the future to * be more unique to the translate extension? Some themes use warning, * error classes to style elements, and we do take help from these. */ var noticeTypes = { warning: 'warning', error: 'error', translateFail: 'translation-saving', diff: 'diff', fuzzy: 'fuzzy', getAllClasses: function () { var prop, classes = []; for ( prop in this ) { if ( typeof this[ prop ] === 'string' ) { classes.push( this[ prop ] ); } } return classes; } }; /** * TranslateEditor Plugin * Prepare the translation editor UI for a translation unit (message). * This is mainly used with the messagetable plugin, * but it is independent of messagetable. * Example usage: * * $( 'div.messageRow' ).translateeditor( { * message: messageObject // Mandatory message object * } ); * * Assumptions: The jquery element to which translateeditor is applied will * internally contain the editor's generated UI. So it is going to have the same width * and inherited properies of the container. * The container can mark the message item with class 'message'. This is not * mandatory, but if found, when the editor is opened, the message item will be hidden * and the editor will appear as if the message is replaced by the editor. * See the UI of Translate messagetable for a demo. * * @param {HTMLElement} element * @param {Object} options * @param {Function} [options.beforeSave] Callback to call when translation is going to be saved. * @param {Function} [options.onReady] Callback to call when the editor is ready. * @param {Function} [options.onSave] Callback to call when translation has been saved. * @param {Function} [options.onSkip] Callback to call when a message is skipped. * @param {Object} options.message Object as returned by messagecollection api. * @param {mw.translate.TranslationApiStorage} [options.storage] */ function TranslateEditor( element, options ) { this.$editTrigger = $( element ); this.$editor = null; this.options = options; this.message = this.options.message; this.$messageItem = this.$editTrigger.find( '.message' ); this.shown = false; this.dirty = false; this.saving = false; this.expanded = false; this.listen(); this.storage = this.options.storage || new mw.translate.TranslationApiStorage(); this.canDelete = mw.translate.canDelete(); this.delayValidation = delayer(); this.validating = null; } TranslateEditor.prototype = { /** * Initialize the plugin */ init: function () { // In case we have already created the editor earlier, // don't add a new one. The existing one may have unsaved // changes. if ( this.$editor ) { return; } this.render(); // onReady callback if ( this.options.onReady ) { this.options.onReady.call( this ); } }, /** * Render the editor UI */ render: function () { this.$editor = $( '