// Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com) // // MIT License (MIT) // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // /* Form page structure + this.parent (either FormContainer or Dialog) + this.wrapper + this.content + this.saved_wrapper + wn.PageLayout (this.page_layout) + this.wrapper + this.wtab (table) + this.main + this.head + this.body + this.layout + this.footer + this.sidebar + this.print_wrapper + this.head */ wn.provide('_f'); _f.frms = {}; _f.Frm = function(doctype, parent, in_form) { this.docname = ''; this.doctype = doctype; this.display = 0; var me = this; this.is_editable = {}; this.opendocs = {}; this.sections = []; this.grids = []; this.cscript = {}; 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).bind('rename', function(event, dt, old_name, new_name) { //console.log(arguments) 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 = {}; // wrapper this.wrapper = this.parent; // create area for print fomrat this.setup_print_layout(); // thank you goes here (in case of Guest, don't refresh, just say thank you!) this.saved_wrapper = $a(this.wrapper, 'div'); // 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.setup_client_script(); this.setup_done = true; } // ====================================================================================== _f.Frm.prototype.setup_print_layout = function() { this.print_wrapper = $a(this.wrapper, 'div'); this.print_head = $a(this.print_wrapper, 'div'); this.print_body = $a(this.print_wrapper,'div', 'layout_wrapper', { padding:'23px', minHeight: '800px' }); var t= make_table(this.print_head, 1 ,2, '100%', [], {padding: '6px'}); this.view_btn_wrapper = $a($td(t,0,0) , 'span', 'green_buttons'); this.view_btn = $btn(this.view_btn_wrapper, 'View Details', function() { cur_frm.edit_doc() }, {marginRight:'4px'}, 'green'); this.print_btn = $btn($td(t,0,0), 'Print', function() { cur_frm.print_doc() }); $y($td(t,0,1), {textAlign: 'right'}); this.print_close_btn = $btn($td(t,0,1), 'Close', function() { window.history.back(); }); } _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.page_layout = new wn.PageLayout({ parent: this.wrapper, main_width: (this.meta.in_dialog && !this.in_form) ? '100%' : '75%', sidebar_width: (this.meta.in_dialog && !this.in_form) ? '0%' : '25%' }) // only tray this.meta.section_style='Simple'; // always simple! // layout this.layout = new Layout(this.page_layout.body, '100%'); // sidebar if(this.meta.in_dialog && !this.in_form) { // hide sidebar $(this.page_layout.wrapper).removeClass('layout-wrapper-background'); $(this.page_layout.main).removeClass('layout-main-section'); $(this.page_layout.sidebar_area).toggle(false); } else { // module link this.setup_sidebar(); } // footer this.setup_footer(); // header - no headers for tables and guests if(!(this.meta.istable || user=='Guest' || (this.meta.in_dialog && !this.in_form))) this.frm_head = new _f.FrmHeader(this.page_layout.head, this); // create fields this.setup_fields_std(); } _f.Frm.prototype.setup_print = function() { var l = [] this.default_format = 'Standard'; for(var key in locals['Print Format']) { if(locals['Print Format'][key].doc_type == this.meta.name) { l.push(locals['Print Format'][key].name); } } // if default print format is given, use it if(this.meta.default_print_format) this.default_format = this.meta.default_print_format; l.push('Standard'); this.print_sel = $a(null, 'select', '', {width:'160px'}); add_sel_options(this.print_sel, l); this.print_sel.value = this.default_format; } _f.Frm.prototype.print_doc = function() { if(this.doc.docstatus==2) { msgprint("Cannot Print Cancelled Documents."); return; } _p.show_dialog(); // multiple options } // email the form _f.Frm.prototype.email_doc = function() { // make selector if(!_e.dialog) _e.make(); _e.dialog.widgets['To'].value = ''; if (cur_frm.doc && cur_frm.doc.contact_email) { _e.dialog.widgets['To'].value = cur_frm.doc.contact_email; } // set print selector sel = this.print_sel; var c = $td(_e.dialog.rows['Format'].tab,0,1); if(c.cur_sel) { c.removeChild(c.cur_sel); c.cur_sel = null; } c.appendChild(this.print_sel); c.cur_sel = this.print_sel; // hide / show attachments _e.dialog.widgets['Send With Attachments'].checked = 0; if(cur_frm.doc.file_list) { $ds(_e.dialog.rows['Send With Attachments']); } else { $dh(_e.dialog.rows['Send With Attachments']); } _e.dialog.widgets['Subject'].value = get_doctype_label(this.meta.name) + ': ' + this.docname; _e.dialog.show(); } // notify this form of renamed records _f.Frm.prototype.rename_notify = function(dt, old, name) { // from form if(this.meta.in_dialog && !this.in_form) return; if(this.docname == old) this.docname = name; else return; // thats it, not for children! // editable this.is_editable[name] = this.is_editable[old]; delete this.is_editable[old]; // cleanup if(this && this.opendocs[old]) { // local doctype copy local_dt[dt][name] = local_dt[dt][old]; local_dt[dt][old] = null; } delete this.opendocs[old]; this.opendocs[name] = true; wn.re_route[window.location.hash] = '#Form/' + encodeURIComponent(this.doctype) + '/' + encodeURIComponent(name); wn.set_route('Form', this.doctype, name); } // SETUP _f.Frm.prototype.setup_meta = function(doctype) { this.meta = get_local('DocType',this.doctype); this.perm = get_perm(this.doctype); // for create if(this.meta.istable) { this.meta.in_dialog = 1 } this.setup_print(); } _f.Frm.prototype.setup_sidebar = function() { this.sidebar = new wn.widgets.form.sidebar.Sidebar(this); } _f.Frm.prototype.setup_footer = function() { var me = this; // footer toolbar var f = this.page_layout.footer; // save buttom f.save_area = $a(this.page_layout.footer,'div','',{display:'none', marginTop:'11px'}); f.help_area = $a(this.page_layout.footer,'div'); var b = $btn(f.save_area, 'Save', function() { cur_frm.save('Save'); },{marginLeft:'0px'},'green'); // show / hide save f.show_save = function() { $ds(me.page_layout.footer.save_area); } f.hide_save = function() { $dh(me.page_layout.footer.save_area); } } _f.Frm.prototype.set_intro = function(txt) { if(!this.intro_area) { this.intro_area = $('
/)==-1) txt = '
' + txt + '
'; this.intro_area.html(txt); } else { this.intro_area.remove(); this.intro_area = null; } } _f.Frm.prototype.set_footnote = function(txt) { if(!this.footnote_area) { this.footnote_area = $('/)==-1) txt = '
' + txt + '
'; this.footnote_area.html(txt); } else { this.footnote_area.remove(); this.footnote_area = null; } } _f.Frm.prototype.setup_fields_std = function() { var fl = wn.meta.docfield_list[this.doctype]; fl.sort(function(a,b) { return a.idx - b.idx}); if(fl[0]&&fl[0].fieldtype!="Section Break" || get_url_arg('embed')) { this.layout.addrow(); // default section break if(fl[0].fieldtype!="Column Break") {// without column too var c = this.layout.addcell(); $y(c.wrapper, {padding: '8px'}); } } var sec; for(var i=0;i