Skip to content

Commit

Permalink
Merge pull request #323 from Edirom/fix/ajax-requests
Browse files Browse the repository at this point in the history
Integrate fix/ajax-requests into develop
  • Loading branch information
bwbohl authored Feb 5, 2024
2 parents b02288a + 628dc2b commit 600f11b
Show file tree
Hide file tree
Showing 23 changed files with 361 additions and 353 deletions.
31 changes: 31 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,34 @@ For LICENSE-Details please refer to the LICENSE file in the root directory of th
### XQuery body

* Strings: escape with U+00027 APOSTROPHE: `'`


# Javascript

## AJAX calls

The class `EdiromOnline.controller.AJAXController` provides a central method `doAJAXRequest` for performing AJAX requests. The method is provided globally as `window.doAJAXRequest`.

`doAJAXRequest` takes the following arguments:

* `url`: The URL of the requestet site or end point.
* `method`: The HTTP method like `PUT`, `GET`, `POST`.
* `params`: An object containing key-value-pairs of parameters for the request.
* `successFn`: A callback function which is called when the AJAX request was successfull.
* `retryNo`: The number of retries, if the requests fails. Standard is 2 retries.
* `async`: Defines the async parameter for AJAX calls. Default is 'true'.

An example of using the function would be:

```javascript
window.doAJAXRequest('data/xql/getAnnotationMeta.xql',
'GET',
{
uri: uri,
lang: lang
},
Ext.bind(function(response){
view.setMeta(response.responseText);
}, this)
);
```
24 changes: 13 additions & 11 deletions add/data/xqm/edition.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ xquery version "3.1";
:)
module namespace edition = "http://www.edirom.de/xquery/edition";

declare namespace edirom="http://www.edirom.de/ns/1.3";
declare namespace xlink="http://www.w3.org/1999/xlink";
declare namespace edirom = "http://www.edirom.de/ns/1.3";
declare namespace xlink = "http://www.w3.org/1999/xlink";

import module namespace functx="http://www.functx.com";
import module namespace functx = "http://www.functx.com";
(:~
: Returns a JSON representation of an Edition
:
Expand All @@ -44,10 +44,10 @@ declare function edition:toJSON($uri as xs:string) as xs:string {
return
concat('
{',
'id: "', $edition/string(@xml:id), '", ',
'doc: "', $uri, '", ',
'name: "', $edition/edirom:editionName, '"',
'}')
'id: "', $edition/string(@xml:id), '", ',
'doc: "', $uri, '", ',
'name: "', $edition/edirom:editionName, '"',
'}')
};

(:~
Expand All @@ -58,7 +58,8 @@ declare function edition:toJSON($uri as xs:string) as xs:string {
declare function edition:getUris() as xs:string* {

for $edition in collection('/db/apps')//edirom:edition
return 'xmldb:exist://' || document-uri($edition/root())
return
'xmldb:exist://' || document-uri($edition/root())
};

(:~
Expand Down Expand Up @@ -156,8 +157,9 @@ declare function edition:findEdition($editionID as xs:string?) as xs:string {
$editionID
else (
let $edition := collection('/db/apps')//edirom:edition/id($editionID)
return 'xmldb:exist://' || document-uri($edition/root())
)
return
'xmldb:exist://' || document-uri($edition/root())
)
};

(:~
Expand All @@ -167,7 +169,7 @@ declare function edition:findEdition($editionID as xs:string?) as xs:string {
: @return the text contents of edirom:edition/edirom:editionName
:)
declare function edition:getName($uri as xs:string) as xs:string {
doc($uri)/edirom:edition/edirom:editionName => fn:normalize-space()
doc($uri)/edirom:edition/edirom:editionName => fn:normalize-space()
};

(:~
Expand Down
38 changes: 19 additions & 19 deletions app/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,34 @@ Ext.define('EdiromOnline.Application', {
if(editionParam !== null)
me.activeEdition = editionParam;

Ext.Ajax.request({
url: 'data/xql/getEditionURI.xql',
async: false,
method: 'GET',
params: {
window.doAJAXRequest('data/xql/getEditionURI.xql',
'GET',
{
uri: me.activeEdition
},success: function(response){
this.activeEdition = response.responseText;
},
scope: this
});

Ext.bind(function(response){
this.activeEdition = response.responseText;
}, this),
2, // retries
false // async
);

var workParam = me.getURLParameter('work');
if(workParam !== null)
me.activeWork = workParam;

Ext.Ajax.request({
url: 'data/xql/getWorkID.xql',
async: false,
method: 'GET',
params: {
window.doAJAXRequest('data/xql/getWorkID.xql',
'GET',
{
uri: me.activeEdition,
workId: me.activeWork
},success: function(response){
this.activeWork = response.responseText;
},
scope: this
});
Ext.bind(function(response){
this.activeWork = response.responseText;
}, this),
2, // retries
false // async
);

me.getController('PreferenceController').initPreferences(me.activeEdition);
me.getController('LanguageController').initLangFile(me.activeEdition, 'de');
Expand Down
35 changes: 31 additions & 4 deletions app/controller/AJAXController.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,45 @@ Ext.define('EdiromOnline.controller.AJAXController', {
window.doAJAXRequest = Ext.bind(this.doAJAXRequest, this);
},

doAJAXRequest: function(url, method, params, successFn, retryNo) {
/**
* Performs an AJAX request.
*
* @param {String} url The URL of the requestet site or end point.
* @param {String} method The HTTP method like 'PUT', 'GET', 'POST'.
* @param {Object} params An object containing key-value-pairs of parameters for the request.
* @param {Function} successFn A callback function which is called when the AJAX request was successfull.
* @param {Number} retryNo The number of retries, if the requests fails. Standard is 2 retries.
* @param {Boolean} async Defines the async parameter for AJAX calls. Default is 'true'.
*/
doAJAXRequest: function(url, method, params, successFn, retryNo, async) {
var me = this;

var editionId = this.application.activeEdition;
params = Ext.applyIf(params, {edition: editionId});

var override = window.getPreference(url, true);
var lang = window.getLanguage();
params = Ext.applyIf(params, {lang: lang});

// define some requests that will make Edirom Online fail if it looks for an override
var doNotOverride = [
'data/xql/getEditionURI.xql',
'data/xql/getPreferences.xql',
'data/xql/getWorkID.xql',
];

var override = null;

if(doNotOverride.indexOf(url) == -1)
override = window.getPreference(url, true);

if(override != null)
url = override;

if(typeof(retryNo) === 'undefined')
if(typeof retryNo === 'undefined')
retryNo = 2;

if(typeof async === 'undefined')
async = true;

var fn = Ext.bind(function(response, options, retryNoInt) {

Expand All @@ -58,7 +84,8 @@ Ext.define('EdiromOnline.controller.AJAXController', {
url: url,
method: method,
params: params,
success: fn
success: fn,
async: async
});
}
});
19 changes: 9 additions & 10 deletions app/controller/LanguageController.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,19 @@ Ext.define('EdiromOnline.controller.LanguageController', {

initLangFile: function(editionURI, lang) {

Ext.Ajax.request({
url: 'data/xql/getLanguageFile.xql',
async: false,
method: 'GET',
params: {
window.doAJAXRequest('data/xql/getLanguageFile.xql',
'GET',
{
lang: lang,
mode: 'json',
edition: editionURI
},success: function(response){

this.langFiles.add(lang, Ext.JSON.decode(response.responseText));
},
scope: this
});
Ext.bind(function(response){
this.langFiles.add(lang, Ext.JSON.decode(response.responseText));
}, this),
2, // retries
false // async
);
},

getLanguageString: function(key) {
Expand Down
14 changes: 6 additions & 8 deletions app/controller/LinkController.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,16 @@ Ext.define('EdiromOnline.controller.LinkController', {

if(singleUri.indexOf('#') != -1) {

Ext.Ajax.request({
url: 'data/xql/getInternalIdType.xql',
method: 'GET',
params: {
window.doAJAXRequest('data/xql/getInternalIdType.xql',
'GET',
{
uri: singleUri
},
success: function(response){
Ext.bind(function(response){
win.loadInternalId(singleUri.split('#')[1], response.responseText.trim());
win.show();
},
scope: this
});
}, this)
);
}else
win.showView('summaryView');

Expand Down
32 changes: 16 additions & 16 deletions app/controller/PreferenceController.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ Ext.define('EdiromOnline.controller.PreferenceController', {

initPreferences: function(editionURI) {

Ext.Ajax.request({
url: 'data/xql/getPreferences.xql',
async: false,
method: 'GET',
params: {
window.doAJAXRequest('data/xql/getPreferences.xql',
'GET',
{
mode: 'json',
edition: editionURI
},success: function(response){
this.setPreferences(Ext.JSON.decode(response.responseText));
},
scope: this
});
Ext.bind(function(response){
this.setPreferences(Ext.JSON.decode(response.responseText));
}, this),
2, // retries
false // async
);
},

setPreferences: function(preferences) {
Expand All @@ -50,14 +50,14 @@ Ext.define('EdiromOnline.controller.PreferenceController', {

for(var key in me.preferences) {
if(key.indexOf('plugin_') == 0)
Ext.Ajax.request({
url: me.preferences[key],
method: 'GET',
success: function(response){

window.doAJAXRequest(me.preferences[key],
'GET',
{},
Ext.bind(function(response){
eval(response.responseText);
},
scope: this
});
}, this)
);
}
},

Expand Down
14 changes: 6 additions & 8 deletions app/controller/navigator/Navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,22 @@ Ext.define('EdiromOnline.controller.navigator.Navigator', {
var editionId = this.application.activeEdition;
var lang = window.getLanguage('application_language');

Ext.Ajax.request({
url: 'data/xql/getNavigatorConfig.xql',
params: {
window.doAJAXRequest('data/xql/getNavigatorConfig.xql',
'GET',
{
editionId: editionId,
workId: workId,
lang: lang
},
success: function(response){

Ext.bind(function(response){
this.navigatorContents.add(workId, response.responseText);

Ext.Array.each(this.navigators, function(navigator) {
navigator.body.update(this.getNavigatorContent(workId));
navigator.setLoading(false);
}, this);
},
scope: this
});
}, this)
);
},

getNavigatorContent: function(workId) {
Expand Down
14 changes: 6 additions & 8 deletions app/controller/window/HeaderView.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ Ext.define('EdiromOnline.controller.window.HeaderView', {
var uri = view.uri;
var type = view.type;

Ext.Ajax.request({
url: 'data/xql/getHeader.xql',
method: 'GET',
params: {
window.doAJAXRequest('data/xql/getHeader.xql',
'GET',
{
uri: uri,
type: type
},
success: function(response){
Ext.bind(function(response){
view.setContent(response.responseText);
},
scope: this
});
}, this)
);
}
});
20 changes: 9 additions & 11 deletions app/controller/window/SummaryView.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,18 @@ Ext.define('EdiromOnline.controller.window.SummaryView', {
var app = EdiromOnline.getApplication();
var activeEdition = app.activeEdition

Ext.Ajax.request({
url: 'data/xql/getSummary.xql',
method: 'GET',
params: {
uri: uri,
window.doAJAXRequest('data/xql/getSummary.xql',
'GET',
{
uri: uri,
type: type,
edition: activeEdition
},
success: function (response) {
var data = response.responseText;
},
Ext.bind(function(response){
var data = response.responseText;

view.setContent(data);
},
scope: this
});
}, this)
);
}
});
Loading

0 comments on commit 600f11b

Please sign in to comment.