Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. function init() {
  2. initCalendarUI();
  3. initNewActivityUI();
  4. }
  5.  
  6. function initCalendarUI() {
  7. initHeader();
  8. initNavBar();
  9. initNavEventListeners();
  10. initCalendarSection();
  11. initActivitySection();
  12. }
  13.  
  14. function initNewActivityUI() {
  15. initForm();
  16. initFormActivity();
  17. initFormDate();
  18. initFormButton();
  19. }
  20.  
  21. const appWrapper = document.createElement('div'); // create div element
  22. appWrapper.className = 'container'; // give div elemnet a classname of 'appWrapper'
  23.  
  24. document.body.appendChild(appWrapper); // add wrapper element to body
  25.  
  26. /**
  27. * @desc creates a header element
  28. */
  29. function initHeader() {
  30. const header = document.createElement('header'); // create header element
  31. header.className = 'header'; // give header elemnet a classname of 'header'
  32. header.innerHTML = '<p>Calendar application 2000</p>'; // create p tag inside header elemnt
  33.  
  34. appWrapper.appendChild(header); // add header elemnt to body
  35. }
  36.  
  37. /**
  38. * @desc creates a section element for the Calendar page
  39. */
  40. function initCalendarSection() {
  41. const calendarSection = document.createElement('section'); // create section element
  42. calendarSection.id = 'calendar'; // give section elemnet an className of 'new-activity'
  43. calendarSection.innerHTML = '<p>Calendar</p>'; // create p tag inside Section elemnt
  44.  
  45. appWrapper.appendChild(calendarSection); // add Section elemnt to body
  46. }
  47.  
  48. /**
  49. * @desc creates a section element for the Activity page
  50. */
  51. function initActivitySection() {
  52. const activitySection = document.createElement('section'); // create Section element
  53. activitySection.id = 'new-activity'; // give Section elemnet an classname of 'calendar'
  54. activitySection.innerHTML = '<p>New activity</p>'; // create h1 tag inside Section elemnt
  55.  
  56. appWrapper.appendChild(activitySection); // add Section elemnt to body
  57. }
  58.  
  59. /**
  60. * @desc creates navbar elements.
  61. */
  62. function initNavBar() {
  63. const navBar = document.createElement('nav'); // create nav element
  64. navBar.className = 'nav-bar'; // give nav element a classname of 'nav-bar'
  65. navBar.innerHTML = `
  66. <a href="#">New activity</a>
  67. <a href="#">Calendar</a>
  68. `
  69.  
  70. appWrapper.appendChild(navBar); // add nav element to body
  71. }
  72.  
  73. /**
  74. * @desc Event listeners for nav-links.
  75. */
  76. function initNavEventListeners() {
  77. const navBarATags = document.getElementsByTagName('a');
  78.  
  79. navBarATags[0].addEventListener('click', hideCalendar);
  80. navBarATags[1].addEventListener('click', hideNewActivity);
  81. }
  82.  
  83. /**
  84. * @desc hides calendar content and shows New Activity page
  85. */
  86. function hideCalendar(){
  87. document.getElementById('new-activity').style.display = 'initial';
  88. document.getElementById('calendar').style.display = 'none';
  89. }
  90.  
  91. /**
  92. * @desc hides New activity content and shows Calendar page
  93. */
  94. function hideNewActivity(){
  95. document.getElementById('new-activity').style.display = 'none';
  96. document.getElementById('calendar').style.display = 'initial';
  97. }
  98.  
  99. //--------------------New Activity Form-----------------------
  100.  
  101. /**
  102. * @desc Creates form element for activity and date input,label.. with the id activity-form
  103. */
  104.  
  105. function initForm() {
  106. const activityForm = document.createElement('form');
  107. activityForm.setAttribute('id', 'activity-form');
  108.  
  109. document.getElementById('new-activity').appendChild(activityForm);
  110. }
  111.  
  112. /**
  113. * @desc creates Activity input and label for with id activity-input
  114. */
  115.  
  116. function initFormActivity() {
  117. const activityInput = document.createElement('input');
  118. activityInput.name = 'activity-input';
  119. activityInput.type = 'text';
  120. activityInput.placeholder = 'Write your Activity';
  121.  
  122. const activityInputLabel = document.createElement('label')
  123. activityInputLabel.htmlFor = 'activity-input'
  124. activityInputLabel.textContent = 'Activity';
  125.  
  126. document.getElementById('activity-form').appendChild(activityInputLabel);
  127. document.getElementById('activity-form').appendChild(activityInput);
  128.  
  129. }
  130.  
  131. /**
  132. * @desc creates Date input and label for with id date-input
  133. */
  134.  
  135. function initFormDate() {
  136. const dateInput = document.createElement('input');
  137. dateInput.name = 'date-input';
  138. dateInput.type = 'date'
  139.  
  140. const dateInputLabel = document.createElement('label');
  141. dateInputLabel.htmlFor = 'date-input';
  142. dateInputLabel.textContent = 'Date';
  143.  
  144. document.getElementById('activity-form').appendChild(dateInputLabel);
  145. document.getElementById('activity-form').appendChild(dateInput);
  146. }
  147.  
  148. /**
  149. * @desc creates Submit button element for activity-form, with id activity-form-submitbtn
  150. */
  151.  
  152. function initFormButton() {
  153. const submitButton = document.createElement('button');
  154. submitButton.innerText = 'Post Activity';
  155. submitButton.id = 'activity-form-submitbtn'
  156. document.getElementById('activity-form').appendChild(submitButton);
  157. submitButton.addEventListener('click', createActivityObject);
  158. }
  159.  
  160.  
  161. document.addEventListener('DOMContentLoaded', init);
  162.  
  163.  
  164. //---------------------activity object------------------------
  165.  
  166. /**
  167. * @desc creates a new activity object from new activity form inputs
  168. * @param event e - the passed event from addEventListener
  169. */
  170. function createActivityObject(e){
  171.  
  172. // prevent defalt button behavior (submit) to not lose user input due to reload of page
  173. e.preventDefault();
  174.  
  175. // create an object and store inputs in it
  176. let newActivity = {
  177. name: document.getElementsByName('activity-input')[0].value,
  178. date: document.getElementsByName('date-input')[0].value
  179. }
  180.  
  181. // reset the form
  182. document.getElementById('activity-form').reset();
  183.  
  184. // just to cofirm if eerything is working - delete when no need
  185. console.log(newActivity);
  186. displayActivity(newActivity);
  187. }
  188.  
  189. //-----------------Display Activity------------------
  190.  
  191. /**
  192. * @desc Creates two h4 elements, takes values passed down by createActivityObj function and appends these elements as 'Activity' & 'Date'
  193. * @param activity takes variable 'newActivity' from createActivityObject function and passes it as argument to use in displayActivity
  194. */
  195.  
  196. function displayActivity(activity) {
  197. console.log(activity);
  198. let activityName = document.createElement('h4')
  199. let activityDate = document.createElement('h4')
  200. activityName.textContent = 'Activity: ' + activity.name;
  201. activityDate.textContent = 'Date: ' + activity.date;
  202.  
  203. document.getElementById('calendar').appendChild(activityName);
  204. document.getElementById('calendar').appendChild(activityDate);
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement