UNPKG

5.35 kBJavaScriptView Raw
1function _typeof(obj) {
2 if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
3 _typeof = function _typeof(obj) {
4 return typeof obj;
5 };
6 } else {
7 _typeof = function _typeof(obj) {
8 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
9 };
10 }
11
12 return _typeof(obj);
13}
14
15function _classCallCheck(instance, Constructor) {
16 if (!(instance instanceof Constructor)) {
17 throw new TypeError("Cannot call a class as a function");
18 }
19}
20
21function _defineProperties(target, props) {
22 for (var i = 0; i < props.length; i++) {
23 var descriptor = props[i];
24 descriptor.enumerable = descriptor.enumerable || false;
25 descriptor.configurable = true;
26 if ("value" in descriptor) descriptor.writable = true;
27 Object.defineProperty(target, descriptor.key, descriptor);
28 }
29}
30
31function _createClass(Constructor, protoProps, staticProps) {
32 if (protoProps) _defineProperties(Constructor.prototype, protoProps);
33 if (staticProps) _defineProperties(Constructor, staticProps);
34 return Constructor;
35}
36
37function _possibleConstructorReturn(self, call) {
38 if (call && (_typeof(call) === "object" || typeof call === "function")) {
39 return call;
40 }
41
42 return _assertThisInitialized(self);
43}
44
45function _assertThisInitialized(self) {
46 if (self === void 0) {
47 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
48 }
49
50 return self;
51}
52
53function _getPrototypeOf(o) {
54 _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
55 return o.__proto__ || Object.getPrototypeOf(o);
56 };
57 return _getPrototypeOf(o);
58}
59
60function _inherits(subClass, superClass) {
61 if (typeof superClass !== "function" && superClass !== null) {
62 throw new TypeError("Super expression must either be null or a function");
63 }
64
65 subClass.prototype = Object.create(superClass && superClass.prototype, {
66 constructor: {
67 value: subClass,
68 writable: true,
69 configurable: true
70 }
71 });
72 if (superClass) _setPrototypeOf(subClass, superClass);
73}
74
75function _setPrototypeOf(o, p) {
76 _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
77 o.__proto__ = p;
78 return o;
79 };
80
81 return _setPrototypeOf(o, p);
82}
83/**
84 * Copyright IBM Corp. 2016, 2018
85 *
86 * This source code is licensed under the Apache-2.0 license found in the
87 * LICENSE file in the root directory of this source tree.
88 */
89
90
91import settings from '../../globals/js/settings';
92import mixin from '../../globals/js/misc/mixin';
93import createComponent from '../../globals/js/mixins/create-component';
94import initComponentBySearch from '../../globals/js/mixins/init-component-by-search';
95import handles from '../../globals/js/mixins/handles';
96import on from '../../globals/js/misc/on';
97
98var CopyButton =
99/*#__PURE__*/
100function (_mixin) {
101 _inherits(CopyButton, _mixin);
102 /**
103 * CopyBtn UI.
104 * @extends CreateComponent
105 * @extends InitComponentBySearch
106 * @extends Handles
107 * @param {HTMLElement} element The element working as a copy button UI.
108 */
109
110
111 function CopyButton(element, options) {
112 var _this;
113
114 _classCallCheck(this, CopyButton);
115
116 _this = _possibleConstructorReturn(this, _getPrototypeOf(CopyButton).call(this, element, options));
117
118 _this.manage(on(_this.element, 'click', function () {
119 return _this.handleClick();
120 }));
121
122 return _this;
123 }
124 /**
125 * Show the feedback tooltip on click. Hide the feedback tooltip after specified timeout value.
126 */
127
128
129 _createClass(CopyButton, [{
130 key: "handleClick",
131 value: function handleClick() {
132 var _this2 = this;
133
134 var feedback = this.element.querySelector(this.options.feedbackTooltip);
135
136 if (feedback) {
137 feedback.classList.add(this.options.classShowFeedback);
138 setTimeout(function () {
139 feedback.classList.remove(_this2.options.classShowFeedback);
140 }, this.options.timeoutValue);
141 }
142 }
143 /**
144 * The map associating DOM element and copy button UI instance.
145 * @member CopyBtn.components
146 * @type {WeakMap}
147 */
148
149 }], [{
150 key: "options",
151
152 /**
153 * The component options.
154 * If `options` is specified in the constructor, {@linkcode CopyBtn.create .create()}, or {@linkcode CopyBtn.init .init()},
155 * properties in this object are overriden for the instance being create and how {@linkcode CopyBtn.init .init()} works.
156 * @member CopyBtn.options
157 * @type {Object}
158 * @property {string} selectorInit The data attribute to find copy button UIs.
159 * @property {string} feedbackTooltip The data attribute to find feedback tooltip.
160 * @property {string} classShowFeedback The CSS selector for showing the feedback tooltip.
161 * @property {number} timeoutValue The specified timeout value before the feedback tooltip is hidden.
162 */
163 get: function get() {
164 var prefix = settings.prefix;
165 return {
166 selectorInit: '[data-copy-btn]',
167 feedbackTooltip: '[data-feedback]',
168 classShowFeedback: "".concat(prefix, "--btn--copy__feedback--displayed"),
169 timeoutValue: 2000
170 };
171 }
172 }]);
173
174 CopyButton.components = new WeakMap();
175 return CopyButton;
176}(mixin(createComponent, initComponentBySearch, handles));
177
178export default CopyButton;
\No newline at end of file