r20151206

Signed-off-by: Flashwave <me@flash.moe>
This commit is contained in:
flash 2015-12-06 23:13:29 +01:00
parent 2178eaf7ba
commit 4c7cb4f338
3 changed files with 285 additions and 86 deletions

View file

@ -26,16 +26,12 @@ function notifyUI(content) {
// Add icon
notifIcon .className = 'notification-icon';
if(content.img.substring(0, 5) == "FONT:") {
iconCont = document.createElement('div');
iconCont.className = 'font-icon fa ' + content.img.replace('FONT:', '') + ' fa-4x';
} else {
iconCont = document.createElement('img');
iconCont.setAttribute('alt', identifier);
iconCont.setAttribute('src', content.img);
}
notifIcon .appendChild(iconCont);
notif .appendChild(notifIcon);
@ -49,10 +45,8 @@ function notifyUI(content) {
notifTitle .appendChild(notifTitleNode);
notifText .appendChild(notifTextNode);
if(content.link) {
notif .setAttribute('sakurahref', content.link);
notifContent.setAttribute('onclick', content.link.substring(0, 11) == 'javascript:' ? content.link.substring(11) : 'notifyOpen(this.parentNode.id);');
}
notifContent .appendChild(notifTitle);
notifContent .appendChild(notifText);
@ -133,54 +127,35 @@ function notifyRequest(session) {
return;
}
// Create XMLHttpRequest and notifyURL
var notificationWatcher = new XMLHttpRequest();
var notifyURL = '//' + sakuraVars.urlMain + '/settings.php?request-notifications=true&time=' + Sakura.epoch() + '&session=' + session;
// Create AJAX
var alertGet = new AJAX();
alertGet.setUrl('/settings.php?request-notifications=true&time=' + Sakura.epoch() + '&session=' + session);
// Wait for the ready state to change
notificationWatcher.onreadystatechange = function() {
// Wait for it to reach the "complete" stage
if(notificationWatcher.readyState === 4) {
// Continue if the HTTP return was 200
if(notificationWatcher.status === 200) {
// Assign the JSON parsed content to a variable
var notifyGet = JSON.parse(notificationWatcher.responseText);
alertGet.addCallback(200, function () {
// Assign the JSON parsed content to a variable
var data = JSON.parse(alertGet.response());
// If nothing was set stop
if(typeof notifyGet == 'undefined') {
// Tell the user something went wrong...
notifyUI({
"title": "An error occurred!",
"text": "If this problem persists please report this to the administrator.",
"img": "FONT:fa-exclamation-triangle",
"timeout": 60000,
"sound": false
});
// If nothing was set stop
if (typeof data == 'undefined') {
// Tell the user something went wrong...
throw "No data returned";
// ...then prevent the function from contiuing
return;
}
// Go over every return notification and pass the object to it
for(var notifyID in notifyGet) {
notifyUI(notifyGet[notifyID]);
}
} else if((notificationWatcher.status + '').substring(0, 1) == '5') {
// ELse tell the user there was an internal server error...
notifyUI({
"title": "An internal server error occurred!",
"text": "If this problem persists please report this to the administrator.",
"img": "FONT:fa-chain-broken",
"timeout": 60000,
"sound": false
});
}
// ...then prevent the function from contiuing
return;
}
};
// Make the request
notificationWatcher.open('GET', notifyURL, true);
notificationWatcher.send();
// Go over every return notification and pass the object to it
for (var id in data) {
notifyUI(data[id]);
}
});
alertGet.addCallback(0, function () {
// Tell the user something went wrong...
throw "Notification request failed";
});
alertGet.start(HTTPMethods.GET);
}
// Show the full-page busy window
@ -268,48 +243,30 @@ function ajaxBusyView(show, message, type) {
// Making a post request using AJAX
function ajaxPost(url, data, callback) {
// Combine name and value with an = inbetween
var query = [];
for(var i in data) {
query.push(encodeURIComponent(i) +"="+ encodeURIComponent(data[i]));
}
// Create AJAX
var request = new AJAX();
// Join the array
query = query.join("&");
// Set url
request.setUrl(url);
// Create XMLHttpRequest
var request = new XMLHttpRequest();
// Add callbacks
request.addCallback(200, function () {
callback.call(request.response())
});
request.addCallback(0, function () {
ajaxBusyView(false);
// Open a post request
request.open('POST', url, true);
throw "POST Request failed";
});
// Wait for the readiness to change
request.onreadystatechange = function() {
// Wait for completion
if(request.readyState === 4) {
if(request.status === 200) {
callback.call(request.responseText);
} else {
ajaxBusyView(false);
// Add header
request.addHeader('Content-Type', 'application/x-www-form-urlencoded');
notifyUI({
"title": "An internal server error occurred!",
"text": "If this problem persists please report this to the administrator.",
"img": "FONT:fa-chain-broken",
"timeout": 60000,
"sound": false
});
// Set the post data
request.setSend(data);
return null;
}
}
};
// Set headers
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
// Submit the request
request.send(query);
// Make the request
request.start(HTTPMethods.POST);
}
// Convert href attribute to an object

View file

@ -124,3 +124,113 @@ var utf8 = (function () {
};
return utf8;
})();
// HTTP methods
var HTTPMethods;
(function (HTTPMethods) {
HTTPMethods[HTTPMethods["GET"] = 0] = "GET";
HTTPMethods[HTTPMethods["HEAD"] = 1] = "HEAD";
HTTPMethods[HTTPMethods["POST"] = 2] = "POST";
HTTPMethods[HTTPMethods["PUT"] = 3] = "PUT";
HTTPMethods[HTTPMethods["DELETE"] = 4] = "DELETE";
})(HTTPMethods || (HTTPMethods = {}));
// AJAX functions
var AJAX = (function () {
// Prepares the XMLHttpRequest and stuff
function AJAX() {
this.send = null;
this.request = new XMLHttpRequest();
this.callbacks = new Object();
this.headers = new Object();
}
// Start
AJAX.prototype.start = function (method) {
var _this = this;
// Open the connection
this.request.open(HTTPMethods[method], this.url, true);
// Set headers
this.prepareHeaders();
// Watch the ready state
this.request.onreadystatechange = function () {
// Only invoke when complete
if (_this.request.readyState === 4) {
// Check if a callback if present
if ((typeof _this.callbacks[_this.request.status]).toLowerCase() === 'function') {
_this.callbacks[_this.request.status]();
}
else {
if ((typeof _this.callbacks['0']).toLowerCase() === 'function') {
// Call that
_this.callbacks['0']();
}
}
}
};
this.request.send(this.send);
};
// Stop
AJAX.prototype.stop = function () {
this.request = null;
};
// Add post data
AJAX.prototype.setSend = function (data) {
// Storage array
var store = new Array();
// Iterate over the object and them in the array with an equals sign inbetween
for (var item in data) {
store.push(encodeURIComponent(item) + "=" + encodeURIComponent(data[item]));
}
// Assign to send
this.send = store.join('&');
};
// Set raw post
AJAX.prototype.setRawSend = function (data) {
this.send = data;
};
// Get response
AJAX.prototype.response = function () {
return this.request.responseText;
};
// Set charset
AJAX.prototype.contentType = function (type, charset) {
if (charset === void 0) { charset = null; }
this.addHeader('Content-Type', type + ';charset=' + (charset ? charset : 'utf-8'));
};
// Add a header
AJAX.prototype.addHeader = function (name, value) {
// Attempt to remove a previous instance
this.removeHeader(name);
// Add the new header
this.headers[name] = value;
};
// Remove a header
AJAX.prototype.removeHeader = function (name) {
if ((typeof this.headers[name]).toLowerCase() !== 'undefined') {
delete this.headers[name];
}
};
// Prepare request headers
AJAX.prototype.prepareHeaders = function () {
for (var header in this.headers) {
this.request.setRequestHeader(header, this.headers[header]);
}
};
// Adds a callback
AJAX.prototype.addCallback = function (status, callback) {
// Attempt to remove previous instances
this.removeCallback(status);
// Add the new callback
this.callbacks[status] = callback;
};
// Delete a callback
AJAX.prototype.removeCallback = function (status) {
// Delete the callback if present
if ((typeof this.callbacks[status]).toLowerCase() === 'function') {
delete this.callbacks[status];
}
};
// Sets the URL
AJAX.prototype.setUrl = function (url) {
this.url = url;
};
return AJAX;
})();

View file

@ -142,3 +142,135 @@ class utf8 {
return decodeURIComponent(escape(string));
}
}
// HTTP methods
enum HTTPMethods {
GET,
HEAD,
POST,
PUT,
DELETE
}
// AJAX functions
class AJAX {
// XMLHTTPRequest container
private request: XMLHttpRequest;
private callbacks: Object;
private headers: Object;
private url: string;
private send: string = null;
// Prepares the XMLHttpRequest and stuff
constructor() {
this.request = new XMLHttpRequest();
this.callbacks = new Object();
this.headers = new Object();
}
// Start
public start(method: HTTPMethods): void {
// Open the connection
this.request.open(HTTPMethods[method], this.url, true);
// Set headers
this.prepareHeaders();
// Watch the ready state
this.request.onreadystatechange = () => {
// Only invoke when complete
if (this.request.readyState === 4) {
// Check if a callback if present
if ((typeof this.callbacks[this.request.status]).toLowerCase() === 'function') {
this.callbacks[this.request.status]();
} else { // Else check if there's a generic fallback present
if ((typeof this.callbacks['0']).toLowerCase() === 'function') {
// Call that
this.callbacks['0']();
}
}
}
}
this.request.send(this.send);
}
// Stop
public stop(): void {
this.request = null;
}
// Add post data
public setSend(data: Object): void {
// Storage array
var store: Array<string> = new Array<string>();
// Iterate over the object and them in the array with an equals sign inbetween
for (var item in data) {
store.push(encodeURIComponent(item) + "=" + encodeURIComponent(data[item]));
}
// Assign to send
this.send = store.join('&');
}
// Set raw post
public setRawSend(data: string) {
this.send = data;
}
// Get response
public response(): string {
return this.request.responseText;
}
// Set charset
public contentType(type: string, charset: string = null): void {
this.addHeader('Content-Type', type + ';charset=' + (charset ? charset : 'utf-8'));
}
// Add a header
public addHeader(name: string, value: string): void {
// Attempt to remove a previous instance
this.removeHeader(name);
// Add the new header
this.headers[name] = value;
}
// Remove a header
public removeHeader(name: string): void {
if ((typeof this.headers[name]).toLowerCase() !== 'undefined') {
delete this.headers[name];
}
}
// Prepare request headers
public prepareHeaders(): void {
for (var header in this.headers) {
this.request.setRequestHeader(header, this.headers[header]);
}
}
// Adds a callback
public addCallback(status: number, callback: Function): void {
// Attempt to remove previous instances
this.removeCallback(status);
// Add the new callback
this.callbacks[status] = callback;
}
// Delete a callback
public removeCallback(status: number): void {
// Delete the callback if present
if ((typeof this.callbacks[status]).toLowerCase() === 'function') {
delete this.callbacks[status];
}
}
// Sets the URL
public setUrl(url: string): void {
this.url = url;
}
}