template.pupper 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import ImportedComponent(from="./ImportedComponent.pupper")
  2. template
  3. .container-fluid
  4. //- Stylesheets
  5. link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/bootswatch@4.5.2/dist/litera/bootstrap.min.css", integrity="sha384-enpDwFISL6M3ZGZ50Tjo8m65q06uLVnyvkFO3rsoW0UC15ATBFz3QEhr3hmxpYsn", crossorigin="anonymous")
  6. .text-center
  7. .d-flex.p-3.mx-auto.flex-column
  8. header.masthead.mb-auto
  9. .inner
  10. h3.masthead-brand=page.title
  11. //- Main contents
  12. main.inner.cover.my-3(role="main")
  13. h1.cover-heading=page.description
  14. p.lead=page.lead
  15. hr.my-5
  16. .row.mt-5.justify-content-around.align-items-center
  17. if puppies === undefined || puppies.length === 0
  18. ="Oh noe! No puppies to show :("
  19. else
  20. //- Render the puppies and share the onClickPuppy method with it
  21. each index, puppy in puppies
  22. .col-5.mb-5
  23. .puppy.card.px-0.text-dark(:data-pop="index", :data-id="puppy.id")
  24. .img-responsive
  25. unless puppy.shibe
  26. .cover
  27. a(href="#", @click="replacePuppy(puppy, $event)")|Load a new pupper
  28. img.card-img-top(:src="puppy.thumbnail", crossorigin="auto")
  29. .card-header
  30. h5.card-title=puppy.title
  31. small.text-muted="Served by pupper.js"
  32. .card-body
  33. !=puppy.description
  34. //- Displays a custom message if it's a shibe
  35. if puppy.shibe === true
  36. p.text-warning="shibbe!!!"
  37. //- Displays a puppy properties
  38. if puppy.properties
  39. .row.justify-content-center.mt-3
  40. each property in puppy.properties
  41. .col
  42. span.badge.badge-info.w-100=property
  43. div
  44. h3|Testing slots and components
  45. slot(name="slot")
  46. data
  47. page = {
  48. title: "pupper.js is awesome!",
  49. description: "I use pupper.js because I love puppies!",
  50. lead: "Also we love puppers, shiberinos and other doggos too! 🐶"
  51. }
  52. puppies = [
  53. {
  54. id: 1,
  55. title: "A cutie pup",
  56. description: "Look at this cutie",
  57. thumbnail: "https://placedog.net/800",
  58. properties: ["beautiful", "doge"]
  59. },
  60. {
  61. id: 2,
  62. title: "Another cute pup",
  63. description: "Isn't it a cute doggo?!",
  64. thumbnail: "https://placedog.net/400",
  65. properties: ["wow", "much woof"]
  66. }
  67. ]
  68. style(lang="scss", scoped)
  69. .row {
  70. .puppy {
  71. box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.3);
  72. .img-responsive {
  73. position: relative;
  74. .cover {
  75. display: none;
  76. position: absolute;
  77. top: 0;
  78. left: 0;
  79. width: 100%;
  80. height: 100%;
  81. background-color: rgba(0, 0, 0, 0.3);
  82. a {
  83. color: #fff;
  84. position: absolute;
  85. top: 50%;
  86. left: 50%;
  87. transform: translate(-50%, -50%);
  88. }
  89. }
  90. &:hover .cover {
  91. display: block;
  92. }
  93. }
  94. }
  95. }
  96. implementation
  97. //- Declaring methods
  98. #replacePuppy(puppy, e)
  99. e.preventDefault();
  100. if (!puppy.thumbnail.includes("?random"))
  101. puppy.thumbnail += "?random";
  102. const url = new URL(puppy.thumbnail);
  103. url.searchParams.set("u", +new Date());
  104. puppy.thumbnail = url.toString();
  105. //- Listening to pupper.js events.
  106. when#created
  107. console.log("The component was created.");
  108. when#mounted
  109. console.log("The component was mounted.");
  110. this.$nextTick(() => {
  111. this.puppies.push({
  112. id: 3,
  113. title: "Wow, a shibe!",
  114. description: "Cute shiberino!!!",
  115. thumbnail: "https://media.istockphoto.com/photos/happy-shiba-inu-dog-on-yellow-redhaired-japanese-dog-smile-portrait-picture-id1197121742?k=20&m=1197121742&s=170667a&w=0&h=SDkUmO-JcBKWXl7qK2GifsYzVH19D7e6DAjNpAGJP2M=",
  116. shibe: true
  117. });
  118. ImportedComponent.mount(this.$slots.slot);
  119. });