ExamplesPlaygroundReference Source

coral-spectrum/coral-component-multifield/src/scripts/MultifieldItem.js

  1. /**
  2. * Copyright 2019 Adobe. All rights reserved.
  3. * This file is licensed to you under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License. You may obtain a copy
  5. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. *
  7. * Unless required by applicable law or agreed to in writing, software distributed under
  8. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
  9. * OF ANY KIND, either express or implied. See the License for the specific language
  10. * governing permissions and limitations under the License.
  11. */
  12.  
  13. import {BaseComponent} from '../../../coral-base-component';
  14. import '../../../coral-component-button';
  15. import item from '../templates/item';
  16. import {DragAction} from '../../../coral-dragaction';
  17. import {i18n, transform, commons} from '../../../coral-utils';
  18. import {Decorator} from '../../../coral-decorator';
  19.  
  20. const CLASSNAME = '_coral-Multifield-item';
  21.  
  22. /**
  23. @class Coral.Multifield.Item
  24. @classdesc A Multifield item component. It can have a pre-filled content different from the Multifield template but
  25. added items will always be rendered based on the template.
  26. @htmltag coral-multifield-item
  27. @extends {HTMLElement}
  28. @extends {BaseComponent}
  29. */
  30. const MultifieldItem = Decorator(class extends BaseComponent(HTMLElement) {
  31. /** @ignore */
  32. constructor() {
  33. super();
  34.  
  35. // Prepare templates
  36. this._elements = {
  37. // Create or fetch the content zones
  38. content: this.querySelector('coral-multifield-item-content') || document.createElement('coral-multifield-item-content')
  39. };
  40.  
  41. const uid = this.id || commons.getUID();
  42. this.setAttribute('id', uid);
  43. this._elements.content.setAttribute('id', `${uid}-content`);
  44. item.call(this._elements, {i18n, uid});
  45. }
  46.  
  47. /**
  48. The item content.
  49.  
  50. @type {MultifieldItemContent}
  51. @contentzone
  52. */
  53. get content() {
  54. return this._getContentZone(this._elements.content);
  55. }
  56.  
  57. set content(value) {
  58. this._setContentZone('content', value, {
  59. handle: 'content',
  60. tagName: 'coral-multifield-item-content',
  61. insert: function (content) {
  62. // Insert the content zone before the move and remove buttons
  63. this.insertBefore(content, this.firstChild);
  64. }
  65. });
  66. }
  67.  
  68. /**
  69. Specify whether the remove button is in disabled state or not.
  70.  
  71. @type {Boolean}
  72. @default false
  73. @private
  74. */
  75. get _deletable() {
  76. return typeof this.__deletable === 'boolean' ? this.__deletable : true;
  77. }
  78.  
  79. set _deletable(value) {
  80. value = transform.boolean(value);
  81. this.__deletable = value;
  82.  
  83. if(!this._readOnly) {
  84. this._elements.remove.disabled = !value;
  85. }
  86. }
  87.  
  88. /**
  89. Whether the item is set to be reorder using the keyboard
  90.  
  91. @type {boolean}
  92. @private
  93. */
  94. get _dragging() {
  95. return this.__dragging || false;
  96. }
  97.  
  98. set _dragging(value) {
  99. this.__dragging = transform.boolean(value);
  100. if (this.__dragging) {
  101. // Setting role="application" to the move button forces
  102. // NVDA and JAWS screen readers into forms mode,
  103. // so arrow keys can be used to reorder.
  104. this._elements.move.setAttribute('role', 'application');
  105. } else {
  106. // when reordering stops, restore the default role for the move button
  107. this._elements.move.removeAttribute('role');
  108. }
  109. // aria-grabbed, may be deprecated in WAI-ARIA 1.1, but it is still reported by NVDA as "draggable" or "dragging"
  110. this._elements.move.setAttribute('aria-grabbed', this.__dragging);
  111. this._elements.move.setAttribute('aria-pressed', this.__dragging);
  112. this._elements.move.selected = this.__dragging;
  113. }
  114.  
  115. /**
  116. Whether this multifieldItem is readOnly or not. Indicating that the user cannot modify the value of the multifieldItem fields.
  117. @type {Boolean}
  118. @default false
  119. @private
  120. */
  121. get _readOnly() {
  122. return this.__readOnly || false;
  123. }
  124.  
  125. set _readOnly(value) {
  126. value = transform.booleanAttr(value);
  127. this.__readOnly = value;
  128. this._reflectAttribute('_readonly', value);
  129.  
  130. // get all fields and set readonly to those whose has this property
  131. let allFields = this.querySelectorAll("*");
  132. Array.prototype.forEach.call(allFields, (field) => {
  133. if(typeof field.readOnly === "boolean") {
  134. field.readOnly = value;
  135. }
  136. });
  137.  
  138. this._elements.move.disabled = value;
  139. this._elements.remove.disabled = value;
  140. this._elements.reorderup.disabled = value;
  141. this._elements.reorderdown.disabled = value;
  142. }
  143.  
  144. get _contentZones() {
  145. return {'coral-multifield-item-content': 'content'};
  146. }
  147.  
  148. /** @ignore */
  149. static get observedAttributes() {
  150. return super.observedAttributes.concat([
  151. '_readonly'
  152. ]);
  153. }
  154.  
  155. /** @ignore */
  156. static get _attributePropertyMap() {
  157. return commons.extend(super._attributePropertyMap, {
  158. _readonly: '_readOnly',
  159. });
  160. }
  161.  
  162. /** @ignore */
  163. render() {
  164. super.render();
  165.  
  166. this.classList.add(CLASSNAME);
  167.  
  168. // a11y
  169. this.setAttribute('role', 'listitem');
  170.  
  171. // Create a fragment
  172. const fragment = document.createDocumentFragment();
  173.  
  174. const templateHandleNames = ['move', 'remove', 'reorderup', 'reorderdown'];
  175.  
  176. // Render the main template
  177. fragment.appendChild(this._elements.remove);
  178. fragment.appendChild(this._elements.move);
  179. fragment.appendChild(this._elements.reorderup);
  180. fragment.appendChild(this._elements.reorderdown);
  181.  
  182. const content = this._elements.content;
  183.  
  184. // Remove it so we can process children
  185. if (content.parentNode) {
  186. this.removeChild(content);
  187. }
  188.  
  189. // Process remaining elements as necessary
  190. while (this.firstChild) {
  191. const child = this.firstChild;
  192. if (child.nodeType === Node.TEXT_NODE ||
  193. child.nodeType === Node.ELEMENT_NODE && templateHandleNames.indexOf(child.getAttribute('handle')) === -1) {
  194. // Add non-template elements to the label
  195. content.appendChild(child);
  196. } else {
  197. // Remove anything else
  198. this.removeChild(child);
  199. }
  200. }
  201.  
  202. // Add the frag to the component
  203. this.appendChild(fragment);
  204.  
  205. // Assign the content zones, moving them into place in the process
  206. this.content = content;
  207.  
  208. // Attach drag events
  209. const dragAction = new DragAction(this);
  210. dragAction.axis = 'vertical';
  211. dragAction.handle = this._elements.move;
  212. dragAction.scroll = true;
  213. dragAction.useScrollParent = true;
  214. }
  215. });
  216.  
  217. export default MultifieldItem;