UNPKG

6.08 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _react = require('react');
8
9var _react2 = _interopRequireDefault(_react);
10
11var _componentHelpers = require('./componentHelpers');
12
13var helpers = _interopRequireWildcard(_componentHelpers);
14
15function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
19/**
20 * The base class for all pure (state-less) view components.
21 *
22 * Unlike the "regular" components, pure components do not have any external
23 * state, and therefore are pure functions of their props and state. This
24 * allows for some nice optimizations on react's part (see the link).
25 *
26 * Because of this, this class does not provide all the APIs provided by the
27 * {@linkcode AbstractComponent} class (e.g. {@code listen}) as there is next
28 * to none use of them with pure components.
29 *
30 * @abstract
31 * @see https://facebook.github.io/react/docs/react-api.html#react.purecomponent
32 */
33class AbstractPureComponent extends _react2.default.PureComponent {
34 static get contextTypes() {
35 return helpers.getContextTypes(this);
36 }
37
38 static set contextTypes(contextTypes) {
39 helpers.setContextTypes(this, contextTypes);
40 }
41
42 /**
43 * Initializes the component.
44 *
45 * @param {Object<string, *>} props The component properties.
46 * @param {Object<string, *>} context The component context.
47 */
48 constructor(props, context) {
49 super(props, context);
50
51 /**
52 * The view utilities, initialized lazily upon first use from either
53 * the context, or the component's props.
54 *
55 * @type {?Object<string, *>}
56 */
57 this._utils = null;
58 }
59
60 /**
61 * Returns the utilities for the view components. The returned value is the
62 * value bound to the {@code $Utils} object container constant.
63 *
64 * @return {Object<string, *>} The utilities for the view components.
65 */
66 get utils() {
67 if (!this._utils) {
68 this._utils = helpers.getUtils(this.props, this.context);
69 }
70
71 return this._utils;
72 }
73
74 /**
75 * Returns the localized phrase identified by the specified key. The
76 * placeholders in the localization phrase will be replaced by the provided
77 * values.
78 *
79 * @param {string} key Localization key.
80 * @param {Object<string, (number|string)>=} params Values for replacing
81 * the placeholders in the localization phrase.
82 * @return {string} Localized phrase.
83 */
84 localize(key, params = {}) {
85 return helpers.localize(this, key, params);
86 }
87
88 /**
89 * Generates an absolute URL using the provided route name (see the
90 * <code>app/config/routes.js</code> file). The provided parameters will
91 * replace the placeholders in the route pattern, while the extraneous
92 * parameters will be appended to the generated URL's query string.
93 *
94 * @param {string} name The route name.
95 * @param {Object<string, (number|string)>=} params Router parameters and
96 * extraneous parameters to add to the URL as a query string.
97 * @return {string} The generated URL.
98 */
99 link(name, params = {}) {
100 return helpers.link(this, name, params);
101 }
102
103 /**
104 * Generate a string of CSS classes from the properties of the passed-in
105 * object that resolve to true.
106 *
107 * @example
108 * this.cssClasses('my-class my-class-modificator', true);
109 * @example
110 * this.cssClasses({
111 * 'my-class': true,
112 * 'my-class-modificator': this.props.modificator
113 * }, true);
114 *
115 * @param {(string|Object<string, boolean>)} classRules CSS classes in a
116 * string separated by whitespace, or a map of CSS class names to
117 * boolean values. The CSS class name will be included in the result
118 * only if the value is {@code true}.
119 * @param {boolean} includeComponentClassName
120 * @return {string} String of CSS classes that had their property resolved
121 * to {@code true}.
122 */
123 cssClasses(classRules, includeComponentClassName = false) {
124 return helpers.cssClasses(this, classRules, includeComponentClassName);
125 }
126
127 /**
128 * Creates and sends a new IMA.js DOM custom event from this component.
129 *
130 * @param {string} eventName The name of the event.
131 * @param {*=} data Data to send within the event.
132 */
133 fire(eventName, data = null) {
134 helpers.fire(this, eventName, data);
135 }
136
137 /**
138 * Registers the provided event listener for execution whenever an IMA.js
139 * DOM custom event of the specified name occurs at the specified event
140 * target.
141 *
142 * @param {(React.Element|EventTarget)} eventTarget The react component or
143 * event target at which the listener should listen for the event.
144 * @param {string} eventName The name of the event for which to listen.
145 * @param {function(Event)} listener The listener for event to register.
146 */
147 listen(eventTarget, eventName, listener) {
148 helpers.listen(this, eventTarget, eventName, listener);
149 }
150
151 /**
152 * Deregisters the provided event listener for an IMA.js DOM custom event
153 * of the specified name at the specified event target.
154 *
155 * @param {(React.Element|EventTarget)} eventTarget The react component or
156 * event target at which the listener should listen for the event.
157 * @param {string} eventName The name of the event for which to listen.
158 * @param {function(Event)} listener The listener for event to register.
159 */
160 unlisten(eventTarget, eventName, listener) {
161 helpers.unlisten(this, eventTarget, eventName, listener);
162 }
163}
164exports.default = AbstractPureComponent;
165
166typeof $IMA !== 'undefined' && $IMA !== null && $IMA.Loader && $IMA.Loader.register('ima/page/AbstractPureComponent', [], function (_export, _context) {
167 'use strict';
168 return {
169 setters: [],
170 execute: function () {
171 _export('default', exports.default);
172 }
173 };
174});