Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.15 KB | None | 0 0
  1. import { Component, NgZone, OnInit } from '@angular/core';
  2. import { FormGroup, FormControl } from '@angular/forms';
  3. import {
  4. AdviceRepository, DiseaseRepository, DrugTypeRepository, ExaminationRepository, PrescriptionRepository,
  5. SymptomRepository
  6. } from '../../providers/offline/Repositories';
  7. import {
  8. Prescription, PrescriptionAdvice, PrescriptionDiseases, PrescriptionExamination,
  9. PrescriptionSymptom
  10. } from '../../models/Prescription';
  11. import Advice from '../../models/Advice';
  12. import Examination from '../../models/Examination';
  13. import Disease from '../../models/Disease';
  14. import Symptom from '../../models/Symptom';
  15.  
  16. @Component({
  17. selector: 'app-prescription-template',
  18. templateUrl: './prescription-template.component.html',
  19. styleUrls: ['./prescription-template.component.css']
  20. })
  21. export class PrescriptionTemplateComponent implements OnInit {
  22. prescription: Prescription;
  23. prescriptionForm: FormGroup;
  24. symptomOptions = [];
  25. symptomOptionsConfig: any;
  26. diseaseOptions = [];
  27. diseaseOptionsConfig: any;
  28. examinationOptions = [];
  29. examinationOptionsConfig: any;
  30. adviceOptions = [];
  31. adviceOptionsConfig: any;
  32.  
  33. currentlySelectedAdvice = null;
  34.  
  35. form: FormGroup = new FormGroup({});
  36.  
  37. constructor(private zone: NgZone,
  38. private symptomRepo: SymptomRepository,
  39. private diseaseRepo: DiseaseRepository,
  40. private examinationRepo: ExaminationRepository,
  41. private adviceRepo: AdviceRepository,
  42. private drugTypeRepo: DrugTypeRepository,
  43. private prescriptionRepository: PrescriptionRepository) {
  44.  
  45. // TODO: take it as input. for simplicity it's created here.
  46. this.prescription = new Prescription();
  47. }
  48.  
  49. onSymptomSelected(value, $element) {
  50. this.symptomRepo.findBy({id: Number(value)}, 0, 1).then((results: any) => {
  51. console.log(results, value);
  52.  
  53. const selectize = $element.parent().parent().siblings('select')[0].selectize;
  54. selectize.clear();
  55.  
  56. if (results.length > 0) {
  57. // TODO: create a prescriptionSymptom
  58. const ps = new PrescriptionSymptom();
  59. ps.symptom = results[0];
  60.  
  61. this.zone.run(() => {
  62. this.prescription.symptoms.push(ps);
  63. });
  64. }
  65. });
  66. }
  67.  
  68. onDiseaseSelected(value, $element) {
  69. this.diseaseRepo.findBy({id: Number(value)}, 0, 1).then((results: any) => {
  70. console.log(results, value);
  71.  
  72. const selectize = $element.parent().parent().siblings('select')[0].selectize;
  73. selectize.clear();
  74.  
  75. if (results.length > 0) {
  76. // TODO: create a prescriptionSymptom
  77. const ps = new PrescriptionDiseases();
  78. ps.disease = results[0];
  79.  
  80. this.zone.run(() => {
  81. this.prescription.diseases.push(ps);
  82. });
  83. }
  84. });
  85. }
  86.  
  87. onExaminationSelected(value, $element) {
  88. this.examinationRepo.findBy({id: Number(value)}, 0, 1).then((results: any) => {
  89. console.log(results, value);
  90.  
  91. const selectize = $element.parent().parent().siblings('select')[0].selectize;
  92. selectize.clear();
  93.  
  94. if (results.length > 0) {
  95. // TODO: create a prescriptionSymptom
  96. const ps = new PrescriptionExamination();
  97. ps.examination = results[0];
  98.  
  99. this.zone.run(() => {
  100. this.prescription.examinations.push(ps);
  101. });
  102. }
  103. });
  104. }
  105.  
  106. onAdviceSelected(value, $element) {
  107. this.adviceRepo.findBy({id: Number(value)}, 0, 1).then((results: any) => {
  108. console.log(results, value);
  109.  
  110. const selectize = $element.parent().parent().siblings('select')[0].selectize;
  111. selectize.clear();
  112.  
  113. if (results.length > 0) {
  114. // TODO: create a prescriptionSymptom
  115. const ps = new PrescriptionAdvice();
  116. ps.advice = results[0];
  117.  
  118. this.zone.run(() => {
  119. this.prescription.advices.push(ps);
  120. });
  121. }
  122. });
  123. }
  124.  
  125. ngOnInit() {
  126. this.prescriptionForm = new FormGroup({
  127. 'templateName': new FormControl(),
  128. 'otherAdvice': new FormControl(),
  129. 'remarks': new FormControl(),
  130. 'advices': new FormControl(),
  131. 'diseases': new FormControl(),
  132. 'examinations': new FormControl(),
  133. 'medicines': new FormControl(),
  134. 'symptoms': new FormControl(),
  135. });
  136.  
  137. this.symptomOptionsConfig = this.getSymptomSelectConfig();
  138. this.diseaseOptionsConfig = this.getDiseaseSelectConfig();
  139. this.examinationOptionsConfig = this.getExaminationSelectConfig();
  140. this.adviceOptionsConfig = this.getAdviceSelectConfig();
  141. }
  142.  
  143. onSubmit() {
  144. const formModel = this.prescriptionForm.value;
  145. // console.log(formModel);
  146. console.log(this.prescription);
  147. delete this.prescription.doctorFee;
  148. delete this.prescription.visitType;
  149. console.log(formModel);
  150. // this.prescriptionRepository.insertOrUpdate(this.prescription);
  151. }
  152.  
  153. getSymptomSelectConfig() {
  154. return {
  155. valueField: 'id',
  156. labelField: 'title',
  157. searchField: ['title'],
  158. placeholder: 'Select a symptom',
  159. hideSelected: true,
  160. highlight: false,
  161. preload: true,
  162. onItemAdd: this.onSymptomSelected.bind(this),
  163. load: (query, callback) => {
  164. this.symptomRepo.autoComplete('title', query, null, 0, 100).then((items) => {
  165. // this.symptomOptions = items;
  166. callback(items);
  167. // this.prescription.symptoms = items;
  168. })
  169. },
  170. create: (input, callback) => {
  171. const symptom = new Symptom();
  172. symptom.title = input;
  173.  
  174. this.symptomRepo.insertOrUpdate(symptom).then((value) => {
  175. callback(symptom);
  176. });
  177. }
  178. }
  179. }
  180.  
  181. getDiseaseSelectConfig() {
  182. return {
  183. valueField: 'id',
  184. labelField: 'title',
  185. searchField: ['title'],
  186. placeholder: 'Select a disease',
  187. hideSelected: true,
  188. highlight: false,
  189. preload: true,
  190. onItemAdd: this.onDiseaseSelected.bind(this),
  191. load: (query, callback) => {
  192. this.diseaseRepo.autoComplete('title', query, null, 0, 100).then((items) => {
  193. // this.symptomOptions = items;
  194. callback(items);
  195. // this.prescription.symptoms = items;
  196. })
  197. },
  198. create: (input, callback) => {
  199. const disease = new Disease();
  200. disease.title = input;
  201.  
  202. this.diseaseRepo.insertOrUpdate(disease).then((value) => {
  203. callback(disease);
  204. });
  205. }
  206. }
  207. }
  208.  
  209. getExaminationSelectConfig() {
  210. return {
  211. valueField: 'id',
  212. labelField: 'title',
  213. searchField: ['title'],
  214. placeholder: 'Select a diagnostic test',
  215. hideSelected: true,
  216. highlight: false,
  217. preload: true,
  218. onItemAdd: this.onExaminationSelected.bind(this),
  219. load: (query, callback) => {
  220. this.examinationRepo.autoComplete('title', query, null, 0, 100).then((items) => {
  221. // this.symptomOptions = items;
  222. callback(items);
  223. // this.prescription.symptoms = items;
  224. })
  225. },
  226. create: (input, callback) => {
  227. const examination = new Examination();
  228. examination.title = input;
  229.  
  230. this.examinationRepo.insertOrUpdate(examination).then((value) => {
  231. callback(examination);
  232. });
  233. }
  234. }
  235. }
  236.  
  237. private getAdviceSelectConfig() {
  238. return {
  239. persist: false,
  240. valueField: 'id',
  241. labelField: 'name',
  242. searchField: ['name'],
  243. placeholder: 'Select an advice',
  244. hideSelected: true,
  245. highlight: false,
  246. preload: true,
  247. onItemAdd: this.onAdviceSelected.bind(this),
  248. load: (query, callback) => {
  249. this.adviceRepo.autoComplete('title', query, null, 0, 100).then((items) => {
  250. // this.symptomOptions = items;
  251. callback(items);
  252. // this.prescription.symptoms = items;
  253. })
  254. },
  255. create: (input, callback) => {
  256. const advice = new Advice();
  257. advice.name = input;
  258. advice.alternateName = input;
  259.  
  260. this.adviceRepo.insertOrUpdate(advice).then((value) => {
  261. callback(advice);
  262. });
  263. }
  264. }
  265. }
  266.  
  267. onMedicineSelection = (med) => {
  268. this.prescription.medicines.push(med);
  269. };
  270.  
  271. onMedicineRemove(med) {
  272. const index: number = this.prescription.medicines.indexOf(med);
  273. if (index !== -1) {
  274. this.prescription.medicines.splice(index, 1);
  275. }
  276.  
  277. return false;
  278. }
  279.  
  280. onSymptomRemove(symptom) {
  281. const index: number = this.prescription.symptoms.indexOf(symptom);
  282. if (index !== -1) {
  283. this.prescription.symptoms.splice(index, 1);
  284. }
  285. }
  286.  
  287. onDiseaseRemove(disease) {
  288. const index: number = this.prescription.diseases.indexOf(disease);
  289. if (index !== -1) {
  290. this.prescription.diseases.splice(index, 1);
  291. }
  292. }
  293.  
  294. onExaminationRemove(exam) {
  295. const index: number = this.prescription.examinations.indexOf(exam);
  296. if (index !== -1) {
  297. this.prescription.examinations.splice(index, 1);
  298. }
  299. }
  300.  
  301. onAdviceRemove(advice) {
  302. const index: number = this.prescription.advices.indexOf(advice);
  303. if (index !== -1) {
  304. this.prescription.advices.splice(index, 1);
  305. }
  306. }
  307.  
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement