(function() { var activate, collectIds, contentBrowserButtonTemplate, getCmsField, onContentBrowserOpen, remove, render, renderItem, save; renderItem = function(objId) { return $("
  • \n \n
    \n \n \n \n
    \n
  • "); }; contentBrowserButtonTemplate = function() { return $(''); }; getCmsField = function(element) { return element.closest('[data-scrivito-field-type=referencelist]'); }; save = function(ids, cmsField, reload) { var lastSaved; if (reload == null) { reload = true; } lastSaved = cmsField.scrivito('content'); if (JSON.stringify(ids) !== JSON.stringify(lastSaved)) { return cmsField.scrivito('save', ids).done(function() { cmsField.trigger('save.scrivito_editors'); if (reload) { return cmsField.scrivito('reload'); } }); } }; onContentBrowserOpen = function(event) { var cmsField, filterContext, filters; event.preventDefault(); cmsField = getCmsField($(event.currentTarget)); filters = cmsField.data('scrivitoEditorsFilters'); filterContext = cmsField.data('scrivitoEditorsFilterContext'); return scrivito.content_browser.open({ filters: filters, filter_context: filterContext }).done((function(_this) { return function(selection) { var ids; ids = cmsField.scrivito('content').concat(selection); return save(ids, cmsField); }; })(this)); }; collectIds = function(cmsField) { var item, items, value; items = $(cmsField).find('[data-id]'); return value = (function() { var i, len, results; results = []; for (i = 0, len = items.length; i < len; i++) { item = items[i]; results.push($(item).data('id')); } return results; })(); }; remove = function(event) { var cmsField, ids, target; event.preventDefault(); target = $(event.currentTarget); cmsField = getCmsField(target); target.closest('li').remove(); ids = collectIds(cmsField); return save(ids, cmsField, false); }; render = function(cmsField) { var i, len, objId, objIds, ul; cmsField.empty(); objIds = cmsField.scrivito('content'); if (objIds.length) { ul = $('').appendTo(cmsField); for (i = 0, len = objIds.length; i < len; i++) { objId = objIds[i]; ul.append(renderItem(objId)); } scrivito.description_for_editor(objIds).then(function(descriptions) { return ul.find('li').each(function(index, element) { return $(element).find('> span').text(descriptions[index]); }); }); } return cmsField.append(contentBrowserButtonTemplate); }; activate = function(dom_element) { var cmsField; cmsField = $(dom_element); render(cmsField); return cmsField.on('click', 'li a.delete', remove).on('click', 'button.content-browser-open', onContentBrowserOpen).find('ul').sortable({ update: function(event) { var ids; cmsField = getCmsField($(event.target)); ids = collectIds(cmsField); return save(ids, cmsField, false); } }); }; scrivito.editors.referencelist_editor = { can_edit: function(element) { return $(element).is('[data-scrivito-field-type=referencelist]'); }, activate: function(element) { return activate(element); } }; scrivito.on('load', function() { return scrivito.define_editor('referencelist', scrivito.editors.referencelist_editor); }); }).call(this);