(function() {
var ScrivitoAnchor, activate, customOptions, defaultOptions, editorOptions;
ScrivitoAnchor = MediumEditor.extensions.anchor.extend({
name: 'scrivito_anchor',
contentDefault: '',
handleClick: function(event) {
var firstTextElement, linkElement, linkValue, selectedParent, selectionRange;
if (!this.isDisplayed()) {
selectionRange = MediumEditor.selection.getSelectionRange(this.document);
selectedParent = MediumEditor.selection.getSelectedParentElement(selectionRange);
firstTextElement = MediumEditor.util.getFirstTextNode(selectedParent);
linkElement = MediumEditor.util.getClosestTag(firstTextElement, 'a');
linkValue = $(linkElement).attr('href');
this.showForm(linkValue);
}
return false;
},
getTemplate: function() {
return "" + ("") + "" + "";
},
attachFormEvents: function(form) {
var input;
MediumEditor.extensions.anchor.prototype.attachFormEvents.call(this, form);
form = $(form);
input = form.find('.medium-editor-toolbar-input');
return form.find('.medium-editor-toolbar-browse').on('click', (function(_this) {
return function() {
var cmsField, id, selection;
selection = (id = scrivito.id_from_path(input.val())) ? [id] : [];
cmsField = $(_this.base.origElements);
scrivito.content_browser.open({
filter: cmsField.data('scrivitoEditorsFilter'),
filter_context: cmsField.data('scrivitoEditorsFilterContext'),
selection: selection,
selection_mode: 'single'
}).always(function() {
return input.focus();
}).done(function(selection) {
if (selection.length) {
return input.val(scrivito.path_for_id(selection[0]));
} else {
return input.val('');
}
});
return false;
};
})(this));
},
completeFormSave: function(opts) {
this.base.restoreSelection();
if (opts.value) {
this.execAction(this.action, opts);
} else {
this.execAction('unlink');
}
return this.base.checkSelection();
}
});
editorOptions = function() {
return $.extend({}, defaultOptions(), customOptions());
};
defaultOptions = function() {
return {
anchorPreview: false,
extensions: {
scrivito_anchor: new ScrivitoAnchor,
imageDragging: {}
},
placeholder: false,
toolbar: {
buttons: ['bold', 'italic', 'scrivito_anchor', 'h2', 'h3', 'unorderedlist', 'orderedlist'],
standardizeSelectionStart: true
}
};
};
customOptions = function() {
if (typeof scrivito.editors.medium_editor.options === 'function') {
return scrivito.editors.medium_editor.options();
} else {
return scrivito.editors.medium_editor.options;
}
};
activate = function(element) {
var cmsField;
cmsField = $(element);
return new MediumEditor(cmsField, editorOptions()).subscribe('editableInput', function() {
return cmsField.scrivito('save', cmsField.html());
});
};
scrivito.editors.medium_editor = {
can_edit: function(element) {
return $(element).is('[data-scrivito-field-type=html]');
},
activate: function(element) {
return activate(element);
},
default_options: defaultOptions,
options: function() {
return {};
}
};
scrivito.on('load', function() {
return scrivito.define_editor('medium', scrivito.editors.medium_editor);
});
}).call(this);