UNPKG

7.37 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define(['exports', '../../globals/js/settings', '../../globals/js/misc/mixin', '../../globals/js/mixins/create-component', '../../globals/js/mixins/init-component-by-search', '../../globals/js/mixins/handles', '../../globals/js/misc/event-matches', '../../globals/js/misc/on'], factory);
4 } else if (typeof exports !== "undefined") {
5 factory(exports, require('../../globals/js/settings'), require('../../globals/js/misc/mixin'), require('../../globals/js/mixins/create-component'), require('../../globals/js/mixins/init-component-by-search'), require('../../globals/js/mixins/handles'), require('../../globals/js/misc/event-matches'), require('../../globals/js/misc/on'));
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory(mod.exports, global.settings, global.mixin, global.createComponent, global.initComponentBySearch, global.handles, global.eventMatches, global.on);
11 global.textInput = mod.exports;
12 }
13})(this, function (exports, _settings, _mixin2, _createComponent, _initComponentBySearch, _handles, _eventMatches, _on) {
14 'use strict';
15
16 Object.defineProperty(exports, "__esModule", {
17 value: true
18 });
19
20 var _settings2 = _interopRequireDefault(_settings);
21
22 var _mixin3 = _interopRequireDefault(_mixin2);
23
24 var _createComponent2 = _interopRequireDefault(_createComponent);
25
26 var _initComponentBySearch2 = _interopRequireDefault(_initComponentBySearch);
27
28 var _handles2 = _interopRequireDefault(_handles);
29
30 var _eventMatches2 = _interopRequireDefault(_eventMatches);
31
32 var _on2 = _interopRequireDefault(_on);
33
34 function _interopRequireDefault(obj) {
35 return obj && obj.__esModule ? obj : {
36 default: obj
37 };
38 }
39
40 function _classCallCheck(instance, Constructor) {
41 if (!(instance instanceof Constructor)) {
42 throw new TypeError("Cannot call a class as a function");
43 }
44 }
45
46 var _createClass = function () {
47 function defineProperties(target, props) {
48 for (var i = 0; i < props.length; i++) {
49 var descriptor = props[i];
50 descriptor.enumerable = descriptor.enumerable || false;
51 descriptor.configurable = true;
52 if ("value" in descriptor) descriptor.writable = true;
53 Object.defineProperty(target, descriptor.key, descriptor);
54 }
55 }
56
57 return function (Constructor, protoProps, staticProps) {
58 if (protoProps) defineProperties(Constructor.prototype, protoProps);
59 if (staticProps) defineProperties(Constructor, staticProps);
60 return Constructor;
61 };
62 }();
63
64 function _possibleConstructorReturn(self, call) {
65 if (!self) {
66 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
67 }
68
69 return call && (typeof call === "object" || typeof call === "function") ? call : self;
70 }
71
72 function _inherits(subClass, superClass) {
73 if (typeof superClass !== "function" && superClass !== null) {
74 throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
75 }
76
77 subClass.prototype = Object.create(superClass && superClass.prototype, {
78 constructor: {
79 value: subClass,
80 enumerable: false,
81 writable: true,
82 configurable: true
83 }
84 });
85 if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
86 }
87
88 var TextInput = function (_mixin) {
89 _inherits(TextInput, _mixin);
90
91 /**
92 * Text Input.
93 * @extends CreateComponent
94 * @extends InitComponentBySearch
95 * @extends Handles
96 * @param {HTMLElement} element - The element functioning as a text field.
97 */
98 function TextInput(element, options) {
99 _classCallCheck(this, TextInput);
100
101 var _this = _possibleConstructorReturn(this, (TextInput.__proto__ || Object.getPrototypeOf(TextInput)).call(this, element, options));
102
103 _initialiseProps.call(_this);
104
105 _this.manage((0, _on2.default)(_this.element, 'click', function (event) {
106 var toggleVisibilityButton = (0, _eventMatches2.default)(event, _this.options.selectorPasswordVisibilityButton);
107 if (toggleVisibilityButton) {
108 _this._toggle({ element: element, button: toggleVisibilityButton });
109 }
110 }));
111 return _this;
112 }
113
114 /**
115 *
116 * @param {Object} obj - Object containing selectors and visibility status
117 * @param {HTMLElement} obj.iconVisibilityOn - The element functioning as
118 * the SVG icon for visibility on
119 * @param {HTMLElement} obj.iconVisibilityOff - The element functioning as
120 * the SVG icon for visibility off
121 * @param {boolean} obj.passwordIsVisible - The visibility of the password in the
122 * input field
123 */
124
125
126 /**
127 * Toggles the visibility of the password in the input field and changes the
128 * SVG icon indicating password visibility
129 * @param {Object} obj - The elements that can change in the component
130 * @param {HTMLElement} obj.element - The element functioning as a text field
131 * @param {HTMLElement} obj.button - The button toggling password visibility
132 */
133
134
135 _createClass(TextInput, null, [{
136 key: 'options',
137 get: function get() {
138 var prefix = _settings2.default.prefix;
139
140 return {
141 selectorInit: '[data-text-input]',
142 selectorPasswordField: '.' + prefix + '--text-input[data-toggle-password-visibility]',
143 selectorPasswordVisibilityButton: '.' + prefix + '--text-input--password__visibility',
144 passwordIsVisible: prefix + '--text-input--password-visible',
145 svgIconVisibilityOn: 'svg.icon--visibility-on',
146 svgIconVisibilityOff: 'svg.icon--visibility-off'
147 };
148 }
149 }]);
150
151 return TextInput;
152 }((0, _mixin3.default)(_createComponent2.default, _initComponentBySearch2.default, _handles2.default));
153
154 TextInput.components = new WeakMap();
155
156 var _initialiseProps = function _initialiseProps() {
157 var _this2 = this;
158
159 this._setIconVisibility = function (_ref) {
160 var iconVisibilityOn = _ref.iconVisibilityOn,
161 iconVisibilityOff = _ref.iconVisibilityOff,
162 passwordIsVisible = _ref.passwordIsVisible;
163
164 if (passwordIsVisible) {
165 iconVisibilityOn.setAttribute('hidden', true);
166 iconVisibilityOff.removeAttribute('hidden');
167 return;
168 }
169 iconVisibilityOn.removeAttribute('hidden');
170 iconVisibilityOff.setAttribute('hidden', true);
171 };
172
173 this._toggle = function (_ref2) {
174 var element = _ref2.element,
175 button = _ref2.button;
176
177 // toggle action must come before querying the classList
178 element.classList.toggle(_this2.options.passwordIsVisible);
179 var passwordIsVisible = element.classList.contains(_this2.options.passwordIsVisible);
180 var iconVisibilityOn = button.querySelector(_this2.options.svgIconVisibilityOn);
181 var iconVisibilityOff = button.querySelector(_this2.options.svgIconVisibilityOff);
182 var input = element.querySelector(_this2.options.selectorPasswordField);
183 _this2._setIconVisibility({
184 iconVisibilityOn: iconVisibilityOn,
185 iconVisibilityOff: iconVisibilityOff,
186 passwordIsVisible: passwordIsVisible
187 });
188 input.type = passwordIsVisible ? 'text' : 'password';
189 };
190 };
191
192 exports.default = TextInput;
193});
\No newline at end of file