(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);
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, 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) {
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.attr('data-value');
if (raw) {
input.val(raw);
}
return input.attr('data-value', null);
});
};
linkTemplate = function(link) {
var obj_url;
obj_url = link.obj_id && scrivito.path_for_id(link.obj_id);
return $("\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.attr('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.attr('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.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);