Solucionado error en login al actualizar, solucionado fallo al borrar fila en Htk.Repeater

This commit is contained in:
Juan Ferrer Toribio 2016-05-05 17:47:45 +02:00
parent fe070a7e6b
commit 8e42a5e262
13 changed files with 271 additions and 190 deletions

2
debian/changelog vendored
View File

@ -1,4 +1,4 @@
hedera-web (1.337-deb8) stable; urgency=low hedera-web (1.340-deb8) stable; urgency=low
* Initial Release. * Initial Release.

View File

@ -12,8 +12,8 @@ Db.CalcSum = new Class
if (this._func) if (this._func)
{ {
this.form.row = row; this._set.row = row;
value = this._func (this.form); value = this._func (this._set);
} }
else else
value = this._model.getByIndex (row, this.columnIndex); value = this._model.getByIndex (row, this.columnIndex);

View File

@ -21,8 +21,8 @@ Db.Calc = new Class
,'row-inserted': this.onRowInsert ,'row-inserted': this.onRowInsert
}); });
var form = new Db.Form ({model: x}); var set = new Db.SimpleIterator ({model: x});
this.link ({form: form}); this.link ({_set: set});
} }
,get: function () ,get: function ()
{ {

View File

@ -28,8 +28,8 @@ Db.Conn.implement
({ ({
Extends: Vn.Object Extends: Vn.Object
,connected: false ,_connected: false
,requestsCount: 0 ,_requestsCount: 0
/** /**
* Initilizes the connection object. * Initilizes the connection object.
@ -47,92 +47,71 @@ Db.Conn.implement
* @param {Boolean} remember Specifies if the user should be remembered * @param {Boolean} remember Specifies if the user should be remembered
* @param {Function} openCallback The function to call when operation is done * @param {Function} openCallback The function to call when operation is done
**/ **/
,open: function (user, pass, remember, openCallback, guest) ,open: function (user, pass, remember, callback, guest)
{ {
this.signalEmit ('loading-changed', true); this.signalEmit ('loading-changed', true);
var request = new Vn.HttpRequest ();
request.add ({'action': 'login'});
if (user != null) if (user != null)
{ {
request.add var params = {
({
'user': user 'user': user
,'password': pass ,'password': pass
,'remember': remember ,'remember': remember
}); };
} }
else if (guest) else if (guest)
request.add ({'guest': true}); var params = {'guest': true};
request.send ('rest.php', var request = new Vn.JsonRequest ('login');
this.opened.bind (this, request, openCallback)); request.send (params,
this._onOpen.bind (this, callback));
} }
/* /*
* Called when open operation is done. * Called when open operation is done.
*/ */
,opened: function (request, openCallback, success) ,_onOpen: function (callback, request, json, error)
{ {
var error = null; this._connected = json == true;
var openSuccess = false;
if (success)
try {
var json = request.getJson ();
openSuccess = json.data == true;
if (json.error)
error = new Vn.Error (
json.error.domain,
json.error.code,
json.error.message);
}
catch (e)
{
error = e;
}
if (openSuccess)
{
this.connected = true;
this.signalEmit ('openned');
}
this.signalEmit ('loading-changed', false); this.signalEmit ('loading-changed', false);
if (openCallback) if (this._connected)
openCallback (this, openSuccess, error); this.signalEmit ('openned');
if (error)
this.signalEmit ('error', error);
if (callback)
callback (this, this._connected, error);
} }
/** /**
* Closes the connection to the database. * Closes the connection to the database.
* *
* @param {Function} closeCallback The function to call when operation is done * @param {Function} callback The function to call when operation is done
**/ **/
,close: function (closeCallback) ,close: function (callback)
{ {
this.signalEmit ('loading-changed', true); this.signalEmit ('loading-changed', true);
var request = new Vn.HttpRequest (); var request = new Vn.JsonRequest ('logout');
request.add ({'action': 'logout'}); request.send (null,
request.send ('rest.php', this._onClose.bind (this, callback));
this.closed.bind (this, closeCallback));
} }
/* /*
* Called when close operation is done. * Called when close operation is done.
*/ */
,closed: function (closeCallback) ,_onClose: function (callback, request, json, error)
{ {
this.connected = false; this._connected = false;
this.signalEmit ('loading-changed', false);
this.signalEmit ('closed'); this.signalEmit ('closed');
this.signalEmit ('loading-changed', false); if (error)
this.signalEmit ('error', error);
if (closeCallback) if (callback)
closeCallback (this); callback (this, json == true, error);
} }
/** /**
@ -143,19 +122,14 @@ Db.Conn.implement
**/ **/
,execSql: function (sql, callback) ,execSql: function (sql, callback)
{ {
this.requestsCount++; this._requestsCount++;
if (this.requestsCount == 1) if (this._requestsCount === 1)
this.signalEmit ('loading-changed', true); this.signalEmit ('loading-changed', true);
var httpRequest = new Vn.HttpRequest () var request = new Vn.JsonRequest ('query');
httpRequest.add request.send ({'sql': sql},
({ this._onExec.bind (this, callback));
'action': 'query'
,'sql': sql
});
httpRequest.send ('rest.php',
this.execDone.bind (this, httpRequest, callback));
} }
/** /**
@ -193,37 +167,21 @@ Db.Conn.implement
/* /*
* Called when a query is executed. * Called when a query is executed.
*/ */
,execDone: function (httpRequest, callback, success) ,_onExec: function (callback, request, json, error)
{ {
var e; this._requestsCount--;
var error = null;
var results = null;
this.requestsCount--; if (this._requestsCount === 0)
if (this.requestsCount == 0)
this.signalEmit ('loading-changed', false); this.signalEmit ('loading-changed', false);
if (!success) if (json)
{
error = new Vn.Error ('Conn', 'connError', _('ConnError'));
this.signalEmit ('error', error);
}
else
try { try {
var json = httpRequest.getJson (); if (json && json instanceof Array)
results = json.data; for (var i = 0; i < json.length; i++)
if (json[i] !== true)
if (json.error !== null)
error = new Vn.Error (json.error.domain,
json.error.code, json.error.message);
if (results instanceof Array)
for (var i = 0; i < results.length; i++)
if (results[i] !== true)
{ {
var data = results[i].data; var data = json[i].data;
var columns = results[i].columns; var columns = json[i].columns;
for (var j = 0; j < columns.length; j++) for (var j = 0; j < columns.length; j++)
{ {
@ -260,7 +218,7 @@ Db.Conn.implement
if (callback) if (callback)
try { try {
callback (new Db.ResultSet (results, error)); callback (new Db.ResultSet (json, error));
} }
catch (e) catch (e)
{ {

View File

@ -8,6 +8,7 @@ Vn.includeLib ('db',
,'result-set' ,'result-set'
,'model' ,'model'
,'iterator' ,'iterator'
,'simple-iterator'
,'form' ,'form'
,'param' ,'param'
,'query' ,'query'

View File

@ -0,0 +1,70 @@
/**
* A light iterator for models.
**/
Db.SimpleIterator = new Class
({
Extends: Vn.Object
,Implements: Db.Iterator
,Properties:
{
/**
* The model associated to this form.
**/
model:
{
type: Db.Model
,set: function (x)
{
this._model = x;
}
,get: function ()
{
return this._model;
}
},
/**
* The row where the form positioned, has -1 if the row is unselected.
**/
row:
{
type: Number
,set: function (x)
{
this._row = x;
}
,get: function ()
{
return this._row;
}
},
/**
* The number of rows in the form.
**/
numRows:
{
type: Number
,get: function ()
{
if (this._model)
return this._model.numRows;
return 0;
}
},
/**
* Checks if the form data is ready.
**/
ready:
{
type: Boolean
,get: function ()
{
if (this._model)
return this._model.ready;
return false;
}
}
}
});

View File

@ -107,8 +107,6 @@ Vn.App = new Class
var error = new Error (message); var error = new Error (message);
error.fileName = file; error.fileName = file;
error.lineNumber = line; error.lineNumber = line;
Htk.Toast.showError (_('There was an internal error'));
this._notifyError (error); this._notifyError (error);
} }
@ -123,16 +121,20 @@ Vn.App = new Class
switch (error.domain) switch (error.domain)
{ {
case 'Auth': case 'Auth':
Htk.Toast.showError (_('You\'ve been too idle')); if (error.code === 'badLogin')
Htk.Toast.showError (_('Invalid login'));
else if (error.code === 'sessionExpired')
Htk.Toast.showError (_('You\'ve been too idle'));
if (this._gui) if (this._gui)
this._gui.logout (); this._gui.logout ();
break; break;
case 'Version': case 'Version':
this._newVersion (error); this._newVersion (error);
break; break;
case 'User': case 'User':
Htk.Toast.showError (error.message); Htk.Toast.showError (error.message);
break; break;
default: default:
console.error (error.message); console.error (error.message);
Htk.Toast.showError (_('There was an internal error')); Htk.Toast.showError (_('There was an internal error'));
@ -140,7 +142,6 @@ Vn.App = new Class
else else
{ {
console.error (error); console.error (error);
Htk.Toast.showError (_('There was an internal error'));
this._notifyError (error); this._notifyError (error);
} }
} }
@ -174,18 +175,20 @@ Vn.App = new Class
,_notifyError: function (error) ,_notifyError: function (error)
{ {
if (error instanceof Error) Htk.Toast.showError (_('There was an internal error'));
{
var httpRequest = new Vn.HttpRequest () var params = {
httpRequest.add 'file': error.fileName
({ ,'line': error.lineNumber
'file': error.fileName ,'message': error.message
,'line': error.lineNumber ,'stack': error.stack
,'message': error.message };
,'stack': error.stack
}); var request = new XMLHttpRequest ();
httpRequest.send ('log.php'); request.open ('post', 'log.php', true);
} request.setRequestHeader ('Content-Type',
'application/x-www-form-urlencoded');
request.send (Vn.Url.makeUri (params));
} }
,_freeLogin: function () ,_freeLogin: function ()

View File

@ -92,10 +92,7 @@ Vn.Login = new Class
if (success) if (success)
this.signalEmit ('login'); this.signalEmit ('login');
else else
if (error instanceof Vn.Error && error.domain === 'Auth') this._focusUserInput ();
Htk.Toast.showError (_('Invalid login'));
this._focusUserInput ();
} }
}); });

View File

@ -34,23 +34,54 @@ Vn.Tpv = new Class
{ {
if (amount > 0) if (amount > 0)
{ {
var request = new Vn.HttpRequest (); var params = {
request.add 'amount': parseInt (amount)
({
'action': 'tpv'
,'amount': parseInt (amount)
,'urlOk': this._makeUrl ('ok') ,'urlOk': this._makeUrl ('ok')
,'urlKo': this._makeUrl ('ko') ,'urlKo': this._makeUrl ('ko')
,'company': company ,'company': company
}); };
request.send ('rest.php', var request = new Vn.JsonRequest ('tpv');
this._onTransactionStart.bind (this, request)); request.send (params,
this._onTransactionStart.bind (this));
} }
else if (!isNaN (amount)) else if (!isNaN (amount))
Htk.Toast.showError (_('AmountError')); Htk.Toast.showError (_('AmountError'));
} }
,_onTransactionStart: function (request, json, error)
{
if (json)
{
var form = document.createElement ('form');
form.method = 'post';
form.action = json.url;
document.body.appendChild (form);
var fieldsMap =
{
'Ds_SignatureVersion': 'HMAC_SHA256_V1'
,'Ds_MerchantParameters': json.params
,'Ds_Signature': json.signature
};
for (var field in fieldsMap)
{
var input = document.createElement ('input');
input.type = 'hidden';
input.name = field;
form.appendChild (input);
if (fieldsMap[field])
input.value = fieldsMap[field];
}
form.submit ();
}
else
Htk.Toast.showWarning (_('PayError'));
}
,retryPay: function () ,retryPay: function ()
{ {
var batch = new Sql.Batch (); var batch = new Sql.Batch ();
@ -74,58 +105,6 @@ Vn.Tpv = new Class
Htk.Toast.showError (_('AmountError')); Htk.Toast.showError (_('AmountError'));
} }
,_onTransactionStart: function (request, success)
{
var data = null;
var error = null;
if (success)
try {
var json = request.getJson ();
var data = json.data;
if (json.error)
error = new Vn.Error (
json.error.domain,
json.error.code,
json.error.message);
}
catch (e)
{
error = e;
}
if (success && data)
{
var form = document.createElement ('form');
form.method = 'post';
form.action = data.url;
document.body.appendChild (form);
var fieldsMap =
{
'Ds_SignatureVersion': 'HMAC_SHA256_V1'
,'Ds_MerchantParameters': data.params
,'Ds_Signature': data.signature
};
for (var field in fieldsMap)
{
var input = document.createElement ('input');
input.type = 'hidden';
input.name = field;
form.appendChild (input);
if (fieldsMap[field])
input.value = fieldsMap[field];
}
form.submit ();
}
else
Htk.Toast.showWarning (_('PayError'));
}
,_makeUrl: function (status, order) ,_makeUrl: function (status, order)
{ {
var path = location.protocol +'//'+ location.host; var path = location.protocol +'//'+ location.host;

View File

@ -23,8 +23,8 @@ Htk.Grid = new Class
,'updatable-changed': this.onUpdatableChange ,'updatable-changed': this.onUpdatableChange
}); });
var form = new Db.Form ({model: x}); var set = new Db.SimpleIterator ({model: x});
this.link ({form: form}); this.link ({_set: set});
this.onUpdatableChange (); this.onUpdatableChange ();
this.onModelChange (); this.onModelChange ();
@ -61,7 +61,7 @@ Htk.Grid = new Class
} }
,_model: null ,_model: null
,form: null ,_set: null
,columns: new Array () ,columns: new Array ()
,internalColumn: null ,internalColumn: null
,internalColumns: 0 ,internalColumns: 0
@ -120,8 +120,8 @@ Htk.Grid = new Class
if (column.renderer) if (column.renderer)
{ {
this.form.row = row; this._set.row = row;
column.renderer (column, this.form); column.renderer (column, this._set);
} }
return column.render (tr); return column.render (tr);

View File

@ -106,27 +106,27 @@ Htk.Repeater = new Class
,getForm: function (index) ,getForm: function (index)
{ {
return this._childsData[index].form; return this._childsData[index].set;
} }
,_buildBox: function (index) ,_buildBox: function (index)
{ {
var form = new Db.Form ({ var set = new Db.SimpleIterator ({
model: this._model, model: this._model,
row: index row: index
}); });
this._builder.add (this._formId, form); this._builder.add (this._formId, set);
var res = this._builder.load (); var res = this._builder.load ();
res.link (); res.link ();
this._childsData.push ({ this._childsData.push ({
builder: res, builder: res,
form: form set: set
}); });
if (this._renderer) if (this._renderer)
this._renderer (res, form); this._renderer (res, set);
return res.getMain (); return res.getMain ();
} }
@ -200,12 +200,17 @@ Htk.Repeater = new Class
{ {
Vn.Node.remove (this._container.childNodes[row]); Vn.Node.remove (this._container.childNodes[row]);
this._unrefChildData (row); this._unrefChildData (row);
this._childsData.splice (row, 1);
for (var i = row; i < this._model.numRows; i++)
this._childsData[i].set.row = i;
this._showNoRecordsFound (); this._showNoRecordsFound ();
} }
,_onRowUpdate: function (model, row, columns) ,_onRowUpdate: function (model, row, columns)
{ {
// this.form[row].signalEmit ('iter-changed'); this._childsData[row].set.iterChanged ();
} }
,_onRowInsert: function (model, row) ,_onRowInsert: function (model, row)
@ -224,7 +229,7 @@ Htk.Repeater = new Class
,_unrefChildData: function (index) ,_unrefChildData: function (index)
{ {
var childData = this._childsData[index]; var childData = this._childsData[index];
childData.form.unref (); childData.set.unref ();
childData.builder.unref (); childData.builder.unref ();
} }

68
web/js/vn/json-request.js Normal file
View File

@ -0,0 +1,68 @@
/**
* Handler for JSON rest requests.
**/
Vn.JsonRequest = new Class
({
initialize: function (methodName)
{
this._methodName = methodName;
}
,send: function (params, callback)
{
var url = 'rest.php?action='+ encodeURIComponent (this._methodName);
var request = new XMLHttpRequest ();
request.open ('post', url, true);
request.setRequestHeader ('Content-Type',
'application/x-www-form-urlencoded');
request.onreadystatechange =
this._onStateChange.bind (this, request, callback);
request.send (Vn.Url.makeUri (params));
}
,_onStateChange: function (request, callback)
{
if (request.readyState !== 4)
return;
var jsData = null;
var error = null;
try {
if (request.status !== 200)
throw new Error (request.statusText);
var json = JSON.parse (request.responseText);
var jsData = json.data;
var jsError = json.error;
if (jsError)
throw new Vn.Error (
jsError.domain,
jsError.code,
jsError.message);
}
catch (e)
{
jsData = null;
error = e;
}
if (callback)
callback (this, jsData, error);
}
,sendForm: function (form, callback)
{
var params = {};
var elements = form.elements;
for (var i = 0; i < elements.length; i++)
if (elements[i].name)
params[elements[i].name] = elements[i].value;
this.send (params, callback);
}
});

View File

@ -16,6 +16,6 @@ Vn.includeLib ('vn',
,'hash-param' ,'hash-param'
,'node' ,'node'
,'builder' ,'builder'
,'http-request' ,'json-request'
]); ]);