(function() { var activate, openContentBrowser, save, template; scrivito.on('load', function() { return scrivito.define_editor('link', scrivito.editors.link_editor); }); scrivito.editors.link_editor = { can_edit: function(element) { return $(element).is('[data-scrivito-field-type=link]'); }, activate: function(element) { return activate($(element)); } }; activate = function(cmsField) { var link, obj_id; link = cmsField.scrivito('content') || {}; cmsField.html(template(link)); if (obj_id = link.obj_id) { scrivito.description_for_editor([obj_id]).then(function(descriptions) { var urlInput; urlInput = cmsField.find('[name=url]'); if (!urlInput.is(':focus')) { return urlInput.val(descriptions[0]); } }); } cmsField.on('blur', 'li input', function() { return save(cmsField); }).on('click', 'a.scrivito_open_content_browser', function() { return openContentBrowser(cmsField); }); return cmsField.find('[name=url]').on('focus', function() { var input, raw; input = $(this); raw = input.data('value'); if (raw) { input.val(raw); } return input.data('value', null); }); }; template = function(link) { var obj_url; obj_url = link.obj_id && ("/" + link.obj_id); return $(""); }; openContentBrowser = function(cmsField) { var id, linkItem; linkItem = cmsField.find('li'); id = linkItem.data('id'); scrivito.content_browser.open({ selection: id ? [id] : [], selection_mode: 'single', filters: cmsField.data('scrivitoEditorsFilters'), filter_context: cmsField.data('scrivitoEditorsFilterContext') }).done(function(selection) { var url, val; if (id = selection[0]) { url = "/" + id; val = "Content browser selection (" + url + ")"; linkItem.data('id', id).find('[name=url]').data('value', url).val(val); return save(cmsField); } }); return false; }; save = function(cmsField) { var li, title, url, urlInput, value; li = cmsField.find('li'); title = li.find('[name=title]').val(); urlInput = li.find('[name=url]'); url = urlInput.data('value') || urlInput.val(); value = url ? { title: title, url: url } : null; return cmsField.scrivito('save', value).done(function() { return cmsField.trigger('save.scrivito_editors'); }); }; }).call(this);