ionic-coreo
Version:
Ionic2 module for integration with Coreo
108 lines • 3.98 kB
JavaScript
;
var lodash_1 = require('lodash');
var CoreoRecord = (function () {
function CoreoRecord(options) {
this.data = {};
this.records = [];
this.files = [];
Object.assign(this, options || {});
}
CoreoRecord.prototype.setData = function (key, value) {
this.data[key] = value;
};
CoreoRecord.prototype.addFile = function (path, url, type) {
if (type === void 0) { type = 'image/jpeg'; }
this.files.push({
url: url,
path: path,
type: type
});
};
CoreoRecord.prototype.loadFiles = function () {
var _this = this;
return this.files.length === 0 ? Promise.resolve([]) : Promise.all(this.files.map(function (f) { return _this.loadFile(f); }));
};
CoreoRecord.prototype.dataWithFiles = function () {
var _this = this;
return this.loadFiles().then(function (files) {
var data = lodash_1.omit(_this, ['files']);
for (var i = 0; i < _this.files.length; i++) {
if (typeof files[i] !== 'undefined') {
data.data[_this.files[i].path] = files[i];
}
}
// Recursively call for all children
return Promise.all(data.records.map(function (child) {
return child.dataWithFiles();
})).then(function (childData) {
data.records = childData;
return data;
});
});
};
/**
* Convert the record to a FormData object ready for upload
*/
CoreoRecord.prototype.toFormData = function () {
return this.dataWithFiles().then(function (data) {
var fd = objectToFormData(data);
return fd;
}, function (err) {
console.log('ERRORS!', err);
});
};
CoreoRecord.prototype.createChildRecord = function (options) {
var child = new CoreoRecord(Object.assign({}, options || {}, { surveyId: this.surveyId }));
this.records.push(child);
return child;
};
/**
* Load a file from the filesystem into a blob
*/
CoreoRecord.prototype.loadFile = function (file) {
var type = file.type;
var url = file.url;
return new Promise(function (resolve, reject) {
if (!window.hasOwnProperty('resolveLocalFileSystemURL')) {
console.warn('Unable to load file in browser');
return resolve();
}
window.resolveLocalFileSystemURL(url, function (entry) {
entry.file(function (f) {
var reader = new FileReader();
reader.onloadend = function () {
var blob = new Blob([this.result], { type: type });
resolve(blob);
};
reader.readAsArrayBuffer(f);
}, function (e) { return reject(e); });
}, function (e) { return reject(e); });
});
};
return CoreoRecord;
}());
exports.CoreoRecord = CoreoRecord;
function objectToFormData(obj, form, nspace) {
var fd = form || new FormData();
var formKey;
for (var property in obj) {
if (obj.hasOwnProperty(property) && typeof obj[property] !== 'function') {
if (nspace) {
formKey = nspace + '[' + property + ']';
}
else {
formKey = property;
}
// If this property is an object and NOT a File or a Blob, recursively process
if (typeof obj[property] === 'object' && !(obj[property] instanceof File || obj[property] instanceof Blob)) {
objectToFormData(obj[property], fd, formKey);
}
else {
// Else just append it - it's a file
fd.append(formKey, obj[property]);
}
}
}
return fd;
}
//# sourceMappingURL=record.js.map