|
| 1 | +<template> |
| 2 | + <v-list-item :class="[{ styleAvatar: avatarOn }]" @click.native="navigate(href)"> |
| 3 | + <v-list-item-action v-if="iconOn && !avatarOn"> |
| 4 | + <v-icon |
| 5 | + :style="{color: isActive(href) ? activeColor : iconColor, cursor: href ? 'pointer' : ''}" |
| 6 | + >{{ icon }}</v-icon> |
| 7 | + </v-list-item-action> |
| 8 | + <v-list-item-avatar v-if="iconOn && avatarOn"> |
| 9 | + <v-img :src="avatar" /> |
| 10 | + </v-list-item-avatar> |
| 11 | + <v-list-item-content> |
| 12 | + <v-list-item-title :style="{color: isActive(href) ? activeColor : linkColor}"> |
| 13 | + <span :style="{cursor: href ? 'pointer' : ''}">{{ title }}</span> |
| 14 | + </v-list-item-title> |
| 15 | + </v-list-item-content> |
| 16 | + <v-list-item-action v-if="iconOn && avatarOn"> |
| 17 | + <v-icon |
| 18 | + :style="{color: isActive(href) ? activeColor : iconColor, cursor: href ? 'pointer' : ''}" |
| 19 | + >{{ icon }}</v-icon> |
| 20 | + </v-list-item-action> |
| 21 | + </v-list-item> |
| 22 | +</template> |
| 23 | + |
| 24 | +<script> |
| 25 | +export default { |
| 26 | + props: { |
| 27 | + dark: { |
| 28 | + type: Boolean, |
| 29 | + default() { |
| 30 | + return false; |
| 31 | + } |
| 32 | + }, |
| 33 | + href: { |
| 34 | + type: String, |
| 35 | + required: true |
| 36 | + }, |
| 37 | + title: { |
| 38 | + type: String, |
| 39 | + required: true |
| 40 | + }, |
| 41 | + avatar: { |
| 42 | + type: String, |
| 43 | + default() { |
| 44 | + return ""; |
| 45 | + } |
| 46 | + }, |
| 47 | + icon: { |
| 48 | + type: String, |
| 49 | + default() { |
| 50 | + return ""; |
| 51 | + } |
| 52 | + }, |
| 53 | + iconColor: { |
| 54 | + type: String, |
| 55 | + default() { |
| 56 | + return this.dark ? "#fafafa" : "#78909C"; // white or blue-grey lighten-1 |
| 57 | + } |
| 58 | + }, |
| 59 | + linkColor: { |
| 60 | + type: String, |
| 61 | + default() { |
| 62 | + return this.dark ? "#fafafa" : "#e3b500"; // white or blue-grey lighten-1 |
| 63 | + } |
| 64 | + }, |
| 65 | + activeColor: { |
| 66 | + type: String, |
| 67 | + default() { |
| 68 | + return "#f5c300"; // teal lighten 2 |
| 69 | + } |
| 70 | + } |
| 71 | + }, |
| 72 | + computed: { |
| 73 | + isDark() { |
| 74 | + return this.dark === true; |
| 75 | + }, |
| 76 | + avatarOn() { |
| 77 | + return !!this.avatar; |
| 78 | + }, |
| 79 | + iconOn() { |
| 80 | + return !!this.icon; |
| 81 | + } |
| 82 | + }, |
| 83 | + methods: { |
| 84 | + isActive() { |
| 85 | + return this.route().current(this.href); |
| 86 | + }, |
| 87 | + navigate(href) { |
| 88 | + let self = this; |
| 89 | + /* if valid url */ |
| 90 | + if (self.isURL(href)) { |
| 91 | + // check if link belongs to same domain |
| 92 | + if (self.getHostName(href) == window.location.hostname) { |
| 93 | + this.$inertia.replace(href); |
| 94 | + } else { |
| 95 | + window.open(href); |
| 96 | + } |
| 97 | + } else if (this.routeExists(href)) { |
| 98 | + /* when using vue router path */ |
| 99 | +
|
| 100 | + this.$inertia.visit(this.route(href).url()); |
| 101 | + } else { |
| 102 | + this.$inertia.visit(href); |
| 103 | + } |
| 104 | + }, |
| 105 | + routeExists(name) { |
| 106 | + try { |
| 107 | + route(name); |
| 108 | + return true; |
| 109 | + } catch (Err) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + }, |
| 113 | + getHostName(url) { |
| 114 | + var match = url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/i); |
| 115 | + if ( |
| 116 | + match != null && |
| 117 | + match.length > 2 && |
| 118 | + typeof match[2] === "string" && |
| 119 | + match[2].length > 0 |
| 120 | + ) { |
| 121 | + return match[2]; |
| 122 | + } else { |
| 123 | + return null; |
| 124 | + } |
| 125 | + }, |
| 126 | + isURL(str) { |
| 127 | + var urlRegex = |
| 128 | + "^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$"; |
| 129 | + var url = new RegExp(urlRegex, "i"); |
| 130 | + return str.length < 2083 && url.test(str); |
| 131 | + } |
| 132 | + } |
| 133 | +}; |
| 134 | +</script> |
| 135 | + |
| 136 | +<style lang="scss" scoped> |
| 137 | +.styleAvatar { |
| 138 | + position: relative; |
| 139 | + margin-left: -55px; |
| 140 | +} |
| 141 | +</style> |
0 commit comments