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.

195 lines
8.0 KiB

  1. define(["webL10n.sugarizer",
  2. "sugar-web/activity/shortcut",
  3. "sugar-web/bus",
  4. "sugar-web/env",
  5. "sugar-web/datastore",
  6. "sugar-web/presence",
  7. "sugar-web/graphics/icon",
  8. "sugar-web/graphics/activitypalette"], function (
  9. l10n, shortcut, bus, env, datastore, presence, icon, activitypalette) {
  10. 'use strict';
  11. var datastoreObject = null;
  12. var presenceCallback = null;
  13. var presenceResponse = null;
  14. var activity = {};
  15. activity.setup = function () {
  16. bus.listen();
  17. l10n.start();
  18. function sendPauseEvent() {
  19. var pauseEvent = document.createEvent("CustomEvent");
  20. pauseEvent.initCustomEvent('activityPause', false, false, {
  21. 'cancelable': true
  22. });
  23. window.dispatchEvent(pauseEvent);
  24. }
  25. bus.onNotification("activity.pause", sendPauseEvent);
  26. // An activity that handles 'activityStop' can also call
  27. // event.preventDefault() to prevent the close, and explicitly
  28. // call activity.close() after storing.
  29. function sendStopEvent() {
  30. var stopEvent = document.createEvent("CustomEvent");
  31. stopEvent.initCustomEvent('activityStop', false, false, {
  32. 'cancelable': true
  33. });
  34. var result = window.dispatchEvent(stopEvent);
  35. if (result) {
  36. datastoreObject.save(function() {
  37. datastore.waitPendingSave(function() {
  38. activity.close();
  39. });
  40. });
  41. }
  42. }
  43. bus.onNotification("activity.stop", sendStopEvent);
  44. datastoreObject = new datastore.DatastoreObject();
  45. var activityButton = document.getElementById("activity-button");
  46. var activityPalette = new activitypalette.ActivityPalette(
  47. activityButton, datastoreObject);
  48. // Colorize the activity icon.
  49. activity.getXOColor(function (error, colors) {
  50. icon.colorize(activityButton, colors);
  51. var invokerElem =
  52. document.querySelector("#activity-palette .palette-invoker");
  53. icon.colorize(invokerElem, colors);
  54. var buddyIcon='<?xml version="1.0" encoding="UTF-8" standalone="no"?>\
  55. <svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" width="16" height="16" version="1.0" >\
  56. <g transform="translate(37.943468,-309.4636)">\
  57. <g transform="matrix(0.05011994,0,0,0.05012004,-41.76673,299.66011)" style="fill:&fill_color;;fill-opacity:1;stroke:&stroke_color;;stroke-opacity:1">\
  58. <circle transform="matrix(0.969697,0,0,0.969697,-90.879133,125.06999)" style="fill:&fill_color;;fill-opacity:1;stroke:&stroke_color;;stroke-width:20.62502098;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" cx="331.38321" cy="134.2677" r="51.220825" />\
  59. <path d="m 290.55846,302.47333 -58.81513,59.20058 -59.39461,-59.40024 c -25.19828,-24.48771 -62.7038,13.33148 -38.1941,37.98719 l 60.04451,58.9817 -59.73639,59.42563 c -24.83976,24.97559 12.91592,63.26505 37.66786,37.75282 l 59.95799,-59.28294 58.75912,59.21065 c 24.50656,25.09065 62.43116,-13.00322 37.87956,-37.85772 l -59.24184,-59.02842 58.87574,-59.14782 c 25.1689,-25.18348 -13.0489,-62.75154 -37.80271,-37.84143 z" style="fill:&fill_color;;fill-opacity:1;stroke:&stroke_color;;stroke-width:20.00002098;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" />\
  60. </g></g></svg>';
  61. document.title = document.title+" - Sugarizer";
  62. var newicon = buddyIcon.replace(new RegExp("&fill_color;","g"),colors.fill).replace(new RegExp("&stroke_color;","g"),colors.stroke);
  63. var svg_xml = (new XMLSerializer()).serializeToString((new DOMParser()).parseFromString(newicon, "text/xml"));
  64. var canvas = document.createElement('canvas');
  65. canvas.width = canvas.height = 16;
  66. var ctx = canvas.getContext('2d');
  67. var img = new Image();
  68. img.src = "data:image/svg+xml;base64," + btoa(svg_xml);
  69. img.onload = function() {
  70. ctx.drawImage(img, 0, 0);
  71. var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
  72. link.type = 'image/x-icon';
  73. link.rel = 'shortcut icon';
  74. link.href = canvas.toDataURL("image/x-icon");
  75. document.getElementsByTagName('head')[0].appendChild(link);
  76. }
  77. });
  78. // Make the activity stop with the stop button.
  79. var stopButton = document.getElementById("stop-button");
  80. stopButton.addEventListener('click', function (e) {
  81. sendStopEvent();
  82. });
  83. shortcut.add("Ctrl", "Q", this.close);
  84. env.getEnvironment(function (error, environment) {
  85. var l10n ={"en":"{{name}} Activity","fr":"Activité {{name}}","es":"Actividad {{name}}","pt":"{{name}} Atividade","de":"Aktivität {{name}}"};
  86. if (!environment.objectId) {
  87. datastoreObject.setMetadata({
  88. "title": (l10n[environment.user.language]||l10n["en"]).replace("{{name}}", environment.activityName),
  89. "title_set_by_user": "0",
  90. "activity": environment.bundleId,
  91. "activity_id": environment.activityId
  92. });
  93. }
  94. if (env.isSugarizer()) {
  95. presence.joinNetwork(function(error, presence) {
  96. if (environment.sharedId) {
  97. presence.joinSharedActivity(environment.sharedId, function() {
  98. var group_color = presence.getSharedInfo().colorvalue;
  99. icon.colorize(activityButton, group_color);
  100. datastoreObject.setMetadata({"buddy_color":group_color});
  101. datastoreObject.save(function() {});
  102. });
  103. }
  104. if (presenceCallback) {
  105. presenceCallback(error, presence);
  106. } else {
  107. presenceResponse = {error: error, presence: presence};
  108. }
  109. });
  110. }
  111. datastoreObject.save(function () {
  112. datastoreObject.getMetadata(function (error, metadata) {
  113. activityPalette.setTitleDescription(metadata);
  114. });
  115. });
  116. if (environment.standAlone) {
  117. document.getElementById("stop-button").style.visibility = "hidden";
  118. };
  119. });
  120. };
  121. activity.getDatastoreObject = function () {
  122. return datastoreObject;
  123. };
  124. activity.getPresenceObject = function(connectionCallback) {
  125. if (presenceResponse == null) {
  126. presenceCallback = connectionCallback;
  127. } else {
  128. connectionCallback(presenceResponse.error, presenceResponse.presence);
  129. presenceResponse = null;
  130. }
  131. return presence;
  132. };
  133. activity.getXOColor = function (callback) {
  134. function onResponseReceived(error, result) {
  135. if (error === null) {
  136. callback(null, {
  137. stroke: result[0][0],
  138. fill: result[0][1]
  139. });
  140. } else {
  141. callback(null, {
  142. stroke: "#00A0FF",
  143. fill: "#8BFF7A"
  144. });
  145. }
  146. }
  147. bus.sendMessage("activity.get_xo_color", [], onResponseReceived);
  148. };
  149. activity.close = function (callback) {
  150. function onResponseReceived(error, result) {
  151. if (error === null) {
  152. callback(null);
  153. } else {
  154. callback(error, null);
  155. }
  156. }
  157. bus.sendMessage("activity.close", [], onResponseReceived);
  158. };
  159. activity.showObjectChooser = function (callback) {
  160. function onResponseReceived(error, result) {
  161. if (error === null) {
  162. callback(null, result[0]);
  163. } else {
  164. callback(error, null);
  165. }
  166. }
  167. bus.sendMessage("activity.show_object_chooser", [], onResponseReceived);
  168. };
  169. return activity;
  170. });