weex-nuke
Version:
基于 Rax 、Weex 的高性能组件体系 ~~
168 lines (148 loc) • 5.38 kB
JavaScript
/** @jsx createElement */
/**
* Copyright (c) 2015-present, Alibaba Group Holding Limited.
* All rights reserved.
*
*/
;
/**
* 一个实例中相同的iconfont资源只需被调用一次,如果对iconfont有二次封装会导致调用次数过多在客户端出现闪动
* 单例模式 相同的url算一个单例
*/
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var isWeex = typeof callNative === 'function';
var ICON_CACHE_KEY = '_nuke_icon_cache_';
var Iconfont = function () {
function Iconfont() {
_classCallCheck(this, Iconfont);
}
_createClass(Iconfont, [{
key: 'init',
value: function init(options) {
var url = options.url,
name = options.name;
if (!url || !name) return;
var urlFit = url;
if (isWeex) {
if (url.indexOf('//at.alicdn.com') === 0) {
urlFit = 'https:' + urlFit;
}
var domModule = require('@weex-module/dom');
domModule.addRule('fontFace', {
fontFamily: name,
src: 'url(\'' + urlFit + '\')'
});
} else {
if (isDefined(name, urlFit)) return;
var css = '@font-face {font-family: \'' + name + '\';src: url(\'' + urlFit + '\');}';
var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
cacheDefineResult(name, urlFit);
}
}
}]);
return Iconfont;
}();
/**
* check if this font family has been defined
* @param {*} name iconfontName
* @param {*} url iconfontURL
*/
function isDefined(name, url) {
if (isWeex) return false;
var CacheArray = window[ICON_CACHE_KEY] && window[ICON_CACHE_KEY][name] ? window[ICON_CACHE_KEY][name] : null;
if (!CacheArray) return false;
if ('name' in CacheArray && 'url' in CacheArray && CacheArray.name === name && CacheArray.url === url) {
return true;
}
return false;
}
/**
*
* @param {*} name
* @param {*} url
*/
function cacheDefineResult(name, url) {
if (isWeex) return;
window[ICON_CACHE_KEY] = window[ICON_CACHE_KEY] || {};
window[ICON_CACHE_KEY][name] = { name: name, url: url };
}
/**
* @param {string/obj} unicode e6DD;\e6dd;
*/
function formatUnicode(unicode) {
if (typeof unicode !== 'string' && process.env.NODE_ENV === 'development') {
console.warn('formatUnicode only support <string> arguments');
}
var tmpUnicode = unicode.raw || unicode;
if (tmpUnicode.indexOf('&#x') === 0) {
tmpUnicode = fromCodePoint(parseInt(tmpUnicode.replace('&#x', '0x').replace(';', ''), 16));
} else if (tmpUnicode.indexOf('\\u') === 0) {
tmpUnicode = unicode;
} else {
tmpUnicode = fromCodePoint(parseInt('0x' + tmpUnicode.replace(/(e[\w\d]{3})/, '$1').replace(/\\/, '').replace(';', ''), 16));
}
return tmpUnicode;
}
function fromCodePoint(_) {
if (String.fromCodePoint) {
return String.fromCodePoint(_);
}
var stringFromCharCode = String.fromCharCode;
var floor = Math.floor;
var MAX_SIZE = 0x4000;
var codeUnits = [];
var highSurrogate = void 0;
var lowSurrogate = void 0;
var index = -1;
var length = arguments.length;
if (!length) {
return "";
}
var result = "";
while (++index < length) {
var codePoint = Number(arguments[index]);
if (!isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
codePoint < 0 || // not a valid Unicode code point
codePoint > 0x10FFFF || // not a valid Unicode code point
floor(codePoint) != codePoint // not an integer
) {
throw RangeError("Invalid code point: " + codePoint);
}
if (codePoint <= 0xFFFF) {
// BMP code point
codeUnits.push(codePoint);
} else {
// Astral code point; split in surrogate halves
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
codePoint -= 0x10000;
highSurrogate = (codePoint >> 10) + 0xD800;
lowSurrogate = codePoint % 0x400 + 0xDC00;
codeUnits.push(highSurrogate, lowSurrogate);
}
if (index + 1 == length || codeUnits.length > MAX_SIZE) {
result += stringFromCharCode.apply(null, codeUnits);
codeUnits.length = 0;
}
}
return result;
}
function Output(params) {
new Iconfont().init(params);
}
Output.isDefined = isDefined;
Output.cacheDefineResult = cacheDefineResult;
Output.formatUnicode = formatUnicode;
exports.default = Output;
module.exports = exports['default'];