MySC/static/mysc_objects.js
2024-09-22 19:09:41 +02:00

659 lines
14 KiB
JavaScript

function SCaseList() {
lastChange = 0;
this.importExampleData = function () {
var exampleData = {
Vacances: {
Papier: {
color: "#f00",
things: [
{ label: _("White paper"), nb: 1 },
{ label: _("Pen"), nb: 3 },
{ label: _("ID card/passport"), nb: 1 },
],
},
Multimédia: {
color: "#0f0",
things: [
{ label: _("Watch"), nb: 1 },
{ label: _("Watch charger"), nb: 1 },
{ label: _("Laptop"), nb: 1 },
{ label: _("Laptop charger"), nb: 1 },
],
},
},
};
for (scaseName in exampleData) {
var scase = this.newSCase(scaseName);
for (catName in exampleData[scaseName]) {
var cat = scase.cats.newCat(catName);
for (idx in exampleData[scaseName][catName].things) {
cat.newThing(
exampleData[scaseName][catName].things[idx]["label"],
exampleData[scaseName][catName].things[idx]["nb"]
);
}
}
}
};
this.loadFromLocalStorage = function (data) {
if (jQuery.type(localStorage.scases) != "undefined") {
try {
return this.loadFromJsonData(JSON.parse(localStorage.scases));
} catch (e) {
return false;
}
}
return null;
};
this.loadFromJsonData = function (data) {
try {
this.lastChange = data.lastChange;
for (el in data.scases) {
this[el] = new SCase(false, false, data.scases[el]);
}
return true;
} catch (e) {
for (el in this) {
if (this.isSCase(this[el])) {
delete this[el];
}
}
}
return false;
};
this.export = function () {
return {
lastChange: this.lastChange,
scases: this.each(function (idx, scase) {
return scase.export();
}),
};
};
this.import = function (data) {
ret = {};
for (el in this) {
if (this.isSCase(this[el])) {
delete ret[el];
}
}
this.lastChange = data.lastChange;
for (el in data.scases) {
this[el] = new SCase(false, false, data.scases[el]);
}
return true;
};
this.save = function () {
localStorage.scases = JSON.stringify(this.export());
};
this.each = function (fct) {
var idx = 0;
var ret = {};
for (el in this) {
if (this.isSCase(this[el])) {
ret[el] = fct(idx++, this[el]);
}
}
return ret;
};
this.count = function () {
len = 0;
this.each(function (idx, scase) {
len = len + 1;
});
return len;
};
this.isSCase = function (el) {
return (
jQuery.type(el) == "object" &&
jQuery.type(el.isSCase) == "function" &&
el.isSCase()
);
};
this.byName = function (name) {
for (el in this) {
if (this.isSCase(this[el])) {
if (this[el].name == name) {
return this[el];
}
}
}
return false;
};
this.byUUID = function (uuid) {
return this.isCase(this[uuid]) ? this[uuid] : null;
};
this.removeSCase = function (name) {
for (el in this) {
if (this.isSCase(this[el]) && this[el].name == name) {
this[el].remove();
return true;
}
}
return false;
};
this.newSCase = function (name) {
if (this.byName(this[name])) {
var scase = this.byName(name);
if (scase.removed) {
scase.restore();
return true;
}
} else {
var uuid = uuid || generate_uuid();
this[uuid] = new SCase(uuid, name);
return this[uuid];
}
return false;
};
this.renameSCase = function (name, newname) {
var scase = this.byName(name);
if (scase && !this.byName(newname)) {
scase.name = newname;
scase.lastChange = new Date().getTime();
return scase;
}
return false;
};
this.copySCase = function (name, newname) {
var orig_scase = this.byName(name);
if (this.isSCase(orig_scase) && !this.byName(newname)) {
var uuid = uuid || generate_uuid();
this[uuid] = new SCase(false, false, orig_scase.export());
this[uuid].uuid = uuid;
this[uuid].lastChange = new Date().getTime();
this[uuid].name = newname;
return this[uuid];
}
return false;
};
this.resetSCase = function (name) {
for (el in this) {
if (this.isSCase(this[el]) && this[el].name == name) {
return this[el].reset();
}
}
return false;
};
}
function SCase(uuid, name, data) {
this.uuid = uuid || generate_uuid();
this.name = name;
this.cats = new CatList();
this.lastChange = new Date().getTime();
this.removed = false;
this.isSCase = function () {
return true;
};
this.import = function (data) {
this.uuid = data.uuid || generate_uuid();
this.lastChange = data.lastChange || new Date().getTime();
this.name = decodeURIComponent(data.name);
this.removed = data.removed || false;
if (jQuery.type(data.cats) == "object") {
this.cats = new CatList(data.cats);
}
return true;
};
this.export = function () {
return {
uuid: this.uuid,
lastChange: this.lastChange,
name: encodeURIComponent(this.name),
removed: this.removed,
cats: this.cats.export(),
};
};
this.byName = function (name) {
for (idx in this.cats) {
if (name == this.cats[idx].name) {
return this.cats[idx];
}
}
return false;
};
this.stats = function () {
var cats = 0;
var things = 0;
var things_done = 0;
this.cats.each(function (cidx, cat) {
if (cat.removed) {
return true;
}
cats++;
for (idx in cat.things) {
if (cat.things[idx].removed) {
continue;
}
things++;
if (cat.things[idx].checked) {
things_done++;
}
}
});
return {
cats: cats,
things: things,
done: things_done,
};
};
this.reset = function () {
this.cats.each(function (idx, cat) {
for (idx in cat.things) {
cat.things[idx].reset();
}
});
return true;
};
this.remove = function () {
this.removed = true;
this.lastChange = new Date().getTime();
};
this.restore = function () {
this.removed = false;
this.lastChange = new Date().getTime();
};
/*
* Constructor
*/
if (jQuery.type(data) == "object") {
try {
this.import(data);
} catch (e) {
console.log(e);
alert(
_(
"An error occurred while loading the %s suitcase from the cache.",
this.name
)
);
}
}
}
function CatList(data) {
this.export = function () {
return this.each(function (idx, cat) {
return cat.export();
});
};
this.import = function (data) {
for (el in this) {
if (this.isCat(this[el])) {
delete this[el];
}
}
for (el in data) {
this[el] = new Cat(el, false, false, data[el]);
}
return true;
};
this.each = function (fct) {
var idx = 0;
var ret = {};
for (el in this) {
if (this.isCat(this[el])) {
ret[el] = fct(idx++, this[el]);
}
}
return ret;
};
this.count = function () {
len = 0;
this.each(function (idx, cat) {
len = len + 1;
});
return len;
};
this.isCat = function (el) {
return (
jQuery.type(el) == "object" &&
jQuery.type(el.isCat) == "function" &&
el.isCat()
);
};
this.byName = function (name) {
for (el in this) {
if (this.isCat(this[el])) {
if (this[el].name == name) {
return this[el];
}
}
}
return false;
};
this.byUUID = function (uuid) {
return this.isCas(this[uuid]) ? this[uuid] : null;
};
this.newCat = function (name) {
if (this.byName(name)) {
var cat = this.byName(name);
if (cat.removed) {
cat.restore();
return true;
}
} else {
var uuid = uuid || generate_uuid();
this[uuid] = new Cat(uuid, name);
return this[uuid];
}
return false;
};
this.renameCat = function (name, newname) {
var cat = this.byName(name);
if (cat && !this.byName(newname)) {
cat.name = newname;
cat.lastChange = new Date().getTime();
return cat;
}
return false;
};
this.removeCat = function (name) {
for (el in this) {
if (this.isCat(this[el]) && this[el].name == name) {
this[el].remove();
return true;
}
}
return false;
};
this.restoreCat = function (name) {
for (el in this) {
if (this.isCat(this[el]) && this[el].name == name && this[el].removed) {
this[el].restore();
return true;
}
}
return false;
};
/*
* Constructor
*/
if (jQuery.type(data) == "object") {
try {
this.import(data);
} catch (e) {
console.log(e);
alert(
_("An error occurred while loading the category list from the cache.")
);
}
}
}
function Cat(uuid, name, color, data) {
this.uuid = generate_uuid();
this.lastChange = new Date().getTime();
this.name = name;
this.color =
color ||
"#" + (0x1000000 + Math.random() * 0xffffff).toString(16).substr(1, 6);
this.things = {};
this.removed = false;
this.isCat = function () {
return true;
};
this.import = function (data) {
this.uuid = data.uuid || generate_uuid();
this.lastChange = data.lastChange || new Date().getTime();
this.name = decodeURIComponent(data.name);
this.color = data.color;
this.removed = data.removed || false;
if (jQuery.type(data.things) == "object") {
for (tuuid in data.things) {
this.things[tuuid] = new Thing(tuuid);
this.things[tuuid].import(data.things[tuuid]);
}
}
return true;
};
this.export = function () {
var things = {};
for (tuuid in this.things) {
things[tuuid] = this.things[tuuid].export();
}
return {
uuid: this.uuid,
lastChange: this.lastChange,
name: encodeURIComponent(this.name),
color: this.color,
removed: this.removed,
things: things,
};
};
this.byLabel = function (label) {
for (idx in this.things) {
if (label == this.things[idx].label) {
return this.things[idx];
}
}
return false;
};
this.count = function () {
return keys(this.things).length;
};
this.stats = function () {
var count = 0;
var done = 0;
for (idx in this.things) {
if (this.things[idx].removed) {
continue;
}
if (this.things[idx].checked) {
done += 1;
}
count += 1;
}
return {
things: count,
done: done,
};
};
this.newThing = function (label, nb) {
if (this.byLabel(label)) {
var thing = this.byLabel(label);
if (thing.removed) {
thing.restore();
thing.setChecked(false);
thing.setNb(nb);
return true;
}
} else {
var uuid = generate_uuid();
this.things[uuid] = new Thing(uuid, label, nb);
return true;
}
return false;
};
this.renameThing = function (label, newlabel) {
var thing = this.byLabel(label);
if (thing && !this.byLabel(newlabel)) {
thing.label = newlabel;
thing.lastChange = new Date().getTime();
return thing;
}
return false;
};
this.removeThing = function (label) {
for (idx in this.things) {
if (this.things[idx].label == label) {
this.things[idx].remove();
return true;
}
}
return false;
};
this.restoreThing = function (label) {
for (idx in this.things) {
if (this.things[idx].label == label && this.things[idx].removed) {
this.things[idx].restore();
return true;
}
}
return false;
};
this.remove = function () {
this.removed = true;
this.lastChange = new Date().getTime();
};
this.restore = function () {
this.removed = false;
this.lastChange = new Date().getTime();
};
/*
* Constructor
*/
if (jQuery.type(data) == "object") {
try {
this.import(data);
} catch (e) {
console.log(e);
alert(
_(
"An error occurred while loading the %s category from the cache.",
this.name
)
);
}
}
}
function Thing(uuid, label, nb, checked) {
this.uuid = uuid || generate_uuid();
this.lastChange = new Date().getTime();
this.label = label;
this.nb = nb || 1;
this.checked = checked;
this.removed = false;
this.import = function (data) {
this.uuid = data.uuid || generate_uuid();
this.lastChange = data.lastChange || new Date().getTime();
(this.label = decodeURIComponent(data.label)), (this.nb = data.nb || 1);
this.checked = data.checked;
this.removed = data.removed || false;
};
this.reset = function () {
this.checked = false;
this.lastChange = new Date().getTime();
};
this.export = function () {
return {
uuid: this.uuid,
lastChange: this.lastChange,
label: encodeURIComponent(this.label),
nb: this.nb,
checked: this.checked,
removed: this.removed,
};
};
this.setNb = function (nb) {
this.nb = nb;
this.lastChange = new Date().getTime();
};
this.setChecked = function (value) {
this.checked = value;
this.lastChange = new Date().getTime();
console.log(
`Thing<${this.uuid}>.setChecked(${this.checked}): ${this.lastChange}`
);
};
this.remove = function () {
this.removed = true;
this.lastChange = new Date().getTime();
};
this.restore = function () {
this.removed = false;
this.lastChange = new Date().getTime();
};
}
function User() {
this.username = null;
this.name = null;
this.token = null;
this.loadFromLocalStorage = function () {
if (jQuery.type(localStorage.user) == "undefined") return;
try {
var data = JSON.parse(localStorage.user);
this.username = data.username;
this.name = data.name;
this.token = data.token;
} catch (e) {
alert(_("Error loading your login information. Please log in again."));
}
};
this.connected = function () {
return this.username && this.token;
};
this.reset = function () {
this.username = null;
this.name = null;
this.token = null;
};
this.save = function () {
localStorage.user = JSON.stringify({
username: this.username,
name: this.name,
token: this.token,
});
};
}