Newer
Older
Dries Buytaert
committed
/**
* @file
Jennifer Hodgdon
committed
* ContentEditable-based in-place editor for plain text content.
Dries Buytaert
committed
*/
(function ($, _, Drupal) {
Dries Buytaert
committed
"use strict";
Dries Buytaert
committed
Jennifer Hodgdon
committed
Drupal.quickedit.editors.plain_text = Drupal.quickedit.EditorView.extend(/** @lends Drupal.quickedit.editors.plain_text# */{
Dries Buytaert
committed
Jennifer Hodgdon
committed
/**
* Stores the textual DOM element that is being in-place edited.
*/
$textElement: null,
Dries Buytaert
committed
/**
Jennifer Hodgdon
committed
* @constructs
*
* @augments Drupal.quickedit.EditorView
*
* @param {object} options
*/
initialize: function (options) {
Drupal.quickedit.EditorView.prototype.initialize.call(this, options);
Dries Buytaert
committed
var editorModel = this.model;
var fieldModel = this.fieldModel;
Dries Buytaert
committed
Jennifer Hodgdon
committed
// Store the original value of this field. Necessary for reverting
// changes.
var $textElement;
var $fieldItems = this.$el.find('.field__item');
if ($fieldItems.length) {
$textElement = this.$textElement = $fieldItems.eq(0);
Dries Buytaert
committed
}
else {
$textElement = this.$textElement = this.$el;
}
editorModel.set('originalValue', $.trim(this.$textElement.text()));
Dries Buytaert
committed
Jennifer Hodgdon
committed
// Sets the state to 'changed' whenever the value changes.
var previousText = editorModel.get('originalValue');
$textElement.on('keyup paste', function (event) {
var currentText = $.trim($textElement.text());
if (previousText !== currentText) {
previousText = currentText;
editorModel.set('currentValue', currentText);
fieldModel.set('state', 'changed');
Dries Buytaert
committed
}
});
},
/**
Jennifer Hodgdon
committed
* @inheritdoc
*
* @return {jQuery}
*/
getEditedElement: function () {
return this.$textElement;
},
Dries Buytaert
committed
/**
Jennifer Hodgdon
committed
* @inheritdoc
*
* @param {object} fieldModel
* @param {string} state
* @param {object} options
*/
stateChange: function (fieldModel, state, options) {
var from = fieldModel.previous('state');
var to = state;
switch (to) {
case 'inactive':
break;
Jennifer Hodgdon
committed
case 'candidate':
if (from !== 'inactive') {
this.$textElement.removeAttr('contenteditable');
}
if (from === 'invalid') {
this.removeValidationErrors();
}
break;
Jennifer Hodgdon
committed
case 'highlighted':
break;
Jennifer Hodgdon
committed
case 'activating':
// Defer updating the field model until the current state change has
// propagated, to not trigger a nested state change event.
_.defer(function () {
fieldModel.set('state', 'active');
});
break;
Jennifer Hodgdon
committed
case 'active':
this.$textElement.attr('contenteditable', 'true');
break;
Jennifer Hodgdon
committed
case 'changed':
break;
Jennifer Hodgdon
committed
case 'saving':
if (from === 'invalid') {
this.removeValidationErrors();
}
this.save(options);
break;
Jennifer Hodgdon
committed
case 'saved':
break;
Jennifer Hodgdon
committed
case 'invalid':
this.showValidationErrors();
break;
}
},
/**
Jennifer Hodgdon
committed
* @inheritdoc
*
* @return {object}
*/
getQuickEditUISettings: function () {
return {padding: true, unifiedToolbar: false, fullWidthToolbar: false, popup: false};
},
Dries Buytaert
committed
/**
Jennifer Hodgdon
committed
* @inheritdoc
*/
revert: function () {
this.$textElement.html(this.model.get('originalValue'));
}
Dries Buytaert
committed
});
Dries Buytaert
committed
})(jQuery, _, Drupal);