Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

base-input.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
  2. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  3. import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
  4. import _createClass from 'babel-runtime/helpers/createClass';
  5. import _inherits from 'babel-runtime/helpers/inherits';
  6. /*
  7. Copyright 2013-2015 ASIAL CORPORATION
  8. Licensed under the Apache License, Version 2.0 (the "License");
  9. you may not use this file except in compliance with the License.
  10. You may obtain a copy of the License at
  11. http://www.apache.org/licenses/LICENSE-2.0
  12. Unless required by applicable law or agreed to in writing, software
  13. distributed under the License is distributed on an "AS IS" BASIS,
  14. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. See the License for the specific language governing permissions and
  16. limitations under the License.
  17. */
  18. import util from '../../ons/util';
  19. import BaseElement from './base-element';
  20. import autoStyle from '../../ons/autostyle';
  21. import ModifierUtil from '../../ons/internal/modifier-util';
  22. import contentReady from '../../ons/content-ready';
  23. var INPUT_ATTRIBUTES = ['autocapitalize', 'autocomplete', 'autocorrect', 'autofocus', 'disabled', 'inputmode', 'max', 'maxlength', 'min', 'minlength', 'name', 'pattern', 'placeholder', 'readonly', 'required', 'size', 'step', 'validator', 'value'];
  24. var BaseInputElement = function (_BaseElement) {
  25. _inherits(BaseInputElement, _BaseElement);
  26. _createClass(BaseInputElement, [{
  27. key: '_update',
  28. value: function _update() {} // Optionally implemented
  29. }, {
  30. key: '_scheme',
  31. get: function get() {
  32. util.throwMember();
  33. }
  34. }, {
  35. key: '_template',
  36. get: function get() {
  37. util.throwMember();
  38. }
  39. }, {
  40. key: 'type',
  41. get: function get() {
  42. util.throwMember();
  43. }
  44. }]);
  45. function BaseInputElement() {
  46. _classCallCheck(this, BaseInputElement);
  47. var _this = _possibleConstructorReturn(this, (BaseInputElement.__proto__ || _Object$getPrototypeOf(BaseInputElement)).call(this));
  48. if (_this.constructor === BaseInputElement) {
  49. util.throwAbstract();
  50. }
  51. contentReady(_this, function () {
  52. return _this._compile();
  53. });
  54. _this._boundDelegateEvent = _this._delegateEvent.bind(_this);
  55. return _this;
  56. }
  57. _createClass(BaseInputElement, [{
  58. key: '_compile',
  59. value: function _compile() {
  60. autoStyle.prepare(this);
  61. this._defaultClassName && this.classList.add(this._defaultClassName);
  62. if (this.children.length !== 0) {
  63. return;
  64. }
  65. this.appendChild(util.createFragment(this._template));
  66. this._setInputId();
  67. this._updateBoundAttributes();
  68. ModifierUtil.initModifier(this, this._scheme);
  69. }
  70. }, {
  71. key: '_updateBoundAttributes',
  72. value: function _updateBoundAttributes() {
  73. var _this2 = this;
  74. INPUT_ATTRIBUTES.forEach(function (attr) {
  75. if (_this2.hasAttribute(attr)) {
  76. _this2._input.setAttribute(attr, _this2.getAttribute(attr));
  77. } else {
  78. _this2._input.removeAttribute(attr);
  79. }
  80. });
  81. this._update();
  82. }
  83. }, {
  84. key: '_delegateEvent',
  85. value: function _delegateEvent(event) {
  86. var e = new CustomEvent(event.type, {
  87. bubbles: false,
  88. cancelable: true
  89. });
  90. return this.dispatchEvent(e);
  91. }
  92. }, {
  93. key: '_setInputId',
  94. value: function _setInputId() {
  95. if (this.hasAttribute('input-id')) {
  96. this._input.id = this.getAttribute('input-id');
  97. }
  98. }
  99. }, {
  100. key: 'connectedCallback',
  101. value: function connectedCallback() {
  102. var _this3 = this;
  103. contentReady(this, function () {
  104. _this3._input.addEventListener('focus', _this3._boundDelegateEvent);
  105. _this3._input.addEventListener('blur', _this3._boundDelegateEvent);
  106. });
  107. }
  108. }, {
  109. key: 'disconnectedCallback',
  110. value: function disconnectedCallback() {
  111. var _this4 = this;
  112. contentReady(this, function () {
  113. _this4._input.removeEventListener('focus', _this4._boundDelegateEvent);
  114. _this4._input.removeEventListener('blur', _this4._boundDelegateEvent);
  115. });
  116. }
  117. }, {
  118. key: 'attributeChangedCallback',
  119. value: function attributeChangedCallback(name, last, current) {
  120. var _this5 = this;
  121. switch (name) {
  122. case 'modifier':
  123. contentReady(this, function () {
  124. return ModifierUtil.onModifierChanged(last, current, _this5, _this5._scheme);
  125. });
  126. break;
  127. case 'input-id':
  128. contentReady(this, function () {
  129. return _this5._setInputId();
  130. });
  131. break;
  132. case 'class':
  133. util.restoreClass(this, this._defaultClassName, this._scheme);
  134. break;
  135. }
  136. if (INPUT_ATTRIBUTES.indexOf(name) >= 0) {
  137. contentReady(this, function () {
  138. return _this5._updateBoundAttributes();
  139. });
  140. }
  141. }
  142. }, {
  143. key: '_defaultClassName',
  144. get: function get() {
  145. return '';
  146. }
  147. }, {
  148. key: '_input',
  149. get: function get() {
  150. return this.querySelector('input');
  151. }
  152. }, {
  153. key: 'value',
  154. get: function get() {
  155. return this._input === null ? this.getAttribute('value') : this._input.value;
  156. },
  157. set: function set(val) {
  158. var _this6 = this;
  159. contentReady(this, function () {
  160. if (val instanceof Date) {
  161. val = val.toISOString().substring(0, 10);
  162. }
  163. _this6._input.value = val;
  164. _this6._update();
  165. });
  166. }
  167. }, {
  168. key: 'disabled',
  169. set: function set(value) {
  170. return util.toggleAttribute(this, 'disabled', value);
  171. },
  172. get: function get() {
  173. return this.hasAttribute('disabled');
  174. }
  175. }], [{
  176. key: 'observedAttributes',
  177. get: function get() {
  178. return ['modifier', 'input-id', 'class'].concat(INPUT_ATTRIBUTES);
  179. }
  180. }]);
  181. return BaseInputElement;
  182. }(BaseElement);
  183. export default BaseInputElement;