Skip to content

Commit 2fb0d90

Browse files
committed
Merge branch 'master' into next
# Conflicts: # demo.html # package-lock.json # package.json # src/core.js # src/index.js
2 parents c55d23c + 8b00f30 commit 2fb0d90

39 files changed

+423
-538
lines changed

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
message = "chore: up to %s"

assets/gifs/ball-scale-multiple.gif

20.3 KB
Loading

assets/gifs/line-scale-party.gif

59.2 KB
Loading

demo.html

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<body style="text-align: center; padding: 20px">
1212
<div id="app">
1313
<vue-loaders name="unsupported-name" color="black"></vue-loaders>
14+
<vue-loaders-ball-beat color="black"></vue-loaders-ball-beat>
15+
<hr/>
1416
<vue-loaders name="ball-beat" color="black"></vue-loaders>
1517
<hr/>
1618
<vue-loaders name="ball-grid-beat" color="black"></vue-loaders>
@@ -76,6 +78,7 @@
7678
<script>
7779
function v2() {
7880
Vue.use(VueLoaders);
81+
Vue.use(VueLoadersBallBeat);
7982
Vue.component('ball-beat', VueLoadersBallBeat.component);
8083
new Vue().$mount('#app')
8184
}

package.json

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
},
1212
"files": [
1313
"dist/",
14-
"src/",
15-
"demo.html",
16-
"vue-loaders.gif",
1714
"README.md",
1815
"LICENSE.md"
1916
],

rollup.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import postcss from 'rollup-plugin-postcss';
66
import autoprefixer from 'autoprefixer';
77
import csso from 'postcss-csso';
88
import fs from 'fs';
9-
import {getLoaderName} from './src/core';
9+
import {formatLoaderName} from './src/componentApi';
1010
import pkg from './package.json';
1111

1212
const loadersList = fs.readdirSync('./src/loaders/');
@@ -15,7 +15,7 @@ function createBundleOptionsForLoaders(loaders) {
1515
return loaders.map(fileName => ({
1616
input: `src/loaders/${fileName}`,
1717
output: {
18-
name: getLoaderName(fileName),
18+
name: formatLoaderName(fileName),
1919
file: `./dist/loaders/${fileName}`,
2020
format: 'umd',
2121
esModule: true,

src/LoadersPackage.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export class LoadersPackage {
2+
constructor(loaders) {
3+
this.loaders = {};
4+
5+
if (Array.isArray(loaders)) {
6+
for (const loader of loaders) {
7+
this.add(loader);
8+
}
9+
}
10+
}
11+
12+
get(name) {
13+
return this.loaders[name];
14+
}
15+
16+
add(loader) {
17+
this.loaders[loader.originName] = loader;
18+
}
19+
20+
install(Vue) {
21+
for (const name in this.loaders) {
22+
if (this.loaders.hasOwnProperty(name)) {
23+
this.loaders[name].install(Vue);
24+
}
25+
}
26+
}
27+
}

src/componentApi.js

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+

src/core.js

-131
This file was deleted.

0 commit comments

Comments
 (0)