diff --git a/activity/activity.js b/activity/activity.js index 859ad10..1463fb2 100644 --- a/activity/activity.js +++ b/activity/activity.js @@ -1,10 +1,11 @@ define(["webL10n", - "sugar-web/activity/shortcut", - "sugar-web/bus", - "sugar-web/env", - "sugar-web/datastore", - "sugar-web/graphics/icon", - "sugar-web/graphics/activitypalette"], function ( + "sugar-web/activity/shortcut", + "sugar-web/bus", + "sugar-web/env", + "sugar-web/datastore", + "sugar-web/graphics/icon", + "sugar-web/graphics/activitypalette" +], function ( l10n, shortcut, bus, env, datastore, icon, activitypalette) { 'use strict'; @@ -21,7 +22,8 @@ define(["webL10n", function sendPauseEvent() { var pauseEvent = document.createEvent("CustomEvent"); pauseEvent.initCustomEvent('activityPause', false, false, { - 'cancelable': true}); + 'cancelable': true + }); window.dispatchEvent(pauseEvent); } bus.onNotification("activity.pause", sendPauseEvent); @@ -29,15 +31,22 @@ define(["webL10n", // An activity that handles 'activityStop' can also call // event.preventDefault() to prevent the close, and explicitly // call activity.close() after storing. - function sendStopEvent() { var stopEvent = document.createEvent("CustomEvent"); stopEvent.initCustomEvent('activityStop', false, false, { - 'cancelable': true}); - + 'cancelable': true + }); var result = window.dispatchEvent(stopEvent); if (result) { - activity.close(); + if (env.isSugarizer()) { + datastoreObject.save(function () { + datastore.waitPendingSave(function () { + activity.close(); + }); + }); + } else { + activity.close(); + } } } bus.onNotification("activity.stop", sendStopEvent); @@ -49,14 +58,6 @@ define(["webL10n", var activityPalette = new activitypalette.ActivityPalette( activityButton, datastoreObject); - // Colorize the activity icon. - activity.getXOColor(function (error, colors) { - icon.colorize(activityButton, colors); - var invokerElem = - document.querySelector("#activity-palette .palette-invoker"); - icon.colorize(invokerElem, colors); - }); - // Make the activity stop with the stop button. var stopButton = document.getElementById("stop-button"); stopButton.addEventListener('click', function (e) { @@ -66,19 +67,65 @@ define(["webL10n", shortcut.add("Ctrl", "Q", this.close); env.getEnvironment(function (error, environment) { + var l10n = { + "en": "{{name}} Activity", + "fr": "Activité {{name}}", + "es": "Actividad {{name}}", + "pt": "{{name}} Atividade", + "de": "Aktivität {{name}}" + }; if (!environment.objectId) { datastoreObject.setMetadata({ - "title": environment.activityName + " Activity", + "title": env.isSugarizer() ? + (l10n[environment.user.language] || l10n["en"]).replace("{{name}}", environment.activityName) : environment.activityName + " Activity", "title_set_by_user": "0", "activity": environment.bundleId, "activity_id": environment.activityId }); } + datastoreObject.save(function () { datastoreObject.getMetadata(function (error, metadata) { activityPalette.setTitleDescription(metadata); }); }); + if (environment.standAlone) { + document.getElementById("stop-button").style.visibility = "hidden"; + }; + }); + + // Colorize the activity icon. + activity.getXOColor(function (error, colors) { + icon.colorize(activityButton, colors); + var invokerElem = + document.querySelector("#activity-palette .palette-invoker"); + icon.colorize(invokerElem, colors); + + if (env.isSugarizer()) { + var buddyIcon = '\ + \ + \ + \ + \ + \ + '; + document.title = document.title + " - Sugarizer"; + var newicon = buddyIcon.replace(new RegExp("&fill_color;", "g"), colors.fill).replace(new RegExp("&stroke_color;", "g"), colors.stroke); + var svg_xml = (new XMLSerializer()).serializeToString((new DOMParser()).parseFromString(newicon, "text/xml")); + var canvas = document.createElement('canvas'); + canvas.width = canvas.height = 16; + var ctx = canvas.getContext('2d'); + var img = new Image(); + img.src = "data:image/svg+xml;base64," + btoa(svg_xml); + img.onload = function () { + ctx.drawImage(img, 0, 0); + var link = document.querySelector("link[rel*='icon']") || document.createElement('link'); + link.type = 'image/x-icon'; + link.rel = 'shortcut icon'; + link.href = canvas.toDataURL("image/x-icon"); + document.getElementsByTagName('head')[0].appendChild(link); + } + } }); }; @@ -160,7 +207,7 @@ define(["webL10n", }; activity.showConfirmationAlert = function (title, message, btnOkLabel, - btnCancelLabel, btnCallback) { + btnCancelLabel, btnCallback) { /* title, message, btnOkLabel and btCancelLabel are str parameters callback is a function called when the user press a button, @@ -215,7 +262,7 @@ define(["webL10n", }; - activity.hideAlert = function() { + activity.hideAlert = function () { var alertNode = document.getElementById("activity-alert"); if (alertNode && alertNode.parentNode) { alertNode.parentNode.removeChild(alertNode); diff --git a/env.js b/env.js index 32d02a1..db2342d 100644 --- a/env.js +++ b/env.js @@ -8,7 +8,33 @@ define(function () { // FIXME: we assume this code runs on the same thread as the // javascript executed from sugar-toolkit-gtk3 (python) - if (env.isStandalone()) { + if (env.isSugarizer()) { + var getUrlParameter = function(name) { + var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search); + return match && decodeURIComponent(match[1].replace(/\+/g, ' ')); + }; + window.top.sugar = {}; + window.top.sugar.environment = { + activityId: getUrlParameter("aid"), + activityName: getUrlParameter("n"), + bundleId: getUrlParameter("a"), + objectId: getUrlParameter("o"), + sharedId: getUrlParameter("s"), + help: getUrlParameter("h"), + standAlone: getUrlParameter("sa") + }; + if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) { + chrome.storage.local.get('sugar_settings', function(values) { + window.top.sugar.environment.user = JSON.parse(values.sugar_settings); + callback(null, window.top.sugar.environment); + }); + } else { + window.top.sugar.environment.user = JSON.parse(localStorage.sugar_settings); + setTimeout(function () { + callback(null, window.top.sugar.environment); + }, 0); + } + } else if (env.isStandalone()) { setTimeout(function () { callback(null, {}); }, 0); @@ -40,15 +66,42 @@ define(function () { return window.location.protocol; }; + env.getHost = function() { + return window.location.hostname; + }; + env.isStandalone = function () { - var webActivityURLScheme = "activity:"; + var webActivityURLScheme = "activity:"; + var fileURLScheme = "file:"; var currentURLScheme = env.getURLScheme(); // the control of hostname !== '0.0.0.0' is used // for compatibility with F18 and webkit1 return currentURLScheme !== webActivityURLScheme && - window.location.hostname !== '0.0.0.0'; + currentURLScheme !== fileURLScheme && + window.location.hostname !== '0.0.0.0'; }; + env.isSugarizer = function() { + // HACK: If in Chrome App automatic deduction that in Sugarizer + if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) { + return true; + } else if (typeof(Storage)!=="undefined" && typeof(window.localStorage)!=="undefined") { + try { + return (window.localStorage.getItem('sugar_settings') !== null); + } catch(err) { + return false; + } + } + return false; + }; + + env.isSugarizerOS = function() { + if (typeof window.sugarizerOS != 'undefined') + return true; + return false; + } + return env; }); +