From cb7f485761d19cee43bf42825d82c5e96c44e204 Mon Sep 17 00:00:00 2001 From: mbauskar Date: Wed, 22 Jun 2016 19:55:25 +0530 Subject: [PATCH] [enhancement] fetched all document images in single frappe call for image view navigation --- frappe/public/js/frappe/list/imageview.js | 115 +++++++++++++++------- 1 file changed, 78 insertions(+), 37 deletions(-) diff --git a/frappe/public/js/frappe/list/imageview.js b/frappe/public/js/frappe/list/imageview.js index a5e1b77246..342289df45 100644 --- a/frappe/public/js/frappe/list/imageview.js +++ b/frappe/public/js/frappe/list/imageview.js @@ -1,5 +1,7 @@ frappe.views.ImageView = Class.extend({ init: function(opts){ + this.docnames = [] + this.doc_images = {} this.doctype = opts.doctype; this.docname = opts.docname; this.container = opts.container; @@ -10,44 +12,83 @@ frappe.views.ImageView = Class.extend({ get_images: function(doctype, docname){ /* get the list of all the Images associated with doc */ var me = this; + if(Object.keys(this.doc_images).length == 0){ + frappe.call({ + method: "frappe.client.get_list", + args: { + doctype: "File", + order_by: "attached_to_name", + fields: [ + "'image/*' as type", "ifnull(thumbnail_url, file_url) as thumbnail", + "concat(attached_to_name, ' - ', file_name) as title", "file_url as href", + "attached_to_name as name" + ], + filters: [ + ["File", "attached_to_doctype", "=", this.doctype], + ["File", "attached_to_name", "in", this.get_docname_list()], + ["File", "is_folder", "!=", 1] + ] + }, + freeze: true, + freeze_message: "Fetching Images ..", + callback: function(r){ + if(!r.message){ + msgprint("No Images found") + } else{ + // filter image files from other + images = r.message.filter(function(image){ + return frappe.utils.is_image_file(image.title); + }); - frappe.call({ - method: "frappe.client.get_list", - args: { - doctype: "File", - fields: [ - "file_name as title", "file_url as href", - "'image/*' as type", "ifnull(thumbnail_url, file_url) as thumbnail" - ], - filters: [ - ["File", "attached_to_doctype", "=", this.doctype], - ["File", "attached_to_name", "=", this.docname], - ["File", "is_folder", "!=", 1] - ] - }, - freeze: true, - freeze_message: "Fetching Images ..", - callback: function(r){ - if(!r.message){ - msgprint("No Images found") - } else{ - // filter image files from other - images = r.message.filter(function(image){ - return frappe.utils.is_image_file(image.title); - }); - - if(images){ - me.render(images); + if(images){ + me.prepare_images(images); + me.render(); + } } } + }); + } else { + this.render() + } + }, + + get_docname_list: function(){ + var names = [] + $.each(cur_list.data, function(idx, doc){ + names.push(doc.name); + }); + return names + }, + + prepare_images: function(images){ + var me = this; + this.doc_images = {} + + $.each(images, function(idx, image){ + name = image.name; + delete image.name; + + _images = me.doc_images[name] || []; + _images.push(image) + + me.doc_images[name] = _images; + delete _images; + }); + + this.docnames = $.map(cur_list.data, function(doc, idx){ + if(inList(Object.keys(me.doc_images), doc.name)){ + return doc.name; } }); }, - render: function(image_list){ - this.gallery = null; - this.gallery = blueimp.Gallery(image_list, this.get_options()); - this.setup_navigation(); + render: function(){ + if(this.docname in this.doc_images){ + this.gallery = blueimp.Gallery(this.doc_images[this.docname], this.get_options()); + this.setup_navigation(); + } else { + msgprint("No Images found for "+ this.doctype +" : "+ this.docname); + } }, setup_navigation: function(){ @@ -71,10 +112,10 @@ frappe.views.ImageView = Class.extend({ navigate: function(args){ var index = 0; var me = this; - var last_idx = cur_list.data.length - 1; + var last_idx = this.docnames.length - 1; - $.each(cur_list.data, function(idx, doc){ - if(doc.name == me.docname){ + $.each(this.docnames, function(idx, name){ + if(name == me.docname){ if(idx == last_idx && args.offset == 1){ index = 0 } else if(idx == 0 && args.offset == -1) { @@ -82,14 +123,14 @@ frappe.views.ImageView = Class.extend({ } else { index = idx + args.offset } - me.docname = cur_list.data[index].name; + me.docname = me.docnames[index]; return false; } }); this.gallery.close(); window.setTimeout(function(){ - me.get_images(this.doctype, this.docname) - }, 500); + me.get_images(me.doctype, me.docname) + }, 300); }, get_options: function(){