not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
6.1 KiB

  1. define(["sugar-web/activity/activity","webL10n","sugar-web/datastore","activity/model","activity/view","activity/controller"], function (activity, webL10n, datastore, model, view, controller) {
  2. // Manipulate the DOM only when it is ready.
  3. requirejs(['domReady!'], function (doc) {
  4. var todo;
  5. // Initialize the activity.
  6. activity.setup();
  7. document.getElementById("new-todo").placeholder = l10n_s.get("new-todo");
  8. var stopButton = document.getElementById("stop-button");
  9. stopButton.addEventListener('click', function (event) {
  10. console.log("writing...");
  11. var jsonData = JSON.stringify(todo.model.items);
  12. activity.getDatastoreObject().setDataAsText(jsonData);
  13. activity.getDatastoreObject().save(function (error) {
  14. if (error === null) {
  15. console.log("write done.");
  16. }
  17. else {
  18. console.log("write failed.");
  19. }
  20. });
  21. });
  22. var draggedElement; // save the dragged element globally
  23. document.allowDrop = function(ev) {
  24. ev.preventDefault();
  25. }
  26. document.drag = function(ev) {
  27. draggedElement = ev.target;
  28. ev.dataTransfer.setData("text/plain", ev.target.id);
  29. }
  30. document.drop = function(ev) {
  31. ev.preventDefault();
  32. var draggedElementLabel = draggedElement.childNodes[0].childNodes[1];
  33. var draggedElementID = parseInt(draggedElement.dataset.id);
  34. var droppedOnElementLabel = ev.target;
  35. var droppedOnElement = ev.target.parentNode.parentNode;
  36. var droppedOnElementID = parseInt(droppedOnElement.dataset.id);
  37. // Swap the labels (visual)
  38. var tempSaveLabel = droppedOnElementLabel.innerText;
  39. droppedOnElementLabel.innerText = draggedElementLabel.innerText;
  40. draggedElementLabel.innerText = tempSaveLabel;
  41. // Swap the complete and checked boxes (visual)
  42. // swap complete attribute
  43. var tempSaveCompleteVisual = droppedOnElement.className;
  44. droppedOnElement.className = draggedElement.className;
  45. draggedElement.className = tempSaveCompleteVisual;
  46. // swap check box (visual)
  47. if (droppedOnElement.childNodes[0].childNodes[0].hasAttribute("checked")) {
  48. if (! draggedElement.childNodes[0].childNodes[0].hasAttribute("checked")) {
  49. // swap: uncheck droppedOnElement and check draggedElement
  50. droppedOnElement.childNodes[0].childNodes[0].removeAttribute("checked");
  51. draggedElement.childNodes[0].childNodes[0].setAttribute("checked", null);
  52. } // else do nothing
  53. }
  54. else if (draggedElement.childNodes[0].childNodes[0].hasAttribute("checked")) {
  55. if (! droppedOnElement.childNodes[0].childNodes[0].hasAttribute("checked")) {
  56. // swap: uncheck draggedOnElement and check droppedOnElement
  57. draggedElement.childNodes[0].childNodes[0].removeAttribute("checked");
  58. droppedOnElement.childNodes[0].childNodes[0].setAttribute("checked", null);
  59. } // else do nothing
  60. }
  61. // Find the index of the labels in the controler and overwritte the labels and completed elements
  62. var draggedElementIndex = todo.controller.model.items.map(function(x) { return x.id; }).indexOf(draggedElementID);
  63. var droppedOnElementIndex = todo.controller.model.items.map(function(x) { return x.id; }).indexOf(droppedOnElementID);
  64. // swap the labels in the controller
  65. todo.controller.model.items[draggedElementIndex].title = draggedElementLabel.innerText;
  66. todo.controller.model.items[droppedOnElementIndex].title = droppedOnElementLabel.innerText;
  67. // swap the completed elements in the controller
  68. var tempSaveCompleteInController = parseInt(todo.controller.model.items[droppedOnElementIndex].completed);
  69. todo.controller.model.items[droppedOnElementIndex].completed = todo.controller.model.items[draggedElementIndex].completed;
  70. todo.controller.model.items[draggedElementIndex].completed = tempSaveCompleteInController;
  71. }
  72. // Set up a brand new TODO list
  73. function Todo() {
  74. this.model = new model.Model();
  75. this.view = new view.View();
  76. this.controller = new controller.Controller(this.model, this.view);
  77. }
  78. todo = new Todo();
  79. var datastoreObject = activity.getDatastoreObject();
  80. function onLoaded(error, metadata, data) {
  81. todo.controller.loadItems(JSON.parse(data));
  82. }
  83. datastoreObject.loadAsText(onLoaded);
  84. var input = document.getElementById("new-todo");
  85. input.addEventListener('keypress', function (e) {
  86. if (e.keyCode === todo.controller.ENTER_KEY) {
  87. var success = todo.controller.addItem(e.target.value);
  88. if (success) {
  89. e.target.value = '';
  90. }
  91. }
  92. });
  93. var inputButton = document.getElementById("new-todo-button");
  94. inputButton.addEventListener('click', function (e) {
  95. var success = todo.controller.addItem(input.value);
  96. if (success) {
  97. input.value = '';
  98. }
  99. });
  100. // Find the model ID of the clicked DOM element
  101. function lookupId(target) {
  102. var lookup = target;
  103. while (lookup.nodeName !== 'LI') {
  104. lookup = lookup.parentNode;
  105. }
  106. return lookup.dataset.id;
  107. }
  108. var list = document.getElementById("todo-list");
  109. list.addEventListener('click', function (e) {
  110. var target = e.target;
  111. if (target.className.indexOf('remove') > -1) {
  112. todo.controller.removeItem(lookupId(target));
  113. }
  114. if (target.className.indexOf('toggle') > -1) {
  115. todo.controller.toggleComplete(lookupId(target), target);
  116. }
  117. });
  118. });
  119. });