// Generated by CoffeeScript 1.12.7
/**
* Form to request Databrary to assist the user.
* @module
*/
'use strict';
app.directive('volumeAssist', [
'$sce', 'constantService', 'uploadService', 'messageService', 'routerService', function($sce, constants, uploads, messages, router) {
return {
restrict: 'E',
templateUrl: 'volume/assist.html',
link: function($scope) {
var $flow, a, asset, form, remove, slot, staffPerm, updateStaff, volume;
volume = $scope.volume;
form = $scope.volumeAssist;
$flow = $scope.$flow;
$scope.flowOptions = uploads.flowOptions();
staffPerm = constants.accessPreset.staff;
updateStaff = function() {
form.staff = !!volume.accessStaff;
};
updateStaff();
$scope.setStaff = function(set) {
var p;
p = set ? staffPerm : 0;
form.$setSubmitted();
return volume.accessSave(constants.party.STAFF, set ? staffPerm : 0).then(function() {
form.$setPristine();
updateStaff();
}, function(res) {
form.$setUnsubmitted();
messages.addError({
body: constants.message('access.preset.save.error'),
report: res,
owner: form
});
});
};
slot = volume.top;
$scope.assets = (function() {
var ref, results;
ref = slot.assets;
results = [];
for (a in ref) {
asset = ref[a];
results.push(asset);
}
return results;
})();
$scope.assets.sort(function(a, b) {
return b.id - a.id;
});
$scope.uploads = [];
$scope.progress = 0;
remove = function(file) {
$scope.uploads.remove(file);
if (!$scope.uploads.length) {
form.uploads.$setPristine();
}
$scope.progress = file.flowObj.progress();
};
$scope.fileAdded = function(file) {
$flow = file.flowObj;
$scope.uploads.unshift(file);
form.uploads.$setDirty();
uploads.upload(volume, file).then(function(res) {
file.progressValue = 0;
}, function(res) {
remove(file);
});
};
$scope.fileSuccess = function(file) {
file.progressValue = 1;
slot.createAsset({
upload: file.uniqueIdentifier,
name: file.relativePath,
position: null,
classification: null
}).then(function(asset) {
$scope.assets.unshift(asset);
file.cancel();
remove(file);
}, function(res) {
messages.addError({
type: 'red',
body: constants.message('asset.update.error', file.relativePath),
report: res,
owner: form
});
file.cancel();
remove(file);
});
};
$scope.fileProgress = function(file) {
file.progressValue = file.progress();
$scope.progress = file.flowObj.progress();
};
$scope.fileError = function(file, message) {
messages.addError({
type: 'red',
body: constants.message('asset.upload.error', file.relativePath, message || 'unknown error'),
owner: form
});
file.cancel();
remove(file);
};
$scope.remove = function(asset) {
asset.remove().then(function(asset) {
$scope.assets.remove(asset);
}, function(res) {
messages.addError({
type: 'red',
body: constants.message('asset.remove.error', this.name),
report: res,
owner: this
});
});
};
$scope.cancel = function(file) {
file.cancel();
remove(file);
};
form.questions = [
{
name: 'released',
text: "Are you using the Databrary release?"
}, {
name: 'equivalent',
text: "If not, are you using an equivalent consent/media release (see http://databrary.org/access/guide/investigators/release/grandfathering-data.html)?"
}, {
name: 'complete',
text: "Is data collection completed?"
}, {
name: 'share',
text: "Are you ready to share now?"
}
];
form.submit = function() {
var body, i, len, q, ref;
body = "";
ref = form.questions;
for (i = 0, len = ref.length; i < len; i++) {
q = ref[i];
if (q.answer) {
body += q.text + " " + q.answer + "\n";
}
}
body += "\n" + form.additional;
router.http(router.controllers.postVolumeAssist, [volume.id], body, {
headers: {
'Content-Type': "text/plain"
}
}).then(function() {
var j, len1, ref1;
delete form.additional;
ref1 = form.questions;
for (j = 0, len1 = ref1.length; j < len1; j++) {
q = ref1[j];
delete q.answer;
}
form.mail.$setPristine();
messages.add({
type: 'green',
body: 'Your request has been submitted, and you will receive a copy by email',
owner: form.mail
});
}, function(res) {
form.mail.$setUnsubmitted();
messages.addError({
type: 'red',
body: 'An error occured sending the request; please try again',
report: res,
owner: form.mail
});
});
};
$scope.$on('$destroy', function() {
if ($flow != null) {
$flow.cancel();
}
});
}
};
}
]);
//# sourceMappingURL=assist.js.map