// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
// MIT License. See license.txt
/* Form page structure
+ this.parent (either FormContainer or Dialog)
+ this.wrapper
+ this.toolbar
+ this.form_wrapper
+ this.main
+ this.head
+ this.body
+ this.layout
+ this.sidebar
+ this.print_wrapper
+ this.head
+ this.footer
*/
wn.provide('_f');
wn.provide('wn.ui.form');
wn.ui.form.Controller = Class.extend({
init: function(opts) {
$.extend(this, opts);
this.setup && this.setup();
}
});
_f.frms = {};
_f.Frm = function(doctype, parent, in_form) {
this.docname = '';
this.doctype = doctype;
this.display = 0;
this.refresh_if_stale_for = 120;
var me = this;
this.last_view_is_edit = {};
this.opendocs = {};
this.sections = [];
this.grids = [];
this.cscript = new wn.ui.form.Controller({frm:this});
this.pformat = {};
this.fetch_dict = {};
this.parent = parent;
this.tinymce_id_list = [];
this.setup_meta(doctype);
// show in form instead of in dialog, when called using url (router.js)
this.in_form = in_form ? true : false;
// notify on rename
var me = this;
$(document).on('rename', function(event, dt, old_name, new_name) {
if(dt==me.doctype)
me.rename_notify(dt, old_name, new_name)
});
}
_f.Frm.prototype.check_doctype_conflict = function(docname) {
var me = this;
if(this.doctype=='DocType' && docname=='DocType') {
msgprint('Allowing DocType, DocType. Be careful!')
} else if(this.doctype=='DocType') {
if (wn.views.formview[docname] || wn.pages['List/'+docname]) {
msgprint("Cannot open DocType when its instance is open")
throw 'doctype open conflict'
}
} else {
if (wn.views.formview.DocType && wn.views.formview.DocType.frm.opendocs[this.doctype]) {
msgprint("Cannot open instance when its DocType is open")
throw 'doctype open conflict'
}
}
}
_f.Frm.prototype.setup = function() {
var me = this;
this.fields = [];
this.fields_dict = {};
this.state_fieldname = wn.workflow.get_state_fieldname(this.doctype);
// wrapper
this.wrapper = this.parent;
wn.ui.make_app_page({
parent: this.wrapper,
single_column: true
});
this.appframe = this.wrapper.appframe;
this.layout_main = $(this.wrapper)
.find(".layout-main")
.css({"padding-bottom": "0px"})
.get(0);
this.toolbar = new wn.ui.form.Toolbar({
frm: this,
appframe: this.appframe
});
this.frm_head = this.toolbar;
// create area for print format
this.setup_print_layout();
// 2 column layout
this.setup_std_layout();
// client script must be called after "setup" - there are no fields_dict attached to the frm otherwise
this.script_manager = new wn.ui.form.ScriptManager({
frm: this
});
this.script_manager.setup();
this.watch_model_updates();
this.footer = new wn.ui.form.Footer({
frm: this,
parent: this.layout_main
})
this.setup_done = true;
}
_f.Frm.prototype.watch_model_updates = function() {
// watch model updates
var me = this;
// on main doc
wn.model.on(me.doctype, "*", function(fieldname, value, doc) {
// set input
if(doc.name===me.docname) {
me.fields_dict[fieldname]
&& me.fields_dict[fieldname].refresh(fieldname);
me.refresh_dependency();
me.script_manager.trigger(fieldname, doc.doctype, doc.name);
}
})
// on table fields
$.each(wn.model.get("DocField", {fieldtype:"Table", parent: me.doctype}), function(i, df) {
wn.model.on(df.options, "*", function(fieldname, value, doc) {
if(doc.parent===me.docname && doc.parentfield===df.fieldname) {
me.fields_dict[df.fieldname].grid.set_value(fieldname, value, doc);
me.script_manager.trigger(fieldname, doc.doctype, doc.name);
}
})
})
}
_f.Frm.prototype.setup_print_layout = function() {
var me = this;
this.print_wrapper = $('
').appendTo(this.layout_main).get(0);
//appframe.add_ripped_paper_effect(this.print_wrapper);
this.print_body = $(this.print_wrapper).find(".print-format-area").get(0);
}
_f.Frm.prototype.onhide = function() {
if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect();
}
_f.Frm.prototype.setup_std_layout = function() {
this.form_wrapper = $('').appendTo(this.layout_main).get(0);
$parent = $(this.form_wrapper);
this.head = $parent.find(".layout-appframe").get(0);
this.main = this.form_wrapper;
this.body_header = $a(this.main, 'div');
this.body = $a(this.main, 'div');
// only tray
this.meta.section_style='Simple'; // always simple!
// layout
this.layout = new wn.ui.form.Layout({
parent: this.body,
doctype: this.doctype,
frm: this,
});
this.dashboard = new wn.ui.form.Dashboard({
frm: this,
});
// state
this.states = new wn.ui.form.States({
frm: this
});
}
_f.Frm.prototype.setup_print = function() {
this.print_formats = wn.meta.get_print_formats(this.meta.name);
this.print_sel = $("