calcite-inline-editable.cjs.entry.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. const index = require('./index-a0010f96.js');
  9. const dom = require('./dom-2ec8c9ed.js');
  10. const label = require('./label-28060b83.js');
  11. const observers = require('./observers-5706326b.js');
  12. const interactive = require('./interactive-32293bca.js');
  13. require('./resources-b5a5f8a7.js');
  14. require('./guid-f4f03a7a.js');
  15. const CSS = {
  16. wrapper: "wrapper",
  17. confirmChangesButton: "confirm-changes-button",
  18. cancelEditingButton: "cancel-editing-button",
  19. inputWrapper: "input-wrapper",
  20. cancelEditingButtonWrapper: "cancel-editing-button-wrapper",
  21. enableEditingButton: "enable-editing-button",
  22. controlsWrapper: "controls-wrapper"
  23. };
  24. const TEXT = {
  25. intlEnablingEditing: "Click to edit",
  26. intlCancelEditing: "Cancel",
  27. intlConfirmChanges: "Save"
  28. };
  29. const inlineEditableCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host{display:block}:host([scale=s]) .controls-wrapper{block-size:1.5rem}:host([scale=m]) .controls-wrapper{block-size:2rem}:host([scale=l]) .controls-wrapper{block-size:2.75rem}:host(:not([editing-enabled]):not([disabled])) .wrapper:hover{background-color:var(--calcite-ui-foreground-2)}.wrapper{box-sizing:border-box;display:flex;justify-content:space-between;background-color:var(--calcite-ui-foreground-1);transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}.wrapper .input-wrapper{flex:1 1 0%}.controls-wrapper{display:flex}:host([disabled]) .cancel-editing-button-wrapper{border-color:var(--calcite-ui-border-2)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}";
  30. const InlineEditable = class {
  31. constructor(hostRef) {
  32. index.registerInstance(this, hostRef);
  33. this.calciteInlineEditableEditCancel = index.createEvent(this, "calciteInlineEditableEditCancel", 6);
  34. this.calciteInlineEditableEditConfirm = index.createEvent(this, "calciteInlineEditableEditConfirm", 6);
  35. this.calciteInternalInlineEditableEnableEditingChange = index.createEvent(this, "calciteInternalInlineEditableEnableEditingChange", 6);
  36. //--------------------------------------------------------------------------
  37. //
  38. // Props
  39. //
  40. //--------------------------------------------------------------------------
  41. /** specify whether editing can be enabled */
  42. this.disabled = false;
  43. /** specify whether the wrapped input element is editable, defaults to false */
  44. this.editingEnabled = false;
  45. /** specify whether the confirm button should display a loading state, defaults to false */
  46. this.loading = false;
  47. /** specify whether save/cancel controls should be displayed when editingEnabled is true, defaults to false */
  48. this.controls = false;
  49. /**
  50. * specify text to be user for the enable editing button's aria-label, defaults to `Click to edit`
  51. *
  52. * @default "Click to edit"
  53. */
  54. this.intlEnableEditing = TEXT.intlEnablingEditing;
  55. /**
  56. * specify text to be user for the cancel editing button's aria-label, defaults to `Cancel`
  57. *
  58. * @default "Cancel"
  59. */
  60. this.intlCancelEditing = TEXT.intlCancelEditing;
  61. /**
  62. * specify text to be user for the confirm changes button's aria-label, defaults to `Save`
  63. *
  64. * @default "Save"
  65. */
  66. this.intlConfirmChanges = TEXT.intlConfirmChanges;
  67. this.mutationObserver = observers.createObserver("mutation", () => this.mutationObserverCallback());
  68. this.enableEditing = () => {
  69. var _a, _b;
  70. this.valuePriorToEditing = (_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.value;
  71. this.editingEnabled = true;
  72. (_b = this.inputElement) === null || _b === void 0 ? void 0 : _b.setFocus();
  73. this.calciteInternalInlineEditableEnableEditingChange.emit();
  74. };
  75. this.disableEditing = () => {
  76. this.editingEnabled = false;
  77. };
  78. this.cancelEditing = () => {
  79. if (this.inputElement) {
  80. this.inputElement.value = this.valuePriorToEditing;
  81. }
  82. this.disableEditing();
  83. this.enableEditingButton.setFocus();
  84. if (!this.editingEnabled && !!this.shouldEmitCancel) {
  85. this.calciteInlineEditableEditCancel.emit();
  86. }
  87. };
  88. this.escapeKeyHandler = async (event) => {
  89. var _a;
  90. if (event.defaultPrevented) {
  91. return;
  92. }
  93. if (event.key === "Escape") {
  94. event.preventDefault();
  95. this.cancelEditing();
  96. }
  97. if (event.key === "Tab" && this.shouldShowControls) {
  98. if (!event.shiftKey && event.target === this.inputElement) {
  99. event.preventDefault();
  100. this.cancelEditingButton.setFocus();
  101. }
  102. if (!!event.shiftKey && event.target === this.cancelEditingButton) {
  103. event.preventDefault();
  104. (_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.setFocus();
  105. }
  106. }
  107. };
  108. this.cancelEditingHandler = async (event) => {
  109. event.preventDefault();
  110. this.cancelEditing();
  111. };
  112. this.enableEditingHandler = async (event) => {
  113. if (this.disabled ||
  114. event.target === this.cancelEditingButton ||
  115. event.target === this.confirmEditingButton) {
  116. return;
  117. }
  118. event.preventDefault();
  119. if (!this.editingEnabled) {
  120. this.enableEditing();
  121. }
  122. };
  123. this.confirmChangesHandler = async (event) => {
  124. event.preventDefault();
  125. this.calciteInlineEditableEditConfirm.emit();
  126. try {
  127. if (this.afterConfirm) {
  128. this.loading = true;
  129. await this.afterConfirm();
  130. this.disableEditing();
  131. this.enableEditingButton.setFocus();
  132. }
  133. }
  134. catch (error) {
  135. }
  136. finally {
  137. this.loading = false;
  138. }
  139. };
  140. }
  141. disabledWatcher(disabled) {
  142. if (this.inputElement) {
  143. this.inputElement.disabled = disabled;
  144. }
  145. }
  146. editingEnabledWatcher(newValue, oldValue) {
  147. if (this.inputElement) {
  148. this.inputElement.editingEnabled = newValue;
  149. }
  150. if (!newValue && !!oldValue) {
  151. this.shouldEmitCancel = true;
  152. }
  153. }
  154. //--------------------------------------------------------------------------
  155. //
  156. // Lifecycle
  157. //
  158. //--------------------------------------------------------------------------
  159. connectedCallback() {
  160. var _a;
  161. label.connectLabel(this);
  162. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true });
  163. this.mutationObserverCallback();
  164. }
  165. disconnectedCallback() {
  166. var _a;
  167. label.disconnectLabel(this);
  168. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  169. }
  170. componentDidRender() {
  171. interactive.updateHostInteraction(this);
  172. }
  173. render() {
  174. return (index.h("div", { class: CSS.wrapper, onClick: this.enableEditingHandler, onKeyDown: this.escapeKeyHandler }, index.h("div", { class: CSS.inputWrapper }, index.h("slot", null)), index.h("div", { class: CSS.controlsWrapper }, index.h("calcite-button", { appearance: "transparent", class: CSS.enableEditingButton, color: "neutral", disabled: this.disabled, iconStart: "pencil", label: this.intlEnableEditing, onClick: this.enableEditingHandler, ref: (el) => (this.enableEditingButton = el), scale: this.scale, style: {
  175. opacity: this.editingEnabled ? "0" : "1",
  176. width: this.editingEnabled ? "0" : "inherit"
  177. }, type: "button" }), this.shouldShowControls && [
  178. index.h("div", { class: CSS.cancelEditingButtonWrapper }, index.h("calcite-button", { appearance: "transparent", class: CSS.cancelEditingButton, color: "neutral", disabled: this.disabled, iconStart: "x", label: this.intlCancelEditing, onClick: this.cancelEditingHandler, ref: (el) => (this.cancelEditingButton = el), scale: this.scale, type: "button" })),
  179. index.h("calcite-button", { appearance: "solid", class: CSS.confirmChangesButton, color: "blue", disabled: this.disabled, iconStart: "check", label: this.intlConfirmChanges, loading: this.loading, onClick: this.confirmChangesHandler, ref: (el) => (this.confirmEditingButton = el), scale: this.scale, type: "button" })
  180. ])));
  181. }
  182. //--------------------------------------------------------------------------
  183. //
  184. // Event Listeners
  185. //
  186. //--------------------------------------------------------------------------
  187. blurHandler() {
  188. if (!this.controls) {
  189. this.disableEditing();
  190. }
  191. }
  192. //--------------------------------------------------------------------------
  193. //
  194. // Public Methods
  195. //
  196. //--------------------------------------------------------------------------
  197. async setFocus() {
  198. var _a, _b;
  199. if (this.editingEnabled) {
  200. (_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.setFocus();
  201. }
  202. else {
  203. (_b = this.enableEditingButton) === null || _b === void 0 ? void 0 : _b.setFocus();
  204. }
  205. }
  206. //--------------------------------------------------------------------------
  207. //
  208. // Private Methods
  209. //
  210. //--------------------------------------------------------------------------
  211. mutationObserverCallback() {
  212. var _a;
  213. this.updateSlottedInput();
  214. this.scale =
  215. this.scale || ((_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.scale) || dom.getElementProp(this.el, "scale", undefined);
  216. }
  217. onLabelClick() {
  218. this.setFocus();
  219. }
  220. updateSlottedInput() {
  221. const inputElement = dom.getSlotted(this.el, {
  222. matches: "calcite-input"
  223. });
  224. this.inputElement = inputElement;
  225. if (!inputElement) {
  226. return;
  227. }
  228. this.inputElement.disabled = this.disabled;
  229. this.inputElement.label = this.inputElement.label || label.getLabelText(this);
  230. }
  231. get shouldShowControls() {
  232. return this.editingEnabled && this.controls;
  233. }
  234. get el() { return index.getElement(this); }
  235. static get watchers() { return {
  236. "disabled": ["disabledWatcher"],
  237. "editingEnabled": ["editingEnabledWatcher"]
  238. }; }
  239. };
  240. InlineEditable.style = inlineEditableCss;
  241. exports.calcite_inline_editable = InlineEditable;