You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LegacySearchResult.vue 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <!--
  2. - @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
  3. -
  4. - @author John Molakvoæ <skjnldsv@protonmail.com>
  5. -
  6. - @license GNU AGPL version 3 or any later version
  7. -
  8. - This program is free software: you can redistribute it and/or modify
  9. - it under the terms of the GNU Affero General Public License as
  10. - published by the Free Software Foundation, either version 3 of the
  11. - License, or (at your option) any later version.
  12. -
  13. - This program is distributed in the hope that it will be useful,
  14. - but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. - GNU Affero General Public License for more details.
  17. -
  18. - You should have received a copy of the GNU Affero General Public License
  19. - along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. -
  21. -->
  22. <template>
  23. <a :href="resourceUrl || '#'"
  24. class="unified-search__result"
  25. :class="{
  26. 'unified-search__result--focused': focused,
  27. }"
  28. @click="reEmitEvent"
  29. @focus="reEmitEvent">
  30. <!-- Icon describing the result -->
  31. <div class="unified-search__result-icon"
  32. :class="{
  33. 'unified-search__result-icon--rounded': rounded,
  34. 'unified-search__result-icon--no-preview': !hasValidThumbnail && !loaded,
  35. 'unified-search__result-icon--with-thumbnail': hasValidThumbnail && loaded,
  36. [icon]: !loaded && !isIconUrl,
  37. }"
  38. :style="{
  39. backgroundImage: isIconUrl ? `url(${icon})` : '',
  40. }">
  41. <img v-if="hasValidThumbnail"
  42. v-show="loaded"
  43. :src="thumbnailUrl"
  44. alt=""
  45. @error="onError"
  46. @load="onLoad">
  47. </div>
  48. <!-- Title and sub-title -->
  49. <span class="unified-search__result-content">
  50. <span class="unified-search__result-line-one" :title="title">
  51. <NcHighlight :text="title" :search="query" />
  52. </span>
  53. <span v-if="subline" class="unified-search__result-line-two" :title="subline">{{ subline }}</span>
  54. </span>
  55. </a>
  56. </template>
  57. <script>
  58. import NcHighlight from '@nextcloud/vue/dist/Components/NcHighlight.js'
  59. export default {
  60. name: 'LegacySearchResult',
  61. components: {
  62. NcHighlight,
  63. },
  64. props: {
  65. thumbnailUrl: {
  66. type: String,
  67. default: null,
  68. },
  69. title: {
  70. type: String,
  71. required: true,
  72. },
  73. subline: {
  74. type: String,
  75. default: null,
  76. },
  77. resourceUrl: {
  78. type: String,
  79. default: null,
  80. },
  81. icon: {
  82. type: String,
  83. default: '',
  84. },
  85. rounded: {
  86. type: Boolean,
  87. default: false,
  88. },
  89. query: {
  90. type: String,
  91. default: '',
  92. },
  93. /**
  94. * Only used for the first result as a visual feedback
  95. * so we can keep the search input focused but pressing
  96. * enter still opens the first result
  97. */
  98. focused: {
  99. type: Boolean,
  100. default: false,
  101. },
  102. },
  103. data() {
  104. return {
  105. hasValidThumbnail: this.thumbnailUrl && this.thumbnailUrl.trim() !== '',
  106. loaded: false,
  107. }
  108. },
  109. computed: {
  110. isIconUrl() {
  111. // If we're facing an absolute url
  112. if (this.icon.startsWith('/')) {
  113. return true
  114. }
  115. // Otherwise, let's check if this is a valid url
  116. try {
  117. // eslint-disable-next-line no-new
  118. new URL(this.icon)
  119. } catch {
  120. return false
  121. }
  122. return true
  123. },
  124. },
  125. watch: {
  126. // Make sure to reset state on change even when vue recycle the component
  127. thumbnailUrl() {
  128. this.hasValidThumbnail = this.thumbnailUrl && this.thumbnailUrl.trim() !== ''
  129. this.loaded = false
  130. },
  131. },
  132. methods: {
  133. reEmitEvent(e) {
  134. this.$emit(e.type, e)
  135. },
  136. /**
  137. * If the image fails to load, fallback to iconClass
  138. */
  139. onError() {
  140. this.hasValidThumbnail = false
  141. },
  142. onLoad() {
  143. this.loaded = true
  144. },
  145. },
  146. }
  147. </script>
  148. <style lang="scss" scoped>
  149. @use "sass:math";
  150. $clickable-area: 44px;
  151. $margin: 10px;
  152. .unified-search__result {
  153. display: flex;
  154. align-items: center;
  155. height: $clickable-area;
  156. padding: $margin;
  157. border: 2px solid transparent;
  158. border-radius: var(--border-radius-large) !important;
  159. &--focused {
  160. background-color: var(--color-background-hover);
  161. }
  162. &:active,
  163. &:hover,
  164. &:focus {
  165. background-color: var(--color-background-hover);
  166. border: 2px solid var(--color-border-maxcontrast);
  167. }
  168. * {
  169. cursor: pointer;
  170. }
  171. &-icon {
  172. overflow: hidden;
  173. width: $clickable-area;
  174. height: $clickable-area;
  175. border-radius: var(--border-radius);
  176. background-repeat: no-repeat;
  177. background-position: center center;
  178. background-size: 32px;
  179. &--rounded {
  180. border-radius: math.div($clickable-area, 2);
  181. }
  182. &--no-preview {
  183. background-size: 32px;
  184. }
  185. &--with-thumbnail {
  186. background-size: cover;
  187. }
  188. &--with-thumbnail:not(&--rounded) {
  189. // compensate for border
  190. max-width: $clickable-area - 2px;
  191. max-height: $clickable-area - 2px;
  192. border: 1px solid var(--color-border);
  193. }
  194. img {
  195. // Make sure to keep ratio
  196. width: 100%;
  197. height: 100%;
  198. object-fit: cover;
  199. object-position: center;
  200. }
  201. }
  202. &-icon,
  203. &-actions {
  204. flex: 0 0 $clickable-area;
  205. }
  206. &-content {
  207. display: flex;
  208. align-items: center;
  209. flex: 1 1 100%;
  210. flex-wrap: wrap;
  211. // Set to minimum and gro from it
  212. min-width: 0;
  213. padding-left: $margin;
  214. }
  215. &-line-one,
  216. &-line-two {
  217. overflow: hidden;
  218. flex: 1 1 100%;
  219. margin: 1px 0;
  220. white-space: nowrap;
  221. text-overflow: ellipsis;
  222. // Use the same color as the `a`
  223. color: inherit;
  224. font-size: inherit;
  225. }
  226. &-line-two {
  227. opacity: .7;
  228. font-size: var(--default-font-size);
  229. }
  230. }
  231. </style>