(function() { var activate, fetchDescription, 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)); } }; fetchDescription = function(cmsField, objId) { return scrivito.description_for_editor([objId]).then(function(descriptions) { var urlInput; urlInput = cmsField.find('[name=url][data-value]'); if (descriptions[0]) { return urlInput.val(descriptions[0]); } }); }; activate = function(cmsField) { var link; link = cmsField.scrivito('content') || {}; cmsField.addClass('scrivito_editors_ui').html(template(link)); if (link.obj_id) { fetchDescription(cmsField, link.obj_id); } scrivito.editors._linkTargetEditing.activate(cmsField, cmsField.find('li'), link); cmsField.on('scrivito_editors_private:target_changed', function() { return save(cmsField); }).on('blur', 'input', function() { return save(cmsField); }).on('click', '[data-scrivito-editors-browse]', function() { return openContentBrowser(cmsField); }); return cmsField.find('[name=url]').on('focus', function() { var input, raw; input = $(this); raw = input.attr('data-value'); if (raw) { input.val(raw); } return input.removeAttr('data-value'); }); }; template = function(link) { var objUrl, title, url; if (link.obj_id) { objUrl = URI(scrivito.path_for_id(link.obj_id)).query(link.query || null).fragment(link.fragment || null); } url = link.url || objUrl || ''; title = link.title || ''; return $("").find('[name=title]').val(title).end().find('[name=url]').attr('data-value', url).val(url).end(); }; openContentBrowser = function(cmsField) { var id, linkItem; linkItem = cmsField.find('li'); id = linkItem.attr('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; id = selection[0]; if (id != null) { url = scrivito.path_for_id(id); val = "Content browser selection (" + url + ")"; linkItem.attr('data-id', id).find('[name=url]').attr('data-value', url).val(val); save(cmsField); return fetchDescription(cmsField, id); } }); return false; }; save = function(cmsField) { var li, target, title, url, urlInput, value; li = cmsField.find('li'); target = li.attr('data-target'); title = li.find('[name=title]').val(); urlInput = li.find('[name=url]'); url = urlInput.attr('data-value') || urlInput.val(); value = url ? { target: target, title: title, url: url } : null; return cmsField.scrivito('save', value).done(function() { return cmsField.trigger('scrivito_editors:save'); }); }; }).call(this);