$ -> # An editor for CMS linklist attributes. # Creates the DOM for one link element of the linklist and substitutes the # title and url attribute. template = (attributes) -> attributes ||= {} description = attributes['description'] || '' title = attributes['title'] || '' url = attributes['url'] || '' $("
") contentBrowserButtonTemplate = -> icon = $('') .addClass('editing-icon') .addClass('editing-icon-plus') button = $('') .addClass('editing-button') .addClass('editing-green') .addClass('add-link') .html(icon) button # Returns the closest linklist DOM element. getCmsField = (element) -> element.closest('[data-scrivito-field-type=linklist]') # Saves the entire linklist to the CMS and stores the last successfully saved value. save = (cmsField) -> value = getAttributes(cmsField) lastSaved = getLastSaved(cmsField) unless JSON.stringify(value) == JSON.stringify(lastSaved) cmsField.scrivito('save', value).done -> cmsField.trigger('save.scrivito_editors') storeLastSaved(cmsField, value) # Run when clicking the '...' button inside a li. onOpenContentBrowser = (event) -> event.preventDefault() linkItem = $(event.currentTarget).closest('li') cmsField = getCmsField(linkItem) filters = cmsField.data('filters') || cmsField.data('filter') id = linkItem.data('id') selection = if id then [id] else [] scrivito.content_browser.open selection: selection filters: filters selection_mode: 'single' .done (selection) => onContentBrowserSaveLinkItem(selection, linkItem) # Resource browser callback for saving a single link. onContentBrowserSaveLinkItem = (selection, linkItem) -> url = buildUrl(selection[0]) linkItem.find('[name=url]') .data "value", url .val "Content browser selection (#{url})" # trigger save after inserting the value cmsField = getCmsField(linkItem) save(cmsField) true # Transforms an obj id into an url that can be parsed by Scrivito # to establish an internal link. buildUrl = (id) -> "/#{id}" # Collects all link attributes for a given linklist. getAttributes = (cmsField) -> items = $(cmsField).find('li') attributes = for item in items item = $(item) title = item.find('[name=title]').val() input = item.find('[name=url]') url = input.data('value') || input.val() # Make sure the url is not empty. if !isEmpty(url) 'title': title 'url': url # Remove empty array elements. removeEmptyElements(attributes) isEmpty = (value) -> !value removeEmptyElements = (array) -> $.grep(array, (n) -> n) # Adds a new link to the linklist. addLink = (event) -> event.preventDefault() cmsField = getCmsField($(event.currentTarget)) content = $('