/** * Silex, live web creation * http://projects.silexlabs.org/?/silex/ * * Copyright (c) 2012 Silex Labs * http://www.silexlabs.org/ * * Silex is available under the GPL license * http://www.silexlabs.org/silex/silex-licensing/ */ /** * @fileoverview The dialog to edit links * */ import {LinkData} from '../../types'; import {SilexNotification} from '../../utils/notification'; export const LINK_ATTRIBUTES = ['href', 'rel', 'target', 'type', 'title', 'download']; const DEFAULT_LINK_DATA = { href: '', target: '', title: '', rel: '', type: '', download: '', }; /** * the LinkDialog class */ export class LinkDialog { constructor(private model) {} open( linkDataArg: LinkData, pageNames: string[], cbk: (p1: LinkData) => any) { // default values for new link const linkData = Object.assign({}, DEFAULT_LINK_DATA, linkDataArg); // external link data const isExternal = !linkData.href.startsWith('#!'); SilexNotification.prompt(` Link editor Help `, 'unused', 'unused', 'unused', (accept, unused) => { if (accept) { // get new values const newData: LinkData = LINK_ATTRIBUTES.reduce((acc, attr) => { const el = dialogBody.querySelector('.' + attr) as HTMLInputElement; if (!el) { console.error('could not get data from for attribute', attr); } else { acc[attr] = el.value; } return acc; }, {}); // internal link info const newIsExternal = (dialogBody.querySelector('#link-editor-external') as HTMLInputElement).checked; const page = (dialogBody.querySelector('.page') as HTMLInputElement).value; const options: LinkData = {href: newIsExternal ? newData.href : page}; if (newData.target !== '') { options.target = newData.target; } if (newData.rel !== '') { options.rel = newData.rel; } if (newData.title !== '') { options.title = newData.title; } if (newData.type !== '') { options.type = newData.type; } if (newData.download !== '') { options.download = newData.download; } cbk(options); } }); // add a remove link button const fragmentButtons = document.createElement('fragment'); fragmentButtons.innerHTML = ` `; (fragmentButtons.querySelector('.alertify-button-remove') as HTMLElement).onclick = (e) => { SilexNotification.close(); cbk(null); }; SilexNotification.addButton(fragmentButtons); // add info about the link const dialogBody = document.createElement('div'); dialogBody.insertAdjacentHTML('afterbegin', this.getDialogHtml({isExternal, linkData, pageNames})); Array.from(dialogBody.querySelectorAll('.link-editor-tab-label')) .forEach((el: HTMLElement) => { el.onclick = (_) => { Array .from(dialogBody.querySelectorAll( '.link-editor-tab-label.checked')) .forEach((selected) => selected.classList.remove('checked')); el.classList.add('checked'); }; }); SilexNotification.setContent(dialogBody); } getDialogHtml({isExternal, linkData, pageNames}) { return ` `; } }