Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…lect#1532

- catches expired JWT token exception
- search species when offline now searches all species when first attempt returns no result
- isOffline is not overridden
  • Loading branch information
temi committed Jul 28, 2023
1 parent f276cc8 commit ae2bc50
Show file tree
Hide file tree
Showing 7 changed files with 780 additions and 137 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ plugins {
}


version "6.1-COGNITO-SNAPSHOT"
version "6.1-PWA-SNAPSHOT"
group "org.grails.plugins"

apply plugin:"eclipse"
Expand Down
142 changes: 119 additions & 23 deletions grails-app/assets/javascripts/forms-knockout-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
var progress = ko.observable();
var error = ko.observable();
var complete = ko.observable(true);
var db;

if (typeof getDB === 'function') {
db = getDB();
}

var config = valueAccessor();
config = $.extend({}, config, defaultConfig);
Expand Down Expand Up @@ -219,7 +224,27 @@
}

}).on(eventPrefix+'fail', function(e, data) {
error(data.errorThrown);
if (isOffline()) {
var file = data.files[0];
db.file.put(file).then(function(fileId) {
// var data = {
// thumbnailUrl: f.thumbnail_url,
// url: f.url,
// contentType: f.contentType,
// filename: f.name,
// name: f.name,
// filesize: f.size,
// dateTaken: f.isoDate,
// staged: true,
// attribution: f.attribution,
// licence: f.licence
// };
//
// target.push(new ImageViewModel(data, true, context));
});
} else {
error(data.errorThrown);
}
});

ko.applyBindingsToDescendants(innerContext, element);
Expand Down Expand Up @@ -399,6 +424,84 @@
return result;
};

function onlineQuery(url, data) {
return $.ajax({
url: url,
dataType:'json',
data: data
});
}

function offlineQuery(url, data) {
var deferred = $.Deferred()

if ( typeof URLSearchParams == 'function') {
var paramIndex = url.indexOf('?'),
paramsString = paramIndex > -1 ? url.substring(paramIndex + 1) : url,
params = new URLSearchParams(paramsString),
limit = parseInt(params.get('limit') || "10"),
db = getDB(),
projectActivityId = params.get('projectActivityId'),
dataFieldName = params.get('dataFieldName'),
outputName = params.get('output');

db.open().then(function () {
db.taxon.where({'projectActivityId': projectActivityId,'dataFieldName': dataFieldName,'outputName': outputName})
.count(function (count){
if (count > 0) {
db.taxon.where({'projectActivityId': projectActivityId,'dataFieldName': dataFieldName,'outputName': outputName})
.and(function (item) {
if(data.q) {
var query = data.q.toLowerCase();
return (item.name && item.name.toLowerCase().startsWith(query)) ||
(item.scientificName && item.scientificName.toLowerCase().startsWith(query)) ||
(item.commonName && item.commonName.toLowerCase().startsWith(query));
}
else
return true
})
.limit(limit).toArray()
.then(function (data) {
deferred.resolve({autoCompleteList: data});
})
.catch(function (e) {
deferred.reject(e);
});
}
else {
var promises = []
promises.push(db.taxon.where('scientificName').startsWithAnyOfIgnoreCase(data.q)
.limit(limit).toArray());

promises.push(db.taxon.where('commonName').startsWithAnyOfIgnoreCase(data.q)
.limit(limit).toArray());

Promise.all(promises).then(function (responses) {
var data = [];
data.push.apply(data, responses[1]);
data.push.apply(data, responses[0]);
deferred.resolve({autoCompleteList: data})
})
}
});
});

return deferred.promise();
}

deferred.resolve({autoCompleteList: []});
return deferred.promise();
}

function searchSpecies(url, data) {
if (isOffline()) {
return offlineQuery(url, data);
}
else {
return onlineQuery(url, data);
}
}

options.source = function(request, response) {
$(element).addClass("ac_loading");

Expand All @@ -409,29 +512,22 @@
if (list) {
$.extend(data, {listId: list});
}
$.ajax({
url: url,
dataType:'json',
data: data,
success: function(data) {
var items = $.map(data.autoCompleteList, function(item) {
return {
label:item.name,
value: item.name,
source: item
}
});
items = [{label:"Missing or unidentified species", value:request.term, source: {listId:'unmatched', name: request.term}}].concat(items);
response(items);

},
error: function() {
items = [{label:"Error during species lookup", value:request.term, source: {listId:'error-unmatched', name: request.term}}];
response(items);
},
complete: function() {
$(element).removeClass("ac_loading");
}
searchSpecies(url,data).then(function(data) {
var items = $.map(data.autoCompleteList, function(item) {
return {
label:item.name,
value: item.name,
source: item
}
});
items = [{label:"Missing or unidentified species", value:request.term, source: {listId:'unmatched', name: request.term}}].concat(items);
response(items);
}).fail(function(e) {
items = [{label:"Error during species lookup", value:request.term, source: {listId:'error-unmatched', name: request.term}}];
response(items);
}).always(function() {
$(element).removeClass("ac_loading");
});
};
options.select = function(event, ui) {
Expand Down
55 changes: 52 additions & 3 deletions grails-app/assets/javascripts/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ function ImageViewModel(prop, skipFindingDocument, context){

self.dateTaken = ko.observable(prop.dateTaken || (new Date()).toISOStringNoMillis()).extend({simpleDate:false});
self.contentType = ko.observable(prop.contentType);
self.url = prop.url;
self.url = ko.observable(prop.url);
self.filesize = prop.filesize;
self.thumbnailUrl = prop.thumbnailUrl;
self.thumbnailUrl = ko.observable(prop.thumbnailUrl);
self.filename = prop.filename;
self.attribution = ko.observable(prop.attribution);
self.licence = ko.observable(prop.licence);
Expand All @@ -41,6 +41,7 @@ function ImageViewModel(prop, skipFindingDocument, context){
self.activityId = prop.activityId;
self.isEmbargoed = prop.isEmbargoed;
self.identifier=prop.identifier;
self.blob = undefined;


self.remove = function(images, data, event){
Expand All @@ -52,6 +53,21 @@ function ImageViewModel(prop, skipFindingDocument, context){
}
}

/**
* any document that is in index db. Their url will be prefixed with blob:.
*/
self.isBlobDocument = function(){
return !!document.blob;
}

self.getBlob = function(){
return document.blobObject;
}

self.isBlobUrl = function(url){
return url && url.indexOf('blob:') === 0;
}

self.getActivityLink = function(){
return fcConfig.activityViewUrl + '/' + self.activityId;
}
Expand All @@ -61,7 +77,30 @@ function ImageViewModel(prop, skipFindingDocument, context){
}

self.getImageViewerUrl = function(){
return fcConfig.imageLeafletViewer + '?file=' + encodeURIComponent(self.url);
return fcConfig.imageLeafletViewer + '?file=' + encodeURIComponent(self.url());
}

/**
* Check if the url is a valid object url.
*/
self.fetchImage = function() {
if (!isUuid(self.documentId) && !isNaN(self.documentId)) {
var documentId = parseInt(self.documentId);
entities.offlineGetDocument(documentId).then(function(result) {
var doc = result.data;
document = doc;
if (self.isBlobDocument()) {
var url = self.url();
if (self.isBlobUrl(url)) {
URL.revokeObjectURL(url);
}

url = ImageViewModel.createObjectURL(doc);
self.url(url);
self.thumbnailUrl(url);
}
});
}
}

self.summary = function(){
Expand All @@ -75,4 +114,14 @@ function ImageViewModel(prop, skipFindingDocument, context){
message += takenOn;
return "<p>" + self.notes() + '</p><i>' + message + '</i>';
}

self.fetchImage();
}

ImageViewModel.createObjectURL = function addObjectURL(document){
if (document.blob) {
var blob = document.blobObject = new Blob([document.blob], {type: document.contentType});
var url = URL.createObjectURL(blob);
return url;
}
}
Loading

0 comments on commit ae2bc50

Please sign in to comment.