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

Fixes #23: Updated- lib/sugar-web #24

Merged
merged 2 commits into from
Dec 22, 2024
Merged
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
610 changes: 610 additions & 0 deletions lib/mustache.js

Large diffs are not rendered by default.

161 changes: 151 additions & 10 deletions lib/sugar-web/activity/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ define(["webL10n",
"sugar-web/graphics/activitypalette"], function (
l10n, shortcut, bus, env, datastore, icon, activitypalette) {

'use strict';

var datastoreObject = null;

var activity = {};
Expand All @@ -16,34 +18,67 @@ define(["webL10n",

l10n.start();

function sendPauseEvent() {
var pauseEvent = document.createEvent("CustomEvent");
pauseEvent.initCustomEvent('activityPause', false, false, {
'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});

var result = window.dispatchEvent(stopEvent);
if (result) {
activity.close();
}
}
bus.onNotification("activity.stop", sendStopEvent);

datastoreObject = new datastore.DatastoreObject();

var activityButton = document.getElementById("activity-button");

var activityPalette = new activitypalette.ActivityPalette();
var activityPalette = new activitypalette.ActivityPalette(
activityButton, datastoreObject);

// Colorize the activity icon.
activity.getXOColor(function (error, colors) {
icon.colorize(activityButton, colors);
invokerElem =
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) {
activity.close();
sendStopEvent();
});

shortcut.add("Ctrl", "Q", this.close);

datastoreObject = new datastore.DatastoreObject();

env.getEnvironment(function (error, environment) {
datastoreObject.setMetadata({
"activity": environment.bundleId,
"activity_id": environment.activityId
if (!environment.objectId) {
datastoreObject.setMetadata({
"title": 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);
});
});
datastoreObject.save(function () {});
});
};

Expand Down Expand Up @@ -74,12 +109,118 @@ define(["webL10n",
if (error === null) {
callback(null);
} else {
console.log("activity.close called");
callback(error, null);
}
}

bus.sendMessage("activity.close", [], onResponseReceived);
};

activity.showObjectChooser = function (callback) {
function onResponseReceived(error, result) {
if (error === null) {
callback(null, result[0]);
} else {
callback(error, null);
}
}

bus.sendMessage("activity.show_object_chooser", [], onResponseReceived);
};

activity.showAlert = function (title, message, btnLabel, btnCallback) {
if (btnLabel == null) {
btnLabel = 'Ok';
}

// if there are a alert visible, hide it
activity.hideAlert();

var fragment = document.createDocumentFragment();
var div = document.createElement('div');
div.className = 'alert';
div.id = 'activity-alert';
div.innerHTML = '<p><b>' + title + '</b><br/>' + message + '</p>' +
'<p class="button-box">' +
'<button id="actvity-alert-btn" class="icon">' +
'<span class="ok"></span>' +
btnLabel + '</button></p>';
fragment.appendChild(div);

document.body.appendChild(fragment.cloneNode(true));

var btn = document.getElementById("actvity-alert-btn");
btn.addEventListener('click', function (e) {
activity.hideAlert();
if (btnCallback != null) {
btnCallback();
}
});

};

activity.showConfirmationAlert = function (title, message, btnOkLabel,
btnCancelLabel, btnCallback) {
/*
title, message, btnOkLabel and btCancelLabel are str parameters
callback is a function called when the user press a button,
and receives a boolean with true if the user pressed the Ok button
*/
if (btnOkLabel == null) {
btnOkLabel = 'Ok';
}
if (btnCancelLabel == null) {
btnCancelLabel = 'Cancel';
}

// if there are a alert visible, hide it
activity.hideAlert();

var fragment = document.createDocumentFragment();
var div = document.createElement('div');
div.className = 'alert';
div.id = 'activity-alert';

div.innerHTML = '<p><b>' + title + '</b><br/>' + message + '</p>' +
'<p class="button-box">' +
'<button id="actvity-alert-ok-btn" class="icon">' +
'<span class="ok"></span>' +
btnOkLabel + '</button>' +
'<button id="actvity-alert-cancel-btn" class="icon">' +
'<span class="cancel"></span>' +
btnCancelLabel + '</button>' +
'</p>';

fragment.appendChild(div);

document.body.appendChild(fragment.cloneNode(true));

var okBtn = document.getElementById("actvity-alert-ok-btn");
var cancelBtn = document.getElementById("actvity-alert-cancel-btn");

function hideAlertAndReply(result) {
activity.hideAlert();
if (btnCallback != null) {
btnCallback(result);
};
};

okBtn.addEventListener('click', function (e) {
hideAlertAndReply(true);
});

cancelBtn.addEventListener('click', function (e) {
hideAlertAndReply(false);
});

};

activity.hideAlert = function() {
var alertNode = document.getElementById("activity-alert");
if (alertNode && alertNode.parentNode) {
alertNode.parentNode.removeChild(alertNode);
};
};

return activity;
});
5 changes: 4 additions & 1 deletion lib/sugar-web/activity/shortcut.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
define(function () {

'use strict';

var shortcut = {};

shortcut._allShortcuts = [];
Expand Down Expand Up @@ -39,7 +42,7 @@ define(function () {
var key = String.fromCharCode(charCode).toLowerCase();

// Search for a matching shortcut
for (i = 0; i < shortcut._allShortcuts.length; i += 1) {
for (var i = 0; i < shortcut._allShortcuts.length; i += 1) {
var currentShortcut = shortcut._allShortcuts[i];

var match = currentShortcut.key == key &&
Expand Down
24 changes: 21 additions & 3 deletions lib/sugar-web/bus.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
define(["sugar-web/env"], function (env) {

'use strict';

var lastId = 0;
var callbacks = {};
var notificationCallbacks = {};
var client = null;
var inputStreams = [];

Expand All @@ -12,7 +16,7 @@ define(["sugar-web/env"], function (env) {

env.getEnvironment(function (error, environment) {
var port = environment.apiSocketPort;
var socket = new WebSocket("ws://localhost:" + port);
var socket = new WebSocket("ws://0.0.0.0:" + port);

socket.binaryType = "arraybuffer";

Expand Down Expand Up @@ -69,7 +73,7 @@ define(["sugar-web/env"], function (env) {

InputStream.prototype.read = function (count, callback) {
if (this.readCallback) {
throw Error("Read already in progress");
throw new Error("Read already in progress");
}

this.readCallback = callback;
Expand Down Expand Up @@ -144,7 +148,8 @@ define(["sugar-web/env"], function (env) {
var message = {
"method": method,
"id": lastId,
"params": params
"params": params,
"jsonrpc": "2.0"
};

if (callback) {
Expand All @@ -156,6 +161,10 @@ define(["sugar-web/env"], function (env) {
lastId++;
};

bus.onNotification = function (method, callback) {
notificationCallbacks[method] = callback;
};

bus.sendBinary = function (buffer, callback) {
client.send(buffer);
};
Expand All @@ -182,6 +191,15 @@ define(["sugar-web/env"], function (env) {

var parsed = JSON.parse(message.data);
var responseId = parsed.id;

if (parsed.method) {
var notificationCallback = notificationCallbacks[parsed.method];
if (notificationCallback !== undefined) {
notificationCallback(parsed.params);
}
return;
}

if (responseId in callbacks) {
var callback = callbacks[responseId];

Expand Down
17 changes: 12 additions & 5 deletions lib/sugar-web/datastore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
define(["sugar-web/bus", "sugar-web/env"], function (bus, env) {

'use strict';

var datastore = {};

function DatastoreObject(objectId) {
Expand All @@ -8,9 +11,9 @@ define(["sugar-web/bus", "sugar-web/env"], function (bus, env) {
this.ensureObjectId = function (callback) {
var that = this;

env.getEnvironment(function (error, environment) {
if (environment.objectId !== null) {
that.objectId = environment.objectId;
env.getObjectId(function (objectId) {
if (objectId !== null && that.objectId === undefined) {
that.objectId = objectId;
}
callback();
});
Expand Down Expand Up @@ -115,6 +118,10 @@ define(["sugar-web/bus", "sugar-web/env"], function (bus, env) {
};

DatastoreObject.prototype.save = function (callback) {
if (callback === undefined) {
callback = function () {};
}

var that = this;

function onCreated(error, objectId) {
Expand Down Expand Up @@ -167,7 +174,7 @@ define(["sugar-web/bus", "sugar-web/env"], function (bus, env) {
};

datastore.load = function (objectId, callback) {
inputStream = bus.createInputStream();
var inputStream = bus.createInputStream();

inputStream.open(function (error) {
function onResponseReceived(responseError, result) {
Expand Down Expand Up @@ -197,7 +204,7 @@ define(["sugar-web/bus", "sugar-web/env"], function (bus, env) {
};

datastore.save = function (objectId, metadata, callback) {
outputStream = bus.createOutputStream();
var outputStream = bus.createOutputStream();

outputStream.open(function (error) {
function onResponseReceived(responseError, result) {
Expand Down
Loading