UNPKG

4.6 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = create;
7
8var _vue = require("vue");
9
10var _Icon = _interopRequireDefault(require("./Icon"));
11
12var _excluded = ["type"];
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
15
16function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? Object(arguments[i]) : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
17
18function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
19
20function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
21
22function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
23
24var customCache = new Set();
25
26function isValidCustomScriptUrl(scriptUrl) {
27 return typeof scriptUrl === 'string' && scriptUrl.length && !customCache.has(scriptUrl);
28}
29
30function createScriptUrlElements(scriptUrls) {
31 var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
32 var currentScriptUrl = scriptUrls[index];
33
34 if (isValidCustomScriptUrl(currentScriptUrl)) {
35 var script = document.createElement('script');
36 script.setAttribute('src', currentScriptUrl);
37 script.setAttribute('data-namespace', currentScriptUrl);
38
39 if (scriptUrls.length > index + 1) {
40 script.onload = function () {
41 createScriptUrlElements(scriptUrls, index + 1);
42 };
43
44 script.onerror = function () {
45 createScriptUrlElements(scriptUrls, index + 1);
46 };
47 }
48
49 customCache.add(currentScriptUrl);
50 document.body.appendChild(script);
51 }
52}
53
54function create() {
55 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
56 var scriptUrl = options.scriptUrl,
57 _options$extraCommonP = options.extraCommonProps,
58 extraCommonProps = _options$extraCommonP === void 0 ? {} : _options$extraCommonP;
59 /**
60 * DOM API required.
61 * Make sure in browser environment.
62 * The Custom Icon will create a <script/>
63 * that loads SVG symbols and insert the SVG Element into the document body.
64 */
65
66 if (typeof document !== 'undefined' && typeof window !== 'undefined' && typeof document.createElement === 'function') {
67 if (Array.isArray(scriptUrl)) {
68 // 因为iconfont资源会把svg插入before,所以前加载相同type会覆盖后加载,为了数组覆盖顺序,倒叙插入
69 createScriptUrlElements(scriptUrl.reverse());
70 } else {
71 createScriptUrlElements([scriptUrl]);
72 }
73 }
74
75 var Iconfont = function Iconfont(props, context) {
76 var attrs = context.attrs,
77 slots = context.slots;
78
79 var _props$attrs = _objectSpread({}, props, attrs),
80 type = _props$attrs.type,
81 restProps = _objectWithoutProperties(_props$attrs, _excluded);
82
83 var children = slots["default"] && slots["default"](); // children > type
84
85 var content = null;
86
87 if (type) {
88 content = (0, _vue.createVNode)("use", {
89 "xlink:href": "#".concat(type)
90 }, null);
91 }
92
93 if (children && children.length) {
94 content = children;
95 }
96
97 var iconProps = _objectSpread({}, extraCommonProps, restProps);
98
99 return (0, _vue.createVNode)(_Icon["default"], iconProps, {
100 "default": function _default() {
101 return [content];
102 }
103 });
104 };
105
106 Iconfont.props = {
107 spin: Boolean,
108 rotate: Number,
109 type: String
110 };
111 Iconfont.inheritAttrs = false;
112 Iconfont.displayName = 'Iconfont';
113 return Iconfont;
114}
\No newline at end of file