2018-06-15 23:03:24 +08: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";
|
2017-05-10 05:20:28 +08:00
|
|
|
|
2018-03-29 00:57:11 +08:00
|
|
|
const THEME_FIELD_VARIABLE_TYPE_IDS = [2, 3, 4];
|
|
|
|
|
2018-05-09 17:54:43 +08:00
|
|
|
const SCSS_VARIABLE_NAMES = [
|
|
|
|
// common/foundation/colors.scss
|
2018-06-15 23:03:24 +08:00
|
|
|
"primary",
|
|
|
|
"secondary",
|
|
|
|
"tertiary",
|
|
|
|
"quaternary",
|
|
|
|
"header_background",
|
|
|
|
"header_primary",
|
|
|
|
"highlight",
|
|
|
|
"danger",
|
|
|
|
"success",
|
|
|
|
"love",
|
2018-05-09 17:54:43 +08:00
|
|
|
// common/foundation/math.scss
|
2018-06-15 23:03:24 +08:00
|
|
|
"E",
|
|
|
|
"PI",
|
|
|
|
"LN2",
|
|
|
|
"SQRT2",
|
2018-05-09 17:54:43 +08:00
|
|
|
// common/foundation/variables.scss
|
2018-06-15 23:03:24 +08:00
|
|
|
"small-width",
|
|
|
|
"medium-width",
|
|
|
|
"large-width",
|
|
|
|
"google",
|
|
|
|
"instagram",
|
|
|
|
"facebook",
|
|
|
|
"cas",
|
|
|
|
"twitter",
|
|
|
|
"yahoo",
|
|
|
|
"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 17:54:43 +08:00
|
|
|
];
|
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
export default Ember.Controller.extend(ModalFunctionality, {
|
|
|
|
adminCustomizeThemesShow: Ember.inject.controller(),
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
uploadUrl: "/admin/themes/upload_asset",
|
2018-03-28 09:31:32 +08:00
|
|
|
|
2017-05-11 02:43:05 +08:00
|
|
|
onShow() {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.set("name", null);
|
|
|
|
this.set("fileSelected", false);
|
2017-05-11 02:43:05 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 00:54:23 +08:00
|
|
|
enabled: Ember.computed.and("nameValid", "fileSelected"),
|
|
|
|
disabled: Ember.computed.not("enabled"),
|
2017-05-11 02:43:05 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@computed("name", "adminCustomizeThemesShow.model.theme_fields")
|
2018-05-09 17:54:43 +08:00
|
|
|
errorMessage(name, themeFields) {
|
|
|
|
if (name) {
|
|
|
|
if (!name.match(/^[a-z_][a-z0-9_-]*$/i)) {
|
2018-06-15 23:03:24 +08:00
|
|
|
return I18n.t(
|
|
|
|
"admin.customize.theme.variable_name_error.invalid_syntax"
|
|
|
|
);
|
2018-05-09 17:54:43 +08:00
|
|
|
} else if (SCSS_VARIABLE_NAMES.includes(name.toLowerCase())) {
|
|
|
|
return I18n.t("admin.customize.theme.variable_name_error.no_overwrite");
|
2018-06-15 23:03:24 +08: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 17:54:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@computed("errorMessage")
|
2018-05-09 17:54:43 +08:00
|
|
|
nameValid(errorMessage) {
|
|
|
|
return null === errorMessage;
|
2017-05-11 02:43:05 +08:00
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@observes("name")
|
2017-12-19 23:10:44 +08:00
|
|
|
uploadChanged() {
|
2018-06-15 23:03:24 +08:00
|
|
|
const file = $("#file-input")[0];
|
|
|
|
this.set("fileSelected", file && file.files[0]);
|
2017-05-11 02:43:05 +08:00
|
|
|
},
|
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
actions: {
|
|
|
|
updateName() {
|
2018-06-15 23:03:24 +08:00
|
|
|
let name = this.get("name");
|
2019-01-12 00:54:23 +08:00
|
|
|
if (Ember.isEmpty(name)) {
|
2018-06-15 23:03:24 +08:00
|
|
|
name = $("#file-input")[0].files[0].name;
|
|
|
|
this.set("name", name.split(".")[0]);
|
2017-05-10 05:20:28 +08:00
|
|
|
}
|
2017-05-11 02:43:05 +08:00
|
|
|
this.uploadChanged();
|
2017-05-10 05:20:28 +08:00
|
|
|
},
|
2017-12-19 23:10:44 +08:00
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
upload() {
|
2018-06-15 23:03:24 +08:00
|
|
|
const file = $("#file-input")[0].files[0];
|
2017-05-10 05:20:28 +08:00
|
|
|
|
2017-12-19 23:10:44 +08:00
|
|
|
const options = {
|
2018-06-15 23:03:24 +08:00
|
|
|
type: "POST",
|
2017-12-19 23:10:44 +08:00
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
data: new FormData()
|
2017-05-10 05:20:28 +08:00
|
|
|
};
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
options.data.append("file", file);
|
|
|
|
|
|
|
|
ajax(this.get("uploadUrl"), options)
|
|
|
|
.then(result => {
|
|
|
|
const upload = {
|
|
|
|
upload_id: result.upload_id,
|
|
|
|
name: this.get("name"),
|
|
|
|
original_filename: file.name
|
|
|
|
};
|
|
|
|
this.get("adminCustomizeThemesShow").send("addUpload", upload);
|
|
|
|
this.send("closeModal");
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
popupAjaxError(e);
|
|
|
|
});
|
2017-05-10 05:20:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|