Advertisement
Guest User

Untitled

a guest
Sep 1st, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. function App(title, type) {
  2. this.title = title;
  3. this.body = document.querySelector('body');
  4. }
  5.  
  6. App.prototype.contTemplate = function() {
  7. this.contTmpl = document.createElement('div');
  8. this.contTmpl.classList.add('tasks-cont');
  9. };
  10.  
  11. App.prototype.render = function() {
  12. this.contTemplate();
  13. document.body.appendChild(this.contTmpl);
  14. };
  15.  
  16. function Task(title, id, type) {
  17. this.title = title;
  18. this.id = id;
  19. this.completed = false;
  20. this.typeTask = type || 'regular';
  21. }
  22.  
  23. Task.prototype.template = function() {
  24. var htmlTemplate = document.createElement('div');
  25. htmlTemplate
  26. .setAttribute('data-id', this.id);
  27.  
  28. htmlTemplate
  29. .classList
  30. .add('task', (this.typeTask == 'regular' ? 'task-default' : 'task-urgent'));
  31.  
  32. var span = document.createElement('span');
  33. var spanContent = document.createTextNode(this.title);
  34. span.appendChild(spanContent);
  35.  
  36. var checkbox = document.createElement('input');
  37. checkbox.type = 'checkbox';
  38. checkbox.name = 'name';
  39. // checkbox.value = 'false';
  40. checkbox
  41. .setAttribute('cont-id', this.id);
  42.  
  43. htmlTemplate.appendChild(span);
  44. htmlTemplate.appendChild(checkbox);
  45.  
  46. this.event(checkbox, 'change');
  47.  
  48. return htmlTemplate;
  49. };
  50.  
  51. Task.prototype.complete = function(event) {
  52. var isChecked = event.target.checked;
  53. var dataId = event.target.getAttribute('cont-id');
  54. var el = document.querySelector('[data-id="' + dataId + '"]');
  55.  
  56. if (isChecked) {
  57. el.classList.add('complete');
  58. } else {
  59. el.classList.remove('complete');
  60. }
  61.  
  62. };
  63.  
  64. Task.prototype.event = function(el, event) {
  65. el.addEventListener(event, this.complete.bind(this));
  66. };
  67.  
  68. function RegularTask(title, id) {
  69. Task.call(this, title, id);
  70. }
  71.  
  72. RegularTask.prototype = Object.create(Task.prototype);
  73.  
  74. function UrgentTask(title, id) {
  75. Task.call(this, title, id, 'urgent');
  76. };
  77.  
  78. UrgentTask.prototype = Object.create(Task.prototype);
  79.  
  80. function TaskManager() {
  81. this.id = 0;
  82. }
  83.  
  84. TaskManager.prototype.getId = function() {
  85. return this.id++;
  86. };
  87.  
  88. TaskManager.prototype.add = function(title) {
  89. var taskId = this.getId()
  90. var task = new RegularTask(title, taskId);
  91. var html = task.template();
  92. this.render(task.typeTask, html);
  93. };
  94.  
  95. TaskManager.prototype.addUrgent = function(title) {
  96. var taskId = this.getId();
  97. var task = new UrgentTask(title, taskId);
  98. var html = task.template();
  99. this.render(task.typeTask, html);
  100. };
  101.  
  102. TaskManager.prototype.render = function(type, html) {
  103. var taskcont = document.querySelector('.tasks-cont');
  104. if (type === 'regular') {
  105. taskcont.appendChild(html);
  106. }else {
  107. taskcont.insertBefore(html, taskcont.childNodes[0]);
  108. }
  109. };
  110.  
  111. var myApp = new App('js Tasks');
  112. myApp.render();
  113.  
  114. myTm = new TaskManager();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement