User.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {f, dEvent} from "../main.js";
  2. export default class User extends HTMLElement {
  3. constructor() {
  4. super();
  5. this.users = [];
  6. this.data = {
  7. name: "",
  8. login: "",
  9. status: "",
  10. group: "",
  11. id: ""
  12. }
  13. this.user = null;
  14. this.bindEvents();
  15. }
  16. connectedCallback() {
  17. this.id = this.dataset.id;
  18. this.name = this.dataset.name;
  19. this.status = this.dataset.status;
  20. this.group = this.dataset.group;
  21. this.render(this.getTemplateUser());
  22. this.attachModel();
  23. }
  24. render(template) {
  25. this.innerHTML = template;
  26. }
  27. bindEvents() {
  28. console.log("BIND EVENT - " + this.constructor.name)
  29. document.addEventListener('user-login', (e) => {
  30. this.user = e.detail;
  31. this.render(this.getTemplateUser());
  32. });
  33. document.addEventListener('user-out', () => {
  34. this.render(null);
  35. });
  36. }
  37. attachModel() {
  38. this.querySelector('button')
  39. .addEventListener('click', e => this.clickButton(e));
  40. }
  41. getTemplateUser() {
  42. return `
  43. <div class="basic-card basic-card-aqua">
  44. <div class="card-content">
  45. <span class="card-title"><h3>ID:${this.id}</h3></span>
  46. <p class="card-text">
  47. Name: ${this.name}, Status: ${this.status}, Group: ${this.group}
  48. <div class="buttons">
  49. <button data-click="see" data-id="${this.id}">Просмотр</button>
  50. <button style="background: firebrick; color: white">Уволить</button>
  51. </div>
  52. </p>
  53. </div>
  54. </div>
  55. `;
  56. }
  57. clickButton(e) {
  58. this[e.target.dataset.click](e);
  59. }
  60. see(e) {
  61. console.log(e.target.dataset.id);
  62. }
  63. }