123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 { Attribute, ChangeDetectionStrategy, Component, ElementRef, Input, Renderer, ViewEncapsulation } from '@angular/core';
  12. import { Config } from '../../config/config';
  13. import { Ion } from '../ion';
  14. import { isTrueProperty } from '../../util/util';
  15. /**
  16. * @name Button
  17. * @module ionic
  18. * @description
  19. * Buttons are simple components in Ionic. They can consist of text and icons
  20. * and be enhanced by a wide range of attributes.
  21. *
  22. * @usage
  23. *
  24. * ```html
  25. *
  26. * <!-- Colors -->
  27. * <button ion-button>Default</button>
  28. *
  29. * <button ion-button color="secondary">Secondary</button>
  30. *
  31. * <button ion-button color="danger">Danger</button>
  32. *
  33. * <button ion-button color="light">Light</button>
  34. *
  35. * <button ion-button color="dark">Dark</button>
  36. *
  37. * <!-- Shapes -->
  38. * <button ion-button full>Full Button</button>
  39. *
  40. * <button ion-button block>Block Button</button>
  41. *
  42. * <button ion-button round>Round Button</button>
  43. *
  44. * <!-- Outline -->
  45. * <button ion-button full outline>Outline + Full</button>
  46. *
  47. * <button ion-button block outline>Outline + Block</button>
  48. *
  49. * <button ion-button round outline>Outline + Round</button>
  50. *
  51. * <!-- Icons -->
  52. * <button ion-button icon-start>
  53. * <ion-icon name="star"></ion-icon>
  54. * Left Icon
  55. * </button>
  56. *
  57. * <button ion-button icon-end>
  58. * Right Icon
  59. * <ion-icon name="star"></ion-icon>
  60. * </button>
  61. *
  62. * <button ion-button icon-only>
  63. * <ion-icon name="star"></ion-icon>
  64. * </button>
  65. *
  66. * <!-- Sizes -->
  67. * <button ion-button large>Large</button>
  68. *
  69. * <button ion-button>Default</button>
  70. *
  71. * <button ion-button small>Small</button>
  72. * ```
  73. *
  74. * @advanced
  75. *
  76. * ```html
  77. *
  78. * <!-- Bind the color and outline inputs to an expression -->
  79. * <button ion-button [color]="isDanger ? 'danger' : 'primary'" [outline]="isOutline">
  80. * Danger (Solid)
  81. * </button>
  82. *
  83. * <!-- Bind the color and round inputs to an expression -->
  84. * <button ion-button [color]="myColor" [round]="isRound">
  85. * Secondary (Round)
  86. * </button>
  87. *
  88. * <!-- Bind the color and clear inputs to an expression -->
  89. * <button ion-button [color]="isSecondary ? 'secondary' : 'primary'" [clear]="isClear">
  90. * Primary (Clear)
  91. * </button>
  92. *
  93. * <!-- Bind the color, outline and round inputs to an expression -->
  94. * <button ion-button [color]="myColor2" [outline]="isOutline" [round]="isRound">
  95. * Dark (Solid + Round)
  96. * </button>
  97. *
  98. * <!-- Bind the click event to a method -->
  99. * <button ion-button (click)="logEvent($event)">
  100. * Click me!
  101. * </button>
  102. * ```
  103. *
  104. * ```ts
  105. * @Component({
  106. * templateUrl: 'main.html'
  107. * })
  108. * class E2EPage {
  109. * isDanger: boolean = true;
  110. * isSecondary: boolean = false;
  111. * isRound: boolean = true;
  112. * isOutline: boolean = false;
  113. * isClear: boolean = true;
  114. * myColor: string = 'secondary';
  115. * myColor2: string = 'dark';
  116. *
  117. * logEvent(event) {
  118. * console.log(event)
  119. * }
  120. * }
  121. *
  122. * ```
  123. *
  124. * @demo /docs/demos/src/button/
  125. * @see {@link /docs/components#buttons Button Component Docs}
  126. * @see {@link /docs/components#fabs FabButton Docs}
  127. * @see {@link ../../fab/FabButton FabButton API Docs}
  128. * @see {@link ../../fab/FabContainer FabContainer API Docs}
  129. */
  130. var Button = (function (_super) {
  131. __extends(Button, _super);
  132. function Button(ionButton, config, elementRef, renderer) {
  133. var _this = _super.call(this, config, elementRef, renderer) || this;
  134. /** @hidden */
  135. _this._role = 'button'; // bar-button
  136. /** @hidden */
  137. _this._style = 'default'; // outline/clear/solid
  138. _this._mode = config.get('mode');
  139. if (config.get('hoverCSS') === false) {
  140. _this.setElementClass('disable-hover', true);
  141. }
  142. if (ionButton.trim().length > 0) {
  143. _this.setRole(ionButton);
  144. }
  145. return _this;
  146. }
  147. Object.defineProperty(Button.prototype, "large", {
  148. /**
  149. * @input {boolean} If true, activates the large button size.
  150. */
  151. set: function (val) {
  152. this._attr('_size', 'large', val);
  153. },
  154. enumerable: true,
  155. configurable: true
  156. });
  157. Object.defineProperty(Button.prototype, "small", {
  158. /**
  159. * @input {boolean} If true, activates the small button size.
  160. */
  161. set: function (val) {
  162. this._attr('_size', 'small', val);
  163. },
  164. enumerable: true,
  165. configurable: true
  166. });
  167. Object.defineProperty(Button.prototype, "default", {
  168. /**
  169. * @input {boolean} If true, activates the default button size. Normally the default, useful for buttons in an item.
  170. */
  171. set: function (val) {
  172. this._attr('_size', 'default', val);
  173. },
  174. enumerable: true,
  175. configurable: true
  176. });
  177. Object.defineProperty(Button.prototype, "outline", {
  178. /**
  179. * @input {boolean} If true, activates a transparent button style with a border.
  180. */
  181. set: function (val) {
  182. this._attr('_style', 'outline', val);
  183. },
  184. enumerable: true,
  185. configurable: true
  186. });
  187. Object.defineProperty(Button.prototype, "clear", {
  188. /**
  189. * @input {boolean} If true, activates a transparent button style without a border.
  190. */
  191. set: function (val) {
  192. this._attr('_style', 'clear', val);
  193. },
  194. enumerable: true,
  195. configurable: true
  196. });
  197. Object.defineProperty(Button.prototype, "solid", {
  198. /**
  199. * @input {boolean} If true, activates a solid button style. Normally the default, useful for buttons in a toolbar.
  200. */
  201. set: function (val) {
  202. this._attr('_style', 'solid', val);
  203. },
  204. enumerable: true,
  205. configurable: true
  206. });
  207. Object.defineProperty(Button.prototype, "round", {
  208. /**
  209. * @input {boolean} If true, activates a button with rounded corners.
  210. */
  211. set: function (val) {
  212. this._attr('_shape', 'round', val);
  213. },
  214. enumerable: true,
  215. configurable: true
  216. });
  217. Object.defineProperty(Button.prototype, "block", {
  218. /**
  219. * @input {boolean} If true, activates a button style that fills the available width.
  220. */
  221. set: function (val) {
  222. this._attr('_display', 'block', val);
  223. },
  224. enumerable: true,
  225. configurable: true
  226. });
  227. Object.defineProperty(Button.prototype, "full", {
  228. /**
  229. * @input {boolean} If true, activates a button style that fills the available width without
  230. * a left and right border.
  231. */
  232. set: function (val) {
  233. this._attr('_display', 'full', val);
  234. },
  235. enumerable: true,
  236. configurable: true
  237. });
  238. Object.defineProperty(Button.prototype, "strong", {
  239. /**
  240. * @input {boolean} If true, activates a button with a heavier font weight.
  241. */
  242. set: function (val) {
  243. this._attr('_decorator', 'strong', val);
  244. },
  245. enumerable: true,
  246. configurable: true
  247. });
  248. Object.defineProperty(Button.prototype, "mode", {
  249. /**
  250. * @input {string} The mode determines which platform styles to use.
  251. * Possible values are: `"ios"`, `"md"`, or `"wp"`.
  252. * For more information, see [Platform Styles](/docs/theming/platform-specific-styles).
  253. */
  254. set: function (val) {
  255. this._assignCss(false);
  256. this._mode = val;
  257. this._assignCss(true);
  258. },
  259. enumerable: true,
  260. configurable: true
  261. });
  262. /** @hidden */
  263. Button.prototype._attr = function (type, attrName, attrValue) {
  264. if (type === '_style') {
  265. this._updateColor(this._color, false);
  266. }
  267. this._setClass(this[type], false);
  268. if (isTrueProperty(attrValue)) {
  269. this[type] = attrName;
  270. this._setClass(attrName, true);
  271. }
  272. else {
  273. // Special handling for '_style' which defaults to 'default'.
  274. this[type] = (type === '_style' ? 'default' : null);
  275. this._setClass(this[type], true);
  276. }
  277. if (type === '_style') {
  278. this._updateColor(this._color, true);
  279. }
  280. };
  281. Object.defineProperty(Button.prototype, "color", {
  282. /**
  283. * @input {string} The color to use from your Sass `$colors` map.
  284. * Default options are: `"primary"`, `"secondary"`, `"danger"`, `"light"`, and `"dark"`.
  285. * For more information, see [Theming your App](/docs/theming/theming-your-app).
  286. */
  287. set: function (val) {
  288. this._updateColor(this._color, false);
  289. this._updateColor(val, true);
  290. this._color = val;
  291. },
  292. enumerable: true,
  293. configurable: true
  294. });
  295. /** @hidden */
  296. Button.prototype.ngAfterContentInit = function () {
  297. this._init = true;
  298. this._assignCss(true);
  299. };
  300. /**
  301. * @hidden
  302. */
  303. Button.prototype.setRole = function (val) {
  304. this._assignCss(false);
  305. this._role = val;
  306. this._assignCss(true);
  307. };
  308. /**
  309. * @hidden
  310. */
  311. Button.prototype._assignCss = function (assignCssClass) {
  312. var role = this._role;
  313. if (role) {
  314. this.setElementClass(role, assignCssClass); // button
  315. this.setElementClass(role + "-" + this._mode, assignCssClass); // button
  316. this._setClass(this._style, assignCssClass); // button-clear
  317. this._setClass(this._shape, assignCssClass); // button-round
  318. this._setClass(this._display, assignCssClass); // button-full
  319. this._setClass(this._size, assignCssClass); // button-small
  320. this._setClass(this._decorator, assignCssClass); // button-strong
  321. this._updateColor(this._color, assignCssClass); // button-secondary, bar-button-secondary
  322. }
  323. };
  324. /**
  325. * @hidden
  326. */
  327. Button.prototype._setClass = function (type, assignCssClass) {
  328. if (type && this._init) {
  329. type = type.toLocaleLowerCase();
  330. this.setElementClass(this._role + "-" + type, assignCssClass);
  331. this.setElementClass(this._role + "-" + type + "-" + this._mode, assignCssClass);
  332. }
  333. };
  334. /**
  335. * @hidden
  336. */
  337. Button.prototype._updateColor = function (color, isAdd) {
  338. if (color && this._init) {
  339. // The class should begin with the button role
  340. // button, bar-button
  341. var className = this._role;
  342. // If the role is not a bar-button, don't apply the solid style
  343. var style = this._style;
  344. style = (this._role !== 'bar-button' && style === 'solid' ? 'default' : style);
  345. className += (style !== null && style !== '' && style !== 'default' ? '-' + style.toLowerCase() : '');
  346. if (color !== null && color !== '') {
  347. this.setElementClass(className + "-" + this._mode + "-" + color, isAdd);
  348. }
  349. }
  350. };
  351. Button.decorators = [
  352. { type: Component, args: [{
  353. selector: '[ion-button]',
  354. template: '<span class="button-inner">' +
  355. '<ng-content></ng-content>' +
  356. '</span>' +
  357. '<div class="button-effect"></div>',
  358. changeDetection: ChangeDetectionStrategy.OnPush,
  359. encapsulation: ViewEncapsulation.None,
  360. },] },
  361. ];
  362. /** @nocollapse */
  363. Button.ctorParameters = function () { return [
  364. { type: undefined, decorators: [{ type: Attribute, args: ['ion-button',] },] },
  365. { type: Config, },
  366. { type: ElementRef, },
  367. { type: Renderer, },
  368. ]; };
  369. Button.propDecorators = {
  370. 'large': [{ type: Input },],
  371. 'small': [{ type: Input },],
  372. 'default': [{ type: Input },],
  373. 'outline': [{ type: Input },],
  374. 'clear': [{ type: Input },],
  375. 'solid': [{ type: Input },],
  376. 'round': [{ type: Input },],
  377. 'block': [{ type: Input },],
  378. 'full': [{ type: Input },],
  379. 'strong': [{ type: Input },],
  380. 'mode': [{ type: Input },],
  381. 'color': [{ type: Input },],
  382. };
  383. return Button;
  384. }(Ion));
  385. export { Button };
  386. //# sourceMappingURL=button.js.map