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

Fix datastore integration issue on fix #4858 #2

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -38,7 +38,9 @@ define(["webL10n",
});
var result = window.dispatchEvent(stopEvent);
if (result) {
activity.close();
datastoreObject.save(function() {
activity.close();
});
}
}
bus.onNotification("activity.stop", sendStopEvent);
@@ -51,11 +51,16 @@ define(["sugar-web/env"], function (env) {
window.location = "../../index.html";
} else if (method == "activity.get_xo_color") {
var color = {stroke: "#FF2B34", fill: "#005FE4"};
if (typeof(Storage)!=="undefined" && typeof(window.localStorage)!=="undefined") {
try {
color = JSON.parse(window.localStorage.getItem("sugar_settings")).colorvalue;
} catch(err) {}
}
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
chrome.storage.local.get("sugar_settings", function(values) {
color = JSON.parse(values.sugar_settings).colorvalue;
callback(null, [[color.fill, color.stroke]]);
});
} else if (typeof(Storage)!=="undefined" && typeof(window.localStorage)!=="undefined") {
try {
color = JSON.parse(window.localStorage.getItem("sugar_settings")).colorvalue;
} catch(err) {}
}
callback(null, [[color.fill, color.stroke]]);
}
return;
@@ -16,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://127.0.0.1:" + port);

socket.binaryType = "arraybuffer";

@@ -58,7 +58,7 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
var results = [];
if (!html5storage.test())
return results;
for (var key in localStorage) {
for (var key in html5storage.getAll()) {
if (key.substr(0, datastorePrefix.length) == datastorePrefix) {
var entry = html5storage.getValue(key);
entry.objectId = key.substr(datastorePrefix.length);
@@ -104,13 +104,13 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
// Load metadata
DatastoreObject.prototype.getMetadata = function(callback) {
var callback_c = datastore.callbackChecker(callback);
var result = html5storage.getValue(datastorePrefix + this.objectId);
if (result != null) {
this.setMetadata(result.metadata);
this.setDataAsText(result.text);
this.toload = false;
callback_c(null, result.metadata);
}
var result = html5storage.getValue(datastorePrefix + this.objectId);
if (result != null) {
this.setMetadata(result.metadata);
this.setDataAsText(result.text);
this.toload = false;
callback_c(null, result.metadata);
}
};

// Load text
@@ -174,25 +174,63 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {

// -- HTML5 local storage handling

// Load storage - Need for Chrome App
var storageloadedcalls = [];
html5storage.load = function(then) {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
var that = this;
// Currently load, will call then later
if (storageloadedcalls.length != 0) {
storageloadedcalls.push(then);
return;
}
storageloadedcalls.push(then);
chrome.storage.local.get(null, function(values) {
that.values = values;
// Call all waiting functions
for (var i = 0 ; i < storageloadedcalls.length ; i++) {
if (storageloadedcalls[i]) storageloadedcalls[i]();
}
});
} else {
if (then) then();
}
};
html5storage.load();

// Test if HTML5 storage is available
html5storage.test = function() {
return (typeof(Storage) !== "undefined" && typeof(window.localStorage) !== "undefined");
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime)
return true;
else
return (typeof(Storage) !== "undefined" && typeof(window.localStorage) !== "undefined");
};

// Set a value in the storage
html5storage.setValue = function(key, value) {
if (this.test()) {
try {
window.localStorage.setItem(key, JSON.stringify(value));
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
this.values[key] = JSON.stringify(value);
var item = {};
item[key] = this.values[key];
chrome.storage.local.set(item);
} else {
window.localStorage.setItem(key, JSON.stringify(value));
}
} catch (err) {}
}
};

// Get a value in the storage
html5storage.getValue = function(key) {
if (this.test()) {
try {
return JSON.parse(window.localStorage.getItem(key));
try {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
return JSON.parse(this.values[key]);
} else {
return JSON.parse(window.localStorage.getItem(key));
}
} catch (err) {
return null;
}
@@ -204,10 +242,31 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
html5storage.removeValue = function(key) {
if (this.test()) {
try {
window.localStorage.removeItem(key);
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
this.values[key] = null;
chrome.store.remove(key);
} else {
window.localStorage.removeItem(key);
}
} catch (err) {}
}
};

// Get all values
html5storage.getAll = function() {
if (this.test()) {
try {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
return this.values;
} else {
return window.localStorage;
}
} catch (err) {
return null;
}
}
return null;
};

return datastore;
});
@@ -18,7 +18,8 @@ define(function () {
activityId: getUrlParameter("aid"),
activityName: getUrlParameter("n"),
bundleId: getUrlParameter("a"),
objectId: getUrlParameter("o")
objectId: getUrlParameter("o"),
sharedId: getUrlParameter("s")
};
setTimeout(function () {
callback(null, window.top.sugar.environment);
@@ -67,7 +68,10 @@ define(function () {
};

env.isSugarizer = function() {
if (typeof(Storage)!=="undefined" && typeof(window.localStorage)!=="undefined") {
// 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) {
@@ -452,3 +452,6 @@ ul.flat-list.striped li:nth-child(odd) {
#activity-palette .wrapper {
width: 371px;
}
*:focus {
outline: 0;
}
@@ -452,3 +452,6 @@ ul.flat-list.striped li:nth-child(odd) {
#activity-palette .wrapper {
width: 271px;
}
*:focus {
outline: 0;
}