{"version":3,"sources":["../ts/modules/form/CRMSchoolLookUp.ts"],"names":["exports","CRMSchoolViewModel","constructor","schoolCountry","schoolTypes","schoolRegions","this","schoolName","knockout_1","observable","schoolKey","extend","required","schoolCode","schoolId","address","city","postalCode","region","categories","validation","validatedObservable","schoolLookUpService","crmschoollookupservice_1","CRMSchoolLookUpService","cache","caching_1","LookupCacheAsync","async","partialSchoolKey","lookUp","fixTranslation","setFields","data","name","get","doComplete","let","await","exactMatch","filter","i","length","validate","isValid","errors","showAllMessages"],"mappings":"wRAgBAA,QAAAC,yBAgBCC,YAAaC,cAAuBC,YAAwBC,eAdrDC,KAAAC,YAAa,EAAAC,WAAAC,YAAoB,EAAE,EACnCH,KAAAI,WAAY,EAAAF,WAAAC,YAAoB,EAAE,EAAGE,OAAQ,CAAEC,SAAU,CAAA,EAAMC,WAAYP,KAAKC,UAAU,CAAE,EAC5FD,KAAAQ,UAAW,EAAAN,WAAAC,YAAoB,EAAE,EACjCH,KAAAS,SAAU,EAAAP,WAAAC,YAAoB,EAAE,EAChCH,KAAAU,MAAO,EAAAR,WAAAC,YAAoB,EAAE,EAC7BH,KAAAW,YAAa,EAAAT,WAAAC,YAAoB,EAAE,EACnCH,KAAAY,QAAS,EAAAV,WAAAC,YAAoB,EAAE,EAC/BH,KAAAH,eAAgB,EAAAK,WAAAC,YAAoB,EAAE,EACtCH,KAAAF,aAAc,EAAAI,WAAAC,YAAsB,EAAE,EACtCH,KAAAD,eAAgB,EAAAG,WAAAC,YAAsB,EAAE,EAExCH,KAAAa,YAAa,EAAAX,WAAAC,YAAsB,EAAE,EAClCH,KAAAc,YAAa,EAAAZ,WAAAa,qBAAqBf,IAAI,EAYxCA,KAAAgB,oBAAsB,IAAIC,yBAAAC,uBAwB1BlB,KAAAmB,MAAQ,IAAIC,UAAAC,iBACnBC,MAAQ,CAAEC,iBAAkB1B,cAAeC,YAAaC,iBACjDC,KAAKgB,oBAAoBQ,OAAQD,iBAAkB1B,cAAeC,YAAaC,aAAa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U,WACN,IAAQvB,WAAed,KAAfc,cACR,MAAIA,CAAAA,CAAAA,WAAWwB,QAAO,IAGrBxB,WAAWyB,OAAOC,gBAAe,EAC1B,CAAA,EAET,C","file":"CRMSchoolLookUp.js","sourcesContent":["import 'knockout.validation';\r\n\r\nimport { LookupCacheAsync } from 'caching';\r\nimport { validation, observable, validatedObservable } from 'knockout';\r\nimport { CRMSchoolLookUpService } from 'services/crmschoollookupservice';\r\n\r\nexport interface ICRMSchoolFieldData {\r\n\tschoolKey: string;\r\n\tschoolId: string;\r\n\tname: string;\r\n\taddress: string;\r\n\tcity: string;\r\n\tpostalCode: string;\r\n\tregion: string;\r\n}\r\n\r\nexport class CRMSchoolViewModel {\r\n\t\r\n\tpublic schoolName = observable<string>( '' );\r\n\tpublic schoolKey = observable<string>( '' ).extend( { required: true, schoolCode: this.schoolName } );\r\n\tpublic schoolId = observable<string>( '' );\r\n\tpublic address = observable<string>( '' );\r\n\tpublic city = observable<string>( '' );\r\n\tpublic postalCode = observable<string>( '' );\r\n\tpublic region = observable<string>( '' );\r\n\tpublic schoolCountry = observable<string>( '' );\r\n\tpublic schoolTypes = observable<number[]>( [] );\r\n\tpublic schoolRegions = observable<number[]>( [] );\r\n\r\n\tpublic categories = observable<string[]>( [] );\r\n\tprotected validation = validatedObservable( this ) as KnockoutValidationGroup;\r\n\t\r\n\tconstructor( schoolCountry: string, schoolTypes?: number[], schoolRegions?: number[]) {\r\n\t\tthis.schoolCountry( schoolCountry );\r\n\t\tif(schoolTypes) {\r\n\t\t\tthis.schoolTypes( schoolTypes );\r\n\t\t}\r\n\t\tif(schoolRegions) {\r\n\t\t\tthis.schoolRegions( schoolRegions );\r\n\t\t}\t\t\r\n\t\tvalidation.fixTranslation( this );\r\n\t}\r\n\tprivate schoolLookUpService = new CRMSchoolLookUpService;\r\n\tpublic setFields( data: ICRMSchoolFieldData ) {\r\n\t\tif( data ) {\r\n\t\t\tif( data.schoolKey ) {\r\n\t\t\t\tthis.schoolKey( data.schoolKey );\r\n\t\t\t}\r\n\t\t\tif( data.schoolId ) {\r\n\t\t\t\tthis.schoolId( data.schoolId );\r\n\t\t\t}\r\n\t\t\tthis.schoolName( data.name );\r\n\t\t\tthis.address( data.address );\r\n\t\t\tthis.city( data.city );\r\n\t\t\tthis.postalCode( data.postalCode );\r\n\t\t\tthis.region( data.region );\r\n\t\t} else {\r\n\t\t\tthis.schoolId( '' );\r\n\t\t\tthis.schoolName( '' );\r\n\t\t\tthis.address( '' );\r\n\t\t\tthis.city( '' );\r\n\t\t\tthis.postalCode( '' );\r\n\t\t\tthis.region( '' );\r\n\t\t}\r\n\t}\r\n\r\n\tprivate cache = new LookupCacheAsync<[ string, string, number[], number[] ], ICRMSchoolFieldData[]>(\r\n\t\tasync ( [ partialSchoolKey, schoolCountry, schoolTypes, schoolRegions ] ) =>\r\n\t\t\tawait this.schoolLookUpService.lookUp( partialSchoolKey, schoolCountry, schoolTypes, schoolRegions )\r\n\t);\r\n\r\n\tprivate async lookUp( partialSchoolKey: string, schoolCountry: string, schoolTypes: number[], schoolRegions: number[] ) {\r\n\t\tconst { cache } = this;\r\n\t\treturn await cache.get( [ partialSchoolKey, schoolCountry, schoolTypes, schoolRegions ] );\r\n\t}\r\n\r\n\tpublic async doComplete( partialSchoolKey: string ) {\r\n\t\tlet data = await this.lookUp( partialSchoolKey, this.schoolCountry(), this.schoolTypes(), this.schoolRegions() );\r\n\t\tif( !data ) {\r\n\t\t\tdata = [];\r\n\t\t}\r\n\t\t\r\n\t\tconst exactMatch = data.filter( i => i.schoolKey === partialSchoolKey );\r\n\t\tif( exactMatch && exactMatch.length === 1 ) {\r\n\t\t\tthis.setFields( exactMatch[0] );\r\n\t\t\treturn [];\r\n\t\t}\r\n\r\n\t\tthis.setFields( {\r\n\t\t\tschoolKey: this.schoolKey(),\r\n\t\t\tschoolId: '',\r\n\t\t\tname: '',\r\n\t\t\taddress: '',\r\n\t\t\tcity: '',\r\n\t\t\tpostalCode: '',\r\n\t\t\tregion: ''\r\n\t\t} );\r\n\r\n\t\treturn data;\r\n\t}\r\n\r\n\tpublic validate() {\r\n\t\tconst { validation } = this;\r\n\t\tif( validation.isValid() ) {\r\n\t\t\treturn true;\r\n\t\t} else {\r\n\t\t\tvalidation.errors.showAllMessages();\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n}\r\n"]}