Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Мелкие правки #737

Merged
merged 11 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions _build/data/transport.settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@
'area' => 'ms2_frontend',
],
'ms2_cart_js_class_name' => [
'value' => 'msCart',
'value' => 'MsCart',
'xtype' => 'textfield',
'area' => 'ms2_frontend',
],
Expand All @@ -309,7 +309,7 @@
'area' => 'ms2_frontend',
],
'ms2_order_js_class_name' => [
'value' => 'msOrder',
'value' => 'MsOrder',
'xtype' => 'textfield',
'area' => 'ms2_frontend',
],
Expand All @@ -319,7 +319,7 @@
'area' => 'ms2_frontend',
],
'ms2_notify_js_class_name' => [
'value' => 'msIziToast',
'value' => 'MsIziToast',
'xtype' => 'textfield',
'area' => 'ms2_frontend',
],
Expand Down
28 changes: 28 additions & 0 deletions assets/components/minishop2/css/web/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,31 @@ div.jGrowl > .ui-state-highlight {
word-wrap: normal;
white-space: pre;
}

/* Input Number */
.ms-hidden{
position: absolute !important;
opacity: 0;
visibility: hidden;
z-index: -10000 !important;
}
.ms-input-number-wrap{
display: flex;
font-size: 16px;
}
.ms-input-number-emulator{
width: 3em;
text-align: center;
border: none;
background-color: transparent;
}
.ms-input-number-emulator:focus, .ms-input-number-emulator:active{
outline: none;
box-shadow: none;
border: none;
}
.ms-input-number-btn{
display: flex;
align-items: center;
justify-content: center;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"msNotify": {},
"msIziToast": {
"MsNotify": {},
"MsIziToast": {
"jsPath": "assets/components/minishop2/js/web/vanilajs/lib/izitoast/iziToast.min.js",
"cssPath": "assets/components/minishop2/css/web/lib/izitoast/iziToast.min.css",
"handlerClassName": "iziToast",
Expand Down
114 changes: 43 additions & 71 deletions assets/components/minishop2/js/web/vanilajs/modules/minishop.class.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
export default class MiniShop {
constructor(miniShop2Config) {
this.miniShop2Config = Object.assign(miniShop2Config, {
const defaults = {
notifyClassPath: './msnotify.class.js',
notifyClassName: 'MsNotify',
cartClassPath: './mscart.class.js',
cartClassName: 'MsCart',
orderClassPath: './msorder.class.js',
orderClassName: 'MsOrder',
moduleImportErrorMsg: 'Произошла ошибка при загрузке модуля',
properties: ['Message', 'Cart', 'Order'],
actionUrl: document.location.href,
formMethod: 'POST',
});
};
this.miniShop2Config = Object.assign(defaults, miniShop2Config);

this.miniShop2Config.callbacksObjectTemplate = this.callbacksObjectTemplate;
this.Callbacks = this.miniShop2Config.Callbacks = {
Cart: {
Expand All @@ -28,96 +38,60 @@ export default class MiniShop {
this.formData = null;
this.Message = null;
this.timeout = 300;

this.initialize();
}

async setHandler(property, pathPropertyName, classnamePropertyName, defaultPath, defaultClassName, errorMsg, response) {
const classPath = (this.miniShop2Config.hasOwnProperty(pathPropertyName) && this.miniShop2Config[pathPropertyName]) ?
this.miniShop2Config[pathPropertyName] : defaultPath,
className = (this.miniShop2Config.hasOwnProperty(classnamePropertyName) && this.miniShop2Config[classnamePropertyName]) ?
this.miniShop2Config[classnamePropertyName] : defaultClassName,
config = response ? response[className] : this;
async setHandler(property){
let prefix = property.toLowerCase(),
response = false,
messageSettings = false;
if(prefix === 'message'){
prefix = 'notify';
response = await this.sendResponse({url: this.miniShop2Config.notifySettingsPath, method: 'GET'});
if (response.ok) {
messageSettings = await response.json();
}
}
const classPath = this.miniShop2Config[prefix + 'ClassPath'];
const className = this.miniShop2Config[prefix + 'ClassName'];
const config = messageSettings ? messageSettings[className] : this;

try {
const { default: ModuleName } = await import(classPath);
const {default: ModuleName} = await import(classPath);
this[property] = new ModuleName(config);
} catch (e) {
console.error(e, errorMsg);
throw new Error(this.miniShop2Config.moduleImportErrorMsg);
}
}

async initialize() {
this.setHandler(
'Cart',
'cartClassPath',
'cartClassName',
'./mscart.class.js',
'msCart',
'Произошла ошибка при загрузке модуля корзины');

this.setHandler(
'Order',
'orderClassPath',
'orderClassName',
'./msorder.class.js',
'msOrder',
'Произошла ошибка при загрузке модуля отправки заказа');
if(!this.miniShop2Config.properties.length) { throw new Error('Не передан массив имён обработчиков'); }

if (this.miniShop2Config.notifySettingsPath) {
const response = await this.sendResponse({ url: this.miniShop2Config.notifySettingsPath, method: 'GET' });
if (response.ok) {
const messageSettings = await response.json();
if (messageSettings) {
this.setHandler(
'Message',
'notifyClassPath',
'notifyClassName',
'./msnotify.class.js',
'msNotify',
'Произошла ошибка при загрузке модуля уведомлений',
messageSettings);
}
}
}
await this.miniShop2Config.properties.forEach(property => {
this.setHandler(property);
});

document.addEventListener('submit', e => {
e.preventDefault();
const form = e.target;
const action = form.querySelector(this.action) ? form.querySelector(this.action).value : '';

if (action) {
const formData = new FormData(form);
const formData = new FormData(form),
components = this.getObjectMethod(action);
formData.append(this.actionName, action);
this.formData = formData;

this.controller(action);
this[components.object][components.method](this.formData);
}
});
}

controller(action) {
switch (action) {
case 'cart/add':
this.Cart.add(this.formData);
break;
case 'cart/remove':
this.Cart.remove(this.formData);
break;
case 'cart/change':
this.Cart.change(this.formData);
break;
case 'cart/clean':
this.Cart.clean(this.formData);
break;
case 'order/submit':
this.Order.submit(this.formData);
break;
case 'order/clean':
this.Order.clean(this.formData);
break;
default:
return;
}
getObjectMethod(action) {
const actionComponents = action.split('/'),
object = actionComponents[0].replace(actionComponents[0].substring(0, 1), actionComponents[0].substring(0, 1).toUpperCase()),
method = actionComponents[1];
return {object, method};
}

callbacksObjectTemplate() {
Expand Down Expand Up @@ -226,15 +200,13 @@ export default class MiniShop {
if (response.ok) {
const result = await response.json();
if (result.success) {
if (result.message) {
this.Message.success(result.message);
}
this.runCallback(callbacks.response.success, this, result);
this.runCallback(userCallbacks.response.success, this, result);
result.message ? this.Message.success(result.message) : '';
} else {
this.Message.error(result.message);
this.runCallback(callbacks.response.error, this, result);
this.runCallback(userCallbacks.response.error, this, result);
result.message ? this.Message.error(result.message) : '';
}
this.runCallback(callbacks.ajax.done, this, response);
this.runCallback(userCallbacks.ajax.done, this, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import MsNotify from './msnotify.class.js';

export default class MsIziToast extends MsNotify {
show(type, message) {
if (window[this.config.handlerClassName]) {
if (window[this.config.handlerClassName] && message) {
const options = Object.assign(this.config.handlerOptions, { title: message });
try {
window[this.config.handlerClassName][type](options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default class MsOrder {
}
}

field.value = response.data[key];
field.value = response.data[key] || '';
field.classList.remove('error');
field.closest(this.inputParent).classList.remove('error');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function registerFrontend($ctx = 'web')
$js .= '?v=' . substr(md5($this->version), 0, 10);
}
$js = str_replace($config['pl'], $config['vl'], $js);
$this->modx->regClientScript('<script type="module" src="'.$js.'"></script>',1);
$this->modx->regClientStartupScript('<script type="module" src="'.$js.'"></script>',1);
}

$js_setting = array(
Expand Down