Browse Source

[Feature] Enable overriding Quick Entry in custom app (#3519)

* [Feature] Override Quick Entry Class in custom app

* [minor] remove var from if block

* [minor] Rename QuickEntryController to QuickEntryForm

* [fixes] indentation & syntax fixes

* [fix] indentation fix
version-14
Suyash Phadtare 8 years ago
committed by Rushabh Mehta
parent
commit
17a43a4748
3 changed files with 161 additions and 98 deletions
  1. +8
    -1
      frappe/public/js/frappe/form/control.js
  2. +145
    -96
      frappe/public/js/frappe/form/quick_entry.js
  3. +8
    -1
      frappe/public/js/frappe/model/create_new.js

+ 8
- 1
frappe/public/js/frappe/form/control.js View File

@@ -1289,7 +1289,14 @@ frappe.ui.form.ControlLink = frappe.ui.form.ControlData.extend({
frappe._from_link = this;
frappe._from_link_scrollY = $(document).scrollTop();

frappe.ui.form.quick_entry(doctype, function(doc) {
var trimmed_doctype = doctype.replace(/ /g, '');
var controller_name = "QuickEntryForm";

if(frappe.ui.form[trimmed_doctype + "QuickEntryForm"]){
controller_name = trimmed_doctype + "QuickEntryForm";
}

new frappe.ui.form[controller_name](doctype, function(doc) {
if(me.frm) {
me.parse_validate_and_set_in_model(doc.name);
} else {


+ 145
- 96
frappe/public/js/frappe/form/quick_entry.js View File

@@ -1,129 +1,178 @@
frappe.provide('frappe.ui.form');

frappe.ui.form.quick_entry = function(doctype, success) {
frappe.model.with_doctype(doctype, function() {
var mandatory = $.map(frappe.get_meta(doctype).fields,
function(d) { return (d.reqd || d.bold && !d.read_only) ? d : null });
var meta = frappe.get_meta(doctype);

var doc = frappe.model.get_new_doc(doctype, null, null, true);
frappe.ui.form.QuickEntryForm = Class.extend({
init: function(doctype, success_function){
this.doctype = doctype;
this.success_function = success_function;
this.setup();
},

setup: function(){
var me = this;
frappe.model.with_doctype(this.doctype, function() {
me.set_meta_and_mandatory_fields();
var validate_flag = me.validate_quick_entry();
if(!validate_flag){
me.render_dialog();
}
});
},

set_meta_and_mandatory_fields: function(){
this.mandatory = $.map(frappe.get_meta(this.doctype).fields,
function(d) { return (d.reqd || d.bold && !d.read_only) ? d : null; });
this.meta = frappe.get_meta(this.doctype);
this.doc = frappe.model.get_new_doc(this.doctype, null, null, true);
},

validate_quick_entry: function(){
if(this.meta.quick_entry != 1) {
frappe.set_route('Form', this.doctype, this.doc.name);
return true;
}
var mandatory_flag = this.validate_mandatory_length();
var child_table_flag = this.validate_for_child_table();

if(meta.quick_entry != 1) {
frappe.set_route('Form', doctype, doc.name);
return;
if (mandatory_flag || child_table_flag){
return true;
}
this.validate_for_prompt_autoname();
},

if(mandatory.length > 7) {
validate_mandatory_length: function(){
if(this.mandatory.length > 7) {
// too many fields, show form
frappe.set_route('Form', doctype, doc.name);
return;
frappe.set_route('Form', this.doctype, this.doc.name);
return true;
}
},

if($.map(mandatory, function(d) { return d.fieldtype==='Table' ? d : null }).length) {
validate_for_child_table: function(){
if($.map(this.mandatory, function(d) { return d.fieldtype==='Table' ? d : null; }).length) {
// has mandatory table, quit!
frappe.set_route('Form', doctype, doc.name);
return;
frappe.set_route('Form', this.doctype, this.doc.name);
return true;
}
},

if(meta.autoname && meta.autoname.toLowerCase()==='prompt') {
mandatory = [{fieldname:'__name', label:__('{0} Name', [meta.name]),
reqd: 1, fieldtype:'Data'}].concat(mandatory);
validate_for_prompt_autoname: function(){
if(this.meta.autoname && this.meta.autoname.toLowerCase()==='prompt') {
this.mandatory = [{fieldname:'__name', label:__('{0} Name', [this.meta.name]),
reqd: 1, fieldtype:'Data'}].concat(this.mandatory);
}
},

var dialog = new frappe.ui.Dialog({
title: __("New {0}", [__(doctype)]),
fields: mandatory,
render_dialog: function(){
var me = this;
this.dialog = new frappe.ui.Dialog({
title: __("New {0}", [__(this.doctype)]),
fields: this.mandatory,
});
this.dialog.doc = this.doc;
// refresh dependencies etc
this.dialog.refresh();

var update_doc = function() {
var data = dialog.get_values(true);
$.each(data, function(key, value) {
if(key==='__name') {
dialog.doc.name = value;
} else {
if(!is_null(value)) {
dialog.doc[key] = value;
}
this.register_primary_action();
this.render_edit_in_full_page_link();
// ctrl+enter to save
this.dialog.wrapper.keydown(function(e) {
if((e.ctrlKey || e.metaKey) && e.which==13) {
if(!frappe.request.ajax_count) {
// not already working -- double entry
me.dialog.get_primary_btn().trigger("click");
e.preventDefault();
return false;
}
});
return dialog.doc;
}

var open_doc = function() {
dialog.hide();
update_doc();
frappe.set_route('Form', doctype, doc.name);
}

dialog.doc = doc;
}
});

// refresh dependencies etc
dialog.refresh();
this.dialog.show();
this.set_defaults();
},

dialog.set_primary_action(__('Save'), function() {
if(dialog.working) return;
var data = dialog.get_values();
register_primary_action: function(){
var me = this;
this.dialog.set_primary_action(__('Save'), function() {
if(me.dialog.working) return;
var data = me.dialog.get_values();

if(data) {
dialog.working = true;
var values = update_doc();
frappe.call({
method: "frappe.client.insert",
args: {
doc: values
},
callback: function(r) {
dialog.hide();
// delete the old doc
frappe.model.clear_doc(dialog.doc.doctype, dialog.doc.name);
var doc = r.message;
if(success) {
success(doc);
}
frappe.ui.form.update_calling_link(doc.name);
},
error: function() {
open_doc();
},
always: function() {
dialog.working = false;
},
freeze: true
});
me.dialog.working = true;
var values = me.update_doc();
me.insert_document(values);
}
});
},

insert_document: function(values){
var me = this;
frappe.call({
method: "frappe.client.insert",
args: {
doc: values
},
callback: function(r) {
me.dialog.hide();
// delete the old doc
frappe.model.clear_doc(me.dialog.doc.doctype, me.dialog.doc.name);
var doc = r.message;
if(me.success_function) {
me.success_function(doc);
}
frappe.ui.form.update_calling_link(doc.name);
},
error: function() {
me.open_doc();
},
always: function() {
me.dialog.working = false;
},
freeze: true
});
},

update_doc: function(){
var me = this;
var data = this.dialog.get_values(true);
$.each(data, function(key, value) {
if(key==='__name') {
me.dialog.doc.name = value;
} else {
if(!is_null(value)) {
me.dialog.doc[key] = value;
}
}
});
return this.dialog.doc;
},

open_doc: function(){
this.dialog.hide();
this.update_doc();
frappe.set_route('Form', this.doctype, this.doc.name);
},

render_edit_in_full_page_link: function(){
var me = this;
var $link = $('<div class="text-muted small" style="padding-left: 10px; padding-top: 15px;">' +
__("Ctrl+enter to save") + ' | <a class="edit-full">' + __("Edit in full page") + '</a></div>').appendTo(dialog.body);
__("Ctrl+enter to save") + ' | <a class="edit-full">' + __("Edit in full page") + '</a></div>').appendTo(this.dialog.body);

$link.find('.edit-full').on('click', function() {
// edit in form
open_doc();
});

// ctrl+enter to save
dialog.wrapper.keydown(function(e) {
if((e.ctrlKey || e.metaKey) && e.which==13) {
if(!frappe.request.ajax_count) {
// not already working -- double entry
dialog.get_primary_btn().trigger("click");
e.preventDefault();
return false;
}
}
me.open_doc();
});
},

dialog.show();

set_defaults: function(){
var me = this;
// set defaults
$.each(dialog.fields_dict, function(fieldname, field) {
field.doctype = doc.doctype;
field.docname = doc.name;
$.each(this.dialog.fields_dict, function(fieldname, field) {
field.doctype = me.doc.doctype;
field.docname = me.doc.name;

if(!is_null(doc[fieldname])) {
field.set_input(doc[fieldname]);
if(!is_null(me.doc[fieldname])) {
field.set_input(me.doc[fieldname]);
}
});

});
}
}
});

+ 8
- 1
frappe/public/js/frappe/model/create_new.js View File

@@ -318,7 +318,14 @@ frappe.new_doc = function (doctype, opts) {
if(frappe.create_routes[doctype]) {
frappe.set_route(frappe.create_routes[doctype]);
} else {
frappe.ui.form.quick_entry(doctype, function(doc) {
var trimmed_doctype = doctype.replace(/ /g, '');
var controller_name = "QuickEntryForm";

if(frappe.ui.form[trimmed_doctype + "QuickEntryForm"]){
controller_name = trimmed_doctype + "QuickEntryForm";
}

new frappe.ui.form[controller_name](doctype, function(doc) {
//frappe.set_route('List', doctype);
var title = doc.name;
var title_field = frappe.get_meta(doc.doctype).title_field;


Loading…
Cancel
Save