Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge changes from Sugarizer for env.js #134

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 69 additions & 22 deletions activity/activity.js
Original file line number Diff line number Diff line change
@@ -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",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't change identation style

"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';
Expand All @@ -21,23 +22,31 @@ define(["webL10n",
function sendPauseEvent() {
var pauseEvent = document.createEvent("CustomEvent");
pauseEvent.initCustomEvent('activityPause', false, false, {
'cancelable': true});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just indentation change, should not be updated

'cancelable': true
});
window.dispatchEvent(pauseEvent);
}
bus.onNotification("activity.pause", sendPauseEvent);

// 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still indentation change only

});
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);
Expand All @@ -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) {
Expand All @@ -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",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test in Sugar is the environnement.user.language property exist?

"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 = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\
<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" >\
<g transform="translate(37.943468,-309.4636)">\
<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">\
<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" />\
<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" />\
</g></g></svg>';
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);
}
}
});
};

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
59 changes: 56 additions & 3 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
});