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.

238 lines
8.8 KiB

  1. define(["sugar-web/activity/activity","sugar-web/datastore","sugar-web/env","textpalette","sugar-web/graphics/menupalette","sugar-web/graphics/journalchooser","lzstring","webL10n","toon"], function (activity, datastore, env, textpalette, menupalette, journalchooser, lzstring, l10n, toon) {
  2. // initialize canvas size
  3. var sugarCellSize = 75;
  4. var sugarSubCellSize = 15;
  5. env.getEnvironment(function(err, environment) {
  6. var defaultLanguage = (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18n.getUILanguage() : navigator.language;
  7. var language = environment.user ? environment.user.language : defaultLanguage;
  8. l10n.language.code = language;
  9. console.log('LANG ' + language);
  10. });
  11. function _(text) {
  12. // this function add a fallback for the case of translation not found
  13. // can be removed when we find how to read the localization.ini
  14. // file in the case of local html file opened in the browser
  15. translation = l10n.get(text);
  16. if (translation == '') {
  17. translation = text;
  18. };
  19. return translation;
  20. };
  21. // Manipulate the DOM only when it is ready.
  22. requirejs(['domReady!'], function (doc) {
  23. // Initialize the activity.
  24. activity.setup();
  25. // HERE GO YOUR CODE
  26. var initialData = {"version": "1", "boxs": [{'globes':[]}]};
  27. var mainCanvas = document.getElementById("mainCanvas");
  28. var sortCanvas = document.getElementById("sortCanvas");
  29. // remove 5 more to be sure no scrollbars are visible
  30. mainCanvas.height = window.innerHeight - sugarCellSize - 5;
  31. mainCanvas.width = mainCanvas.height * 4 / 3;
  32. mainCanvas.style.left = ((window.innerWidth - mainCanvas.width) / 2) + "px";
  33. var previousButton = document.getElementById("previous-button");
  34. previousButton.title = _("Previous");
  35. previousButton.addEventListener('click', function (e) {
  36. toonModel.showPreviousBox();
  37. });
  38. var nextButton = document.getElementById("next-button");
  39. nextButton.title = _("Next");
  40. nextButton.addEventListener('click', function (e) {
  41. toonModel.showNextBox();
  42. });
  43. var textButton = document.getElementById("text-button");
  44. textButton.title = _('EditText');
  45. var tp = new textpalette.TextPalette(textButton, toonModel,
  46. _('SetGlobeText'));
  47. // page counter
  48. var pageCounter = document.getElementById("page-counter");
  49. var toonModel = new toon.Model(initialData, mainCanvas, tp);
  50. toonModel.init();
  51. toonModel.attachPageCounterViewer(pageCounter);
  52. toonModel.attachPrevNextButtons(previousButton, nextButton);
  53. var editMode = true;
  54. var addGlobeButton = document.getElementById("add-globe");
  55. addGlobeButton.title = _('AddAglobe');
  56. var menuData = [{'icon': true, 'id': toon.TYPE_GLOBE,
  57. 'label': _('Globe')},
  58. {'icon': true, 'id': toon.TYPE_EXCLAMATION,
  59. 'label': _('Exclamation')},
  60. {'icon': true, 'id': toon.TYPE_WHISPER,
  61. 'label': _('Whisper')},
  62. {'icon': true, 'id': toon.TYPE_CLOUD,
  63. 'label': _('Think')},
  64. {'icon': true, 'id': toon.TYPE_RECTANGLE,
  65. 'label': _('Box')},];
  66. var mp = new menupalette.MenuPalette(addGlobeButton,
  67. _("AddAglobe"), menuData);
  68. for (var i = 0; i < mp.buttons.length; i++) {
  69. mp.buttons[i].addEventListener('click', function(e) {
  70. toonModel.addGlobe(this.id);
  71. });
  72. };
  73. var addButton = document.getElementById("add-button");
  74. addButton.title = _("Add");
  75. addButton.addEventListener('click', function (e) {
  76. journalchooser.show(function (entry) {
  77. // No selection
  78. if (!entry) {
  79. return;
  80. }
  81. // Get object content
  82. var dataentry = new datastore.DatastoreObject(entry.objectId);
  83. dataentry.loadAsText(function (err, metadata, text) {
  84. //We load the drawing inside an image element
  85. var element = document.createElement('img');
  86. if (entry.metadata.activity == 'org.olpcfrance.PaintActivity') {
  87. element.src = lzstring.decompressFromUTF16(JSON.parse(text).src);
  88. } else {
  89. element.src = text;
  90. }
  91. element.onload = function () {
  92. toonModel.addImage(element.src);
  93. };
  94. });
  95. }, { mimetype: 'image/png' }, { mimetype: 'image/jpeg' }, { activity: 'org.olpcfrance.PaintActivity'});
  96. });
  97. // Load from datatore
  98. env.getEnvironment(function(err, environment) {
  99. if (environment.objectId) {
  100. activity.getDatastoreObject().loadAsText(function(error, metadata, JSONdata) {
  101. if (error==null && JSONdata!=null) {
  102. var data = JSON.parse(JSONdata);
  103. var dataWithoutImages = data.dataWithoutImages;
  104. var images = data.dataImages;
  105. dataWithoutImages.images = {};
  106. for(var key in images) {
  107. var imageName = key;
  108. dataWithoutImages.images[imageName] = LZString.decompressFromUTF16(images[imageName]);
  109. }
  110. toonModel.setData(dataWithoutImages);
  111. if (!editMode) {
  112. toonModel.changeToEditMode();
  113. editMode = true;
  114. };
  115. }
  116. });
  117. }
  118. });
  119. var stopButton = document.getElementById("stop-button");
  120. stopButton.addEventListener('click', function (event) {
  121. console.log("writing...");
  122. toonModel.showWait();
  123. if (!editMode) {
  124. toonModel.finishSort();
  125. toonModel.init();
  126. editMode = true;
  127. };
  128. // clone the data to remove the images
  129. var dataWithoutImages = {}
  130. dataWithoutImages['version'] = toonModel.getData()['version'];
  131. dataWithoutImages['boxs'] = toonModel.getData()['boxs'];
  132. dataImages = {};
  133. for(var key in toonModel.getData()['images']) {
  134. var imageName = key;
  135. console.log('saving image ' + imageName);
  136. dataImages[imageName] = LZString.compressToUTF16(toonModel.getData()['images'][imageName]);
  137. };
  138. var fullData = {
  139. dataWithoutImages: dataWithoutImages,
  140. dataImages: dataImages
  141. };
  142. toonModel.hideWait();
  143. var jsonData = JSON.stringify(fullData);
  144. activity.getDatastoreObject().setDataAsText(jsonData);
  145. activity.getDatastoreObject().save(function (error) {
  146. if (error === null) {
  147. console.log("write done.");
  148. } else {
  149. console.log("write failed.");
  150. }
  151. });
  152. });
  153. var saveImageButton = document.getElementById("image-save");
  154. saveImageButton.title = _("SaveAsImage");
  155. var saveImageMenuData = [{'id': '0', 'label': _('OneRow')},
  156. {'id': '1', 'label': _('OneColumn')},
  157. {'id': '2', 'label': _('TwoColumns')}];
  158. var simp = new menupalette.MenuPalette(saveImageButton,
  159. _("SaveAsImage"), saveImageMenuData);
  160. for (var i = 0; i < simp.buttons.length; i++) {
  161. simp.buttons[i].addEventListener('click', function(e) {
  162. toonModel.saveAsImage(this.id);
  163. });
  164. };
  165. var sortButton = document.getElementById("sort-button");
  166. sortButton.title = _('Sort');
  167. toonModel.attachSortButton(sortButton);
  168. sortButton.addEventListener('click', function (e) {
  169. // verify if there are at least 3 boxes
  170. // the first box is the title and can't be moved
  171. // then only have sense sort with more than 2 boxes
  172. if (toonModel.getData()['boxs'].length < 3) {
  173. return;
  174. };
  175. toonModel.showWait();
  176. if (editMode) {
  177. toonModel.initPreviews();
  178. // resize the canvas
  179. sortCanvas.width = window.innerWidth - sugarCellSize * 2;
  180. var boxWidth = sortCanvas.width / toonModel.getData()['boxs'].length;
  181. sortCanvas.height = boxWidth * 3 / 4;
  182. sortCanvas.style.left = ((window.innerWidth - sortCanvas.width) / 2) + "px";
  183. sortCanvas.style.top = ((window.innerHeight - sortCanvas.height) / 2) + "px";
  184. toonModel.initSort(sortCanvas);
  185. } else {
  186. toonModel.finishSort();
  187. toonModel.init();
  188. };
  189. toonModel.hideWait();
  190. // switch editMode
  191. editMode = ! editMode;
  192. });
  193. var cleanAllButton = document.getElementById("clean-all-button");
  194. cleanAllButton.title = _("Clean");
  195. cleanAllButton.addEventListener('click', function (e) {
  196. toonModel.setData(initialData);
  197. if (!editMode) {
  198. toonModel.changeToEditMode();
  199. editMode = true;
  200. };
  201. });
  202. });
  203. });