Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2. /*
  3.     ## Prototype ##
  4. */
  5. const Person = function (data) {
  6.     const self = this;
  7.     const objectKeys = Object.keys(data);
  8.     const objectValues = Object.values(data);
  9.     objectKeys.forEach((key, index) => {
  10.         self[key] = objectValues[index];
  11.     });
  12. };
  13.  
  14. const Patient = function (name, male, age, symptom) {
  15.     this.name = name;
  16.     this.male = male;
  17.     this.age = age;
  18.     this.symptom = symptom;
  19. };
  20.  
  21.  
  22. Patient.prototype = Object.create(Person.prototype);
  23. Patient.prototype.constructor = Patient;
  24.  
  25. Patient.prototype.getPatientAge = function(){
  26.     if(this.age < 3){
  27.         return "baby"
  28.     }
  29.     else if(this.age >= 3 && this.age <= 16){
  30.         return "child"
  31.     }
  32.     else if(this.age >= 16 && this.age <= 75){
  33.         return "adult"
  34.     }
  35.     else if(this.age > 75){
  36.         return "elderly"
  37.     }
  38.  
  39. };
  40.  
  41. Patient.prototype.getPatientGender = function () {
  42.   if(this.male === true){
  43.       return "male";
  44.   }  else {
  45.       return "female";
  46.   }
  47. };
  48. //TODO: Fix the organicIcons
  49.  
  50. const Doctor = function (name, male, speciality) {
  51.     this.name = name;
  52.     this.male = male;
  53.     this.specialty = speciality;
  54. };
  55.  
  56. Doctor.prototype = Object.create(Person.prototype);
  57. Doctor.prototype.constructor = Doctor;
  58.  
  59. Doctor.prototype.getDoctorGender = function () {
  60.   if(this.male === true){
  61.       return true;
  62.   }  else {
  63.       return "female";
  64.   }
  65. };
  66. //TODO: Fix the personIcons for Doctors
  67.  
  68. /*
  69.     ##Code voor drag and drop event handling
  70. */
  71. var listen = function (e) {
  72.     e.dataTransfer = e.originalEvent.dataTransfer;
  73.     e.dataTransfer.setData("person", e.target.id); // Geef gegevens door op deze manier wanneer je de drag released
  74.  
  75.     // Neat little trick to preserve dragged element's style in Chrome even when it is a link
  76.     if (e.dataTransfer.setDragImage)
  77.         e.dataTransfer.setDragImage(e.target, 0, 0);
  78. };
  79.  
  80. var releaseDrag = function (e) {
  81.     console.log('releaseDrag');
  82. };
  83.  
  84. var showFeedback = function () {
  85.     console.log('showFeedback');
  86. };
  87.  
  88. var allowDrop = function (e) {
  89.     e.preventDefault();
  90. };
  91.  
  92. // Eigenlijke verwerking van gedropte element
  93. var checkDropzone = function (e) {
  94.     e.dataTransfer = e.originalEvent.dataTransfer;
  95.  
  96.  
  97.     var draggedPersonId = e.dataTransfer.getData('person'); // gegevens ophalen
  98.     var dataType = $('#' + draggedPersonId).attr('data-type');
  99.     var problem = $('#' + draggedPersonId + ' figcaption').text();
  100.  
  101.     // Hier verdere logica implementeren
  102.     // Plaats de naam onder hospital
  103.     var element = "<li><article>" +
  104.         "<h3>" + draggedPersonId + "</h3>" +
  105.         "<p>" + problem + "</p>" +
  106.         "</article></li>";
  107.     $(".draggedPersons").append(element);
  108.  
  109.     // Als de persoon al eens gedropt is kan hij niet nog eens gedropt worden
  110.     $('#' + draggedPersonId)
  111.         .addClass('dragged')
  112.         .attr('draggable', false);
  113.  
  114.  
  115.     return false; // Niet verwijderen!
  116. };
  117.  
  118. var initDragAndDrop = function () {
  119.     $('header,footer') // all articles in either header or footer are draggable ...
  120.         .on('dragstart', 'article', listen)
  121.         .on('dragend', 'article', releaseDrag);
  122.  
  123.     $('main figure') // the image (of the hospital) in main allows for persons to be dropped ...
  124.         .on('dragenter', 'img', showFeedback)
  125.         .on('dragleave', 'img', showFeedback)
  126.         .on('dragover', 'img', allowDrop)
  127.         .on('drop', 'img', checkDropzone);
  128. };
  129.  
  130.  
  131. /*
  132.     ##Data functies
  133.  */
  134.  
  135. const deepCopyObject = function (objectToCopy) {
  136.     return JSON.parse(JSON.stringify(objectToCopy));
  137. };
  138.  
  139. const getData = function () {
  140.     const personsObject = deepCopyObject(personData);
  141.     let persons = [];
  142.     personsObject.forEach(person => {
  143.         const p = new Person(person);
  144.         persons.push(p);
  145.     });
  146.     return persons;
  147. };
  148.  
  149. let patients = [];
  150. let doctors = [];
  151.  
  152. const getPersonType = function () {
  153.     const personData = getData();
  154.     personData.forEach(person => {
  155.         if (person.age !== undefined) {
  156.             const patient = new Patient(person.name, person.male, person.age, person.symptom);
  157.             patients.push(patient);
  158.         } else {
  159.             const doctor = new Doctor(person.name, person.male, person.specialty);
  160.             doctors.push(doctor);
  161.         }
  162.     })
  163. }();
  164.  
  165. const getCondition = function(conditionType) {
  166.     let conditionObject = [];
  167.     conditionKeys.forEach(condition => {
  168.             if(conditionType === condition.name){
  169.                 conditionObject.push(condition);
  170.             }
  171.     });
  172.     return conditionObject;
  173. };
  174. /*
  175.     ##HTML Functies
  176.  */
  177.  
  178. const addPersonArticle = function (person,personIcon,organicIcon,displayName) {
  179.     //Renders the article element
  180.     let element = '';
  181.     const personName = person.name.replace(" ", "-");
  182.     element += `<article id="${personName}" draggable="true">
  183.                     <h3>${person.name}</h3>
  184.                     <figure>
  185.                         ${organIconMap.create(organicIcon)[0].outerHTML}
  186.                         ${personIconMap.create(personIcon)[0].outerHTML}
  187.                     </figure>
  188.                     <figcaption>${displayName}</figcaption>
  189.                 </article>`;
  190.  
  191.     return element;
  192. };
  193.  
  194. const renderPatients = function () {
  195.     //console.log(patients);
  196.  
  197.     patients.forEach(patient => {
  198.         const renderPersonIcon = patientIconKeys[patient.getPatientGender()][patient.getPatientAge()];
  199.         const renderOrganicIcon = getCondition(patient.symptom)[0].icon;
  200.         const renderDisplayName = getCondition(patient.symptom)[0].displayName;
  201.         $('header').append(addPersonArticle(patient,renderPersonIcon,renderOrganicIcon,renderDisplayName));
  202.     })
  203. };
  204.  
  205. const renderDoctors = function () {
  206.     doctors.forEach(doctor => {
  207.         let renderPersonIcon = '';
  208.         if(doctor.male){
  209.             renderPersonIcon = getCondition(doctor.specialty)[0].maleIcon;
  210.         } else {
  211.             renderPersonIcon = getCondition(doctor.specialty)[0].femaleIcon;
  212.         }
  213.         const renderOrganicIcon = getCondition(doctor.specialty)[0].icon;
  214.         const renderDisplayName = getCondition(doctor.specialty)[0].doctorDisplayName;
  215.         $('footer').append(addPersonArticle(doctor,renderPersonIcon,renderOrganicIcon,renderDisplayName))
  216.     })
  217. };
  218.  
  219. $(() => {
  220.     initDragAndDrop();
  221.     renderPatients();
  222.     renderDoctors();
  223. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement