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