Fixing request & response behavior

With regard to login and report creation.
This commit is contained in:
Dennis Eichhorn 2016-05-03 20:43:52 +02:00
parent cd3b8a221a
commit 4458ddc121
6 changed files with 193 additions and 123 deletions

View File

@ -10,7 +10,7 @@
(function (jsOMS, undefined)
{
jsOMS.Autoloader.defineNamespace('jsOMS.Message.Request');
/**
* @constructor
*
@ -19,38 +19,38 @@
*/
jsOMS.Message.Request.Request = function (uri, method, type)
{
this.uri = typeof uri !== 'undefined' ? uri : null;
this.method = typeof method !== 'undefined' ? method : jsOMS.Message.Request.RequestMethod.GET;
this.uri = typeof uri !== 'undefined' ? uri : null;
this.method = typeof method !== 'undefined' ? method : jsOMS.Message.Request.RequestMethod.GET;
this.requestHeader = [];
this.success = null;
this.type = typeof type !== 'undefined' ? type : jsOMS.Message.Response.ResponseType.JSON;
this.data = {};
this.xhr = new XMLHttpRequest();
this.success = null;
this.type = typeof type !== 'undefined' ? type : jsOMS.Message.Response.ResponseType.JSON;
this.data = {};
this.xhr = new XMLHttpRequest();
};
jsOMS.Message.Request.Request.getBrowser = function()
jsOMS.Message.Request.Request.getBrowser = function ()
{
if((!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0) {
if ((!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0) {
return jsOMS.Message.Request.BrowserType.OPERA;
} else if(typeof InstallTrigger !== 'undefined') {
return jsOMS.Message.Request.BrowserType.FIREFOX;
} else if(Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0) {
} else if (typeof InstallTrigger !== 'undefined') {
return jsOMS.Message.Request.BrowserType.FIREFOX;
} else if (Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0) {
return jsOMS.Message.Request.BrowserType.SAFARI;
} else if(/*@cc_on!@*/false || !!document.documentMode) {
} else if (/*@cc_on!@*/false || !!document.documentMode) {
return jsOMS.Message.Request.BrowserType.IE;
} else if(!!window.StyleMedia) {
} else if (!!window.StyleMedia) {
return jsOMS.Message.Request.BrowserType.EDGE;
} else if(!!window.chrome && !!window.chrome.webstore) {
} else if (!!window.chrome && !!window.chrome.webstore) {
return jsOMS.Message.Request.BrowserType.CHROME;
} else if((isChrome || isOpera) && !!window.CSS) {
} else if ((isChrome || isOpera) && !!window.CSS) {
return jsOMS.Message.Request.BrowserType.BLINK;
}
};
jsOMS.Message.Request.Request.getOS = function()
jsOMS.Message.Request.Request.getOS = function ()
{
for(let os in jsOMS.Message.Request.OSType) {
if(navigator.appVersion.toLowerCase().indexOf(jsOMS.Message.Request.OSType[os]) !== -1) {
for (let os in jsOMS.Message.Request.OSType) {
if (navigator.appVersion.toLowerCase().indexOf(jsOMS.Message.Request.OSType[os]) !== -1) {
return jsOMS.Message.Request.OSType[os];
}
}

View File

@ -21,5 +21,49 @@
*/
jsOMS.Message.Request.RequestManager = function ()
{
this.groups = {};
this.callbacks = {};
};
jsOMS.Message.Request.RequestManager.prototype.addGroup = function(id, group)
{
if(typeof this.groups[group] == 'undefined') {
this.groups[group] = {};
}
this.groups[group][id] = false;
};
jsOMS.Message.Request.RequestManager.prototype.hasOutstanding = function(group)
{
if(typeof this.groups[group] === 'undefined') {
return false;
}
for (let id in this.groups[group]) {
if (!this.groups[group][id]) {
return true;
}
}
return false;
};
jsOMS.Message.Request.RequestManager.prototype.triggerDone = function(id, group)
{
if(typeof this.groups[group] !== 'undefined') {
this.groups[group][id] = true;
}
if(!this.hasOutstanding(group)) {
this.callbacks[group]();
delete this.callbacks[group];
delete this.groups[group];
}
};
jsOMS.Message.Request.RequestManager.prototype.setDone = function(group, callback)
{
this.callbacks[group] = callback;
};
}(window.jsOMS = window.jsOMS || {}));

View File

@ -64,7 +64,6 @@
*/
jsOMS.Message.Response.ResponseManager.prototype.run = function (key, data, request)
{
console.log(data);
if (typeof request !== 'undefined' && typeof this.messages[key][request] !== 'undefined') {
this.messages[key][request](data);
} else if (typeof this.messages[key] !== 'undefined') {

View File

@ -7,45 +7,45 @@
* @license OMS License 1.0
* @version 1.0.0 * @since 1.0.0
*/
(function (jsOMS, undefined)
{
(function (jsOMS, undefined)
{
jsOMS.Autoloader.defineNamespace('jsOMS.UI');
/**
* @constructor
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
jsOMS.UI.FormManager = function(app)
{
this.app = app;
this.forms = {};
jsOMS.UI.FormManager = function (app)
{
this.app = app;
this.forms = {};
this.ignore = {};
};
jsOMS.UI.FormManager.prototype.get = function(id)
jsOMS.UI.FormManager.prototype.get = function (id)
{
return this.forms[id];
};
jsOMS.UI.FormManager.prototype.isIgnored = function(id)
jsOMS.UI.FormManager.prototype.isIgnored = function (id)
{
return this.ignore.indexOf(id) !== -1;
};
jsOMS.UI.FormManager.prototype.unbind = function(id)
jsOMS.UI.FormManager.prototype.unbind = function (id)
{
};
jsOMS.UI.FormManager.prototype.bind = function(id)
jsOMS.UI.FormManager.prototype.bind = function (id)
{
if (typeof id !== 'undefined' && typeof this.ignore[id] === 'undefined') {
this.bindForm(id)
} else {
let forms = document.getElementsByTagName('form'),
length = forms.length;
let forms = document.getElementsByTagName('form'),
length = forms.length;
for (var i = 0; i < length; i++) {
if (typeof this.ignore[forms[i].id] === 'undefined') {
@ -55,9 +55,9 @@
}
};
jsOMS.UI.FormManager.prototype.bindForm = function(id)
jsOMS.UI.FormManager.prototype.bindForm = function (id)
{
if(typeof id === 'undefined' || !id) {
if (typeof id === 'undefined' || !id) {
this.app.logger.info('A form doesn\'t have an ID.');
return;
}
@ -66,22 +66,23 @@
this.unbind(id);
if(typeof this.ignore[id] === 'undefined') {
if (typeof this.ignore[id] === 'undefined') {
this.forms[id] = new jsOMS.Views.FormView(id);
}
this.forms[id].getSubmit().addEventListener('click', function(event) {
this.forms[id].getSubmit().addEventListener('click', function (event)
{
jsOMS.preventAll(event);
self.submit(self.forms[id]);
});
};
jsOMS.UI.FormManager.prototype.unbindForm = function(id)
jsOMS.UI.FormManager.prototype.unbindForm = function (id)
{
// todo: do i need the findex? can't i just use id?
let findex = 0;
if((findex = this.forms[id]) !== 'undefined') {
if ((findex = this.forms[id]) !== 'undefined') {
this.forms[id].unbind();
this.forms.splice(findex, 1);
@ -91,22 +92,34 @@
return false;
};
jsOMS.UI.FormManager.prototype.submit = function(form)
jsOMS.UI.FormManager.prototype.submit = function (form)
{
if(!form.isValid()) {
/* Handle injects */
let self = this,
injects = form.getSubmitInjects(),
counter = 0;
for (let property in injects) {
counter++;
this.app.requestManager.addGroup(counter, form.getId());
injects[property](form.getElement(), counter, form.getId());
}
this.app.requestManager.setDone(form.getId(), function() {self.submitForm(form)});
this.app.requestManager.triggerDone('?', form.getId());
};
jsOMS.UI.FormManager.prototype.submitForm = function(form)
{
if (!form.isValid()) {
this.app.logger.debug('Form "' + form.getId() + '" has invalid values.');
return;
}
/* Handle injects */
let injects = form.getSubmitInjects();
for(let property in injects) {
injects[property](form.getElement());
}
/* Handle default submit */
let request = new jsOMS.Message.Request.Request(),
self = this;
self = this;
request.setData(form.getData());
request.setType(jsOMS.Message.Response.ResponseType.JSON);
@ -116,24 +129,29 @@
request.setSuccess(function (xhr)
{
try {
let o = JSON.parse(xhr.response),
response = new jsOMS.Message.Response.Response(o),
responseLength = response.count(),
tempResponse = null,
success = null;
let o = JSON.parse(xhr.response),
response = new jsOMS.Message.Response.Response(o),
responseLength = response.count(),
tempResponse = null,
success = null;
/* Handle responses (can be multiple response object) */
for (let k = 0; k < responseLength; k++) {
tempResponse = response.getByIndex(k);
if((success = form.getSuccess()) !== null) {
if ((success = form.getSuccess()) !== null) {
success(tempResponse);
} else {
self.app.responseManager.run(tempResponse.type, tempResponse, request);
}
}
} catch (exception) {
self.app.logger.error('Invalid form response: ' + JSON.stringify(xhr));
} catch (e) {
console.log(e);
self.app.logger.error('Invalid form response. \n' +
'URL: ' + form.getAction() + '\n' +
'Request: ' + JSON.stringify(form.getData()) + '\n' +
'Response: ' + xhr.response
);
return false;
}

View File

@ -1,8 +1,10 @@
(function (jsOMS, undefined) {
(function (jsOMS, undefined)
{
"use strict";
jsOMS.Autoloader.defineNamespace('jsOMS.Views');
jsOMS.Views.FormView = function (id) {
jsOMS.Views.FormView = function (id)
{
this.id = id;
this.initializeMembers();
@ -10,104 +12,108 @@
this.success = null;
};
jsOMS.Views.FormView.prototype.initializeMembers = function()
jsOMS.Views.FormView.prototype.initializeMembers = function ()
{
this.submitInjects = [];
this.method = 'POST';
this.action = '';
this.method = 'POST';
this.action = '';
};
jsOMS.Views.FormView.prototype.getMethod = function()
jsOMS.Views.FormView.prototype.getMethod = function ()
{
return this.method;
};
jsOMS.Views.FormView.prototype.getAction = function()
jsOMS.Views.FormView.prototype.getAction = function ()
{
return this.action;
};
jsOMS.Views.FormView.prototype.getSubmit = function()
jsOMS.Views.FormView.prototype.getSubmit = function ()
{
return document.getElementById(this.id).querySelectorAll('input[type=submit]')[0];
};
jsOMS.Views.FormView.prototype.getSuccess = function()
jsOMS.Views.FormView.prototype.getSuccess = function ()
{
return this.success;
};
jsOMS.Views.FormView.prototype.setSuccess = function(callback)
jsOMS.Views.FormView.prototype.setSuccess = function (callback)
{
this.success = callback;
};
jsOMS.Views.FormView.prototype.injectSubmit = function(id, callback)
jsOMS.Views.FormView.prototype.injectSubmit = function (id, callback)
{
this.submitInjects.push(callback);
};
jsOMS.Views.FormView.prototype.getFormElements = function()
jsOMS.Views.FormView.prototype.getFormElements = function ()
{
let form = document.getElementById(this.id),
selects = form.getElementsByTagName('select'),
textareas = form.getElementsByTagName('textarea'),
inputs = form.getElementsByTagName('input'),
external = document.querySelectorAll('[form='+this.id+']'),
elements = Array.prototype.slice.call(inputs).concat(Array.prototype.slice.call(selects), Array.prototype.slice.call(textareas), Array.prototype.slice.call(external));
let form = document.getElementById(this.id),
selects = form.getElementsByTagName('select'),
textareas = form.getElementsByTagName('textarea'),
inputs = form.getElementsByTagName('input'),
external = document.querySelectorAll('[form=' + this.id + ']'),
elements = Array.prototype.slice.call(inputs).concat(Array.prototype.slice.call(selects), Array.prototype.slice.call(textareas), Array.prototype.slice.call(external));
return elements;
};
jsOMS.Views.FormView.prototype.getData = function()
jsOMS.Views.FormView.prototype.getData = function ()
{
let data = {},
elements = this.getFormElements(),
length = elements.length,
i = 0;
let data = {},
elements = this.getFormElements(),
length = elements.length,
i = 0;
for(i = 0; i < length; i++) {
for (i = 0; i < length; i++) {
data[jsOMS.Views.FormView.getElementId(elements[i])] = elements[i].value;
}
return data;
};
jsOMS.Views.FormView.prototype.getId = function()
jsOMS.Views.FormView.prototype.getId = function ()
{
return this.id;
};
jsOMS.Views.FormView.prototype.isValid = function()
jsOMS.Views.FormView.prototype.isValid = function ()
{
let elements = this.getFormElements(),
length = elements.length;
length = elements.length;
for(let i = 0; i < length; i++) {
if((elements[i].required && elements[i].value === '') || (typeof elements[i].pattern !== 'undefined' && elements[i].pattern !== '' && (new RegExp(elements[i].pattern)).test(elements[i].value))) {
return false;
try {
for (let i = 0; i < length; i++) {
if ((elements[i].required && elements[i].value === '') || (typeof elements[i].pattern !== 'undefined' && elements[i].pattern !== '' && (new RegExp(elements[i].pattern)).test(elements[i].value))) {
return false;
}
}
} catch (e) {
console.log(e);
}
return true;
};
jsOMS.Views.FormView.prototype.getElement = function()
jsOMS.Views.FormView.prototype.getElement = function ()
{
return document.getElementById(this.getId());
};
jsOMS.Views.FormView.getElementId = function(e)
jsOMS.Views.FormView.getElementId = function (e)
{
let id = e.getAttribute('name');
if(id === null) {
if (id === null) {
id = e.getAttribute('id');
} else {
return id;
}
if(id === null) {
if (id === null) {
id = e.getAttribute('type');
} else {
return id;
@ -116,12 +122,12 @@
return id;
};
jsOMS.Views.FormView.prototype.getSubmitInjects = function()
jsOMS.Views.FormView.prototype.getSubmitInjects = function ()
{
return this.submitInjects;
};
jsOMS.Views.FormView.prototype.bind = function()
jsOMS.Views.FormView.prototype.bind = function ()
{
this.clean();
@ -129,11 +135,11 @@
this.action = document.getElementById(this.id).action;
let elements = this.getFormElements(),
length = elements.length,
i = 0;
length = elements.length,
i = 0;
for(i = 0; i < length; i++) {
switch(elements[i].tagName) {
for (i = 0; i < length; i++) {
switch (elements[i].tagName) {
case 'input':
jsOMS.UI.Input.bind(elements[i])
break;
@ -146,18 +152,19 @@
case 'button':
this.bindButton(elements[i]);
break;
};
}
;
}
};
jsOMS.Views.FormView.prototype.unbind = function()
jsOMS.Views.FormView.prototype.unbind = function ()
{
let elements = this.getFormElements(),
length = elements.length,
i = 0;
length = elements.length,
i = 0;
for(i = 0; i < length; i++) {
switch(elements[i].tagName) {
for (i = 0; i < length; i++) {
switch (elements[i].tagName) {
case 'input':
jsOMS.UI.Input.unbind(elements[i])
break;
@ -170,11 +177,12 @@
case 'button':
this.bindButton(elements[i]);
break;
};
}
;
}
};
jsOMS.Views.FormView.prototype.clean = function()
jsOMS.Views.FormView.prototype.clean = function ()
{
this.unbind();
this.initializeMembers();

View File

@ -1,19 +1,20 @@
(function (jsOMS, undefined) {
jsOMS.Autoloader = {};
jsOMS.Autoloader.loaded = [];
(function (jsOMS, undefined)
{
jsOMS.Autoloader = {};
jsOMS.Autoloader.loaded = [];
jsOMS.Autoloader.namespaced = [];
jsOMS.Autoloader.defineNamespace = function(namespace)
jsOMS.Autoloader.defineNamespace = function (namespace)
{
if(jsOMS.Autoloader.namespaced.indexOf(namespace) === -1) {
if (jsOMS.Autoloader.namespaced.indexOf(namespace) === -1) {
let paths = namespace.split('.');
paths.splice(0, 1);
let length = paths.length,
current = jsOMS;
let length = paths.length,
current = jsOMS;
for(let i = 0; i < length; i++) {
if(typeof current[paths[i]] === 'undefined') {
for (let i = 0; i < length; i++) {
if (typeof current[paths[i]] === 'undefined') {
current[paths[i]] = {};
}
@ -24,33 +25,33 @@
}
};
jsOMS.Autoloader.initPreloaded = function()
jsOMS.Autoloader.initPreloaded = function ()
{
let scripts = document.getElementsByTagName('script'),
length = scripts.length;
length = scripts.length;
for(let i = 0; i < length; i++) {
for (let i = 0; i < length; i++) {
scripts[i].src.replace(URL + '/', '');
if(jsOMS.Autoloader.loaded.indexOf(scripts[i].src) === -1) {
if (jsOMS.Autoloader.loaded.indexOf(scripts[i].src) === -1) {
jsOMS.Autoloader.loaded.push(scripts[i].src);
}
}
};
jsOMS.Autoloader.setPreloaded = function(file)
jsOMS.Autoloader.setPreloaded = function (file)
{
if(jsOMS.Autoloader.loaded.indexOf(file) === -1) {
if (jsOMS.Autoloader.loaded.indexOf(file) === -1) {
jsOMS.Autoloader.loaded.push(file);
}
};
jsOMS.Autoloader.include = function(file, callback)
jsOMS.Autoloader.include = function (file, callback)
{
let length = file.length;
for(let i = 0; i < length; i++) {
if(jsOMS.Autoloader.loaded.indexOf(file) === -1) {
for (let i = 0; i < length; i++) {
if (jsOMS.Autoloader.loaded.indexOf(file) === -1) {
// todo: implement asset loading and pass callback
jsOMS.Autoloader.loaded.push(file);