$ -> # 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 ||= {} title = attributes['title'] || '' url = attributes['url'] || '' $("
") mediabrowserButtonTemplate = -> 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 -> storeLastSaved(cmsField, value) # Run when clicking the '...' button inside a li. onOpenMediabrowser = (event) -> event.preventDefault() linkItem = $(event.currentTarget).closest('li') cmsField = getCmsField(linkItem) filters = cmsField.data('filters') || cmsField.data('filter') Mediabrowser.open selection: [] filters: filters onSave: (selection) => onMediabrowserSaveLinkItem(selection, linkItem) # Media browser callback for saving a single link. onMediabrowserSaveLinkItem = (selection, linkItem) -> url = buildUrl(selection[0]) linkItem.find('[name=url]').val(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() url = item.find('[name=url]').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 = $('