MySC/static/mysc_objects.js

658 lines
14 KiB
JavaScript
Raw Normal View History

function SCaseList() {
2024-09-21 17:23:21 +02:00
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 },
2024-09-21 17:23:21 +02:00
],
},
2024-09-21 17:23:21 +02:00
Multimédia: {
color: "#0f0",
things: [
{ label: _("Watch"), nb: 1 },
{ label: _("Watch charger"), nb: 1 },
{ label: _("Laptop"), nb: 1 },
{ label: _("Laptop charger"), nb: 1 },
2024-09-21 17:23:21 +02:00
],
},
},
};
for (scaseName in exampleData) {
2024-09-21 17:23:21 +02:00
var scase = this.newSCase(scaseName);
for (catName in exampleData[scaseName]) {
2024-09-21 17:23:21 +02:00
var cat = scase.cats.newCat(catName);
for (idx in exampleData[scaseName][catName].things) {
2024-09-21 17:23:21 +02:00
cat.newThing(
exampleData[scaseName][catName].things[idx]["label"],
exampleData[scaseName][catName].things[idx]["nb"]
);
2016-09-06 01:12:46 +02:00
}
}
}
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.loadFromLocalStorage = function (data) {
if (jQuery.type(localStorage.scases) != "undefined") {
2016-09-06 01:12:46 +02:00
try {
return this.loadFromJsonData(JSON.parse(localStorage.scases));
2024-09-21 17:23:21 +02:00
} catch (e) {
return false;
2016-09-06 01:12:46 +02:00
}
}
return null;
2024-09-21 17:23:21 +02:00
};
2024-09-21 17:23:21 +02:00
this.loadFromJsonData = function (data) {
try {
2024-09-21 17:23:21 +02:00
this.lastChange = data.lastChange;
for (el in data.scases) {
2024-09-21 17:23:21 +02:00
this[el] = new SCase(false, false, data.scases[el]);
}
return true;
2024-09-21 17:23:21 +02:00
} catch (e) {
for (el in this) {
if (this.isSCase(this[el])) {
delete this[el];
}
}
2016-09-06 01:12:46 +02:00
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.export = function () {
2016-09-06 01:12:46 +02:00
return {
2024-09-21 17:23:21 +02:00
lastChange: this.lastChange,
scases: this.each(function (idx, scase) {
2016-09-06 01:12:46 +02:00
return scase.export();
2024-09-21 17:23:21 +02:00
}),
2016-09-06 01:12:46 +02:00
};
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.import = function (data) {
ret = {};
2016-09-06 01:12:46 +02:00
for (el in this) {
if (this.isSCase(this[el])) {
delete ret[el];
}
}
2024-09-21 17:23:21 +02:00
this.lastChange = data.lastChange;
2016-09-06 01:12:46 +02:00
for (el in data.scases) {
2024-09-21 17:23:21 +02:00
this[el] = new SCase(false, false, data.scases[el]);
2016-09-06 01:12:46 +02:00
}
return true;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.save = function () {
localStorage.scases = JSON.stringify(this.export());
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.each = function (fct) {
var idx = 0;
var ret = {};
2016-09-06 01:12:46 +02:00
for (el in this) {
2024-09-21 17:23:21 +02:00
if (this.isSCase(this[el])) {
ret[el] = fct(idx++, this[el]);
2016-09-06 01:12:46 +02:00
}
}
return ret;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.count = function () {
len = 0;
this.each(function (idx, scase) {
len = len + 1;
2016-09-06 01:12:46 +02:00
});
return len;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.isSCase = function (el) {
return (
jQuery.type(el) == "object" &&
jQuery.type(el.isSCase) == "function" &&
el.isSCase()
);
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.byName = function (name) {
2016-09-06 01:12:46 +02:00
for (el in this) {
2024-09-21 17:23:21 +02:00
if (this.isSCase(this[el])) {
if (this[el].name == name) {
2016-09-06 01:12:46 +02:00
return this[el];
}
}
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.byUUID = function (uuid) {
return this.isCase(this[uuid]) ? this[uuid] : null;
2024-09-21 17:23:21 +02:00
};
2024-09-21 17:23:21 +02:00
this.removeSCase = function (name) {
2016-09-06 01:12:46 +02:00
for (el in this) {
2024-09-21 17:23:21 +02:00
if (this.isSCase(this[el]) && this[el].name == name) {
this[el].remove();
2016-09-06 01:12:46 +02:00
return true;
}
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.newSCase = function (name) {
if (this.byName(this[name])) {
2024-09-21 17:23:21 +02:00
var scase = this.byName(name);
if (scase.removed) {
scase.restore();
return true;
}
2024-09-21 17:23:21 +02:00
} else {
var uuid = uuid || generate_uuid();
this[uuid] = new SCase(uuid, name);
return this[uuid];
2016-09-06 01:12:46 +02:00
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.renameSCase = function (name, newname) {
var scase = this.byName(name);
2016-09-06 01:12:46 +02:00
if (scase && !this.byName(newname)) {
2024-09-21 17:23:21 +02:00
scase.name = newname;
scase.lastChange = new Date().getTime();
2016-09-06 01:12:46 +02:00
return scase;
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.copySCase = function (name, newname) {
var orig_scase = this.byName(name);
2016-09-06 01:12:46 +02:00
if (this.isSCase(orig_scase) && !this.byName(newname)) {
2024-09-21 17:23:21 +02:00
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];
2016-09-06 01:12:46 +02:00
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.resetSCase = function (name) {
2016-09-07 00:30:25 +02:00
for (el in this) {
2024-09-21 17:23:21 +02:00
if (this.isSCase(this[el]) && this[el].name == name) {
2016-09-07 00:30:25 +02:00
return this[el].reset();
}
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
}
2024-09-21 17:23:21 +02:00
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;
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.isSCase = function () {
2016-09-06 01:12:46 +02:00
return true;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
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;
2024-09-21 17:23:21 +02:00
};
2024-09-21 17:23:21 +02:00
this.export = function () {
2016-09-06 01:12:46 +02:00
return {
2024-09-21 17:23:21 +02:00
uuid: this.uuid,
lastChange: this.lastChange,
name: encodeURIComponent(this.name),
removed: this.removed,
cats: this.cats.export(),
2016-09-06 01:12:46 +02:00
};
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.byName = function (name) {
2016-09-06 01:12:46 +02:00
for (idx in this.cats) {
2024-09-21 17:23:21 +02:00
if (name == this.cats[idx].name) {
2016-09-06 01:12:46 +02:00
return this.cats[idx];
}
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.stats = function () {
var cats = 0;
var things = 0;
var things_done = 0;
this.cats.each(function (cidx, cat) {
if (cat.removed) {
return true;
}
2016-09-07 19:06:11 +02:00
cats++;
for (idx in cat.things) {
if (cat.things[idx].removed) {
continue;
}
2016-09-07 19:06:11 +02:00
things++;
if (cat.things[idx].checked) {
things_done++;
}
}
});
return {
2024-09-21 17:23:21 +02:00
cats: cats,
things: things,
done: things_done,
};
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.reset = function () {
this.cats.each(function (idx, cat) {
2016-09-07 00:30:25 +02:00
for (idx in cat.things) {
if (cat.things[idx].checked) {
2024-09-21 17:23:21 +02:00
cat.things[idx].checked = false;
2016-09-07 00:30:25 +02:00
}
}
});
2024-09-21 17:23:21 +02:00
this.lastChange = new Date().getTime();
2016-09-07 00:30:25 +02:00
return true;
2024-09-21 17:23:21 +02:00
};
2016-09-07 00:30:25 +02:00
2024-09-21 17:23:21 +02:00
this.remove = function () {
this.removed = true;
this.lastChange = new Date().getTime();
};
2024-09-21 17:23:21 +02:00
this.restore = function () {
this.removed = false;
this.lastChange = new Date().getTime();
};
2016-09-06 01:12:46 +02:00
/*
* Constructor
2016-09-06 01:12:46 +02:00
*/
2024-09-21 17:23:21 +02:00
if (jQuery.type(data) == "object") {
2016-09-06 01:12:46 +02:00
try {
this.import(data);
2024-09-21 17:23:21 +02:00
} catch (e) {
2016-09-06 01:12:46 +02:00
console.log(e);
2024-09-21 17:23:21 +02:00
alert(
_(
"An error occurred while loading the %s suitcase from the cache.",
this.name
)
2024-09-21 17:23:21 +02:00
);
2016-09-06 01:12:46 +02:00
}
}
}
function CatList(data) {
2024-09-21 17:23:21 +02:00
this.export = function () {
return this.each(function (idx, cat) {
return cat.export();
});
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.import = function (data) {
2016-09-06 01:12:46 +02:00
for (el in this) {
if (this.isCat(this[el])) {
delete this[el];
}
}
for (el in data) {
2024-09-21 17:23:21 +02:00
this[el] = new Cat(el, false, false, data[el]);
2016-09-06 01:12:46 +02:00
}
return true;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.each = function (fct) {
var idx = 0;
var ret = {};
2016-09-06 01:12:46 +02:00
for (el in this) {
2024-09-21 17:23:21 +02:00
if (this.isCat(this[el])) {
ret[el] = fct(idx++, this[el]);
2016-09-06 01:12:46 +02:00
}
}
return ret;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.count = function () {
len = 0;
this.each(function (idx, cat) {
len = len + 1;
2016-09-06 01:12:46 +02:00
});
return len;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.isCat = function (el) {
return (
jQuery.type(el) == "object" &&
jQuery.type(el.isCat) == "function" &&
el.isCat()
);
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.byName = function (name) {
2016-09-06 01:12:46 +02:00
for (el in this) {
2024-09-21 17:23:21 +02:00
if (this.isCat(this[el])) {
if (this[el].name == name) {
2016-09-06 01:12:46 +02:00
return this[el];
}
}
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.byUUID = function (uuid) {
return this.isCas(this[uuid]) ? this[uuid] : null;
2024-09-21 17:23:21 +02:00
};
2024-09-21 17:23:21 +02:00
this.newCat = function (name) {
if (this.byName(name)) {
2024-09-21 17:23:21 +02:00
var cat = this.byName(name);
if (cat.removed) {
cat.restore();
return true;
}
2024-09-21 17:23:21 +02:00
} else {
var uuid = uuid || generate_uuid();
this[uuid] = new Cat(uuid, name);
return this[uuid];
2016-09-06 01:12:46 +02:00
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.renameCat = function (name, newname) {
var cat = this.byName(name);
2016-09-06 01:12:46 +02:00
if (cat && !this.byName(newname)) {
2024-09-21 17:23:21 +02:00
cat.name = newname;
cat.lastChange = new Date().getTime();
2016-09-06 01:12:46 +02:00
return cat;
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.removeCat = function (name) {
2016-09-06 01:12:46 +02:00
for (el in this) {
2024-09-21 17:23:21 +02:00
if (this.isCat(this[el]) && this[el].name == name) {
this[el].remove();
2016-09-06 01:12:46 +02:00
return true;
}
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.restoreCat = function (name) {
for (el in this) {
2024-09-21 17:23:21 +02:00
if (this.isCat(this[el]) && this[el].name == name && this[el].removed) {
this[el].restore();
return true;
}
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
/*
* Constructor
2016-09-06 01:12:46 +02:00
*/
2024-09-21 17:23:21 +02:00
if (jQuery.type(data) == "object") {
2016-09-06 01:12:46 +02:00
try {
this.import(data);
2024-09-21 17:23:21 +02:00
} catch (e) {
2016-09-06 01:12:46 +02:00
console.log(e);
2024-09-21 17:23:21 +02:00
alert(
_("An error occurred while loading the category list from the cache.")
2024-09-21 17:23:21 +02:00
);
2016-09-06 01:12:46 +02:00
}
}
}
2024-09-21 17:23:21 +02:00
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 () {
2016-09-06 01:12:46 +02:00
return true;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
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) {
2024-09-21 17:23:21 +02:00
this.things[tuuid] = new Thing(tuuid);
this.things[tuuid].import(data.things[tuuid]);
}
}
return true;
2024-09-21 17:23:21 +02:00
};
2024-09-21 17:23:21 +02:00
this.export = function () {
var things = {};
for (tuuid in this.things) {
2024-09-21 17:23:21 +02:00
things[tuuid] = this.things[tuuid].export();
2016-09-06 01:12:46 +02:00
}
return {
2024-09-21 17:23:21 +02:00
uuid: this.uuid,
lastChange: this.lastChange,
name: encodeURIComponent(this.name),
color: this.color,
removed: this.removed,
things: things,
2016-09-06 01:12:46 +02:00
};
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.byLabel = function (label) {
2016-09-06 01:12:46 +02:00
for (idx in this.things) {
2024-09-21 17:23:21 +02:00
if (label == this.things[idx].label) {
2016-09-06 01:12:46 +02:00
return this.things[idx];
}
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.count = function () {
return keys(this.things).length;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.stats = function () {
var count = 0;
var done = 0;
2016-09-06 01:12:46 +02:00
for (idx in this.things) {
if (this.things[idx].removed) {
continue;
}
2016-09-06 01:12:46 +02:00
if (this.things[idx].checked) {
2024-09-21 17:23:21 +02:00
done += 1;
2016-09-06 01:12:46 +02:00
}
2024-09-21 17:23:21 +02:00
count += 1;
2016-09-06 01:12:46 +02:00
}
return {
2024-09-21 17:23:21 +02:00
things: count,
done: done,
};
2024-09-21 17:23:21 +02:00
};
2024-09-21 17:23:21 +02:00
this.newThing = function (label, nb) {
if (this.byLabel(label)) {
2024-09-21 17:23:21 +02:00
var thing = this.byLabel(label);
if (thing.removed) {
thing.restore();
thing.setChecked(false);
2017-09-17 19:43:57 +02:00
thing.setNb(nb);
return true;
}
2024-09-21 17:23:21 +02:00
} else {
var uuid = generate_uuid();
this.things[uuid] = new Thing(uuid, label, nb);
return true;
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.renameThing = function (label, newlabel) {
var thing = this.byLabel(label);
2016-09-06 01:12:46 +02:00
if (thing && !this.byLabel(newlabel)) {
2024-09-21 17:23:21 +02:00
thing.label = newlabel;
thing.lastChange = new Date().getTime();
2016-09-06 01:12:46 +02:00
return thing;
}
return false;
2024-09-21 17:23:21 +02:00
};
2016-09-06 01:12:46 +02:00
2024-09-21 17:23:21 +02:00
this.removeThing = function (label) {
2016-09-06 01:12:46 +02:00
for (idx in this.things) {
2024-09-21 17:23:21 +02:00
if (this.things[idx].label == label) {
this.things[idx].remove();
return true;
}
}
return false;
2024-09-21 17:23:21 +02:00
};
2024-09-21 17:23:21 +02:00
this.restoreThing = function (label) {
for (idx in this.things) {
2024-09-21 17:23:21 +02:00
if (this.things[idx].label == label && this.things[idx].removed) {
this.things[idx].restore();
2016-09-06 01:12:46 +02:00
return true;
}
}
return false;
2024-09-21 17:23:21 +02:00
};
2024-09-21 17:23:21 +02:00
this.remove = function () {
this.removed = true;
this.lastChange = new Date().getTime();
};
2024-09-21 17:23:21 +02:00
this.restore = function () {
this.removed = false;
this.lastChange = new Date().getTime();
};
2016-09-06 01:12:46 +02:00
/*
* Constructor
2016-09-06 01:12:46 +02:00
*/
2024-09-21 17:23:21 +02:00
if (jQuery.type(data) == "object") {
2016-09-06 01:12:46 +02:00
try {
this.import(data);
2024-09-21 17:23:21 +02:00
} catch (e) {
2016-09-06 01:12:46 +02:00
console.log(e);
2024-09-21 17:23:21 +02:00
alert(
_(
"An error occurred while loading the %s category from the cache.",
this.name
)
2024-09-21 17:23:21 +02:00
);
2016-09-06 01:12:46 +02:00
}
}
}
2024-09-21 17:23:21 +02:00
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;
};
2024-09-21 17:23:21 +02:00
this.export = function () {
2016-09-06 01:12:46 +02:00
return {
2024-09-21 17:23:21 +02:00
uuid: this.uuid,
lastChange: this.lastChange,
label: encodeURIComponent(this.label),
nb: this.nb,
checked: this.checked,
removed: this.removed,
2016-09-06 01:12:46 +02:00
};
2024-09-21 17:23:21 +02:00
};
2024-09-21 17:23:21 +02:00
this.setNb = function (nb) {
this.nb = nb;
this.lastChange = new Date().getTime();
};
2017-09-17 19:43:57 +02:00
2024-09-21 17:23:21 +02:00
this.setChecked = function (value) {
this.checked = value;
this.lastChange = new Date().getTime();
console.log(
`Thing<${this.uuid}>.setChecked(${this.checked}): ${this.lastChange}`
);
};
2024-09-21 17:23:21 +02:00
this.remove = function () {
this.removed = true;
this.lastChange = new Date().getTime();
};
2024-09-21 17:23:21 +02:00
this.restore = function () {
this.removed = false;
this.lastChange = new Date().getTime();
};
2016-09-06 01:12:46 +02:00
}
function User() {
this.username = null;
this.name = null;
this.token = null;
2024-09-21 17:23:21 +02:00
this.loadFromLocalStorage = function () {
if (jQuery.type(localStorage.user) == "undefined") return;
try {
2024-09-21 17:23:21 +02:00
var data = JSON.parse(localStorage.user);
this.username = data.username;
this.name = data.name;
this.token = data.token;
2024-09-21 17:23:21 +02:00
} catch (e) {
alert(_("Error loading your login information. Please log in again."));
}
};
2024-09-21 17:23:21 +02:00
this.connected = function () {
return this.username && this.token;
2024-09-21 17:23:21 +02:00
};
2024-09-21 17:23:21 +02:00
this.reset = function () {
this.username = null;
this.name = null;
this.token = null;
2024-09-21 17:23:21 +02:00
};
2024-09-21 17:23:21 +02:00
this.save = function () {
localStorage.user = JSON.stringify({
username: this.username,
name: this.name,
token: this.token,
});
2024-09-21 17:23:21 +02:00
};
}