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.

196 lines
8.0 KiB

  1. define(["sugar-web/graphics/palette", "sugar-web/env", "webL10n"], function (palette, env, webL10n) {
  2. 'use strict';
  3. var presencepalette = {};
  4. var users = [];
  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. webL10n.language.code = language;
  9. });
  10. presencepalette.PresencePalette = function (invoker, primaryText, presence) {
  11. palette.Palette.call(this, invoker, primaryText);
  12. this.users = users;
  13. this.presence = presence;
  14. this.sharedEvent = document.createEvent("CustomEvent");
  15. this.sharedEvent.initCustomEvent('shared', true, true, {});
  16. var div = document.createElement('div');
  17. var txt = document.createElement('span');
  18. txt.innerHTML = "Private";
  19. txt.className = 'network-text';
  20. var hr = document.createElement('hr');
  21. var privatebutton = document.createElement('button');
  22. privatebutton.className = 'toolbutton';
  23. privatebutton.setAttribute('id', 'private-button');
  24. privatebutton.onclick = function () {
  25. that.setShared(false);
  26. };
  27. var sharedbutton = document.createElement('button');
  28. sharedbutton.className = 'toolbutton';
  29. sharedbutton.setAttribute('id', 'shared-button');
  30. sharedbutton.onclick = function () {
  31. that.setShared(true);
  32. };
  33. this.setShared = function (state) {
  34. if (state) {
  35. txt.innerHTML = "My neighborhood";
  36. privatebutton.disabled = true;
  37. sharedbutton.disabled = true;
  38. invoker.style.backgroundImage = 'url(lib/sugar-web/graphics/icons/actions/zoom-neighborhood.svg)';
  39. that.getPalette().childNodes[0].style.backgroundImage = 'url(lib/sugar-web/graphics/icons/actions/zoom-neighborhood.svg)';
  40. that.getPalette().dispatchEvent(that.sharedEvent);
  41. } else {
  42. txt.innerHTML = "Private";
  43. privatebutton.disabled = false;
  44. sharedbutton.disabled = false;
  45. }
  46. };
  47. div.appendChild(txt);
  48. div.appendChild(hr);
  49. div.appendChild(privatebutton);
  50. div.appendChild(sharedbutton);
  51. var usersDiv = document.createElement('div');
  52. usersDiv.setAttribute("id", "presence-users");
  53. div.appendChild(usersDiv);
  54. this.setContent([div]);
  55. // Pop-down the palette when a item in the menu is clicked.
  56. this.buttons = div.querySelectorAll('button');
  57. var that = this;
  58. function popDownOnButtonClick(event) {
  59. that.popDown();
  60. }
  61. for (var i = 0; i < this.buttons.length; i++) {
  62. if (this.buttons[i].id == "shared-button")
  63. this.buttons[i].addEventListener('shared', popDownOnButtonClick);
  64. }
  65. };
  66. var addEventListener = function (type, listener, useCapture) {
  67. return this.getPalette().addEventListener(type, listener, useCapture);
  68. };
  69. presencepalette.PresencePalette.prototype =
  70. Object.create(palette.Palette.prototype, {
  71. addEventListener: {
  72. value: addEventListener,
  73. enumerable: true,
  74. configurable: true,
  75. writable: true
  76. }
  77. });
  78. presencepalette.PresencePalette.prototype.setShared = function (state) {
  79. this.setShared(state);
  80. };
  81. var xoLogo = '<?xml version="1.0" ?><!DOCTYPE svg PUBLIC \'-//W3C//DTD SVG 1.1//EN\' \'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\' [<!ENTITY stroke_color "#010101"><!ENTITY fill_color "#FFFFFF">]><svg enable-background="new 0 0 55 55" height="55px" version="1.1" viewBox="0 0 55 55" width="55px" x="0px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" y="0px"><g display="block" id="stock-xo_1_"><path d="M33.233,35.1l10.102,10.1c0.752,0.75,1.217,1.783,1.217,2.932 c0,2.287-1.855,4.143-4.146,4.143c-1.145,0-2.178-0.463-2.932-1.211L27.372,40.961l-10.1,10.1c-0.75,0.75-1.787,1.211-2.934,1.211 c-2.284,0-4.143-1.854-4.143-4.141c0-1.146,0.465-2.184,1.212-2.934l10.104-10.102L11.409,24.995 c-0.747-0.748-1.212-1.785-1.212-2.93c0-2.289,1.854-4.146,4.146-4.146c1.143,0,2.18,0.465,2.93,1.214l10.099,10.102l10.102-10.103 c0.754-0.749,1.787-1.214,2.934-1.214c2.289,0,4.146,1.856,4.146,4.145c0,1.146-0.467,2.18-1.217,2.932L33.233,35.1z" fill="&fill_color;" stroke="&stroke_color;" stroke-width="3.5"/><circle cx="27.371" cy="10.849" fill="&fill_color;" r="8.122" stroke="&stroke_color;" stroke-width="3.5"/></g></svg>';
  82. function generateXOLogoWithColor(color) {
  83. var coloredLogo = xoLogo;
  84. coloredLogo = coloredLogo.replace("#010101", color.stroke);
  85. coloredLogo = coloredLogo.replace("#FFFFFF", color.fill);
  86. return "data:image/svg+xml;base64," + btoa(coloredLogo);
  87. }
  88. function displayUsers(users) {
  89. if (onUsersListChangedCallback != null) {
  90. onUsersListChangedCallback(users);
  91. }
  92. var presenceUsersDiv = document.getElementById("presence-users");
  93. var html = "<hr><ul style='list-style: none; padding:0;'>";
  94. for (var key in users) {
  95. html += "<li><img style='height:30px;vertical-align:middle;' src='" + generateXOLogoWithColor(users[key].colorvalue) + "'>" + users[key].name + "</li>";
  96. }
  97. html += "</ul>";
  98. presenceUsersDiv.innerHTML = html
  99. }
  100. function findUsersInsideAllActivities(activity, users) {
  101. var realUsers = [];
  102. for (var i = 0; i < activity.users.length; i++) {
  103. for (var j = 0; j < users.length; j++) {
  104. if (users[j].networkId === activity.users[i]) {
  105. realUsers.push(users[j]);
  106. }
  107. }
  108. }
  109. return realUsers;
  110. }
  111. var tmpDiv = null;
  112. var tmpTimeout = null;
  113. function showQuickModal(text) {
  114. if (tmpDiv !== null) {
  115. tmpDiv.parentElement.removeChild(tmpDiv);
  116. if (tmpTimeout !== null) {
  117. clearTimeout(tmpTimeout)
  118. }
  119. }
  120. var div = document.createElement('div');
  121. tmpDiv = div;
  122. div.style.background = "#000";
  123. div.style.color = "#fff";
  124. div.style.bottom = "5px";
  125. div.style.padding = "15px";
  126. div.style.right = "5px";
  127. div.style.position = "absolute";
  128. div.innerHTML = text;
  129. document.body.appendChild(div);
  130. tmpTimeout = setTimeout(function () {
  131. tmpTimeout = null;
  132. div.parentElement.removeChild(div);
  133. tmpDiv = null;
  134. }, 3000);
  135. }
  136. presencepalette.PresencePalette.prototype.onSharedActivityUserChanged = function (msg) {
  137. var userName = msg.user.name.replace('<', '&lt;').replace('>', '&gt;');
  138. var html = "<img style='height:30px;vertical-align:middle;' src='" + generateXOLogoWithColor(msg.user.colorvalue) + "'>";
  139. if (msg.move === 1) {
  140. showQuickModal(html + webL10n.get("PlayerJoin",{user: userName}))
  141. }
  142. if (msg.move === -1) {
  143. showQuickModal(html + webL10n.get("PlayerLeave",{user: userName}))
  144. }
  145. var that = this;
  146. that.presence.listUsers(function (users) {
  147. that.presence.listSharedActivities(function (activities) {
  148. for (var i = 0; i < activities.length; i++) {
  149. if (activities[i].id === that.presence.sharedInfo.id) {
  150. var activity = activities[i];
  151. displayUsers(findUsersInsideAllActivities(activity, users));
  152. break;
  153. }
  154. }
  155. });
  156. })
  157. };
  158. var onUsersListChangedCallback = null;
  159. presencepalette.PresencePalette.prototype.onUsersListChanged = function (callback) {
  160. onUsersListChangedCallback = callback;
  161. };
  162. return presencepalette;
  163. });