d2-ui
Version:
198 lines (144 loc) • 7.13 kB
JavaScript
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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _inlineStylePrefixAll = require('inline-style-prefix-all');
var _inlineStylePrefixAll2 = _interopRequireDefault(_inlineStylePrefixAll);
var _utilsGetBrowserInformation = require('./utils/getBrowserInformation');
var _utilsGetBrowserInformation2 = _interopRequireDefault(_utilsGetBrowserInformation);
var _utilsGetPrefixedKeyframes = require('./utils/getPrefixedKeyframes');
var _utilsGetPrefixedKeyframes2 = _interopRequireDefault(_utilsGetPrefixedKeyframes);
var _utilsCapitalizeString = require('./utils/capitalizeString');
var _utilsCapitalizeString2 = _interopRequireDefault(_utilsCapitalizeString);
var _utilsAssign = require('./utils/assign');
var _utilsAssign2 = _interopRequireDefault(_utilsAssign);
var _prefixProps = require('./prefixProps');
var _prefixProps2 = _interopRequireDefault(_prefixProps);
var _pluginsCalc = require('./plugins/calc');
var _pluginsCalc2 = _interopRequireDefault(_pluginsCalc);
var _pluginsCursor = require('./plugins/cursor');
var _pluginsCursor2 = _interopRequireDefault(_pluginsCursor);
var _pluginsFlex = require('./plugins/flex');
var _pluginsFlex2 = _interopRequireDefault(_pluginsFlex);
var _pluginsSizing = require('./plugins/sizing');
var _pluginsSizing2 = _interopRequireDefault(_pluginsSizing);
var _pluginsGradient = require('./plugins/gradient');
var _pluginsGradient2 = _interopRequireDefault(_pluginsGradient);
var _pluginsTransition = require('./plugins/transition');
var _pluginsTransition2 = _interopRequireDefault(_pluginsTransition);
// special flexbox specifications
var _pluginsFlexboxIE = require('./plugins/flexboxIE');
var _pluginsFlexboxIE2 = _interopRequireDefault(_pluginsFlexboxIE);
var _pluginsFlexboxOld = require('./plugins/flexboxOld');
var _pluginsFlexboxOld2 = _interopRequireDefault(_pluginsFlexboxOld);
var plugins = [_pluginsCalc2['default'], _pluginsCursor2['default'], _pluginsSizing2['default'], _pluginsGradient2['default'], _pluginsTransition2['default'], _pluginsFlexboxIE2['default'], _pluginsFlexboxOld2['default'],
// this must be run AFTER the flexbox specs
_pluginsFlex2['default']];
var Prefixer = (function () {
/**
* Instantiante a new prefixer
* @param {string} userAgent - userAgent to gather prefix information according to caniuse.com
* @param {string} keepUnprefixed - keeps unprefixed properties and values
*/
function Prefixer() {
var _this = this;
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
_classCallCheck(this, Prefixer);
var defaultUserAgent = typeof navigator !== 'undefined' ? navigator.userAgent : undefined;
this._userAgent = options.userAgent || defaultUserAgent;
this._keepUnprefixed = options.keepUnprefixed || false;
this._browserInfo = (0, _utilsGetBrowserInformation2['default'])(this._userAgent);
// Checks if the userAgent was resolved correctly
if (this._browserInfo && this._browserInfo.prefix) {
// set additional prefix information
this.cssPrefix = this._browserInfo.prefix.css;
this.jsPrefix = this._browserInfo.prefix.inline;
this.prefixedKeyframes = (0, _utilsGetPrefixedKeyframes2['default'])(this._browserInfo);
} else {
this._usePrefixAllFallback = true;
return false;
}
var data = this._browserInfo.browser && _prefixProps2['default'][this._browserInfo.browser];
if (data) {
this._requiresPrefix = Object.keys(data).filter(function (key) {
return data[key] >= _this._browserInfo.version;
}).reduce(function (result, name) {
result[name] = true;
return result;
}, {});
this._hasPropsRequiringPrefix = Object.keys(this._requiresPrefix).length > 0;
} else {
this._usePrefixAllFallback = true;
}
}
/**
* Returns a prefixed version of the style object
* @param {Object} styles - Style object that gets prefixed properties added
* @returns {Object} - Style object with prefixed properties and values
*/
_createClass(Prefixer, [{
key: 'prefix',
value: function prefix(styles) {
var _this2 = this;
// use prefixAll as fallback if userAgent can not be resolved
if (this._usePrefixAllFallback) {
return (0, _inlineStylePrefixAll2['default'])(styles);
}
// only add prefixes if needed
if (!this._hasPropsRequiringPrefix) {
return styles;
}
styles = (0, _utilsAssign2['default'])({}, styles);
Object.keys(styles).forEach(function (property) {
var value = styles[property];
if (value instanceof Object) {
// recurse through nested style objects
styles[property] = _this2.prefix(value);
} else {
// add prefixes if needed
if (_this2._requiresPrefix[property]) {
styles[_this2.jsPrefix + (0, _utilsCapitalizeString2['default'])(property)] = value;
if (!_this2._keepUnprefixed) {
delete styles[property];
}
}
// resolve plugins
plugins.forEach(function (plugin) {
// generates a new plugin interface with current data
var resolvedStyles = plugin({
property: property,
value: value,
styles: styles,
browserInfo: _this2._browserInfo,
prefix: {
js: _this2.jsPrefix,
css: _this2.cssPrefix,
keyframes: _this2.prefixedKeyframes
},
keepUnprefixed: _this2._keepUnprefixed,
requiresPrefix: _this2._requiresPrefix
});
(0, _utilsAssign2['default'])(styles, resolvedStyles);
});
}
});
return styles;
}
/**
* Returns a prefixed version of the style object using all vendor prefixes
* @param {Object} styles - Style object that gets prefixed properties added
* @returns {Object} - Style object with prefixed properties and values
*/
}], [{
key: 'prefixAll',
value: function prefixAll(styles) {
return (0, _inlineStylePrefixAll2['default'])(styles);
}
}]);
return Prefixer;
})();
exports['default'] = Prefixer;
module.exports = exports['default'];
;