(function() { var DOUBLE_CLICK_MS, activate, cleanUp, cmsFieldAndPastedContent, finishEditing, getCurrentContent, isNewlineAllowed, onBlur, onFocus, onInput, onKey, prepareForEditing, save; onKey = function(event, proxy) { var cmsField, key; cmsField = proxy.jQueryElement(); key = event.keyCode || event.which; switch (key) { case 13: if (!isNewlineAllowed(cmsField)) { event.preventDefault(); return cmsField.blur(); } break; case 27: event.stopPropagation(); return cmsField.blur(); } }; onInput = function(proxy) { return save(proxy); }; onFocus = function(proxy) { return prepareForEditing(proxy.jQueryElement()); }; onBlur = function(proxy) { save(proxy).done(function() { return proxy.trigger('scrivito_editors:blur'); }); return finishEditing(proxy); }; save = function(proxy) { var content; content = getCurrentContent(proxy.jQueryElement()); return proxy.save(content).done(function() { return proxy.trigger('scrivito_editors:save'); }); }; getCurrentContent = function(cmsField) { var clone, content; cleanUp(cmsField); clone = cmsFieldAndPastedContent(cmsField).clone(); clone.find('div:not(:has(br)),p:not(:first)').before('\n'); clone.find('br').replaceWith('\n'); content = clone.text(); clone.remove(); if (!isNewlineAllowed(cmsField)) { content = content.replace(/\n$/, ''); } return content; }; cleanUp = function(cmsField) { var pasted, siblings; siblings = cmsFieldAndPastedContent(cmsField); pasted = siblings.not(cmsField); if (pasted.length > 0) { pasted.remove(); return cmsField.text(siblings.text()); } }; cmsFieldAndPastedContent = function(cmsField) { var needsReset, siblings, siblingsBefore; siblingsBefore = cmsField.data('scrivito_editors_siblings_before_edit'); siblings = cmsField.siblings(); needsReset = !siblingsBefore || siblings.filter(siblingsBefore).length < siblingsBefore.length; if (needsReset) { return cmsField.data('scrivito_editors_siblings_before_edit', siblings); } else { return cmsField.siblings().addBack().not(siblingsBefore); } }; isNewlineAllowed = function(cmsField) { if (cmsField.data('scrivitoEditorsMultiline') === true) { return true; } if (cmsField.data('scrivitoEditorsMultiline') === false) { return false; } return cmsField.css('white-space').match(/pre/); }; prepareForEditing = function(cmsField) { var html, htmlNl2Br; if (isNewlineAllowed(cmsField) && !cmsField.data('scrivito_editors_prepared_for_editing')) { cmsField.data('scrivito_editors_prepared_for_editing', true); html = cmsField.html(); htmlNl2Br = html.replace(/\n/g, '
'); if (html !== htmlNl2Br) { return cmsField.html(htmlNl2Br); } } }; finishEditing = function(proxy) { var cmsField; cmsField = proxy.jQueryElement(); cmsField.data('scrivito_editors_prepared_for_editing', false); return cmsField.text(proxy.content()); }; DOUBLE_CLICK_MS = 300; activate = function(proxy) { var cmsField; cmsField = proxy.jQueryElement(); scrivito.editors.applyPlaceholder(cmsField); cmsField.attr('contenteditable', true).blur(function() { return onBlur(proxy); }).focus(function() { return onFocus(proxy); }).keypress(function(event) { return onKey(event, proxy); }).keyup(function(event) { return onKey(event, proxy); }); if (cmsField.attr('data-scrivito-editors-autosave') !== 'false') { cmsField.on('cut input keypress keyup paste', function() { return onInput(proxy); }); } prepareForEditing(cmsField); return cmsField.on('click', function(event) { cmsField.attr('contenteditable', true); cleanUp(cmsField); if (!(event.timeStamp - cmsField.data('scrivito_editors_last_click') < DOUBLE_CLICK_MS)) { event.preventDefault(); } return cmsField.data('scrivito_editors_last_click', event.timeStamp); }); }; scrivito.editors.string_editor = { can_edit: function(element) { return $(element).is('[data-scrivito-field-type=string]'); }, activate: function(element) { return activate(new scrivito.editors.DomContentProxy(element)); }, _activateWithProxy: function(proxy) { return activate(proxy); } }; scrivito.on('load', function() { return scrivito.define_editor('string', scrivito.editors.string_editor); }); }).call(this);