UNPKG

@gravityforms/components

Version:

UI components for use in Gravity Forms development. Both React and vanilla js flavors.

200 lines (184 loc) 5.85 kB
import { consoleInfo, getNode, objectToAttributes, spacerClasses, trigger, uniqueId, } from '@gravityforms/utils'; /** * @function linkTemplate * @description Generates the markup for a link in the admin. * * @since 1.1.16 * * @param {object} options The options for the component template. * @param {object} options.customAttributes Any custom attributes. * @param {Array} options.customClasses An array of additional classes for the toggle. * @param {string} options.href Href for the link. * @param {string} options.id Id for the link, auto generated if not passed. * @param {string} options.label Text label for the link. * @param {string} options.linkTarget Target for the link, eg _blank. * @param {string|number|Array|object} options.spacing The spacing for the component, string, number, object or array. * @param {string} options.theme Theme for the toggle, primary or cosmos. * * @return {string} * @example * import { linkTemplate } from '@gravityforms/components/html/admin/elements/Link'; * * function Example() { * const linkTemplateHTML = linkTemplateTemplate( options ); * document.body.insertAdjacentHTML( 'beforeend', linkTemplateHTML ); * } * */ export const linkTemplate = ( { customAttributes = {}, customClasses = [], href = '', id = uniqueId( 'link' ), label = '', linkTarget = '_self', spacing = '', theme = 'cosmos', } ) => { const componentAttrs = objectToAttributes( { ...customAttributes, id, href, target: linkTarget, class: [ 'gform-link', `gform-link--theme-${ theme }`, ...Object.keys( spacerClasses( spacing ) ), ...customClasses, ], } ); return ` <a ${ componentAttrs }${ linkTarget === '_blank' ? ` rel="noopener"` : `` }> <span class="gform-link__label">${ label }</span> </a> `; }; /** * @class Link * @description A link component . * * @since 1.1.16 * * @borrows linkTemplate as linkTemplate * * @param {object} options The options for the component. * @param {object} options.customAttributes Any custom attributes for the component. * @param {Array} options.customClasses An array of additional classes for the component. * @param {string} options.href Href for the link. * @param {string} options.id Id for the component, auto generated if not passed. * @param {string} options.label Text label for the link. * @param {string} options.linkTarget Target for the link, eg _blank. * @param {string} options.rendered Is the component already rendered in the dom, eg by php? * @param {string} options.renderOnInit Render the component on init of the class? * @param {string} options.spacing Spacing for the component. * @param {string} options.target The target to render to. Any valid css selector string. * @param {string} options.targetPosition The insert position for the component relative to the target. * @param {string} options.theme Theme for the component, primary or cosmos. * * @return {Class} The class instance. * @example * import Link from '@gravityforms/components/html/admin/elements/Link'; * * function Example() { * const linkInstance = new Link( { * id: 'example-link', * renderOnInit: false, * target: '#example-target', * targetPosition: 'beforeend', * theme: 'cosmos', * } ); * * // Some time later we can render it. This is only done if we set renderOnInit to false. * // If true it will render on initialization. * linkInstance.init(); * } * */ export default class Link { constructor( options = {} ) { this.options = {}; Object.assign( this.options, { customAttributes: {}, customClasses: [], href: '', id: '', label: '', linkTarget: '_self', rendered: false, renderOnInit: true, spacing: '', target: '', targetPosition: 'afterbegin', theme: 'cosmos', }, options ); /** * @event gform/link/pre_init * @type {object} * @description Fired before the component has started any internal init functions. A great chance to augment the options. * * @since 1.1.16 * * @property {object} instance The Component class instance. */ trigger( { event: 'gform/link/pre_init', native: false, data: { instance: this } } ); this.options.id = this.options.id || uniqueId( 'toggle' ); this.elements = {}; if ( this.options.renderOnInit ) { this.init(); } } /** * @memberof Link * @description Renders the component into the dom. * * @since 1.1.16 * * @return {void} */ render() { const { rendered, target, targetPosition } = this.options; if ( ! rendered ) { const renderTarget = getNode( target, document, true ); renderTarget.insertAdjacentHTML( targetPosition, linkTemplate( this.options ) ); } this.elements.link = getNode( `#${ this.options.id }`, document, true ); } /** * @memberof Link * @description Initialize the component. * * @fires gform/link/post_render * * @since 1.1.16 * * @return {void} */ init() { this.render(); /** * @event gform/link/post_render * @type {object} * @description Fired when the component has completed rendering and all class init functions have completed. * * @since 1.1.16 * * @property {object} instance The Component class instance. */ trigger( { event: 'gform/link/post_render', native: false, data: { instance: this } } ); consoleInfo( `Gravity Forms Admin: Initialized link component.` ); } }