UNPKG

@gitlab/ui

Version:
125 lines (115 loc) 3.41 kB
import { HAS_WINDOW_SUPPORT } from '../constants/env'; import { setConfig } from './config-set'; /** * Plugin install factory function. * @param {object} { components, directives } * @returns {function} plugin install function */ const installFactory = function () { let { components, directives, plugins } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; const install = function (Vue) { let config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (install.installed) { /* istanbul ignore next */ return; } install.installed = true; setConfig(config, Vue); registerComponents(Vue, components); registerDirectives(Vue, directives); registerPlugins(Vue, plugins); }; install.installed = false; return install; }; /** * Plugin object factory function. * @param {object} { components, directives, plugins } * @returns {object} plugin install object */ const pluginFactory = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; let extend = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return { ...extend, install: installFactory(options) }; }; /** * Load a group of plugins. * @param {object} Vue * @param {object} Plugin definitions */ const registerPlugins = function (Vue) { let plugins = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; for (const plugin in plugins) { if (plugin && plugins[plugin]) { Vue.use(plugins[plugin]); } } }; /** * Load a component. * @param {object} Vue * @param {string} Component name * @param {object} Component definition */ const registerComponent = (Vue, name, def) => { if (Vue && name && def) { Vue.component(name, def); } }; /** * Load a group of components. * @param {object} Vue * @param {object} Object of component definitions */ const registerComponents = function (Vue) { let components = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; for (const component in components) { registerComponent(Vue, component, components[component]); } }; /** * Load a directive. * @param {object} Vue * @param {string} Directive name * @param {object} Directive definition */ const registerDirective = (Vue, name, def) => { if (Vue && name && def) { // Ensure that any leading V is removed from the // name, as Vue adds it automatically Vue.directive(name.replace(/^VB/, 'B'), def); } }; /** * Load a group of directives. * @param {object} Vue * @param {object} Object of directive definitions */ const registerDirectives = function (Vue) { let directives = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; for (const directive in directives) { registerDirective(Vue, directive, directives[directive]); } }; /** * Install plugin if window.Vue available * @param {object} Plugin definition */ const vueUse = VuePlugin => { /* istanbul ignore next */ if (HAS_WINDOW_SUPPORT && window.Vue) { window.Vue.use(VuePlugin); } /* istanbul ignore next */ if (HAS_WINDOW_SUPPORT && VuePlugin.NAME) { window[VuePlugin.NAME] = VuePlugin; } }; export { installFactory, pluginFactory, registerComponent, registerComponents, registerDirective, registerDirectives, registerPlugins, vueUse };