From 019bfd39407e3ad198ac646442805c9f1b1b7468 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Sun, 26 Aug 2018 20:08:40 +0530 Subject: [PATCH] feat: Add helper directives - v-route automatically route to a valid frappe route - v-img-src handle img loading and error handling --- erpnext/public/js/hub/components/ItemCard.vue | 2 +- .../js/hub/components/ItemCardsContainer.vue | 3 +- erpnext/public/js/hub/vue-plugins.js | 44 +++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 erpnext/public/js/hub/vue-plugins.js diff --git a/erpnext/public/js/hub/components/ItemCard.vue b/erpnext/public/js/hub/components/ItemCard.vue index 7975868f8e..f34fddcfd7 100644 --- a/erpnext/public/js/hub/components/ItemCard.vue +++ b/erpnext/public/js/hub/components/ItemCard.vue @@ -15,7 +15,7 @@
- +
diff --git a/erpnext/public/js/hub/components/ItemCardsContainer.vue b/erpnext/public/js/hub/components/ItemCardsContainer.vue index 5af82def82..748c2720f9 100644 --- a/erpnext/public/js/hub/components/ItemCardsContainer.vue +++ b/erpnext/public/js/hub/components/ItemCardsContainer.vue @@ -5,8 +5,7 @@ :message="empty_state_message" :bordered="true" :height="80" - > - + /> frappe.set_route(route)); + }, + unbind(el) { + el.classList.remove('cursor-pointer'); + } +}); + +const handleImage = (el, src) => { + let img = new Image(); + // add loading class + el.src = ''; + el.classList.add('img-loading'); + + img.onload = () => { + // image loaded, remove loading class + el.classList.remove('img-loading'); + // set src + el.src = src; + } + img.onerror = () => { + el.classList.remove('img-loading'); + el.classList.add('no-image'); + el.src = null; + } + img.src = src; +} + +Vue.directive('img-src', { + bind(el, binding) { + handleImage(el, binding.value); + }, + update(el, binding) { + if (binding.value === binding.oldValue) return; + handleImage(el, binding.value); + } +});