1 | import {
|
2 | isFunction
|
3 | } from 'angular';
|
4 |
|
5 | import PropTypes from 'proptypes';
|
6 |
|
7 | function check(props, propTypes, name) {
|
8 | for (let prop in propTypes) {
|
9 | if (propTypes.hasOwnProperty(prop)) {
|
10 | let err = propTypes[prop](props, prop, name, 'prop');
|
11 | if (err) {
|
12 | console.warn(err);
|
13 | return false;
|
14 | }
|
15 | }
|
16 | }
|
17 | return true;
|
18 | }
|
19 |
|
20 | function makeDirectiveApiSuggested(aDirectiveRegisterFun) {
|
21 | return (name, directiveFactory) => {
|
22 | const directiveFun = isFunction(directiveFactory) ? directiveFactory : directiveFactory.slice(-1)[0];
|
23 | const finalDirectiveFactory = [].concat(directiveFactory);
|
24 |
|
25 | function wrappedDirFun(...args) {
|
26 | const dirDef = directiveFun(...args);
|
27 |
|
28 | if (!dirDef._propTypes_) {
|
29 | console.warn(`directive **${name}** not having _propTypes_ defined.
|
30 | Defining the _propTypes_ allow developer easy to understand which data should provide to to directive`);
|
31 | }
|
32 |
|
33 | const dirLinkFun = dirDef.link;
|
34 |
|
35 | function wrappedDirLinkFun($scope, ...more) {
|
36 | if (check($scope, dirDef._propTypes_, name)) {
|
37 | return dirLinkFun($scope, ...more);
|
38 | }
|
39 | throw new Error(`properties of directive **${name}** is not provided correctly!`);
|
40 | }
|
41 | dirDef.link = wrappedDirLinkFun;
|
42 |
|
43 | return dirDef;
|
44 | }
|
45 |
|
46 | finalDirectiveFactory.splice(-1, 1, wrappedDirFun);
|
47 |
|
48 | return aDirectiveRegisterFun(name, finalDirectiveFactory);
|
49 | };
|
50 | }
|
51 |
|
52 | export default makeDirectiveApiSuggested;
|