mirror of
https://github.com/discourse/discourse.git
synced 2025-01-19 05:52:49 +08:00
BUGFIX: change the image upload icon when only images are authorized (fixes #2359)
This commit is contained in:
parent
8e7f0aa268
commit
897f219d61
|
@ -279,7 +279,7 @@ Discourse.Utilities = {
|
|||
**/
|
||||
allowsAttachments: function() {
|
||||
return Discourse.Utilities.authorizesAllExtensions() ||
|
||||
(/(png|jpg|jpeg|gif|bmp|tif|tiff)/i).test(Discourse.SiteSettings.authorized_extensions);
|
||||
!(/((png|jpg|jpeg|gif|bmp|tif|tiff)(,\s)?)+$/i).test(Discourse.Utilities.authorizedExtensions());
|
||||
},
|
||||
|
||||
displayErrorForUpload: function(data) {
|
||||
|
|
|
@ -38,7 +38,7 @@
|
|||
|
||||
<div class="modal-footer">
|
||||
<button class="btn btn-primary" {{action upload target="view"}}>
|
||||
<span class='add-upload'><i {{bind-attr class="view.uploadIcon"}}></i><i class='fa fa-plus'></i></span>
|
||||
<span class='add-upload'><i {{bind-attr class=":fa view.uploadIcon"}}></i><i class='fa fa-plus'></i></span>
|
||||
{{i18n upload}}
|
||||
</button>
|
||||
<a {{action closeModal}}>{{i18n cancel}}</a>
|
||||
|
|
|
@ -202,6 +202,13 @@ Discourse.ComposerView = Discourse.View.extend(Ember.Evented, {
|
|||
}
|
||||
});
|
||||
|
||||
// HACK to change the upload icon of the composer's toolbar
|
||||
Em.run.scheduleOnce("afterRender", function() {
|
||||
if (!Discourse.Utilities.allowsAttachments()) {
|
||||
$("#wmd-image-button").addClass("image-only");
|
||||
}
|
||||
});
|
||||
|
||||
this.editor.hooks.insertImageDialog = function(callback) {
|
||||
callback(null);
|
||||
self.get('controller').send('showUploadSelector', self);
|
||||
|
|
|
@ -18,7 +18,7 @@ Discourse.UploadSelectorView = Discourse.ModalBodyView.extend({
|
|||
classNames: ['upload-selector'],
|
||||
|
||||
title: function() { return uploadTranslate("title"); }.property(),
|
||||
uploadIcon: function() { return Discourse.Utilities.allowsAttachments() ? "fa-file-o" : "fa-picture-o"; }.property(),
|
||||
uploadIcon: function() { return Discourse.Utilities.allowsAttachments() ? "fa-upload" : "fa-picture-o"; }.property(),
|
||||
|
||||
tip: function() {
|
||||
var source = this.get("controller.local") ? "local" : "remote";
|
||||
|
|
|
@ -92,6 +92,10 @@
|
|||
content: "\f093";
|
||||
}
|
||||
|
||||
#wmd-image-button.image-only:before {
|
||||
content: "\f03e";
|
||||
}
|
||||
|
||||
#wmd-olist-button:before {
|
||||
content: "\f0cb";
|
||||
}
|
||||
|
|
|
@ -10,15 +10,15 @@ test("emailValid", function() {
|
|||
var validUpload = utils.validateUploadedFiles;
|
||||
|
||||
test("validateUploadedFiles", function() {
|
||||
ok(!validUpload(null), "no files are invalid");
|
||||
ok(!validUpload(undefined), "undefined files are invalid");
|
||||
ok(!validUpload([]), "empty array of files is invalid");
|
||||
not(validUpload(null), "no files are invalid");
|
||||
not(validUpload(undefined), "undefined files are invalid");
|
||||
not(validUpload([]), "empty array of files is invalid");
|
||||
});
|
||||
|
||||
test("uploading one file", function() {
|
||||
this.stub(bootbox, "alert");
|
||||
|
||||
ok(!validUpload([1, 2]));
|
||||
not(validUpload([1, 2]));
|
||||
ok(bootbox.alert.calledWith(I18n.t('post.errors.too_many_uploads')));
|
||||
});
|
||||
|
||||
|
@ -26,7 +26,7 @@ test("new user cannot upload images", function() {
|
|||
Discourse.SiteSettings.newuser_max_images = 0;
|
||||
this.stub(bootbox, "alert");
|
||||
|
||||
ok(!validUpload([{name: "image.png"}]), 'the upload is not valid');
|
||||
not(validUpload([{name: "image.png"}]), 'the upload is not valid');
|
||||
ok(bootbox.alert.calledWith(I18n.t('post.errors.image_upload_not_allowed_for_new_user')), 'the alert is called');
|
||||
});
|
||||
|
||||
|
@ -34,7 +34,7 @@ test("new user cannot upload attachments", function() {
|
|||
Discourse.SiteSettings.newuser_max_attachments = 0;
|
||||
this.stub(bootbox, "alert");
|
||||
|
||||
ok(!validUpload([{name: "roman.txt"}]));
|
||||
not(validUpload([{name: "roman.txt"}]));
|
||||
ok(bootbox.alert.calledWith(I18n.t('post.errors.attachment_upload_not_allowed_for_new_user')));
|
||||
});
|
||||
|
||||
|
@ -43,7 +43,7 @@ test("ensures an authorized upload", function() {
|
|||
var extensions = Discourse.SiteSettings.authorized_extensions.replace(/\|/g, ", ");
|
||||
this.stub(bootbox, "alert");
|
||||
|
||||
ok(!validUpload([html]));
|
||||
not(validUpload([html]));
|
||||
ok(bootbox.alert.calledWith(I18n.t('post.errors.upload_not_authorized', { authorized_extensions: extensions })));
|
||||
});
|
||||
|
||||
|
@ -53,7 +53,7 @@ test("prevents files that are too big from being uploaded", function() {
|
|||
Discourse.User.currentProp("trust_level", 1);
|
||||
this.stub(bootbox, "alert");
|
||||
|
||||
ok(!validUpload([image]));
|
||||
not(validUpload([image]));
|
||||
ok(bootbox.alert.calledWith(I18n.t('post.errors.image_too_large', { max_size_kb: 5 })));
|
||||
});
|
||||
|
||||
|
@ -80,7 +80,7 @@ test("allows valid uploads to go through", function() {
|
|||
var pastedImage = dummyBlob();
|
||||
ok(validUpload([pastedImage]));
|
||||
|
||||
ok(!bootbox.alert.calledOnce);
|
||||
not(bootbox.alert.calledOnce);
|
||||
});
|
||||
|
||||
var getUploadMarkdown = function(filename) {
|
||||
|
@ -104,15 +104,15 @@ test("isAnImage", function() {
|
|||
ok(utils.isAnImage(image), image + " is recognized as an image");
|
||||
ok(utils.isAnImage("http://foo.bar/path/to/" + image), image + " is recognized as an image");
|
||||
});
|
||||
ok(!utils.isAnImage("file.txt"));
|
||||
ok(!utils.isAnImage("http://foo.bar/path/to/file.txt"));
|
||||
ok(!utils.isAnImage(""));
|
||||
not(utils.isAnImage("file.txt"));
|
||||
not(utils.isAnImage("http://foo.bar/path/to/file.txt"));
|
||||
not(utils.isAnImage(""));
|
||||
});
|
||||
|
||||
test("avatarUrl", function() {
|
||||
blank(Discourse.Utilities.avatarUrl('', 'tiny'), "no template returns blank");
|
||||
equal(Discourse.Utilities.avatarUrl('/fake/template/{size}.png', 'tiny'), "/fake/template/" + 20*window.devicePixelRatio + ".png", "simple avatar url");
|
||||
equal(Discourse.Utilities.avatarUrl('/fake/template/{size}.png', 'large'), "/fake/template/" + 45*window.devicePixelRatio + ".png", "different size");
|
||||
blank(utils.avatarUrl('', 'tiny'), "no template returns blank");
|
||||
equal(utils.avatarUrl('/fake/template/{size}.png', 'tiny'), "/fake/template/" + 20*window.devicePixelRatio + ".png", "simple avatar url");
|
||||
equal(utils.avatarUrl('/fake/template/{size}.png', 'large'), "/fake/template/" + 45*window.devicePixelRatio + ".png", "different size");
|
||||
});
|
||||
|
||||
test("avatarImg", function() {
|
||||
|
@ -120,27 +120,38 @@ test("avatarImg", function() {
|
|||
window.devicePixelRatio = 2;
|
||||
|
||||
var avatarTemplate = "/path/to/avatar/{size}.png";
|
||||
equal(Discourse.Utilities.avatarImg({avatarTemplate: avatarTemplate, size: 'tiny'}),
|
||||
equal(utils.avatarImg({avatarTemplate: avatarTemplate, size: 'tiny'}),
|
||||
"<img width='20' height='20' src='/path/to/avatar/40.png' class='avatar'>",
|
||||
"it returns the avatar html");
|
||||
|
||||
equal(Discourse.Utilities.avatarImg({avatarTemplate: avatarTemplate, size: 'tiny', title: 'evilest trout'}),
|
||||
equal(utils.avatarImg({avatarTemplate: avatarTemplate, size: 'tiny', title: 'evilest trout'}),
|
||||
"<img width='20' height='20' src='/path/to/avatar/40.png' class='avatar' title='evilest trout'>",
|
||||
"it adds a title if supplied");
|
||||
|
||||
equal(Discourse.Utilities.avatarImg({avatarTemplate: avatarTemplate, size: 'tiny', extraClasses: 'evil fish'}),
|
||||
equal(utils.avatarImg({avatarTemplate: avatarTemplate, size: 'tiny', extraClasses: 'evil fish'}),
|
||||
"<img width='20' height='20' src='/path/to/avatar/40.png' class='avatar evil fish'>",
|
||||
"it adds extra classes if supplied");
|
||||
|
||||
blank(Discourse.Utilities.avatarImg({avatarTemplate: "", size: 'tiny'}),
|
||||
blank(utils.avatarImg({avatarTemplate: "", size: 'tiny'}),
|
||||
"it doesn't render avatars for invalid avatar template");
|
||||
|
||||
window.devicePixelRatio = oldRatio;
|
||||
});
|
||||
|
||||
test("allowsAttachments", function() {
|
||||
Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif";
|
||||
not(utils.allowsAttachments(), "no attachments allowed by default");
|
||||
|
||||
Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif|*";
|
||||
ok(utils.allowsAttachments(), "attachments are allowed when all extensions are allowed");
|
||||
|
||||
Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif|pdf";
|
||||
ok(utils.allowsAttachments(), "attachments are allowed when at least one extension is not an image extension");
|
||||
});
|
||||
|
||||
test("defaultHomepage", function() {
|
||||
Discourse.SiteSettings.top_menu = "latest|top|hot";
|
||||
equal(Discourse.Utilities.defaultHomepage(), "latest", "default homepage is the first item in the top_menu site setting");
|
||||
equal(utils.defaultHomepage(), "latest", "default homepage is the first item in the top_menu site setting");
|
||||
});
|
||||
|
||||
module("Discourse.Utilities.cropAvatar with animated avatars", {
|
||||
|
@ -150,7 +161,7 @@ module("Discourse.Utilities.cropAvatar with animated avatars", {
|
|||
asyncTestDiscourse("cropAvatar", function() {
|
||||
expect(1);
|
||||
|
||||
Discourse.Utilities.cropAvatar("/path/to/avatar.gif", "image/gif").then(function(avatarTemplate) {
|
||||
utils.cropAvatar("/path/to/avatar.gif", "image/gif").then(function(avatarTemplate) {
|
||||
equal(avatarTemplate, "/path/to/avatar.gif", "returns the url to the gif when animated gif are enabled");
|
||||
start();
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user