From cf83e3c864a97e7646493311e542c22afb3ee70a Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Sat, 21 Feb 2015 17:34:50 +0530 Subject: [PATCH] [fixes] usability --- frappe/public/js/frappe/form/control.js | 50 +++++++++++++++++++--- frappe/public/js/frappe/upload.js | 13 +++++- frappe/public/js/legacy/clientscriptAPI.js | 7 +-- frappe/public/js/legacy/loaders.js | 18 ++++++-- 4 files changed, 74 insertions(+), 14 deletions(-) diff --git a/frappe/public/js/frappe/form/control.js b/frappe/public/js/frappe/form/control.js index db4610fb4a..40ef4fef8e 100644 --- a/frappe/public/js/frappe/form/control.js +++ b/frappe/public/js/frappe/form/control.js @@ -38,6 +38,10 @@ frappe.ui.form.Control = Class.extend({ this.$wrapper = $("
").appendTo(this.parent); }, + toggle: function(show) { + this.$wrapper.toggleClass("hide-control", !!!show); + }, + // returns "Read", "Write" or "None" // as strings based on permissions get_status: function(explain) { @@ -673,27 +677,60 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlData.extend({ if(!this.dialog) { this.dialog = new frappe.ui.Dialog({ title: __(this.df.label || __("Upload")), + fields: [ + {fieldtype:"HTML", fieldname:"upload_area"}, + {fieldtype:"HTML", fieldname:"or_attach", options: __("Or")}, + {fieldtype:"Select", fieldname:"select", label:__("Select from existing attachments") }, + ] }); } - $(this.dialog.body).empty(); + this.dialog.show(); + + this.dialog.get_field("upload_area").$wrapper.empty(); + + // select from existing attachments + var attachments = this.frm && this.frm.attachments.get_attachments() || []; + var select = this.dialog.get_field("select"); + if(attachments.length) { + attachments = $.map(attachments, function(o) { return o.file_url; }) + select.df.options = [""].concat(attachments); + select.toggle(true); + this.dialog.get_field("or_attach").toggle(true); + select.refresh(); + } else { + this.dialog.get_field("or_attach").toggle(false); + select.toggle(false); + } + select.$input.val(""); this.set_upload_options(); frappe.upload.make(this.upload_options); - this.dialog.show(); }, set_upload_options: function() { var me = this; this.upload_options = { - parent: this.dialog.body, + parent: this.dialog.get_field("upload_area").$wrapper, args: {}, max_width: this.df.max_width, max_height: this.df.max_height, + options: this.df.options, btn: this.dialog.set_primary_action(__("Upload")), + on_no_attach: function() { + // if no attachmemts, + // check if something is selected + var selected = me.dialog.get_field("select").get_value(); + if(selected) { + me.parse_validate_and_set_in_model(selected); + me.dialog.hide(); + } else { + msgprint(__("Please attach a file or set a URL")); + } + }, callback: function(attachment, r) { - me.dialog.hide(); me.on_upload_complete(attachment); + me.dialog.hide(); }, onerror: function() { me.dialog.hide(); @@ -917,10 +954,9 @@ frappe.ui.form.ControlLink = frappe.ui.form.ControlData.extend({ var doctype = this.get_options(); if(!doctype) return; if(this.frm) { - this.frm.new_doc(doctype, this); - + this.frm.new_doc(doctype, this, {"name_field": this.get_value()}); } else { - new_doc(doctype); + new_doc(doctype, {"name_field": this.get_value()}); } }, setup_autocomplete: function() { diff --git a/frappe/public/js/frappe/upload.js b/frappe/public/js/frappe/upload.js index 7f68a75dc9..85fb3474c7 100644 --- a/frappe/public/js/frappe/upload.js +++ b/frappe/public/js/frappe/upload.js @@ -64,7 +64,11 @@ frappe.upload = { }, upload_file: function(fileobj, args, opts) { if(!fileobj && !args.file_url) { - msgprint(__("Please attach a file or set a URL")); + if(opts.on_no_attach) { + opts.on_no_attach(); + } else { + msgprint(__("Please attach a file or set a URL")); + } return; } @@ -100,6 +104,13 @@ frappe.upload = { freader.onload = function() { args.filename = fileobj.name; + if(opts.options && opts.options.toLowerCase()=="image") { + if(!(/\.(gif|jpg|jpeg|tiff|png|svg)$/i).test(args.filename)) { + msgprint(__("Only image extensions (.gif, .jpg, .jpeg, .tiff, .png, .svg) allowed")); + return; + } + } + if((opts.max_width || opts.max_height) && (/\.(gif|jpg|jpeg|tiff|png)$/i).test(args.filename)) { frappe.utils.resize_image(freader, function(_dataurl) { dataurl = _dataurl; diff --git a/frappe/public/js/legacy/clientscriptAPI.js b/frappe/public/js/legacy/clientscriptAPI.js index 48a1c2d81d..1038e15834 100644 --- a/frappe/public/js/legacy/clientscriptAPI.js +++ b/frappe/public/js/legacy/clientscriptAPI.js @@ -279,9 +279,10 @@ _f.Frm.prototype.get_field = function(field) { return cur_frm.fields_dict[field]; }; -_f.Frm.prototype.new_doc = function(doctype, field) { - frappe._from_link = field; frappe._from_link_scrollY = scrollY; - new_doc(doctype); +_f.Frm.prototype.new_doc = function(doctype, field, opts) { + frappe._from_link = field; + frappe._from_link_scrollY = scrollY; + new_doc(doctype, opts); } diff --git a/frappe/public/js/legacy/loaders.js b/frappe/public/js/legacy/loaders.js index 36b6926850..42acaa54ad 100644 --- a/frappe/public/js/legacy/loaders.js +++ b/frappe/public/js/legacy/loaders.js @@ -20,13 +20,25 @@ function loaddoc(doctype, name, onload) { var load_doc = loaddoc; frappe.create_routes = {}; -function new_doc(doctype, in_form) { +function new_doc(doctype, opts) { frappe.model.with_doctype(doctype, function() { if(frappe.create_routes[doctype]) { frappe.set_route(frappe.create_routes[doctype]); } else { - var new_name = frappe.model.make_new_doc_and_get_name(doctype); - frappe.set_route("Form", doctype, new_name); + var new_doc = frappe.model.get_new_doc(doctype); + + // set the name if called from a link field + if(opts && opts.name_field) { + var meta = frappe.get_meta(doctype); + if(meta.autoname && meta.autoname.indexOf("field:")!==-1) { + new_doc[meta.autoname.substr(6)] = opts.name_field; + } else if(meta.title_field) { + new_doc[meta.title_field] = opts.name_field; + } + } + + frappe.set_route("Form", doctype, new_doc.name); + } }); }