UI for Zipcoin Blue

searchbar.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = Object.setPrototypeOf ||
  3. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  4. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  5. return function (d, b) {
  6. extendStatics(d, b);
  7. function __() { this.constructor = d; }
  8. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  9. };
  10. })();
  11. import { Component, ElementRef, EventEmitter, Input, Optional, Output, Renderer, ViewChild, ViewEncapsulation } from '@angular/core';
  12. import { NgControl } from '@angular/forms';
  13. import { Config } from '../../config/config';
  14. import { BaseInput } from '../../util/base-input';
  15. import { isPresent, isTrueProperty } from '../../util/util';
  16. import { TimeoutDebouncer } from '../../util/debouncer';
  17. import { Platform } from '../../platform/platform';
  18. /**
  19. * @name Searchbar
  20. * @module ionic
  21. * @description
  22. * Manages the display of a Searchbar which can be used to search or filter items.
  23. *
  24. * @usage
  25. * ```html
  26. * <ion-searchbar
  27. * [(ngModel)]="myInput"
  28. * [showCancelButton]="shouldShowCancel"
  29. * (ionInput)="onInput($event)"
  30. * (ionCancel)="onCancel($event)">
  31. * </ion-searchbar>
  32. * ```
  33. *
  34. * @demo /docs/demos/src/searchbar/
  35. * @see {@link /docs/components#searchbar Searchbar Component Docs}
  36. */
  37. var Searchbar = (function (_super) {
  38. __extends(Searchbar, _super);
  39. function Searchbar(config, _plt, elementRef, renderer, ngControl) {
  40. var _this = _super.call(this, config, elementRef, renderer, 'searchbar', '', null, null, ngControl) || this;
  41. _this._plt = _plt;
  42. _this._shouldBlur = true;
  43. _this._shouldAlignLeft = true;
  44. _this._isCancelVisible = false;
  45. _this._spellcheck = false;
  46. _this._autocomplete = 'off';
  47. _this._autocorrect = 'off';
  48. _this._isActive = false;
  49. _this._showCancelButton = false;
  50. _this._animated = false;
  51. _this._inputDebouncer = new TimeoutDebouncer(0);
  52. /**
  53. * @input {string} Set the the cancel button text. Default: `"Cancel"`.
  54. */
  55. _this.cancelButtonText = 'Cancel';
  56. /**
  57. * @input {string} Set the input's placeholder. Default `"Search"`.
  58. */
  59. _this.placeholder = 'Search';
  60. /**
  61. * @input {string} Set the type of the input. Values: `"text"`, `"password"`, `"email"`, `"number"`, `"search"`, `"tel"`, `"url"`. Default `"search"`.
  62. */
  63. _this.type = 'search';
  64. /**
  65. * @output {event} Emitted when the Searchbar input has changed, including when it's cleared.
  66. */
  67. _this.ionInput = new EventEmitter();
  68. /**
  69. * @output {event} Emitted when the cancel button is clicked.
  70. */
  71. _this.ionCancel = new EventEmitter();
  72. /**
  73. * @output {event} Emitted when the clear input button is clicked.
  74. */
  75. _this.ionClear = new EventEmitter();
  76. _this.debounce = 250;
  77. return _this;
  78. }
  79. Object.defineProperty(Searchbar.prototype, "showCancelButton", {
  80. /**
  81. * @input {boolean} If true, show the cancel button. Default `false`.
  82. */
  83. get: function () {
  84. return this._showCancelButton;
  85. },
  86. set: function (val) {
  87. this._showCancelButton = isTrueProperty(val);
  88. },
  89. enumerable: true,
  90. configurable: true
  91. });
  92. Object.defineProperty(Searchbar.prototype, "debounce", {
  93. /**
  94. * @input {number} How long, in milliseconds, to wait to trigger the `ionInput` event after each keystroke. Default `250`.
  95. */
  96. get: function () {
  97. return this._debouncer.wait;
  98. },
  99. set: function (val) {
  100. this._debouncer.wait = val;
  101. this._inputDebouncer.wait = val;
  102. },
  103. enumerable: true,
  104. configurable: true
  105. });
  106. Object.defineProperty(Searchbar.prototype, "autocomplete", {
  107. /**
  108. * @input {string} Set the input's autocomplete property. Values: `"on"`, `"off"`. Default `"off"`.
  109. */
  110. set: function (val) {
  111. this._autocomplete = (val === '' || val === 'on') ? 'on' : this._config.get('autocomplete', 'off');
  112. },
  113. enumerable: true,
  114. configurable: true
  115. });
  116. Object.defineProperty(Searchbar.prototype, "autocorrect", {
  117. /**
  118. * @input {string} Set the input's autocorrect property. Values: `"on"`, `"off"`. Default `"off"`.
  119. */
  120. set: function (val) {
  121. this._autocorrect = (val === '' || val === 'on') ? 'on' : this._config.get('autocorrect', 'off');
  122. },
  123. enumerable: true,
  124. configurable: true
  125. });
  126. Object.defineProperty(Searchbar.prototype, "spellcheck", {
  127. /**
  128. * @input {string|boolean} Set the input's spellcheck property. Values: `true`, `false`. Default `false`.
  129. */
  130. set: function (val) {
  131. this._spellcheck = (val === '' || val === 'true' || val === true) ? true : this._config.getBoolean('spellcheck', false);
  132. },
  133. enumerable: true,
  134. configurable: true
  135. });
  136. Object.defineProperty(Searchbar.prototype, "animated", {
  137. /**
  138. * @input {boolean} If true, enable searchbar animation. Default `false`.
  139. */
  140. get: function () {
  141. return this._animated;
  142. },
  143. set: function (val) {
  144. this._animated = isTrueProperty(val);
  145. },
  146. enumerable: true,
  147. configurable: true
  148. });
  149. /**
  150. * @hidden
  151. * On Initialization check for attributes
  152. */
  153. Searchbar.prototype.ngOnInit = function () {
  154. var showCancelButton = this.showCancelButton;
  155. if (typeof showCancelButton === 'string') {
  156. this.showCancelButton = (showCancelButton === '' || showCancelButton === 'true');
  157. }
  158. };
  159. /**
  160. * @hidden
  161. */
  162. Searchbar.prototype._inputUpdated = function () {
  163. var ele = this._searchbarInput.nativeElement;
  164. var value = this._value;
  165. // It is important not to re-assign the value if it is the same, because,
  166. // otherwise, the caret is moved to the end of the input
  167. if (ele.value !== value) {
  168. ele.value = value;
  169. }
  170. this.positionElements();
  171. };
  172. /**
  173. * @hidden
  174. * Positions the input search icon, placeholder, and the cancel button
  175. * based on the input value and if it is focused. (ios only)
  176. */
  177. Searchbar.prototype.positionElements = function () {
  178. var isAnimated = this._animated;
  179. var prevAlignLeft = this._shouldAlignLeft;
  180. var shouldAlignLeft = (!isAnimated || (this._value && this._value.toString().trim() !== '') || this._isFocus === true);
  181. this._shouldAlignLeft = shouldAlignLeft;
  182. if (this._mode !== 'ios') {
  183. return;
  184. }
  185. if (prevAlignLeft !== shouldAlignLeft) {
  186. this.positionPlaceholder();
  187. }
  188. if (isAnimated) {
  189. this.positionCancelButton();
  190. }
  191. };
  192. Searchbar.prototype.positionPlaceholder = function () {
  193. var inputEle = this._searchbarInput.nativeElement;
  194. var iconEle = this._searchbarIcon.nativeElement;
  195. if (this._shouldAlignLeft) {
  196. inputEle.removeAttribute('style');
  197. iconEle.removeAttribute('style');
  198. }
  199. else {
  200. // Create a dummy span to get the placeholder width
  201. var doc = this._plt.doc();
  202. var tempSpan = doc.createElement('span');
  203. tempSpan.innerHTML = this.placeholder;
  204. doc.body.appendChild(tempSpan);
  205. // Get the width of the span then remove it
  206. var textWidth = tempSpan.offsetWidth;
  207. doc.body.removeChild(tempSpan);
  208. // Set the input padding start
  209. var inputLeft = 'calc(50% - ' + (textWidth / 2) + 'px)';
  210. if (this._plt.isRTL) {
  211. inputEle.style.paddingRight = inputLeft;
  212. }
  213. else {
  214. inputEle.style.paddingLeft = inputLeft;
  215. }
  216. // Set the icon margin start
  217. var iconLeft = 'calc(50% - ' + ((textWidth / 2) + 30) + 'px)';
  218. if (this._plt.isRTL) {
  219. iconEle.style.marginRight = iconLeft;
  220. }
  221. else {
  222. iconEle.style.marginLeft = iconLeft;
  223. }
  224. }
  225. };
  226. /**
  227. * @hidden
  228. * Show the iOS Cancel button on focus, hide it offscreen otherwise
  229. */
  230. Searchbar.prototype.positionCancelButton = function () {
  231. var showShowCancel = this._isFocus;
  232. if (showShowCancel !== this._isCancelVisible) {
  233. var cancelStyleEle = this._cancelButton.nativeElement;
  234. var cancelStyle = cancelStyleEle.style;
  235. this._isCancelVisible = showShowCancel;
  236. if (showShowCancel) {
  237. if (this._plt.isRTL) {
  238. cancelStyle.marginLeft = '0';
  239. }
  240. else {
  241. cancelStyle.marginRight = '0';
  242. }
  243. }
  244. else {
  245. var offset = cancelStyleEle.offsetWidth;
  246. if (offset > 0) {
  247. if (this._plt.isRTL) {
  248. cancelStyle.marginLeft = -offset + 'px';
  249. }
  250. else {
  251. cancelStyle.marginRight = -offset + 'px';
  252. }
  253. }
  254. }
  255. }
  256. };
  257. /**
  258. * @hidden
  259. * Update the Searchbar input value when the input changes
  260. */
  261. Searchbar.prototype.inputChanged = function (ev) {
  262. var _this = this;
  263. this.value = ev.target.value;
  264. this._inputDebouncer.debounce(function () {
  265. _this.ionInput.emit(ev);
  266. });
  267. };
  268. /**
  269. * @hidden
  270. * Sets the Searchbar to focused and active on input focus.
  271. */
  272. Searchbar.prototype.inputFocused = function () {
  273. this._isActive = true;
  274. this._fireFocus();
  275. this.positionElements();
  276. };
  277. /**
  278. * @hidden
  279. * Sets the Searchbar to not focused and checks if it should align left
  280. * based on whether there is a value in the searchbar or not.
  281. */
  282. Searchbar.prototype.inputBlurred = function () {
  283. // _shouldBlur determines if it should blur
  284. // if we are clearing the input we still want to stay focused in the input
  285. if (this._shouldBlur === false) {
  286. this._searchbarInput.nativeElement.focus();
  287. this._shouldBlur = true;
  288. return;
  289. }
  290. this._fireBlur();
  291. this.positionElements();
  292. };
  293. /**
  294. * @hidden
  295. * Clears the input field and triggers the control change.
  296. */
  297. Searchbar.prototype.clearInput = function (ev) {
  298. var _this = this;
  299. this.ionClear.emit(ev);
  300. // setTimeout() fixes https://github.com/ionic-team/ionic/issues/7527
  301. // wait for 4 frames
  302. setTimeout(function () {
  303. var value = _this._value;
  304. if (isPresent(value) && value !== '') {
  305. _this.value = ''; // DOM WRITE
  306. _this.ionInput.emit(ev);
  307. }
  308. }, 16 * 4);
  309. this._shouldBlur = false;
  310. };
  311. /**
  312. * @hidden
  313. * Clears the input field and tells the input to blur since
  314. * the clearInput function doesn't want the input to blur
  315. * then calls the custom cancel function if the user passed one in.
  316. */
  317. Searchbar.prototype.cancelSearchbar = function (ev) {
  318. this.ionCancel.emit(ev);
  319. this.clearInput(ev);
  320. this._shouldBlur = true;
  321. this._isActive = false;
  322. };
  323. Searchbar.prototype.setFocus = function () {
  324. this._renderer.invokeElementMethod(this._searchbarInput.nativeElement, 'focus');
  325. };
  326. Searchbar.decorators = [
  327. { type: Component, args: [{
  328. selector: 'ion-searchbar',
  329. template: '<div class="searchbar-input-container">' +
  330. '<button ion-button mode="md" (click)="cancelSearchbar($event)" (mousedown)="cancelSearchbar($event)" clear color="dark" class="searchbar-md-cancel" type="button">' +
  331. '<ion-icon name="md-arrow-back"></ion-icon>' +
  332. '</button>' +
  333. '<div #searchbarIcon class="searchbar-search-icon"></div>' +
  334. '<input #searchbarInput class="searchbar-input" (input)="inputChanged($event)" (blur)="inputBlurred()" (focus)="inputFocused()" ' +
  335. 'dir="auto" ' +
  336. '[attr.placeholder]="placeholder" ' +
  337. '[attr.type]="type" ' +
  338. '[attr.autocomplete]="_autocomplete" ' +
  339. '[attr.autocorrect]="_autocorrect" ' +
  340. '[attr.spellcheck]="_spellcheck">' +
  341. '<button ion-button clear class="searchbar-clear-icon" [mode]="_mode" (click)="clearInput($event)" (mousedown)="clearInput($event)" type="button"></button>' +
  342. '</div>' +
  343. '<button ion-button #cancelButton mode="ios" [tabindex]="_isActive ? 1 : -1" clear (click)="cancelSearchbar($event)" (mousedown)="cancelSearchbar($event)" class="searchbar-ios-cancel" type="button">{{cancelButtonText}}</button>',
  344. host: {
  345. '[class.searchbar-animated]': '_animated',
  346. '[class.searchbar-has-value]': '_value',
  347. '[class.searchbar-active]': '_isActive',
  348. '[class.searchbar-show-cancel]': '_showCancelButton',
  349. '[class.searchbar-left-aligned]': '_shouldAlignLeft',
  350. '[class.searchbar-has-focus]': '_isFocus'
  351. },
  352. encapsulation: ViewEncapsulation.None
  353. },] },
  354. ];
  355. /** @nocollapse */
  356. Searchbar.ctorParameters = function () { return [
  357. { type: Config, },
  358. { type: Platform, },
  359. { type: ElementRef, },
  360. { type: Renderer, },
  361. { type: NgControl, decorators: [{ type: Optional },] },
  362. ]; };
  363. Searchbar.propDecorators = {
  364. 'cancelButtonText': [{ type: Input },],
  365. 'showCancelButton': [{ type: Input },],
  366. 'debounce': [{ type: Input },],
  367. 'placeholder': [{ type: Input },],
  368. 'autocomplete': [{ type: Input },],
  369. 'autocorrect': [{ type: Input },],
  370. 'spellcheck': [{ type: Input },],
  371. 'type': [{ type: Input },],
  372. 'animated': [{ type: Input },],
  373. 'ionInput': [{ type: Output },],
  374. 'ionCancel': [{ type: Output },],
  375. 'ionClear': [{ type: Output },],
  376. '_searchbarInput': [{ type: ViewChild, args: ['searchbarInput',] },],
  377. '_searchbarIcon': [{ type: ViewChild, args: ['searchbarIcon',] },],
  378. '_cancelButton': [{ type: ViewChild, args: ['cancelButton', { read: ElementRef },] },],
  379. };
  380. return Searchbar;
  381. }(BaseInput));
  382. export { Searchbar };
  383. //# sourceMappingURL=searchbar.js.map