define(["sugar-web/graphics/palette", "sugar-web/env", "webL10n"], function (palette, env, webL10n) { 'use strict'; var presencepalette = {}; var users = []; env.getEnvironment(function(err, environment) { var defaultLanguage = (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18n.getUILanguage() : navigator.language; var language = environment.user ? environment.user.language : defaultLanguage; webL10n.language.code = language; }); presencepalette.PresencePalette = function (invoker, primaryText, presence) { palette.Palette.call(this, invoker, primaryText); this.users = users; this.presence = presence; this.sharedEvent = document.createEvent("CustomEvent"); this.sharedEvent.initCustomEvent('shared', true, true, {}); var div = document.createElement('div'); var txt = document.createElement('span'); txt.innerHTML = "Private"; txt.className = 'network-text'; var hr = document.createElement('hr'); var privatebutton = document.createElement('button'); privatebutton.className = 'toolbutton'; privatebutton.setAttribute('id', 'private-button'); privatebutton.onclick = function () { that.setShared(false); }; var sharedbutton = document.createElement('button'); sharedbutton.className = 'toolbutton'; sharedbutton.setAttribute('id', 'shared-button'); sharedbutton.onclick = function () { that.setShared(true); }; this.setShared = function (state) { if (state) { txt.innerHTML = "My neighborhood"; privatebutton.disabled = true; sharedbutton.disabled = true; invoker.style.backgroundImage = 'url(lib/sugar-web/graphics/icons/actions/zoom-neighborhood.svg)'; that.getPalette().childNodes[0].style.backgroundImage = 'url(lib/sugar-web/graphics/icons/actions/zoom-neighborhood.svg)'; that.getPalette().dispatchEvent(that.sharedEvent); } else { txt.innerHTML = "Private"; privatebutton.disabled = false; sharedbutton.disabled = false; } }; div.appendChild(txt); div.appendChild(hr); div.appendChild(privatebutton); div.appendChild(sharedbutton); var usersDiv = document.createElement('div'); usersDiv.setAttribute("id", "presence-users"); div.appendChild(usersDiv); this.setContent([div]); // Pop-down the palette when a item in the menu is clicked. this.buttons = div.querySelectorAll('button'); var that = this; function popDownOnButtonClick(event) { that.popDown(); } for (var i = 0; i < this.buttons.length; i++) { if (this.buttons[i].id == "shared-button") this.buttons[i].addEventListener('shared', popDownOnButtonClick); } }; var addEventListener = function (type, listener, useCapture) { return this.getPalette().addEventListener(type, listener, useCapture); }; presencepalette.PresencePalette.prototype = Object.create(palette.Palette.prototype, { addEventListener: { value: addEventListener, enumerable: true, configurable: true, writable: true } }); presencepalette.PresencePalette.prototype.setShared = function (state) { this.setShared(state); }; var xoLogo = ']>'; function generateXOLogoWithColor(color) { var coloredLogo = xoLogo; coloredLogo = coloredLogo.replace("#010101", color.stroke); coloredLogo = coloredLogo.replace("#FFFFFF", color.fill); return "data:image/svg+xml;base64," + btoa(coloredLogo); } function displayUsers(users) { if (onUsersListChangedCallback != null) { onUsersListChangedCallback(users); } var presenceUsersDiv = document.getElementById("presence-users"); var html = "
"; presenceUsersDiv.innerHTML = html } function findUsersInsideAllActivities(activity, users) { var realUsers = []; for (var i = 0; i < activity.users.length; i++) { for (var j = 0; j < users.length; j++) { if (users[j].networkId === activity.users[i]) { realUsers.push(users[j]); } } } return realUsers; } var tmpDiv = null; var tmpTimeout = null; function showQuickModal(text) { if (tmpDiv !== null) { tmpDiv.parentElement.removeChild(tmpDiv); if (tmpTimeout !== null) { clearTimeout(tmpTimeout) } } var div = document.createElement('div'); tmpDiv = div; div.style.background = "#000"; div.style.color = "#fff"; div.style.bottom = "5px"; div.style.padding = "15px"; div.style.right = "5px"; div.style.position = "absolute"; div.innerHTML = text; document.body.appendChild(div); tmpTimeout = setTimeout(function () { tmpTimeout = null; div.parentElement.removeChild(div); tmpDiv = null; }, 3000); } presencepalette.PresencePalette.prototype.onSharedActivityUserChanged = function (msg) { var userName = msg.user.name.replace('<', '<').replace('>', '>'); var html = ""; if (msg.move === 1) { showQuickModal(html + webL10n.get("PlayerJoin",{user: userName})) } if (msg.move === -1) { showQuickModal(html + webL10n.get("PlayerLeave",{user: userName})) } var that = this; that.presence.listUsers(function (users) { that.presence.listSharedActivities(function (activities) { for (var i = 0; i < activities.length; i++) { if (activities[i].id === that.presence.sharedInfo.id) { var activity = activities[i]; displayUsers(findUsersInsideAllActivities(activity, users)); break; } } }); }) }; var onUsersListChangedCallback = null; presencepalette.PresencePalette.prototype.onUsersListChanged = function (callback) { onUsersListChangedCallback = callback; }; return presencepalette; });