a zip code crypto-currency system good for red ONLY

base-input.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import { EventEmitter, Input, Output } from '@angular/core';
  2. import { deepCopy, isArray, isPresent, isString, isTrueProperty, isUndefined } from './util';
  3. import { Ion } from '../components/ion';
  4. import { TimeoutDebouncer } from './debouncer';
  5. export class BaseInput extends Ion {
  6. constructor(config, elementRef, renderer, name, _defaultValue, _form, _item, _ngControl) {
  7. super(config, elementRef, renderer, name);
  8. this._defaultValue = _defaultValue;
  9. this._form = _form;
  10. this._item = _item;
  11. this._ngControl = _ngControl;
  12. this._isFocus = false;
  13. this._disabled = false;
  14. this._debouncer = new TimeoutDebouncer(0);
  15. this._init = false;
  16. this._initModel = false;
  17. /**
  18. * @output {Range} Emitted when the range selector drag starts.
  19. */
  20. this.ionFocus = new EventEmitter();
  21. /**
  22. * @output {Range} Emitted when the range value changes.
  23. */
  24. this.ionChange = new EventEmitter();
  25. /**
  26. * @output {Range} Emitted when the range selector drag ends.
  27. */
  28. this.ionBlur = new EventEmitter();
  29. _form && _form.register(this);
  30. this._value = deepCopy(this._defaultValue);
  31. if (_item) {
  32. (void 0) /* assert */;
  33. this.id = name + '-' + _item.registerInput(name);
  34. this._labelId = _item.labelId;
  35. this._item.setElementClass('item-' + name, true);
  36. }
  37. // If the user passed a ngControl we need to set the valueAccessor
  38. if (_ngControl) {
  39. _ngControl.valueAccessor = this;
  40. }
  41. }
  42. /**
  43. * @input {boolean} If true, the user cannot interact with this element.
  44. */
  45. get disabled() {
  46. return this._disabled;
  47. }
  48. set disabled(val) {
  49. this.setDisabledState(val);
  50. }
  51. get value() {
  52. return this._value;
  53. }
  54. set value(val) {
  55. if (this._writeValue(val)) {
  56. this.onChange();
  57. this._fireIonChange();
  58. }
  59. }
  60. // 1. Updates the value
  61. // 2. Calls _inputUpdated()
  62. // 3. Dispatch onChange events
  63. setValue(val) {
  64. this.value = val;
  65. }
  66. /**
  67. * @hidden
  68. */
  69. setDisabledState(isDisabled) {
  70. this._disabled = isDisabled = isTrueProperty(isDisabled);
  71. this._item && this._item.setElementClass(`item-${this._componentName}-disabled`, isDisabled);
  72. }
  73. /**
  74. * @hidden
  75. */
  76. writeValue(val) {
  77. if (this._writeValue(val)) {
  78. if (this._initModel) {
  79. this._fireIonChange();
  80. }
  81. else if (this._init) {
  82. // ngModel fires the first time too late, we need to skip the first ngModel update
  83. this._initModel = true;
  84. }
  85. }
  86. }
  87. /**
  88. * @hidden
  89. */
  90. _writeValue(val) {
  91. (void 0) /* assert */;
  92. if (isUndefined(val)) {
  93. return false;
  94. }
  95. const normalized = (val === null)
  96. ? deepCopy(this._defaultValue)
  97. : this._inputNormalize(val);
  98. const notUpdate = isUndefined(normalized) || !this._inputShouldChange(normalized);
  99. if (notUpdate) {
  100. return false;
  101. }
  102. (void 0) /* console.debug */;
  103. this._value = normalized;
  104. if (this._init) {
  105. this._inputUpdated();
  106. }
  107. return true;
  108. }
  109. /**
  110. * @hidden
  111. */
  112. _fireIonChange() {
  113. if (this._init) {
  114. this._debouncer.debounce(() => {
  115. (void 0) /* assert */;
  116. this.ionChange.emit(this._inputChangeEvent());
  117. this._initModel = true;
  118. });
  119. }
  120. }
  121. /**
  122. * @hidden
  123. */
  124. registerOnChange(fn) {
  125. this._onChanged = fn;
  126. }
  127. /**
  128. * @hidden
  129. */
  130. registerOnTouched(fn) {
  131. this._onTouched = fn;
  132. }
  133. /**
  134. * @hidden
  135. */
  136. _initialize() {
  137. if (this._init) {
  138. (void 0) /* assert */;
  139. return;
  140. }
  141. this._init = true;
  142. if (isPresent(this._value)) {
  143. this._inputUpdated();
  144. }
  145. }
  146. /**
  147. * @hidden
  148. */
  149. _fireFocus() {
  150. if (this._isFocus) {
  151. return;
  152. }
  153. (void 0) /* console.debug */;
  154. this._form && this._form.setAsFocused(this);
  155. this._setFocus(true);
  156. this.ionFocus.emit(this);
  157. }
  158. /**
  159. * @hidden
  160. */
  161. _fireBlur() {
  162. if (!this._isFocus) {
  163. return;
  164. }
  165. (void 0) /* console.debug */;
  166. this._form && this._form.unsetAsFocused(this);
  167. this._setFocus(false);
  168. this._fireTouched();
  169. this.ionBlur.emit(this);
  170. }
  171. /**
  172. * @hidden
  173. */
  174. _fireTouched() {
  175. this._onTouched && this._onTouched();
  176. }
  177. /**
  178. * @hidden
  179. */
  180. _setFocus(isFocused) {
  181. (void 0) /* assert */;
  182. (void 0) /* assert */;
  183. (void 0) /* assert */;
  184. this._isFocus = isFocused;
  185. const item = this._item;
  186. if (item) {
  187. item.setElementClass('input-has-focus', isFocused);
  188. item.setElementClass('item-input-has-focus', isFocused);
  189. }
  190. this._inputUpdated();
  191. }
  192. /**
  193. * @hidden
  194. */
  195. onChange() {
  196. this._onChanged && this._onChanged(this._inputNgModelEvent());
  197. }
  198. /**
  199. * @hidden
  200. */
  201. isFocus() {
  202. return this._isFocus;
  203. }
  204. /**
  205. * @hidden
  206. */
  207. hasValue() {
  208. const val = this._value;
  209. if (!isPresent(val)) {
  210. return false;
  211. }
  212. if (isArray(val) || isString(val)) {
  213. return val.length > 0;
  214. }
  215. return true;
  216. }
  217. /**
  218. * @hidden
  219. */
  220. focusNext() {
  221. this._form && this._form.tabFocus(this);
  222. }
  223. /**
  224. * @hidden
  225. */
  226. ngOnDestroy() {
  227. (void 0) /* assert */;
  228. const form = this._form;
  229. form && form.deregister(this);
  230. this._init = false;
  231. }
  232. /**
  233. * @hidden
  234. */
  235. ngAfterContentInit() {
  236. this._initialize();
  237. }
  238. /**
  239. * @hidden
  240. */
  241. initFocus() {
  242. const ele = this._elementRef.nativeElement.querySelector('button');
  243. ele && ele.focus();
  244. }
  245. /**
  246. * @hidden
  247. */
  248. _inputNormalize(val) {
  249. return val;
  250. }
  251. /**
  252. * @hidden
  253. */
  254. _inputShouldChange(val) {
  255. return this._value !== val;
  256. }
  257. /**
  258. * @hidden
  259. */
  260. _inputChangeEvent() {
  261. return this;
  262. }
  263. /**
  264. * @hidden
  265. */
  266. _inputNgModelEvent() {
  267. return this._value;
  268. }
  269. /**
  270. * @hidden
  271. */
  272. _inputUpdated() {
  273. (void 0) /* assert */;
  274. const item = this._item;
  275. if (item) {
  276. setControlCss(item, this._ngControl);
  277. // TODO remove all uses of input-has-value in v4
  278. let hasValue = this.hasValue();
  279. item.setElementClass('input-has-value', hasValue);
  280. item.setElementClass('item-input-has-value', hasValue);
  281. }
  282. }
  283. }
  284. BaseInput.propDecorators = {
  285. 'ionFocus': [{ type: Output },],
  286. 'ionChange': [{ type: Output },],
  287. 'ionBlur': [{ type: Output },],
  288. 'disabled': [{ type: Input },],
  289. };
  290. function setControlCss(element, control) {
  291. if (!control) {
  292. return;
  293. }
  294. element.setElementClass('ng-untouched', control.untouched);
  295. element.setElementClass('ng-touched', control.touched);
  296. element.setElementClass('ng-pristine', control.pristine);
  297. element.setElementClass('ng-dirty', control.dirty);
  298. element.setElementClass('ng-valid', control.valid);
  299. element.setElementClass('ng-invalid', !control.valid);
  300. }
  301. //# sourceMappingURL=base-input.js.map