a1-redux
Version:
Angular >=1.5 and redux
79 lines (63 loc) • 2.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _angular = require('angular');
function check(props, propTypes, name) {
for (var prop in propTypes) {
if (propTypes.hasOwnProperty(prop)) {
var err = propTypes[prop](props, prop, name, 'prop');
if (err) {
console.warn(err);
return false;
}
}
}
return true;
}
function applyDefaultProps($scope, defaultProps) {
var $nuScope = $scope;
for (var key in defaultProps) {
if (typeof $scope[key] === 'undefined') {
$nuScope[key] = defaultProps[key];
}
}
return $nuScope;
}
function makeDirectiveApiSuggested(app) {
var aDirectiveRegisterFun = app.directive.bind(app);
app.directive = function (name, directiveFactory) {
var directiveFun = (0, _angular.isFunction)(directiveFactory) ? directiveFactory : directiveFactory.slice(-1)[0];
var finalDirectiveFactory = [].concat(directiveFactory);
function wrappedDirFun() {
var dirDef = directiveFun.apply(null, arguments);
if (!dirDef._propTypes_) {
console.warn('directive ** ' + name + ' ** not having _propTypes_ defined.\nDefining the _propTypes_ allow developer easy to understand which data should provide to to directive');
}
var dirLinkFun = dirDef.link;
function wrappedDirLinkFun($scope) {
var $nuScope = $scope;
if (typeof dirDef._getDefaultProps_ === 'function') {
$nuScope = applyDefaultProps($nuScope, dirDef._getDefaultProps_());
}
if (check($nuScope, dirDef._propTypes_, name)) {
for (var _len = arguments.length, more = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
more[_key - 1] = arguments[_key];
}
return dirLinkFun.apply(undefined, [$nuScope].concat(more));
}
throw new Error('properties of directive **' + name + '** is not provided correctly!');
}
dirDef.link = wrappedDirLinkFun;
dirDef.scope = dirDef.scope || {}; // make the scope isolate
for (var key in dirDef._propTypes_) {
dirDef.scope[key] = '<?'; // 1 one binding data.
}
return dirDef;
}
finalDirectiveFactory.splice(-1, 1, wrappedDirFun);
return aDirectiveRegisterFun(name, finalDirectiveFactory);
};
return app;
}
exports.default = makeDirectiveApiSuggested;