1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.InvalidOptionError = undefined;
|
7 |
|
8 | var _typeof2 = require('babel-runtime/helpers/typeof');
|
9 |
|
10 | var _typeof3 = _interopRequireDefault(_typeof2);
|
11 |
|
12 | var _map = require('babel-runtime/core-js/map');
|
13 |
|
14 | var _map2 = _interopRequireDefault(_map);
|
15 |
|
16 | var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
|
17 |
|
18 | var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
|
19 |
|
20 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
21 |
|
22 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
23 |
|
24 | var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
25 |
|
26 | var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
27 |
|
28 | var _inherits2 = require('babel-runtime/helpers/inherits');
|
29 |
|
30 | var _inherits3 = _interopRequireDefault(_inherits2);
|
31 |
|
32 | exports.default = createPercyAddon;
|
33 |
|
34 | var _es6Error = require('es6-error');
|
35 |
|
36 | var _es6Error2 = _interopRequireDefault(_es6Error);
|
37 |
|
38 | var _constants = require('./constants');
|
39 |
|
40 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
41 |
|
42 | var InvalidOptionError = exports.InvalidOptionError = function (_ExtendableError) {
|
43 | (0, _inherits3.default)(InvalidOptionError, _ExtendableError);
|
44 |
|
45 | function InvalidOptionError() {
|
46 | (0, _classCallCheck3.default)(this, InvalidOptionError);
|
47 | return (0, _possibleConstructorReturn3.default)(this, (InvalidOptionError.__proto__ || (0, _getPrototypeOf2.default)(InvalidOptionError)).apply(this, arguments));
|
48 | }
|
49 |
|
50 | return InvalidOptionError;
|
51 | }(_es6Error2.default);
|
52 |
|
53 | function assertWidths(widths) {
|
54 | if (typeof widths === 'undefined') {
|
55 | return;
|
56 | }
|
57 | if (!Array.isArray(widths)) {
|
58 | throw new InvalidOptionError('Given widths option is not an array');
|
59 | }
|
60 | if (!widths.length) {
|
61 | throw new InvalidOptionError('Need at least one valid width');
|
62 | }
|
63 | widths.forEach(function (width) {
|
64 | if (isNaN(width) || width !== ~~width) {
|
65 | throw new InvalidOptionError("Given width '" + width + "' is invalid");
|
66 | }
|
67 | });
|
68 | }
|
69 |
|
70 | function assertRtl(rtl) {
|
71 | if (typeof rtl === 'undefined') {
|
72 | return;
|
73 | }
|
74 | if (typeof rtl !== 'boolean') {
|
75 | throw new InvalidOptionError("Given rtl setting '" + rtl + "' is invalid");
|
76 | }
|
77 | }
|
78 |
|
79 | function createPercyAddon() {
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | var optionsTuples = new _map2.default();
|
91 |
|
92 | var percyAddon = {
|
93 | addWithPercyOptions: function addWithPercyOptions(storyName, options, storyFn) {
|
94 | if (typeof options === 'function') {
|
95 | storyFn = options;
|
96 | options = undefined;
|
97 | }
|
98 |
|
99 | if (options) {
|
100 | assertWidths(options.widths);
|
101 | assertRtl(options.rtl);
|
102 |
|
103 | optionsTuples.set(this.kind, optionsTuples.get(this.kind) || new _map2.default());
|
104 | var tuplesForKind = optionsTuples.get(this.kind);
|
105 | tuplesForKind.set(storyName, options);
|
106 | }
|
107 | return this.add(storyName, storyFn);
|
108 | }
|
109 | };
|
110 |
|
111 | var serializeStories = function serializeStories(getStorybook) {
|
112 | var storybook = getStorybook();
|
113 | storybook.forEach(function (storyBucket) {
|
114 | if (!optionsTuples.has(storyBucket.kind)) {
|
115 | return;
|
116 | }
|
117 | var tuplesForKind = optionsTuples.get(storyBucket.kind);
|
118 | storyBucket.stories.forEach(function (story) {
|
119 | if (tuplesForKind.has(story.name)) {
|
120 | story.options = tuplesForKind.get(story.name);
|
121 | }
|
122 | });
|
123 | });
|
124 | if ((typeof window === 'undefined' ? 'undefined' : (0, _typeof3.default)(window)) === 'object') window[_constants.storiesKey] = storybook;
|
125 | return storybook;
|
126 | };
|
127 |
|
128 | return { percyAddon: percyAddon, serializeStories: serializeStories };
|
129 | } |
\ | No newline at end of file |