|
| 1 | +import * as Vue from 'vue'; |
| 2 | + |
| 3 | + |
| 4 | +let InternalVue = Vue; |
| 5 | + |
| 6 | +export function setInternalVue(vue) { |
| 7 | + InternalVue = vue; |
| 8 | +} |
| 9 | + |
| 10 | +function shouldUseGlobalCreateElement() { |
| 11 | + return !!( |
| 12 | + InternalVue && |
| 13 | + InternalVue.version && |
| 14 | + /^[3-4]\./.test(InternalVue.version) && |
| 15 | + InternalVue.h |
| 16 | + ); |
| 17 | +} |
| 18 | + |
| 19 | +export function useCreateElementResolver(component) { |
| 20 | + if (typeof component.render === 'function') { |
| 21 | + if (shouldUseGlobalCreateElement()) { |
| 22 | + const origin = component.render; |
| 23 | + component.render = function() { |
| 24 | + const argsArray = Array.prototype.slice.call(arguments); |
| 25 | + const args = typeof argsArray[0] === 'function' |
| 26 | + ? argsArray |
| 27 | + : [InternalVue.h].concat(argsArray); |
| 28 | + return origin.apply(this, args); |
| 29 | + }; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return component; |
| 34 | +} |
| 35 | + |
| 36 | +export function formatLoaderName(fileName) { |
| 37 | + return 'VueLoaders' + fileName |
| 38 | + .split(/-|\.js/) |
| 39 | + .map(_ => |
| 40 | + _.charAt(0) |
| 41 | + ? _.charAt(0).toUpperCase() + _.slice(1) |
| 42 | + : _ |
| 43 | + ) |
| 44 | + .join(''); |
| 45 | +} |
| 46 | + |
| 47 | +export function createSingleLoaderComponent(options) { |
| 48 | + const component = createComponentBase(); |
| 49 | + |
| 50 | + component.name = formatLoaderName(options.name); |
| 51 | + component.render = createRender(options); |
| 52 | + |
| 53 | + useCreateElementResolver(component); |
| 54 | + |
| 55 | + return { |
| 56 | + originName: options.name, |
| 57 | + component, |
| 58 | + install(Vue) { |
| 59 | + Vue.component(this.component.name, this.component); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function createComponentBase() { |
| 65 | + return { |
| 66 | + props: { |
| 67 | + color: { |
| 68 | + type: String, |
| 69 | + default: '#ffffff' |
| 70 | + }, |
| 71 | + scale: { |
| 72 | + type: [String, Number] |
| 73 | + } |
| 74 | + } |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +function createRender(options) { |
| 79 | + const renderChildren = createChildrenRenderer(options.children); |
| 80 | + return function render(createElement) { |
| 81 | + return renderRoot( |
| 82 | + createElement, |
| 83 | + options.name, |
| 84 | + this.scale, |
| 85 | + renderChildren(createElement, this.color) |
| 86 | + ); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function createChildrenRenderer(children) { |
| 91 | + const args = createChildrenRenderArgs(children); |
| 92 | + return function renderChildren(createElement, color, renderArgs = args) { |
| 93 | + return renderArgs.map(_ => { |
| 94 | + const style = _[0]; |
| 95 | + const nestedRenderArgs = _[1]; |
| 96 | + const children = nestedRenderArgs |
| 97 | + ? renderChildren(createElement, color, nestedRenderArgs) |
| 98 | + : void 0; |
| 99 | + return renderChild(createElement, style(color), children); |
| 100 | + }); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +function createChildrenRenderArgs(children) { |
| 105 | + const args = []; |
| 106 | + |
| 107 | + if (typeof children === 'number') { |
| 108 | + const colorStyleFactory = createColorFactory(); |
| 109 | + let count = children > 0 ? children : 1; |
| 110 | + while (count--) { |
| 111 | + args.push([colorStyleFactory]); |
| 112 | + } |
| 113 | + } else if (Array.isArray(children)) { |
| 114 | + let currentArg; |
| 115 | + for (const child of children) { |
| 116 | + const colorStyleFactory = createColorFactory(child.color); |
| 117 | + let count = child.count > 0 ? child.count : 1; |
| 118 | + while (count--) { |
| 119 | + currentArg = [colorStyleFactory]; |
| 120 | + if (child.children !== undefined) { |
| 121 | + currentArg.push(createChildrenRenderArgs(child.children)); |
| 122 | + } |
| 123 | + args.push(currentArg); |
| 124 | + } |
| 125 | + } |
| 126 | + } else { |
| 127 | + throw new TypeError('Invalid children'); |
| 128 | + } |
| 129 | + |
| 130 | + return args; |
| 131 | +} |
| 132 | + |
| 133 | +function renderRoot(createElement, name, scale, children) { |
| 134 | + return createElement( |
| 135 | + 'div', |
| 136 | + { |
| 137 | + class: { |
| 138 | + 'vue-loaders': true, |
| 139 | + [name]: true |
| 140 | + }, |
| 141 | + style: { |
| 142 | + transform: scale !== void 0 ? `scale(${scale})` : void 0 |
| 143 | + } |
| 144 | + }, |
| 145 | + children |
| 146 | + ); |
| 147 | +} |
| 148 | + |
| 149 | +function renderChild(createElement, style, children) { |
| 150 | + return createElement( |
| 151 | + 'div', |
| 152 | + { |
| 153 | + style |
| 154 | + }, |
| 155 | + children |
| 156 | + ); |
| 157 | +} |
| 158 | + |
| 159 | +function createColorFactory(propsOrFn = 'background-color') { |
| 160 | + return color => { |
| 161 | + if (typeof propsOrFn === 'function') { |
| 162 | + return propsOrFn(color); |
| 163 | + } |
| 164 | + const props = Array.isArray(propsOrFn) ? propsOrFn : [propsOrFn]; |
| 165 | + const style = {}; |
| 166 | + for (const p of props) { |
| 167 | + style[p] = color; |
| 168 | + } |
| 169 | + return style; |
| 170 | + }; |
| 171 | +} |
| 172 | + |
0 commit comments