(function() { var activate, addLink, attachHandlers, attachLinkHandlers, fetchDescriptions, linkTemplate, 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, linklist); attachHandlers(cmsField); 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)); } cmsField.html(ul); return cmsField.append(''); }; fetchDescriptions = function(cmsField, linklist) { var link, obj_ids; obj_ids = (function() { var j, len, 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; })(); if (obj_ids.length) { return scrivito.description_for_editor(obj_ids).then(function(descriptions) { var i, j, len, obj_id, results, urlInput; results = []; for (i = j = 0, len = obj_ids.length; j < len; i = ++j) { obj_id = obj_ids[i]; urlInput = cmsField.find("[data-id='" + obj_id + "'] [name=url]"); if (!urlInput.is(':focus')) { results.push(urlInput.val(descriptions[i])); } else { results.push(void 0); } } return results; }); } }; attachHandlers = function(cmsField) { var j, len, li, ref, results, ul; ul = cmsField.find('ul'); ul.sortable({ update: function() { return save(cmsField); } }); cmsField.on('blur', 'li input', function() { return save(cmsField); }).on('click', 'button.add-link', function() { return addLink(cmsField); }); ref = ul.find('li'); results = []; for (j = 0, len = ref.length; j < len; j++) { li = ref[j]; results.push(attachLinkHandlers(cmsField, $(li))); } return results; }; attachLinkHandlers = function(cmsField, li) { return li.on('click', 'a.scrivito_open_content_browser', 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.data('value'); if (raw) { input.val(raw); } return input.data('value', null); }); }; linkTemplate = function(link) { var obj_url; obj_url = link.obj_id && ("/" + link.obj_id); return $("
  • \n \n \n
    \n \n \n \n \n \n \n
    \n
  • "); }; save = function(cmsField) { var input, lastSaved, li, 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); title = li.find('[name=title]').val(); input = li.find('[name=url]'); url = input.data('value') || input.val(); if (url) { results.push({ title: title, url: url }); } else { results.push(null); } } return results; })(); value = $.grep(value, function(n) { return n; }); lastSaved = cmsField.data('scrivito-editors-linklist-last-saved'); if (JSON.stringify(value) !== JSON.stringify(lastSaved)) { return cmsField.scrivito('save', value).done(function() { cmsField.trigger('save.scrivito_editors'); return storeLastSaved(cmsField, value); }); } }; addLink = function(cmsField) { var li; li = linkTemplate({}); li.appendTo(cmsField.find('ul')); attachLinkHandlers(cmsField, li); return false; }; openContentBrowser = function(cmsField, li) { var id; id = li.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 = "/" + id; val = "Content browser selection (" + url + ")"; li.data('id', id).find('[name=url]').data('value', url).val(val); return save(cmsField); } }; })(this)); return false; }; removeLink = function(cmsField, li) { li.remove(); save(cmsField); return false; }; storeLastSaved = function(cmsField, lastSaved) { return cmsField.data('scrivito-editors-linklist-last-saved', lastSaved); }; }).call(this);