Friendiiverse/App/Main.js

245 lines
6.5 KiB
JavaScript
Raw Normal View History

2023-04-29 13:06:33 +02:00
var Tasker = {};
2023-04-23 19:46:01 +02:00
var Persist = {Servers: {}, Sources: {}, Identities: {},};
2023-04-29 00:06:16 +02:00
var Present = structuredClone(Persist);
2023-04-29 13:06:33 +02:00
Persist.__Set__ = function __Set__() {
// set value to own object
// set value to Present object too
// rewrite own object into cookies
};
Present.__Set__ = function __Set__() {
// like Persist but only set value to own object
};
2023-04-25 00:55:20 +02:00
var ApiCache = {
2023-05-01 00:08:01 +02:00
__Store__(Data, Path) {
eval(`ApiCache${JPath(Path)} = Data;`);
2023-04-25 00:55:20 +02:00
},
__UrlStore__(Data) {
2023-04-29 13:06:33 +02:00
ApiCache.__Store__(Data, `Urls['${Data.Url}']`);
2023-04-25 00:55:20 +02:00
},
Urls: {},
};
2023-04-19 00:16:35 +02:00
2023-04-29 13:06:33 +02:00
Assets.__ = function __(Name) {
2023-04-24 15:03:51 +02:00
if (Name in Assets) {
if (Assets[Name].startsWith('data:')) {
return Assets[Name];
} else {
return `./Assets/${Assets[Name]}`;
};
};
};
2023-04-29 13:06:33 +02:00
Strings.__ = function __(Name) {
2023-04-25 00:55:20 +02:00
// TODO: Handle arbitrary nestation
if (Name in Strings) {
if (Strings[Name]['en']) { // TODO{ Select this language from user config
return Strings[Name]['en'];
} else {
return Strings[Name].en;
};
};
};
function DoAsync(First, Then, Data) {
var Job = RndId();
2023-04-22 17:09:59 +02:00
Tasker[Job] = {
Remains: 0,
Return(Data) {
this.Remains -= 1;
this.Result = Data;
},
2023-04-19 00:16:35 +02:00
};
// Call all First functs
2023-04-25 15:00:41 +02:00
ForceList(First).forEach(function(Fun){
2023-04-19 12:18:14 +02:00
var Task = RndId();
var Proc = [Job, Task];
2023-04-22 17:09:59 +02:00
Tasker[Job][Task] = {};
Tasker[Job].Remains += 1;
//Fun(Proc, Data);
Data ? Fun(Data, Proc) : Fun(Proc);
2023-04-19 12:18:14 +02:00
});
// Continuosly check when First functs completed
2023-04-22 17:09:59 +02:00
Tasker[Job].Interval = setInterval(function(Job, Then){
if (Tasker[Job].Remains <= 0) {
clearInterval(Tasker[Job].Interval);
// Call all Then functs
2023-04-19 12:18:14 +02:00
ForceList(Then).forEach(function(Fun){
2023-04-22 17:09:59 +02:00
Fun(Tasker[Job].Result);
2023-04-19 12:18:14 +02:00
});
2023-04-22 17:09:59 +02:00
delete Tasker[Job];
2023-04-19 12:18:14 +02:00
};
}, 50, Job, Then);
return Job;
2023-04-19 12:18:14 +02:00
};
2023-04-25 23:35:34 +02:00
function HtmlAssign(Id, Data) {
2023-04-26 15:33:51 +02:00
Array.from(document.getElementsByClassName(Id)).forEach(function(El){
HtmlAssignPropper(El, Data);
});
Array.from(document.getElementById(Id).querySelectorAll(`*:not([class~="${Id}"])`)).forEach(function(El){
if (El.dataset.assign) {
HtmlAssignPropper(El, Data);
};
});
};
function HtmlAssignPropper(El, Data) {
El.dataset.assign.trim().split(' ').forEach(function(Att){
2023-04-29 13:06:33 +02:00
var Toks = Att.split('=');
var Key = Toks[0].trim();
var Val = eval(`Data.${Toks[1]}`);
if (Val !== undefined) {
if (Key === 'src') {
2023-05-01 00:08:01 +02:00
Val = MkReqUrl(Val);
2023-04-29 13:06:33 +02:00
};
El[Key] = Val;
2023-04-25 23:35:34 +02:00
};
});
};
function TransNetCall(Data, FromSource, DestType, Proc) {
Data.CallOld = Data.Call;
Data.CallFineOld = Data.CallFine;
Data.CallFailOld = Data.CallFail;
NetCall(_.merge(Data, {
//Call: function(Res){ CallFun(Data.Call, Res); },
CallFine: function(Res){
Res.responseJsonOld = Res.responseJson;
Res.responseJson = ApiTransform(Res.responseJson, FromSource, DestType);
CallFun(Data.CallFineOld, Res);
}, //CallFail: function(Res){ CallFun(Data.CallFail, Res); },
}), Proc);
};
2023-04-22 17:09:59 +02:00
function DisplayProfile(Profile) {
Profile = UrlObj(Profile, DisplayProfile);
2023-05-01 00:08:01 +02:00
if (Profile) {
var Window = MkWindow({className: "Profile"});
Window.innerHTML += Templating.ViewProfile(Profile);
DoAsync(FetchNotes, FillTimeline, Profile);
2023-05-01 00:08:01 +02:00
};
2023-04-23 19:46:01 +02:00
};
function FetchNotes(Profile, Proc) {
var Soft = Profile.ServerSoftware;
var Method = Profile.Type == 'Server'
? ApiEndpoints.ServerTimeline[Soft]
: ApiEndpoints.FetchNotes[Soft](Profile);
2023-04-28 17:25:42 +02:00
var Endp = Method;
var Method = Endp.Method || Endp;
NetCall({Target: UrlBase(Profile.Url), Method: Method, Data: Endp.Data, CallFine: function(Res){
2023-04-25 15:00:41 +02:00
var Notes = ApiTransform(Res.responseJson, Soft, 'Note');
2023-04-25 00:55:20 +02:00
LogDebug(Notes, 'l');
Tasker[Res.Proc[0]].Return(Notes);
}}, Proc);
2023-04-20 22:08:38 +02:00
};
2023-04-19 00:16:35 +02:00
2023-04-19 12:18:14 +02:00
function FetchMastodon(Proc) {
2023-04-25 15:00:41 +02:00
//if (UseFakeApi) {
// ResFetchMastodon({responseJson: [FakeApi.Mastodon.Status], Proc: Proc});
//} else {
NetCall({Target: "Mastodon", Method: "GET timelines/public", CallFine: ResFetchMastodon}, Proc);
2023-04-25 15:00:41 +02:00
//};
2023-04-19 00:16:35 +02:00
};
2023-04-20 18:13:47 +02:00
function ResFetchMastodon(Res) {
2023-04-23 00:48:14 +02:00
var Notes = ApiTransform(Res.responseJson, 'Mastodon', 'Note');
2023-04-20 18:13:47 +02:00
LogDebug(Notes, 'l');
2023-04-22 17:09:59 +02:00
Tasker[Res.Proc[0]].Return(Notes);
2023-04-20 18:13:47 +02:00
};
2023-04-19 00:16:35 +02:00
function FillTimeline(Notes) {
Notes.forEach(function(Note){
2023-04-25 00:55:20 +02:00
ApiCache.__UrlStore__(Note.Profile);
2023-04-29 00:06:16 +02:00
Root.lastChild.innerHTML += Templating.ViewNote(Note);
2023-04-19 00:16:35 +02:00
});
};
2023-04-29 00:06:16 +02:00
function DisplayThread(Note) {
2023-05-01 00:08:01 +02:00
var Window = MkWindow({className: "Thread"});
//Window.innerHTML += Templating.ViewNote(Note);
};
2023-04-25 23:35:34 +02:00
function FillHome() {
2023-04-25 00:55:20 +02:00
var Window = MkWindow({className: "Gallery"});
2023-04-25 23:35:34 +02:00
var Categories = ApiStatic.Featured;
//_.forOwn(Categories, function(CategoryVal, Category){
2023-04-25 23:35:34 +02:00
Object.keys(Categories).forEach(function(Category){
Window.innerHTML += `<h2>Featured ${Category}</h2><ul></ul>`;
2023-04-25 23:35:34 +02:00
Categories[Category].forEach(function(Profile){
ApiCache.Urls[Profile.Url] = Profile;
var Rnd = RndHtmlId();
Window.querySelector('ul').innerHTML += `<li id="${Rnd}">
2023-05-01 00:08:01 +02:00
${Templating.ViewProfile(Profile, {Name: Profile.Url})}
</li>`;
var Endp = ApiEndpoints.ServerInfo[Profile.ServerSoftware];
var Method = Endp.Method || Endp;
TransNetCall({Target: Profile.Url, Method: Method, Data: Endp.Data, CallFine: function(Res){
HtmlAssign(Rnd, Res.responseJson);
_.merge(ApiCache.Urls[Profile.Url], Res.responseJson);
}}, Profile.ServerSoftware, 'Profile');
2023-04-20 18:13:47 +02:00
});
});
};
2023-04-20 22:08:38 +02:00
/*
2023-04-19 00:16:35 +02:00
PlazasView.innerHTML = `
<div>
<h3>Featured</h3>
<ul>
<li onclick="DisplayFriendicaTimeline('statuses/networkpublic_timeline');">Federation</li>
<li onclick="DisplayFriendicaTimeline('statuses/public_timeline');">${FriendicaUrl}</li>
</ul>
</div>
`;
2023-04-20 22:08:38 +02:00
*/
2023-04-20 18:13:47 +02:00
2023-04-23 00:48:14 +02:00
function ComposeNote() {
2023-04-25 00:55:20 +02:00
var Window = MkWindow();
2023-04-23 00:48:14 +02:00
Window.innerHTML += `
<h2>Compose</h2>
2023-04-23 19:46:01 +02:00
<p>Posting in: [Channel]</p>
2023-04-23 00:48:14 +02:00
<textarea style="width: 100%; height: 20em;"></textarea>
<p>
<button onclick="PostNote(this.parentNode.parentNode.querySelector('textarea').value);">Note!</button>
</p>
`;
};
function PostNote(Text) {
Obj = ExtrimObj(ApiSchema.Note);
2023-04-23 19:46:01 +02:00
Obj.Content = Text;
// Find out current profile and destination channel to do a proper net request
};
function ManageSettings() {
2023-04-25 00:55:20 +02:00
MkWindow().innerHTML = `
2023-04-24 15:03:51 +02:00
<h2>Settings</h2>
<h3>Misc</h3>
<p>
2023-04-25 00:55:20 +02:00
Language: ${MkSelectMenu([{innerHTML: "en"}, {innerHTML: "it"},]).outerHTML}
2023-04-24 15:03:51 +02:00
</p>
<p>
Theme:
</p>
<p>
<label>Wait <input type="number"/> seconds after clicking send for a note to be sent</label>
</p>
<h3>Identities</h3>
2023-04-25 23:35:34 +02:00
${MkListMenu(Present.Identities, ElsCfg.ListMenu.FullControl).outerHTML}
2023-04-24 15:03:51 +02:00
<h3>Sources</h3>
2023-04-25 23:35:34 +02:00
${MkListMenu(Present.Sources, ElsCfg.ListMenu.FullControl).outerHTML}
2023-04-24 15:03:51 +02:00
<h3>Data</h3>
<p>
<button>Import User Data</button>
<button>Export User Data</button>
</p>
<p>
<label><input type="checkbox"/> Persist cache on app reload</label>
</p>
2023-04-23 19:46:01 +02:00
`;
2023-04-23 00:48:14 +02:00
};
2023-04-25 23:35:34 +02:00
FillHome();
2023-04-22 10:06:31 +02:00
CoverView.remove();