mirror of
https://github.com/Esri/offline-editor-js.git
synced 2025-12-15 15:20:05 +00:00
refactored reading of files into attachmentStore.js
much simpler and nice!
This commit is contained in:
parent
d2e3d13823
commit
33be209b85
@ -21,34 +21,42 @@ define([], function()
|
||||
{
|
||||
try
|
||||
{
|
||||
var transaction = this._db.transaction([OBJECT_STORE_NAME],"readwrite");
|
||||
|
||||
transaction.oncomplete = function(event)
|
||||
// first of all, read file content
|
||||
this._readFile(attachmentFile, function(fileContent)
|
||||
{
|
||||
callback(true);
|
||||
};
|
||||
// now, store it in the db
|
||||
var newAttachment =
|
||||
{
|
||||
id: attachmentId,
|
||||
objectId: objectId,
|
||||
featureId: featureLayerUrl + "/" + objectId,
|
||||
contentType: attachmentFile.type,
|
||||
name: attachmentFile.name,
|
||||
size: attachmentFile.size,
|
||||
url: this._createLocalURL(attachmentFile),
|
||||
content: fileContent
|
||||
};
|
||||
|
||||
transaction.onerror = function(event)
|
||||
{
|
||||
callback(false,event.target.error.message);
|
||||
};
|
||||
var transaction = this._db.transaction([OBJECT_STORE_NAME],"readwrite");
|
||||
|
||||
var objectStore = transaction.objectStore(OBJECT_STORE_NAME);
|
||||
var request = objectStore.put(
|
||||
{
|
||||
id: attachmentId,
|
||||
objectId: objectId,
|
||||
featureId: featureLayerUrl + "/" + objectId,
|
||||
contentType: attachmentFile.type,
|
||||
name: attachmentFile.name,
|
||||
size: attachmentFile.size,
|
||||
url: attachmentFile.url,
|
||||
content: attachmentFile.content
|
||||
});
|
||||
request.onsuccess = function(event)
|
||||
{
|
||||
//console.log("item added to db " + event.target.result);
|
||||
};
|
||||
transaction.oncomplete = function(event)
|
||||
{
|
||||
callback(true, newAttachment);
|
||||
};
|
||||
|
||||
transaction.onerror = function(event)
|
||||
{
|
||||
callback(false,event.target.error.message);
|
||||
};
|
||||
|
||||
var objectStore = transaction.objectStore(OBJECT_STORE_NAME);
|
||||
var request = objectStore.put(newAttachment);
|
||||
request.onsuccess = function(event)
|
||||
{
|
||||
//console.log("item added to db " + event.target.result);
|
||||
};
|
||||
|
||||
}.bind(this));
|
||||
}
|
||||
catch(err)
|
||||
{
|
||||
@ -302,9 +310,19 @@ define([], function()
|
||||
|
||||
// internal methods
|
||||
|
||||
this._createLocalURL = function(attachment)
|
||||
this._readFile = function(attachmentFile, callback)
|
||||
{
|
||||
var reader = new FileReader();
|
||||
reader.onload = function(evt)
|
||||
{
|
||||
callback(evt.target.result);
|
||||
};
|
||||
reader.readAsBinaryString(attachmentFile);
|
||||
};
|
||||
|
||||
this._createLocalURL = function(attachmentFile)
|
||||
{
|
||||
return window.URL.createObjectURL(attachmentFile)
|
||||
};
|
||||
|
||||
this._revokeLocalURL = function(attachment)
|
||||
|
||||
@ -179,6 +179,7 @@ define([
|
||||
if( !self.attachmentsStore )
|
||||
{
|
||||
console.log("in order to support attachments you need to call initAttachments() method of offlineFeaturesManager");
|
||||
return;
|
||||
}
|
||||
|
||||
// will only return LOCAL attachments
|
||||
@ -210,51 +211,39 @@ define([
|
||||
);
|
||||
}
|
||||
|
||||
if( !self.attachmentsStore )
|
||||
if( !self.attachmentsStore )
|
||||
{
|
||||
console.log("in order to support attachments you need to call initAttachments() method of offlineFeaturesManager");
|
||||
console.log("in order to support attachments you need to call initAttachments() method of offlineFeaturesManager");
|
||||
return;
|
||||
}
|
||||
|
||||
var files = this._getFilesFromForm(formNode);
|
||||
var file = files[0]; // addAttachment can only add one file, so the rest -if any- are ignored
|
||||
|
||||
var deferred = new Deferred();
|
||||
console.assert(objectId);
|
||||
|
||||
// get input node(s)
|
||||
// read file from input into variable
|
||||
this._readFilesFromForm(formNode, function(files)
|
||||
var attachmentId = this._getNextTempId();
|
||||
self.attachmentsStore.store(this.url, attachmentId, objectId, file, function(success, newAttachment)
|
||||
{
|
||||
console.log(files);
|
||||
console.assert(files.length === 1, "we don't support multiple files (yet?)");
|
||||
files.forEach(function(file)
|
||||
var returnValue = { attachmentId: attachmentId, objectId:objectId, success:success };
|
||||
if( success )
|
||||
{
|
||||
// store the attachment
|
||||
var attachmentId = this._getNextTempId();
|
||||
self.attachmentsStore.store(this.url,attachmentId, objectId, file, function(success)
|
||||
{
|
||||
var returnValue = { attachmentId: attachmentId, objectId: objectId, success: success };
|
||||
if( success )
|
||||
{
|
||||
self.emit(self.events.ATTACHMENT_ENQUEUED,returnValue);
|
||||
callback && callback(returnValue);
|
||||
deferred.resolve(returnValue);
|
||||
// PROBLEM HERE: theoretically, the form can have multiple input[file] controls
|
||||
// and each input can contain multiple files BUT here we call the callback and
|
||||
// resolve the deferred with the FIRST of the files
|
||||
// In practice, popups only have one input, and it doesn't allow multiple files
|
||||
self.emit(self.events.ATTACHMENT_ENQUEUED,returnValue);
|
||||
callback && callback(returnValue);
|
||||
deferred.resolve(returnValue);
|
||||
|
||||
// replace the default URL that is set by attachmentEditor with the local file URL
|
||||
var attachmentUrl = this._url.path + "/" + objectId + "/attachments/" + attachmentId;
|
||||
var attachmentElement = query("[href=" + attachmentUrl + "]");
|
||||
attachmentElement.attr("href", file.url);
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue.error = "can't store attachment";
|
||||
errback && errback(returnValue);
|
||||
deferred.reject(returnValue);
|
||||
}
|
||||
}.bind(this));
|
||||
},this);
|
||||
// replace the default URL that is set by attachmentEditor with the local file URL
|
||||
var attachmentUrl = this._url.path + "/" + objectId + "/attachments/" + attachmentId;
|
||||
var attachmentElement = query("[href=" + attachmentUrl + "]");
|
||||
attachmentElement.attr("href", newAttachment.url);
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue.error = "can't store attachment";
|
||||
errback && errback(returnValue);
|
||||
deferred.reject(returnValue);
|
||||
}
|
||||
}.bind(this));
|
||||
|
||||
return deferred;
|
||||
};
|
||||
|
||||
@ -277,7 +266,8 @@ define([
|
||||
|
||||
if( !self.attachmentsStore )
|
||||
{
|
||||
console.log("in order to support attachments you need to call initAttachments() method of offlineFeaturesManager");
|
||||
console.log("in order to support attachments you need to call initAttachments() method of offlineFeaturesManager");
|
||||
return;
|
||||
}
|
||||
|
||||
// case 1.- it is a new attachment
|
||||
@ -421,54 +411,18 @@ define([
|
||||
return deferred;
|
||||
}; // layer.applyEdits()
|
||||
|
||||
|
||||
/* internal methods */
|
||||
|
||||
layer._readFilesFromForm = function(formNode, callback)
|
||||
layer._getFilesFromForm = function(formNode)
|
||||
{
|
||||
var files = [];
|
||||
var inputNodes = array.filter(formNode.elements, function(node) { return node.type === "file"; });
|
||||
console.assert(inputNodes.length <= 1, "we don't support multiple input nodes in one form (yet?)");
|
||||
inputNodes.forEach(function(inputNode)
|
||||
{
|
||||
var file, files = [];
|
||||
var pendingFiles = inputNode.files.length;
|
||||
var i, n = inputNode.files.length;
|
||||
var reader;
|
||||
|
||||
if( !pendingFiles )
|
||||
{
|
||||
return callback(files);
|
||||
}
|
||||
|
||||
for(i=0;i<n; i++)
|
||||
{
|
||||
file = inputNode.files[i];
|
||||
|
||||
console.log("reading file", file.name);
|
||||
|
||||
reader = new FileReader();
|
||||
|
||||
// closure to keep 'file' value
|
||||
reader.onload = (function(theFile){
|
||||
return function(evt) {
|
||||
console.log(evt);
|
||||
files.push({
|
||||
name: theFile.name,
|
||||
type: theFile.type,
|
||||
content: evt.target.result,
|
||||
size: theFile.size,
|
||||
url: window.URL.createObjectURL(theFile)
|
||||
});
|
||||
pendingFiles--;
|
||||
if( !pendingFiles )
|
||||
{
|
||||
return callback(files);
|
||||
}
|
||||
};
|
||||
}(file));
|
||||
|
||||
reader.readAsBinaryString(file);
|
||||
}
|
||||
});
|
||||
files.push.apply(files,inputNode.files);
|
||||
},this);
|
||||
return files;
|
||||
};
|
||||
|
||||
layer._replaceFeatureIds = function(tempObjectIds,newObjectIds,callback)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user