2019-10-31 13:37:24 -04:00
|
|
|
import { isEmpty } from "@ember/utils";
|
2019-10-30 16:28:29 -04:00
|
|
|
import { and, not } from "@ember/object/computed";
|
2019-10-29 13:31:44 -04:00
|
|
|
import { inject } from "@ember/controller";
|
2019-10-23 13:06:54 -04:00
|
|
|
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 {
|
2019-11-07 16:38:28 -05:00
|
|
|
default as discourseComputed,
|
2018-06-15 11:03:24 -04:00
|
|
|
observes
|
2019-11-07 16:38:28 -05:00
|
|
|
} from "discourse-common/utils/decorators";
|
2018-06-15 11:03:24 -04:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2017-05-09 17:20:28 -04:00
|
|
|
|
2018-03-28 12:57:11 -04:00
|
|
|
const THEME_FIELD_VARIABLE_TYPE_IDS = [2, 3, 4];
|
|
|
|
|
2018-05-09 05:54:43 -04:00
|
|
|
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",
|
2018-05-09 05:54:43 -04:00
|
|
|
// common/foundation/math.scss
|
2018-06-15 11:03:24 -04:00
|
|
|
"E",
|
|
|
|
"PI",
|
|
|
|
"LN2",
|
|
|
|
"SQRT2",
|
2018-05-09 05:54:43 -04:00
|
|
|
// 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"
|
2018-05-09 05:54:43 -04:00
|
|
|
];
|
|
|
|
|
2019-10-23 13:06:54 -04:00
|
|
|
export default Controller.extend(ModalFunctionality, {
|
2019-10-29 13:13:31 -04:00
|
|
|
adminCustomizeThemesShow: inject(),
|
2017-05-09 17:20:28 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
uploadUrl: "/admin/themes/upload_asset",
|
2018-03-27 21:31:32 -04:00
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2019-10-30 16:28:29 -04:00
|
|
|
enabled: and("nameValid", "fileSelected"),
|
|
|
|
disabled: not("enabled"),
|
2017-05-10 14:43:05 -04:00
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("name", "adminCustomizeThemesShow.model.theme_fields")
|
2018-05-09 05:54:43 -04:00
|
|
|
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"
|
|
|
|
);
|
2018-05-09 05:54:43 -04:00
|
|
|
} 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"
|
|
|
|
);
|
2018-05-09 05:54:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("errorMessage")
|
2018-05-09 05:54:43 -04:00
|
|
|
nameValid(errorMessage) {
|
|
|
|
return null === errorMessage;
|
2017-05-10 14:43:05 -04:00
|
|
|
},
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
@observes("name")
|
2017-12-19 10:10:44 -05:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2017-05-09 17:20:28 -04:00
|
|
|
actions: {
|
|
|
|
updateName() {
|
2019-05-27 04:15:39 -04:00
|
|
|
let name = this.name;
|
2019-10-31 13:37:24 -04:00
|
|
|
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-09 17:20:28 -04:00
|
|
|
}
|
2017-05-10 14:43:05 -04:00
|
|
|
this.uploadChanged();
|
2017-05-09 17:20:28 -04:00
|
|
|
},
|
2017-12-19 10:10:44 -05:00
|
|
|
|
2017-05-09 17:20:28 -04:00
|
|
|
upload() {
|
2018-06-15 11:03:24 -04:00
|
|
|
const file = $("#file-input")[0].files[0];
|
2017-05-09 17:20:28 -04:00
|
|
|
|
2017-12-19 10:10:44 -05:00
|
|
|
const options = {
|
2018-06-15 11:03:24 -04:00
|
|
|
type: "POST",
|
2017-12-19 10:10:44 -05:00
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
data: new FormData()
|
2017-05-09 17:20:28 -04:00
|
|
|
};
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
options.data.append("file", file);
|
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
ajax(this.uploadUrl, options)
|
2018-06-15 11:03:24 -04:00
|
|
|
.then(result => {
|
|
|
|
const upload = {
|
|
|
|
upload_id: result.upload_id,
|
2019-05-27 04:15:39 -04:00
|
|
|
name: this.name,
|
2018-06-15 11:03:24 -04:00
|
|
|
original_filename: file.name
|
|
|
|
};
|
2019-05-27 04:15:39 -04:00
|
|
|
this.adminCustomizeThemesShow.send("addUpload", upload);
|
2018-06-15 11:03:24 -04:00
|
|
|
this.send("closeModal");
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
popupAjaxError(e);
|
|
|
|
});
|
2017-05-09 17:20:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|