Skip to content

Commit

Permalink
Merge pull request #1389 from ushahidi/develop
Browse files Browse the repository at this point in the history
Release image validation fixes and date fix
  • Loading branch information
rowasc authored Dec 6, 2019
2 parents 03a49e1 + aace5cb commit f413407
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 46 deletions.
45 changes: 29 additions & 16 deletions app/common/directives/file-upload.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,42 @@ function FileUpload() {
replace: true,
scope: {
container: '=',
model: '='
model: '=',
validation: '='
},
controller: [
'$scope', '$attrs',
'$scope', '$attrs', 'Notify',
function (
$scope, $attrs
$scope, $attrs, Notify
) {
$scope.required = typeof $attrs.required !== 'undefined';
$scope.uploadFile = function ($event) {
$scope.container.file = $event.target.files[0];
var reader = new FileReader();
reader.onload = function () {
var dataURL = reader.result;
$scope.container.dataURI = dataURL;
$scope.container.changed = true;
$scope.container.deleted = false;
$scope.model = 'changed';
$scope.$apply();
};
reader.readAsDataURL($event.target.files[0]);


if (validateFile($event.target.files[0])) {
$scope.container.file = $event.target.files[0];
var reader = new FileReader();
reader.onload = function () {
var dataURL = reader.result;
$scope.container.dataURI = dataURL;
$scope.container.changed = true;
$scope.container.deleted = false;
$scope.model = 'changed';
$scope.$apply();
};
reader.readAsDataURL($event.target.files[0]);
} else {
Notify.error('post.media.error_in_upload');
}
};

function validateFile(container) {
if ($scope.validation === 'image') {
var mimeReg = /[\/.](gif|jpg|jpeg|png)$/i;
var mimeCheck = mimeReg.test(container.type);
var sizeCheck = container.size < 1048576;
return mimeCheck && sizeCheck;
}
return true;
}
}]
};
}
4 changes: 2 additions & 2 deletions app/common/directives/mode-bar/support-links.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ <h3 class="modal-title" translate>
<dd translate>app.report_a_bug.description</dd>
<dt class="list-item"><a href="https://www.ushahidi.com/features" target="_blank" translate>app.features.title</a></dt>
<dd translate>app.features.description</dd>
<dt class="list-item" ng-show="loggedin"><a id="intercom_custom_launcher" href="mailto:{{intercomAppId}}@intercom-mail.com" target="_blank" translate>app.intercom.intercom</a></dt>
<dd translate ng-show="loggedin">app.intercom.description</dd>
<dt class="list-item" ng-show="loggedin && intercomAppId"><a id="intercom_custom_launcher" href="mailto:{{intercomAppId}}@intercom-mail.com" target="_blank" translate>app.intercom.intercom</a></dt>
<dd translate ng-show="loggedin && intercomAppId">app.intercom.description</dd>
</dl>
</div>

3 changes: 2 additions & 1 deletion app/common/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,8 @@
"upload" : "Upload",
"add_photo": "Add photo",
"delete_photo": "Delete photo",
"replace_image": "Replace image"
"replace_image": "Replace image",
"error_in_upload": "Your image is too large (maximum size is 1MB) or has the wrong format (allowed formats are gif, png, jpg and jpeg), please check and try again!"
},
"video" : {
"enter_a" : "Enter a",
Expand Down
2 changes: 1 addition & 1 deletion app/main/posts/common/post-metadata.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</li>
<li class="tooltip">
<span class="label">{{timeago}}</span>
<span class="bug" title="{{ displayTimeFull }}">{{ displayTime }}</span>
<span class="bug tooltip-visible-el" aria-label="Post added {{ displayTimeFull }}" role="tooltip" title="{{ displayTimeFull }}">{{ displayTimeFull }}</span>

</li>
<li>
Expand Down
11 changes: 7 additions & 4 deletions app/main/posts/modify/media-edit.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ function (
saveMedia: function (medias, post) {
var deferred = $q.defer();
var calls = [];

// Loop over medias and check for changes
_.each(medias, function (media, key) {
// Check if media needs to be updated
if (media.changed) {
// Check if new media or if the media file has changed
// otherwise just update the caption
if (!media.id && media.deleted) {
// Checking if media is marked for deletion
if (media.deleted) {
MediaEditService.deleteMedia(media.id);
post.values[key][0] = null;
// Check if new media or if the media file has changed
// otherwise just update the caption
// Check if a new file was uploaded
}else if (media.file) {
} else if (media.file) {
calls.push(MediaEditService.uploadFile(media).then(function (media) {
post.values[key][0] = media ? media.id : null;
}));
Expand Down
4 changes: 2 additions & 2 deletions app/main/posts/modify/media.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<form>
<div class="form-field file">
<file-upload id="{{name}}" container="media" model="mediaId">
<file-upload id="{{name}}" validation="'image'" validation=true container="media" model="mediaId">
</file-upload>

<label for="{{name}}" class="button button-gamma" ng-show="showAdd()">
Expand All @@ -23,7 +23,7 @@
<span class="hidden" translate="post.media.delete_photo"></span>
</button>

<file-upload id="{{name}}" container="media" model="mediaId"></file-upload>
<file-upload id="{{name}}" validation="'image'" container="media" model="mediaId"></file-upload>
<label for="{{name}}" class="button button-gamma">
<svg class="iconic">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/img/iconic-sprite.svg#image"></use>
Expand Down
4 changes: 0 additions & 4 deletions app/main/posts/modify/post-data-editor.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,6 @@ function PostDataEditorController(
attributes.map(function (attr) {
// Create associated media entity
if (attr.input === 'upload') {
var media = {};
if ($scope.post.values[attr.key]) {
media = $scope.post.values[attr.key][0];
}
$scope.medias[attr.key] = {};
}
if (attr.input === 'tags') {
Expand Down
4 changes: 0 additions & 4 deletions app/main/posts/modify/post-editor.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ function PostEditorController(
attributes.map(function (attr) {
// Create associated media entity
if (attr.input === 'upload') {
var media = {};
if ($scope.post.values[attr.key]) {
media = $scope.post.values[attr.key][0];
}
$scope.medias[attr.key] = {};
}
if (attr.input === 'tags') {
Expand Down
31 changes: 19 additions & 12 deletions app/main/posts/modify/post-media.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function (
template: require('./media.html'),
link: function ($scope, element, attr, ngModel) {
// Initialize media object
$scope.media = { id: null, file: null, caption: '', dataURI: null, changed: false };
$scope.media = { id: null, file: null, caption: '', dataURI: null, changed: false, deleted: false };
$scope.mediaId = null;
$scope.showAdd = showAdd;
$scope.showReplace = showReplace;
Expand All @@ -43,14 +43,16 @@ function (
// Watch for media changes
$scope.$watch('mediaId', handleMediaIdChange);

// Watch for deleted images
$scope.$watch('media.deleted', handleMediaDeleted);

// Set up rendering any model changes
ngModel.$render = renderViewValue;
}

function renderViewValue() {
if (ngModel.$viewValue) {
$scope.mediaId = parseInt(ngModel.$viewValue);

// Load the media from the API
if ($scope.media.id !== $scope.mediaId) {
MediaEndpoint.get({id: $scope.mediaId}).$promise.then(function (media) {
Expand All @@ -65,39 +67,44 @@ function (
function handleMediaChange(changed) {
if (changed) {
// Make sure the model is set dirty if media changes
ngModel.$setViewValue($scope.mediaId);
ngModel.$setDirty();
}

}

function handleMediaIdChange(id) {
if (id === 'changed') {
// Make sure the model is set dirty if media changes
ngModel.$setViewValue($scope.mediaId);
ngModel.$setDirty();
}
}

function handleMediaDeleted(deleted) {
// // Make sure we update the view-value both if an image is deleted and deleted and then replaced
if (deleted) {
ngModel.$setViewValue(null);
} else {
ngModel.$setViewValue($scope.mediaId);
}
}

function showAdd() {
return (!$scope.media.id && !$scope.media.changed || $scope.media.deleted);
}

function showReplace() {
return $scope.media.dataURI || $scope.media.id;
return $scope.media.dataURI || ($scope.media.id && !$scope.media.deleted);
}

function showDelete() {
return $scope.media.id;
return $scope.media.id || ($scope.media.changed && !$scope.media.deleted);
}

function deleteMedia(mediaId) {
// Mark for deletion
Notify.confirmDelete('notify.post.delete_image_confirm').then(function () {
MediaEditService.deleteMedia(mediaId).then(function () {
$scope.media = {};
$scope.media.changed = true;
$scope.media.deleted = true;
$scope.mediaId = null;
});
});
$scope.media = {id: mediaId, changed: true, deleted: true};
}
}
};
Expand Down
2 changes: 2 additions & 0 deletions sass/overrides/_modal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ modal {
pointer-events: none;
}
}


0 comments on commit f413407

Please sign in to comment.