Button.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const Component = use('util.Component');
  2. export default def(class Button extends Component {
  3. static ID = 'ui.component.Button';
  4. static PROPERTIES = {
  5. label: { value: 'Test Label' },
  6. on_click: { value: null },
  7. enabled: { value: true },
  8. style: { value: 'primary' }
  9. }
  10. static RENDER_MODE = Component.NO_SHADOW;
  11. static CSS = /*css*/`
  12. button {
  13. margin: 0;
  14. color: hsl(220, 25%, 31%);
  15. }
  16. .link-button {
  17. background: none;
  18. border: none;
  19. color: #3b4863;
  20. text-decoration: none;
  21. cursor: pointer;
  22. text-align: center;
  23. display: block;
  24. width: 100%;
  25. }
  26. .link-button:hover {
  27. text-decoration: underline;
  28. }
  29. `;
  30. create_template ({ template }) {
  31. if ( this.get('style') === 'link' ) {
  32. $(template).html(/*html*/`
  33. <button type="submit" class="link-button" style="margin-top:10px;" disabled>${
  34. html_encode(this.get('label'))
  35. }</button>
  36. `);
  37. return;
  38. }
  39. // TODO: Replace hack for 'small' with a better way to configure button classes.
  40. $(template).html(/*html*/`
  41. <button type="submit" class="button ${this.get('style') !== 'small' ? 'button-block' : ''} button-${this.get('style')}" style="margin-top:10px;" disabled>${
  42. html_encode(this.get('label'))
  43. }</button>
  44. `);
  45. }
  46. on_ready ({ listen }) {
  47. if ( this.get('on_click') ) {
  48. const $button = $(this.dom_).find('button');
  49. $button.on('click', async () => {
  50. $button.html(`<svg style="width:20px; margin-top: 5px;" xmlns="http://www.w3.org/2000/svg" height="24" width="24" viewBox="0 0 24 24"><title>circle anim</title><g fill="#fff" class="nc-icon-wrapper"><g class="nc-loop-circle-24-icon-f"><path d="M12 24a12 12 0 1 1 12-12 12.013 12.013 0 0 1-12 12zm0-22a10 10 0 1 0 10 10A10.011 10.011 0 0 0 12 2z" fill="#eee" opacity=".4"></path><path d="M24 12h-2A10.011 10.011 0 0 0 12 2V0a12.013 12.013 0 0 1 12 12z" data-color="color-2"></path></g><style>.nc-loop-circle-24-icon-f{--animation-duration:0.5s;transform-origin:12px 12px;animation:nc-loop-circle-anim var(--animation-duration) infinite linear}@keyframes nc-loop-circle-anim{0%{transform:rotate(0)}100%{transform:rotate(360deg)}}</style></g></svg>`);
  51. const on_click = this.get('on_click');
  52. await on_click();
  53. $button.html(this.get('label'));
  54. });
  55. }
  56. listen('enabled', enabled => {
  57. $(this.dom_).find('button').prop('disabled', ! enabled);
  58. });
  59. }
  60. });