discourse/app/assets/javascripts/admin/controllers/modals/admin-add-upload.js.es6

146 lines
3.3 KiB
Plaintext
Raw Normal View History

import { isEmpty } from "@ember/utils";
import { and, not } from "@ember/object/computed";
2019-10-29 13:31:44 -04:00
import { inject } from "@ember/controller";
import Controller from "@ember/controller";
2018-06-15 11:03:24 -04:00
import ModalFunctionality from "discourse/mixins/modal-functionality";
import { ajax } from "discourse/lib/ajax";
import {
default as computed,
observes
} from "ember-addons/ember-computed-decorators";
import { popupAjaxError } from "discourse/lib/ajax-error";
const THEME_FIELD_VARIABLE_TYPE_IDS = [2, 3, 4];
const SCSS_VARIABLE_NAMES = [
// common/foundation/colors.scss
2018-06-15 11:03:24 -04:00
"primary",
"secondary",
"tertiary",
"quaternary",
"header_background",
"header_primary",
"highlight",
"danger",
"success",
"love",
// common/foundation/math.scss
2018-06-15 11:03:24 -04:00
"E",
"PI",
"LN2",
"SQRT2",
// common/foundation/variables.scss
2018-06-15 11:03:24 -04:00
"small-width",
"medium-width",
"large-width",
"google",
"instagram",
"facebook",
"cas",
"twitter",
"github",
"base-font-size",
"base-line-height",
"base-font-family",
"primary-low",
"primary-medium",
"secondary-low",
"secondary-medium",
"tertiary-low",
"quaternary-low",
"highlight-low",
"highlight-medium",
"danger-low",
"danger-medium",
"success-low",
"love-low"
];
export default Controller.extend(ModalFunctionality, {
adminCustomizeThemesShow: inject(),
2018-06-15 11:03:24 -04:00
uploadUrl: "/admin/themes/upload_asset",
2017-05-10 14:43:05 -04:00
onShow() {
2018-06-15 11:03:24 -04:00
this.set("name", null);
this.set("fileSelected", false);
2017-05-10 14:43:05 -04:00
},
enabled: and("nameValid", "fileSelected"),
disabled: not("enabled"),
2017-05-10 14:43:05 -04:00
2018-06-15 11:03:24 -04:00
@computed("name", "adminCustomizeThemesShow.model.theme_fields")
errorMessage(name, themeFields) {
if (name) {
if (!name.match(/^[a-z_][a-z0-9_-]*$/i)) {
2018-06-15 11:03:24 -04:00
return I18n.t(
"admin.customize.theme.variable_name_error.invalid_syntax"
);
} else if (SCSS_VARIABLE_NAMES.includes(name.toLowerCase())) {
return I18n.t("admin.customize.theme.variable_name_error.no_overwrite");
2018-06-15 11:03:24 -04:00
} else if (
themeFields.some(
tf =>
THEME_FIELD_VARIABLE_TYPE_IDS.includes(tf.type_id) &&
name === tf.name
)
) {
return I18n.t(
"admin.customize.theme.variable_name_error.must_be_unique"
);
}
}
return null;
},
2018-06-15 11:03:24 -04:00
@computed("errorMessage")
nameValid(errorMessage) {
return null === errorMessage;
2017-05-10 14:43:05 -04:00
},
2018-06-15 11:03:24 -04:00
@observes("name")
uploadChanged() {
2018-06-15 11:03:24 -04:00
const file = $("#file-input")[0];
this.set("fileSelected", file && file.files[0]);
2017-05-10 14:43:05 -04:00
},
actions: {
updateName() {
let name = this.name;
if (isEmpty(name)) {
2018-06-15 11:03:24 -04:00
name = $("#file-input")[0].files[0].name;
this.set("name", name.split(".")[0]);
}
2017-05-10 14:43:05 -04:00
this.uploadChanged();
},
upload() {
2018-06-15 11:03:24 -04:00
const file = $("#file-input")[0].files[0];
const options = {
2018-06-15 11:03:24 -04:00
type: "POST",
processData: false,
contentType: false,
data: new FormData()
};
2018-06-15 11:03:24 -04:00
options.data.append("file", file);
ajax(this.uploadUrl, options)
2018-06-15 11:03:24 -04:00
.then(result => {
const upload = {
upload_id: result.upload_id,
name: this.name,
2018-06-15 11:03:24 -04:00
original_filename: file.name
};
this.adminCustomizeThemesShow.send("addUpload", upload);
2018-06-15 11:03:24 -04:00
this.send("closeModal");
})
.catch(e => {
popupAjaxError(e);
});
}
}
});