UNPKG

6.3 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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { 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 * Initializes the component.
43 *
44 * @param {Object<string, *>} props The component properties.
45 * @param {Object<string, *>} context The component context.
46 */
47
48
49 constructor(props, context) {
50 super(props, context);
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
58 this._utils = null;
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
67
68 get utils() {
69 if (!this._utils) {
70 this._utils = helpers.getUtils(this.props, this.context);
71 }
72
73 return this._utils;
74 }
75 /**
76 * Returns the localized phrase identified by the specified key. The
77 * placeholders in the localization phrase will be replaced by the provided
78 * values.
79 *
80 * @param {string} key Localization key.
81 * @param {Object<string, (number|string)>=} params Values for replacing
82 * the placeholders in the localization phrase.
83 * @return {string} Localized phrase.
84 */
85
86
87 localize(key, params = {}) {
88 return helpers.localize(this, key, params);
89 }
90 /**
91 * Generates an absolute URL using the provided route name (see the
92 * <code>app/config/routes.js</code> file). The provided parameters will
93 * replace the placeholders in the route pattern, while the extraneous
94 * parameters will be appended to the generated URL's query string.
95 *
96 * @param {string} name The route name.
97 * @param {Object<string, (number|string)>=} params Router parameters and
98 * extraneous parameters to add to the URL as a query string.
99 * @return {string} The generated URL.
100 */
101
102
103 link(name, params = {}) {
104 return helpers.link(this, name, params);
105 }
106 /**
107 * Generate a string of CSS classes from the properties of the passed-in
108 * object that resolve to true.
109 *
110 * @example
111 * this.cssClasses('my-class my-class-modificator', true);
112 * @example
113 * this.cssClasses({
114 * 'my-class': true,
115 * 'my-class-modificator': this.props.modificator
116 * }, true);
117 *
118 * @param {(string|Object<string, boolean>)} classRules CSS classes in a
119 * string separated by whitespace, or a map of CSS class names to
120 * boolean values. The CSS class name will be included in the result
121 * only if the value is {@code true}.
122 * @param {boolean} includeComponentClassName
123 * @return {string} String of CSS classes that had their property resolved
124 * to {@code true}.
125 */
126
127
128 cssClasses(classRules, includeComponentClassName = false) {
129 return helpers.cssClasses(this, classRules, includeComponentClassName);
130 }
131 /**
132 * Creates and sends a new IMA.js DOM custom event from this component.
133 *
134 * @param {string} eventName The name of the event.
135 * @param {*=} data Data to send within the event.
136 */
137
138
139 fire(eventName, data = null) {
140 helpers.fire(this, eventName, data);
141 }
142 /**
143 * Registers the provided event listener for execution whenever an IMA.js
144 * DOM custom event of the specified name occurs at the specified event
145 * target.
146 *
147 * @param {(React.Element|EventTarget)} eventTarget The react component or
148 * event target at which the listener should listen for the event.
149 * @param {string} eventName The name of the event for which to listen.
150 * @param {function(Event)} listener The listener for event to register.
151 */
152
153
154 listen(eventTarget, eventName, listener) {
155 helpers.listen(this, eventTarget, eventName, listener);
156 }
157 /**
158 * Deregisters the provided event listener for an IMA.js DOM custom event
159 * of the specified name at the specified event target.
160 *
161 * @param {(React.Element|EventTarget)} eventTarget The react component or
162 * event target at which the listener should listen for the event.
163 * @param {string} eventName The name of the event for which to listen.
164 * @param {function(Event)} listener The listener for event to register.
165 */
166
167
168 unlisten(eventTarget, eventName, listener) {
169 helpers.unlisten(this, eventTarget, eventName, listener);
170 }
171
172}
173
174exports.default = AbstractPureComponent;
175
176typeof $IMA !== 'undefined' && $IMA !== null && $IMA.Loader && $IMA.Loader.register('ima/page/AbstractPureComponent', [], function (_export, _context) {
177 'use strict';
178 return {
179 setters: [],
180 execute: function () {
181 _export('default', exports.default);
182 }
183 };
184});