﻿
__trGroup('CMS Form');Ext.namespace('NPSL','NPSL.Cms','NPSL.Cms.Form','Ext.ux');(function(){var Cms=NPSL.Cms,Form=Cms.Form,ColType=Cms.ColType,MimeTypes=Cms.MimeTypes;Form.FormPanel=function(config){if(config.keys){this.keys=new Cms.Keys(config.keys);}else{this.keys=new Cms.Keys({});}
delete config.keys;config.layout="form";config.border=false;if(!config.items){config.items=[];}
var tbar=[];if(config.backTo){tbar.push({text:__tr('Back'),iconCls:'back-icon',handler:function(){xsrf_nav(config.backTo);}});}
if(config.wordTemplate){tbar.push({text:__tr('Download Word'),iconCls:'word-icon',handler:function(){GridHandler.renderFormToWord(config.wordTemplate,this.keys,function(cacheKey){xsrf_nav("/fp/shared/extux/upload/file/download.aspx?cacheKey="+encodeURIComponent(cacheKey));});},scope:this});}
if(tbar.length>0){config.tbar=tbar;}
this.records=config.records;this.buildKeyMap();Form.FormPanel.superclass.constructor.call(this,config);}
Form.FormPanel.fromXML=function(xml,config){config=config||{};config.keys=config.keys?new Cms.Keys(config.keys):new Cms.Keys({});if(!xml){throw"no xml passed to FormPanel.fromXML";}
xml=XML.Node.get(xml);var frm=xml.getChild("form");if(!frm){throw"form node is missing from xml";}
var labelWidth=frm.getAttribute("label-width");Ext.apply(config,{labelWidth:labelWidth?parseInt(labelWidth,10):null,labelAlign:frm.getAttribute("label-align")||'',helpTarget:frm.getAttribute("help-target"),hideMandatory:frm.getAttribute("hide-mandatory-icon")==='true',wordTemplate:frm.getAttribute("word-template"),records:{},items:[]});for(var n=frm.getFirstChild();n;n=n.getNextSibling()){var t=Form.types[n.getName()];if(!t){throw"unknown node: "+n.getName();}else{var item=t.fromXML(n,config,null);if(item){config.items.push(item);}}}
return new Form.FormPanel(config);};Ext.extend(Form.FormPanel,Ext.FormPanel,{initComponent:function(){Form.FormPanel.superclass.initComponent.call(this);this.initItems();},createForm:function(){delete this.initialConfig.listeners;return new Form.BasicForm(null,this.initialConfig);},buildKeyMap:function(){var unmapped={};var unmappedCount=0;for(var tn in this.records){if(!this.records.hasOwnProperty(tn)){continue;}
var record=this.records[tn];record.childRecords=[];unmapped[tn]=record;++unmappedCount;}
this.rootRecords=[];for(var tn in unmapped){if(!unmapped.hasOwnProperty(tn)){continue;}
var record=unmapped[tn];for(var i=0;i<2;++i){var keys=i==0?record.table.uks:record.table.fks;for(var j=0;j<keys.length;++j){var k=keys[j];if(this.keys.hasKey(k)){this.rootRecords.push(record);record.tableKey=k;record.keys=this.keys.extractKeysForTable(record.table.name);delete unmapped[tn];--unmappedCount;record.isRootRecord=true;i=2;break;}}}}
if(unmappedCount==0)
return;var cascadeQ=this.rootRecords.slice(0);for(var i=0;unmappedCount>0&&i<cascadeQ.length;++i){var record=cascadeQ[i];var uk=record.tableKey;if(typeof(uk.children)==="undefined"){continue;}
for(var j=0;unmappedCount>0&&j<uk.children.length;++j){var fk=uk.children[j];for(var cn in unmapped){if(!unmapped.hasOwnProperty(cn)){continue;}
var childRecord=unmapped[cn];if(childRecord.table&&childRecord.table.name==fk.table.name){childRecord.parentTableKey=uk;childRecord.tableKey=fk;childRecord.keys=this.keys.extract(uk,fk);childRecord.directPush=true;record.childRecords.push(childRecord);cascadeQ.push(childRecord);delete unmapped[cn];--unmappedCount;}}}}
if(unmappedCount==0)
return;cascadeQ=this.rootRecords.slice(0);for(var i=0;unmappedCount>0&&i<cascadeQ.length;++i){var record=cascadeQ[i];for(var j=0;unmappedCount>0&&j<record.table.uks.length;++j){var uk=record.table.uks[j];for(var k=0;unmappedCount>0&&k<uk.children.length;++k){var fk=uk.children[k];for(var cn in unmapped){if(!unmapped.hasOwnProperty(cn)){continue;}
var childRecord=unmapped[cn];if(childRecord.table&&childRecord.table.name==fk.table.name){childRecord.parentTableKey=uk;childRecord.tableKey=fk;childRecord.keys=this.keys.extract(uk,fk);childRecord.directPush=false;record.childRecords.push(childRecord);cascadeQ.push(childRecord);delete unmapped[cn];--unmappedCount;}}}}}
if(unmappedCount==0)
return;for(var cn in unmapped){if(!unmapped.hasOwnProperty(cn)){continue;}
var record=unmapped[cn];this.rootRecords.push(record);record.keys=new Cms.Keys();record.tableKey=null;record.isRootRecord=true;}},setKeys:function(keys){var oldKeys=this.keys;this.keys=new Cms.Keys(keys);if(!oldKeys||!oldKeys.hasSameStructure(this.keys)){this.buildKeyMap();}
for(var i=0;i<this.rootRecords.length;++i){var record=this.rootRecords[i];record.setKeys(record.tableKey?this.keys.extractKeysForTable(record.table.name):this.keys);}},afterRender:function(){Form.FormPanel.superclass.afterRender.call(this);if(!Form.LayoutMode)
return;this.dropZone=new Ext.dd.DropZone(this.getEl(),{ddGroup:'layout',getTargetFromEvent:function(e){return e.getTarget('.layout-target');},onNodeEnter:function(target,dd,e,data){Ext.fly(target).addClass('layout-target-hover');},onNodeOut:function(target,dd,e,data){Ext.fly(target).removeClass('layout-target-hover');},topHalf:function(target,e){var targetEl=Ext.fly(target);var box=targetEl.getBox();var y=e.getXY()[1];return y<=box.y+box.height/2;},isChild:function(p,n){while(n&&n!==p){n=n.parentNode;}
return n;},isChildComponent:function(p,n){while(n&&n!==p){n=n.ownerCt;}
return n;},onNodeOver:function(target,dd,e,data){if(this.isChild(data.sourceEl,target)){return this.dropNotAllowed;}
if(Ext.fly(target).hasClass('layout-target-container'))
return"x-dd-drop-ok";if(this.topHalf(target,e))
return"x-tree-drop-ok-above";return"x-tree-drop-ok-below";},onNodeDrop:function(target,dd,e,data){if(data.handled)
return true;if(this.isChild(data.sourceEl,target))
return false;data.handled=true;var into=Ext.fly(target).hasClass('layout-target-container');var above=this.topHalf(target,e);var c=Ext.getCmp(target.id);if(!c){var fp=Ext.get(target.id);var ff=fp.query('.x-form-field');if(ff.length>0){c=Ext.getCmp(ff[0].id);}
if(!c){alert("Dropped onto something that's not a component");return false;}}
var p=into?c:c.ownerCt;if(!p){alert("Can't find the parent container of the drop target");return false;}
if(data.ttype){var newCmp=Ext.ComponentMgr.create({xtype:data.ttype});if(into){p.insert(above?0:p.items.getCount(),newCmp);}else if(above){p.insertBefore(newCmp,c);}else{p.insertAfter(newCmp,c);}
p.doLayout();return true;}
if(data.sourceEl===target){return false;}
var moveCmp=typeof(data.cmp)==="string"?Ext.getCmp(data.sourceEl.id):data.cmp;var newCmp=moveCmp.clone();var moveOwnerCt=moveCmp.ownerCt;moveOwnerCt.remove(moveCmp,true);if(into){p.insert(above?0:p.items.getCount(),newCmp);}else if(above){p.insertBefore(newCmp,c);}else{p.insertAfter(newCmp,c);}
moveOwnerCt.doLayout();if(moveOwnerCt!=p){p.doLayout();}
return true;}});},insertAfter:function(newcomp,comp){Form.insertOffset(this,newcomp,comp,1);},insertBefore:function(newcomp,comp){Form.insertOffset(this,newcomp,comp,0);},saveXml:function(p){var root=p;if(!root){root=new XML.Document();p=root.createChild({tag:"form"});}
if(this.initialConfig.labelWidth){p.setAttribute("label-width",this.initialConfig.labelWidth);}
this.items.each(function(item){if(item.saveXml){item.saveXml(p);}});return root;}});}());