(function() { var activate, addLink, attachHandlers, attachLinkHandlers, fetchDescriptions, linkTemplate, objIdsFor, openContentBrowser, removeLink, render, save, storeLastSaved; scrivito.on('load', function() { return scrivito.define_editor('linklist', scrivito.editors.linklist_editor); }); scrivito.editors.linklist_editor = { can_edit: function(element) { return $(element).is('[data-scrivito-field-type=linklist]'); }, activate: function(element) { return activate($(element)); } }; activate = function(cmsField) { var linklist; linklist = cmsField.scrivito('content'); render(cmsField, linklist); fetchDescriptions(cmsField, objIdsFor(linklist)); attachHandlers(cmsField, linklist); return storeLastSaved(cmsField, linklist); }; render = function(cmsField, linklist) { var j, len, link, ul; ul = $(''); for (j = 0, len = linklist.length; j < len; j++) { link = linklist[j]; ul.append(linkTemplate(link)); } return cmsField.addClass('scrivito_editors_ui').html(ul).append(''); }; fetchDescriptions = function(cmsField, objIds) { if (objIds.length) { return scrivito.description_for_editor(objIds).then(function(descriptions) { var i, j, len, objId, results, urlInput; results = []; for (i = j = 0, len = objIds.length; j < len; i = ++j) { objId = objIds[i]; urlInput = cmsField.find("[data-id='" + objId + "'] [name=url]"); if (descriptions[i] && urlInput.not(':focus')) { results.push(urlInput.val(descriptions[i])); } else { results.push(void 0); } } return results; }); } }; objIdsFor = function(linklist) { var j, len, link, results; results = []; for (j = 0, len = linklist.length; j < len; j++) { link = linklist[j]; if (link.obj_id) { results.push(link.obj_id); } } return results; }; attachHandlers = function(cmsField, linklist) { var index, j, len, li, ref, results, ul; ul = cmsField.find('ul'); scrivito.editors.Sortable.create(ul.get(0), { ghostClass: 'scrivito-sortable-placeholder', handle: '.scrivito-editors-handle', onUpdate: function() { return save(cmsField); } }); cmsField.on('scrivito_editors_private:target_changed', function() { return save(cmsField); }).on('blur', 'li input', function() { return save(cmsField); }).on('click', 'button.add-link', function() { return addLink(cmsField); }); ref = ul.find('li'); results = []; for (index = j = 0, len = ref.length; j < len; index = ++j) { li = ref[index]; results.push(attachLinkHandlers(cmsField, $(li), linklist[index])); } return results; }; attachLinkHandlers = function(cmsField, li, link) { scrivito.editors._linkTargetEditing.activate(cmsField, li, link); return li.on('click', '[data-scrivito-editors-browse]', function() { return openContentBrowser(cmsField, li); }).on('click', 'a.delete', function() { return removeLink(cmsField, li); }).find('[name=url]').on('focus', function() { var input, raw; input = $(this); raw = input.attr('data-value'); if (raw) { input.val(raw); } return input.attr('data-value', null); }); }; linkTemplate = function(link) { var objUrl, 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 || ''; return $("
  • \n \n \n \n
    \n
    \n \n \n \n \n
      \n
      \n \n \n \n \n \n \n
      \n
    • "); }; save = function(cmsField) { var input, lastSaved, li, target, title, url, value; value = (function() { var j, len, ref, results; ref = $(cmsField).find('li'); results = []; for (j = 0, len = ref.length; j < len; j++) { li = ref[j]; li = $(li); target = li.attr('data-target'); title = li.find('[name=title]').val(); input = li.find('[name=url]'); url = input.attr('data-value') || input.val(); if (url) { results.push({ target: target, title: title, url: url }); } else { results.push(null); } } return results; })(); value = $.grep(value, function(n) { return n; }); lastSaved = cmsField.attr('data-scrivito-editors-linklist-last-saved'); if (JSON.stringify(value) !== JSON.stringify(lastSaved)) { return cmsField.scrivito('save', value).done(function() { cmsField.trigger('scrivito_editors:save'); return storeLastSaved(cmsField, value); }); } }; addLink = function(cmsField) { var li; li = linkTemplate({}).appendTo(cmsField.children('ul')); attachLinkHandlers(cmsField, li, {}); return false; }; openContentBrowser = function(cmsField, li) { var id; id = li.attr('data-id'); scrivito.content_browser.open({ selection: id ? [id] : [], selection_mode: 'single', filters: cmsField.data('scrivitoEditorsFilters'), filter_context: cmsField.data('scrivitoEditorsFilterContext') }).done((function(_this) { return function(selection) { var url, val; if (id = selection[0]) { url = scrivito.path_for_id(id); val = "Content browser selection (" + url + ")"; li.attr('data-id', id).find('[name=url]').attr('data-value', url).val(val); save(cmsField); return fetchDescriptions(cmsField, [id]); } }; })(this)); return false; }; removeLink = function(cmsField, li) { li.remove(); save(cmsField); return false; }; storeLastSaved = function(cmsField, lastSaved) { return cmsField.attr('data-scrivito-editors-linklist-last-saved', lastSaved); }; }).call(this);