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.

290 lines
10 KiB

  1. define(["sugar-web/activity/activity","sugar-web/datastore", "sugar-web/env", "webL10n"], function (activity, datastore, env, webL10n) {
  2. // Manipulate the DOM only when it is ready.
  3. requirejs(['domReady!', 'humane'], function (doc, humane) {
  4. // Initialize the activity.
  5. activity.setup();
  6. var isMobile = (/Android/i.test(navigator.userAgent) || /iPad/i.test(navigator.userAgent) || /iPhone/i.test(navigator.userAgent)) && document.location.protocol.substr(0,4) != "http";
  7. var currentCamera = 0;
  8. var history = [];
  9. env.getEnvironment(function(err, environment) {
  10. var defaultLanguage = (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18n.getUILanguage() : navigator.language;
  11. var language = environment.user ? environment.user.language : defaultLanguage;
  12. webL10n.language.code = language;
  13. });
  14. // Compute size of QR Code
  15. var headerSize = 55 + 40;
  16. var marginPercent = 20;
  17. var qrSize = document.getElementById("canvas").parentNode.offsetHeight - headerSize;
  18. qrSize -= (marginPercent*qrSize)/100;
  19. // Init QR Code object
  20. var qrCode = new QRCode("qr-code", {
  21. width: qrSize,
  22. height: qrSize,
  23. colorDark : "#000000",
  24. colorLight : "#ffffff",
  25. correctLevel : QRCode.CorrectLevel.H
  26. });
  27. var margin = (document.getElementById("canvas").parentNode.offsetWidth - qrSize) / 2;
  28. document.getElementById("qr-code").style.marginLeft = margin + "px";
  29. document.getElementById("qr-code").style.marginTop = "20px";
  30. // Generate Code
  31. var generateCode = function(text) {
  32. qrCode.clear();
  33. qrCode.makeCode(text);
  34. addToHistory(text);
  35. var text = userText.value.toLowerCase();
  36. if (text.length > 0) {
  37. document.getElementById("erasetext-button").style.visibility = "visible";
  38. } else {
  39. document.getElementById("erasetext-button").style.visibility = "hidden";
  40. }
  41. if (text.indexOf("http://") == 0 || text.indexOf("https://") == 0) {
  42. document.getElementById("user-text").classList.add("text-url");
  43. } else {
  44. document.getElementById("user-text").classList.remove("text-url");
  45. }
  46. }
  47. // Process Resize events
  48. window.addEventListener('resize', function() {
  49. var windowSize = document.body.clientHeight - headerSize;
  50. var zoom = windowSize/((qrSize*(100+marginPercent))/100);
  51. document.getElementById("qr-code").style.zoom = zoom;
  52. var useragent = navigator.userAgent.toLowerCase();
  53. if (useragent.indexOf('chrome') == -1) {
  54. document.getElementById("qr-code").style.MozTransform = "scale("+zoom+")";
  55. document.getElementById("qr-code").style.MozTransformOrigin = "0 0";
  56. }
  57. if (photoInitialized) {
  58. document.getElementById("outdiv").style.zoom = zoom;
  59. if (useragent.indexOf('chrome') == -1) {
  60. document.getElementById("outdiv").style.MozTransform = "scale("+zoom+")";
  61. document.getElementById("outdiv").style.MozTransformOrigin = "0 0";
  62. }
  63. }
  64. });
  65. // Get settings
  66. var userText = document.getElementById("user-text");
  67. userText.addEventListener('keyup', function() {
  68. var text = userText.value.toLowerCase();
  69. if (text.length > 0) {
  70. document.getElementById("erasetext-button").style.visibility = "visible";
  71. } else {
  72. document.getElementById("erasetext-button").style.visibility = "hidden";
  73. }
  74. if (text.indexOf("http://") == 0 || text.indexOf("https://") == 0) {
  75. document.getElementById("user-text").classList.add("text-url");
  76. } else {
  77. document.getElementById("user-text").classList.remove("text-url");
  78. }
  79. });
  80. userText.addEventListener('click', function() {
  81. var text = userText.value.toLowerCase();
  82. if (text.indexOf("http://") == 0 || text.indexOf("https://") == 0) {
  83. if (isMobile) {
  84. cordova.InAppBrowser.open(userText.value, '_system');
  85. } else {
  86. window.open(userText.value);
  87. }
  88. }
  89. });
  90. document.getElementById("erasetext-button").addEventListener('click', function() {
  91. userText.value = "";
  92. userText.focus();
  93. document.getElementById("erasetext-button").style.visibility = "hidden";
  94. document.getElementById("user-text").classList.remove("text-url");
  95. });
  96. // Handle text change
  97. document.getElementById("generate-button").addEventListener('click', function() {
  98. generateCode(userText.value);
  99. });
  100. // Handle text scan
  101. var codeScanned = function(code) {
  102. document.getElementById("outdiv").style.visibility = "hidden";
  103. document.getElementById("video-stream").style.visibility = "hidden";
  104. document.getElementById("qr-code").style.visibility = "visible";
  105. document.getElementById("loading-spinner").style.visibility = "hidden";
  106. document.getElementById("photo-button").classList.remove("active");
  107. userText.value = code;
  108. generateCode(code);
  109. };
  110. // Export as PNG image
  111. document.getElementById("png-button").addEventListener('click', function() {
  112. var mimetype = 'image/png';
  113. var inputData = document.getElementById("qr-code").childNodes[1].src;
  114. var metadata = {
  115. mimetype: mimetype,
  116. title: "Image QR Code",
  117. activity: "org.olpcfrance.MediaViewerActivity",
  118. timestamp: new Date().getTime(),
  119. creation_time: new Date().getTime(),
  120. file_size: 0
  121. };
  122. datastore.create(metadata, function() {
  123. humane.log(webL10n.get('QRCodeSaved'))
  124. console.log("export done.")
  125. }, inputData);
  126. });
  127. // Full screen
  128. document.getElementById("fullscreen-button").addEventListener('click', function() {
  129. if (document.getElementById("photo-button").classList.contains('active')) {
  130. return;
  131. }
  132. document.getElementById("main-toolbar").style.opacity = 0;
  133. document.getElementById("input-box").style.opacity = 0;
  134. document.getElementById("canvas").style.top = "0px";
  135. document.getElementById("unfullscreen-button").style.visibility = "visible";
  136. });
  137. document.getElementById("unfullscreen-button").addEventListener('click', function() {
  138. document.getElementById("main-toolbar").style.opacity = 1;
  139. document.getElementById("input-box").style.opacity = 1;
  140. document.getElementById("canvas").style.top = "55px";
  141. document.getElementById("unfullscreen-button").style.visibility = "hidden";
  142. });
  143. // Capture photo
  144. var photoButton = document.getElementById("photo-button");
  145. var photoInitialized = false;
  146. var oldUserText = "";
  147. photoButton.addEventListener('click', function() {
  148. // Handle mobile capture
  149. if (isMobile) {
  150. document.getElementById("main-toolbar").style.opacity = 0;
  151. document.getElementById("canvas").style.opacity = 0;
  152. document.getElementById("close-button").style.visibility = "visible";
  153. document.getElementById("switchcamera-button").style.visibility = "visible";
  154. QRScanner.prepare(function(err, status) {
  155. if (err) {
  156. console.log("Error "+err);
  157. } else {
  158. console.log(status);
  159. QRScanner.scan(function(err, code) {
  160. if (err) {
  161. console.log("Error "+err);
  162. } else {
  163. userText.value = code;
  164. generateCode(code);
  165. }
  166. QRScanner.cancelScan(function(status){});
  167. document.getElementById("main-toolbar").style.opacity = 1;
  168. document.getElementById("canvas").style.opacity = 1;
  169. document.getElementById("close-button").style.visibility = "hidden";
  170. document.getElementById("switchcamera-button").style.visibility = "hidden";
  171. });
  172. QRScanner.show(function(status) {});
  173. }
  174. });
  175. return;
  176. }
  177. // Handle HTML5 capture
  178. var outdiv = document.getElementById("outdiv");
  179. var videostream = document.getElementById("video-stream");
  180. if (!photoButton.classList.contains('active')) {
  181. document.getElementById("qr-code").style.visibility = "hidden";
  182. document.getElementById("loading-spinner").style.visibility = "visible";
  183. oldUserText = userText.value;
  184. userText.value = "";
  185. if (!photoInitialized) {
  186. outdiv.innerHTML = '<video id="video-window" autoplay></video>';
  187. load(qrSize, margin, codeScanned);
  188. photoInitialized = true;
  189. } else {
  190. outdiv.style.visibility = "visible";
  191. videostream.style.visibility = "visible";
  192. }
  193. } else {
  194. outdiv.style.visibility = "hidden";
  195. videostream.style.visibility = "hidden";
  196. document.getElementById("loading-spinner").style.visibility = "hidden";
  197. document.getElementById("qr-code").style.visibility = "visible";
  198. userText.value = oldUserText;
  199. generateCode(userText.value);
  200. }
  201. photoButton.classList.toggle("active");
  202. });
  203. // Mobile buttons: Cancel capture, Switch camera
  204. document.getElementById("close-button").addEventListener('click', function (event) {
  205. if (isMobile) {
  206. QRScanner.cancelScan(function(status){});
  207. }
  208. });
  209. document.getElementById("switchcamera-button").addEventListener('click', function (event) {
  210. if (isMobile) {
  211. currentCamera = (currentCamera + 1) % 2;
  212. QRScanner.useCamera(currentCamera, function(err, status){});
  213. }
  214. });
  215. // Add entry to history
  216. function addToHistory(text) {
  217. if(!history.includes(text)){
  218. history.push(text);
  219. updateHistory();
  220. }
  221. }
  222. // Update dropdown with user history
  223. function updateHistory() {
  224. var mhtml = '';
  225. var index = history.length;
  226. while(index--) {
  227. mhtml += '<option value="'+(history[index])+'">'+(history[index])+'</option>';
  228. }
  229. document.getElementById("qrtextdropdown").innerHTML = mhtml;
  230. }
  231. // QR history dropdown change
  232. document.getElementById("qrtextdropdown").addEventListener('change', function () {
  233. document.getElementById("user-text").value = document.getElementById("qrtextdropdown").value;
  234. generateCode(document.getElementById("qrtextdropdown").value);
  235. });
  236. // Handle graph save/world
  237. var stopButton = document.getElementById("stop-button");
  238. stopButton.addEventListener('click', function (event) {
  239. console.log("writing...");
  240. var data = { userText: (photoButton.classList.contains('active') ? oldUserText : userText.value), uHistory: history };
  241. var jsonData = JSON.stringify(data);
  242. activity.getDatastoreObject().setDataAsText(jsonData);
  243. activity.getDatastoreObject().save(function (error) {
  244. if (error === null) {
  245. console.log("write done.");
  246. } else {
  247. console.log("write failed.");
  248. }
  249. });
  250. });
  251. // Initialize code
  252. env.getEnvironment(function(err, environment) {
  253. if (!environment.objectId) {
  254. console.log("New instance");
  255. userText.value = environment.user.name;
  256. generateCode(environment.user.name);
  257. } else {
  258. activity.getDatastoreObject().loadAsText(function(error, metadata, data) {
  259. if (error==null && data!=null) {
  260. data = JSON.parse(data);
  261. userText.value = data.userText;
  262. history = data.uHistory;
  263. generateCode(data.userText);
  264. }
  265. });
  266. }
  267. });
  268. });
  269. });