UNPKG

9.58 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getContextTypes = getContextTypes;
7exports.setContextTypes = setContextTypes;
8exports.getUtils = getUtils;
9exports.localize = localize;
10exports.link = link;
11exports.cssClasses = cssClasses;
12exports.defaultCssClasses = defaultCssClasses;
13exports.fire = fire;
14exports.listen = listen;
15exports.unlisten = unlisten;
16
17var _classnames = require("classnames");
18
19var _classnames2 = _interopRequireDefault(_classnames);
20
21var _propTypes = require("prop-types");
22
23var _propTypes2 = _interopRequireDefault(_propTypes);
24
25var _react = require("react");
26
27var _react2 = _interopRequireDefault(_react);
28
29var _reactDom = require("react-dom");
30
31var _reactDom2 = _interopRequireDefault(_reactDom);
32
33function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
34
35const PRIVATE = {
36 contextTypes: Symbol('contextTypes')
37};
38
39if (typeof $Debug !== 'undefined' && $Debug) {
40 Object.freeze(PRIVATE);
41}
42/**
43 * Retrieves the context type declarations for the specified component.
44 *
45 * @param {function(new: React.Component, ...*)} classConstructor The
46 * constructor of the react component.
47 * @return {Object<string, function(...*): boolean>} The context type
48 * declarations associated with the specified component.
49 */
50
51
52function getContextTypes(classConstructor) {
53 if (classConstructor.hasOwnProperty(PRIVATE.contextTypes)) {
54 return this[PRIVATE.contextTypes];
55 }
56
57 return {
58 $Utils: _propTypes2.default.object.isRequired
59 };
60}
61/**
62 * Overrides the previously associated context type declarations for the
63 * specified component to the provided ones.
64 *
65 * @param {function(new: React.Component, ...*)} classConstructor The
66 * constructor of the react component.
67 * @param {Object<string, function(...*): boolean>} contextTypes The new
68 * context type declarations to associate with the specified component.
69 * @return {Object<string, function(...*): boolean>} The provided context type
70 * declarations.
71 */
72
73
74function setContextTypes(classConstructor, contextTypes) {
75 return classConstructor[PRIVATE.contextTypes] = contextTypes;
76}
77/**
78 * Retrieves the view utilities from the component's current context or
79 * properties (preferring the context).
80 *
81 * @param {Object<string, *>} props The component's current properties.
82 * @param {Object<string, *>} context The component's current context.
83 * @return {Object<string, *>} The retrieved view utilities.
84 * @throws Error Throw if the view utils cannot be located in the provided
85 * properties nor context.
86 */
87
88
89function getUtils(props, context) {
90 const utils = context ? context.$Utils || props.$Utils : props.$Utils;
91
92 if ($Debug && !utils) {
93 throw new Error('The component cannot access the view utils because they were ' + 'not passed in the initial props or context as $Utils.');
94 }
95
96 return utils;
97}
98/**
99 * Returns the localized phrase identified by the specified key. The
100 * placeholders in the localization phrase will be replaced by the provided
101 * values.
102 *
103 * @param {(AbstractComponent|AbstractPureComponent)} component The component
104 * requiring the localization.
105 * @param {string} key Localization key.
106 * @param {Object<string, (number|string)>=} params Values for replacing the
107 * placeholders in the localization phrase.
108 * @return {string} Localized phrase.
109 */
110
111
112function localize(component, key, params) {
113 return component.utils.$Dictionary.get(key, params);
114}
115/**
116 * Generates an absolute URL using the provided route name (see the
117 * <code>app/config/routes.js</code> file). The provided parameters will
118 * replace the placeholders in the route pattern, while the extraneous
119 * parameters will be appended to the generated URL's query string.
120 *
121 * @param {(AbstractComponent|AbstractPureComponent)} component The component
122 * requiring the generating of the URL.
123 * @param {string} name The route name.
124 * @param {Object<string, (number|string)>=} params Router parameters and
125 * extraneous parameters to add to the URL as a query string.
126 * @return {string} The generated URL.
127 */
128
129
130function link(component, name, params) {
131 return component.utils.$Router.link(name, params);
132}
133/**
134 * Generate a string of CSS classes from the properties of the passed-in
135 * object that resolve to {@code true}.
136 *
137 * @example
138 * this.cssClasses('my-class my-class-modificator', true);
139 * @example
140 * this.cssClasses({
141 * 'my-class': true,
142 * 'my-class-modificator': this.props.modificator
143 * }, true);
144 * @param {(AbstractComponent|AbstractPureComponent)} component The component
145 * requiring the composition of the CSS class names.
146 * @param {(string|Object<string, boolean>)} classRules CSS classes in a
147 * string separated by whitespace, or a map of CSS class names to
148 * boolean values. The CSS class name will be included in the result
149 * only if the value is {@code true}.
150 * @param {boolean} includeComponentClassName
151 * @return {string} String of CSS classes that had their property resolved
152 * to {@code true}.
153 */
154
155
156function cssClasses(component, classRules, includeComponentClassName) {
157 return component.utils.$CssClasses(classRules, includeComponentClassName && component);
158}
159/**
160 * Generate a string of CSS classes from the properties of the passed-in
161 * object that resolve to {@code true}.
162 *
163 * @param {(string|Object<string, boolean>)} classRules CSS classes in a
164 * string separated by whitespace, or a map of CSS class names to
165 * boolean values. The CSS class name will be included in the result
166 * only if the value is {@code true}.
167 * @param {?(AbstractComponent|AbstractPureComponent|string)} component The component
168 * requiring the composition of the CSS class names, if it has the
169 * {@code className} property set and requires its inclusion this time.
170 * @return {string} String of CSS classes that had their property resolved
171 * to {@code true}.
172 */
173
174
175function defaultCssClasses(classRules, component) {
176 let extraClasses = typeof component === 'string' ? component : null;
177
178 if (!extraClasses && (component instanceof _react2.default.Component || component instanceof _react2.default.PureComponent)) {
179 extraClasses = component.props.className;
180 }
181
182 return (0, _classnames2.default)(classRules, extraClasses);
183}
184/**
185 * Creates and sends a new IMA.js DOM custom event from the provided component.
186 *
187 * @param {(AbstractComponent|AbstractPureComponent)} component The component
188 * at which's root element the event will originate.
189 * @param {string} eventName The name of the event.
190 * @param {*=} data Data to send within the event.
191 */
192
193
194function fire(component, eventName, data = null) {
195 return component.utils.$EventBus.fire(_reactDom2.default.findDOMNode(component), //eslint-disable-line react/no-find-dom-node
196 eventName, data);
197}
198/**
199 * Registers the provided event listener for execution whenever an IMA.js
200 * DOM custom event of the specified name occurs at the specified event
201 * target.
202 *
203 * @param {(AbstractComponent|AbstractPureComponent)} component The component
204 * requesting the registration of the event listener.
205 * @param {(React.Element|EventTarget)} eventTarget The react component or
206 * event target at which the listener should listen for the event.
207 * @param {string} eventName The name of the event for which to listen.
208 * @param {function(Event)} listener The listener for event to register.
209 */
210
211
212function listen(component, eventTarget, eventName, listener) {
213 if (eventTarget && !eventTarget.addEventListener) {
214 // Safari doesn't have EventTarget
215 eventTarget = _reactDom2.default.findDOMNode(eventTarget); //eslint-disable-line react/no-find-dom-node
216 }
217
218 return component.utils.$EventBus.listen(eventTarget, eventName, listener);
219}
220/**
221 * Deregisters the provided event listener for an IMA.js DOM custom event
222 * of the specified name at the specified event target.
223 *
224 * @param {(AbstractComponent|AbstractPureComponent)} component The component
225 * that requested the registration of the event listener.
226 * @param {(React.Element|EventTarget)} eventTarget The react component or
227 * event target at which the listener should listen for the event.
228 * @param {string} eventName The name of the event for which to listen.
229 * @param {function(Event)} listener The listener for event to register.
230 */
231
232
233function unlisten(component, eventTarget, eventName, listener) {
234 if (eventTarget && !eventTarget.addEventListener) {
235 // Safari doesn't have EventTarget
236 eventTarget = _reactDom2.default.findDOMNode(eventTarget); //eslint-disable-line react/no-find-dom-node
237 }
238
239 const eventBus = component.utils.$EventBus;
240 return eventBus.unlisten(eventTarget, eventName, listener);
241}
242
243typeof $IMA !== 'undefined' && $IMA !== null && $IMA.Loader && $IMA.Loader.register('ima/page/componentHelpers', [], function (_export, _context) {
244 'use strict';
245 return {
246 setters: [],
247 execute: function () {
248 _export('getContextTypes', exports.getContextTypes);
249 _export('setContextTypes', exports.setContextTypes);
250 _export('getUtils', exports.getUtils);
251 _export('localize', exports.localize);
252 _export('link', exports.link);
253 _export('cssClasses', exports.cssClasses);
254 _export('defaultCssClasses', exports.defaultCssClasses);
255 _export('fire', exports.fire);
256 _export('listen', exports.listen);
257 _export('unlisten', exports.unlisten);
258 }
259 };
260});