vgapp 1.1.8 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,6 +32,7 @@
32
32
  import BaseModule from "../../base-module";
33
33
  import VGModal from "../../vgmodal/js/vgmodal";
34
34
  import VGCollapse from "../../vgcollapse/js/vgcollapse";
35
+ import VGToast from "../../vgtoast/js/vgtoast";
35
36
  import VGHideShowPass from "./hideshowpass";
36
37
  import {lang_titles, lang_messages} from "../../../utils/js/components/lang";
37
38
  import Html from "../../../utils/js/components/templater";
@@ -133,7 +134,8 @@ class VGFormSender extends BaseModule {
133
134
  enabled: true,
134
135
  type: 'modal',
135
136
  errors: true,
136
- delay: 0
137
+ delay: 0,
138
+ toast: {}
137
139
  },
138
140
  ajax: {
139
141
  route: '',
@@ -510,6 +512,10 @@ class VGFormSender extends BaseModule {
510
512
  if (this._params.alert.type === 'collapse') {
511
513
  this._alertCollapse(data, status)
512
514
  }
515
+
516
+ if (this._params.alert.type === 'toast') {
517
+ this._alertToast(data, status)
518
+ }
513
519
  }
514
520
 
515
521
  /**
@@ -602,6 +608,65 @@ class VGFormSender extends BaseModule {
602
608
  }
603
609
  }
604
610
 
611
+ /**
612
+ * Показ алерта в виде toast
613
+ * @param {Object} data - Данные для отображения
614
+ * @param {string} status - Статус (success/error)
615
+ * @private
616
+ */
617
+ _alertToast(data, status) {
618
+ const response = this._prepareAlertResponse(status, data);
619
+ const toastParams = this._getToastParams(status);
620
+
621
+ if (response.title) {
622
+ VGToast.run([response.title, response.message], toastParams);
623
+ return;
624
+ }
625
+
626
+ VGToast.run(response.message, toastParams);
627
+ }
628
+
629
+ /**
630
+ * Сборка параметров toast из alert.* и alert.toast
631
+ * @param {string} status - Статус алерта
632
+ * @returns {Object}
633
+ * @private
634
+ */
635
+ _getToastParams(status) {
636
+ const theme = status === 'error' ? 'danger' : status;
637
+ const delay = this._params.alert.delay > 0 ? this._params.alert.delay : 3000;
638
+ const flatToastParams = {};
639
+ const allowedKeys = [
640
+ 'static',
641
+ 'placement',
642
+ 'autohide',
643
+ 'delay',
644
+ 'enableClickToast',
645
+ 'enableButtonClose',
646
+ 'keyboard',
647
+ 'theme',
648
+ 'type',
649
+ 'drag',
650
+ 'resize',
651
+ 'stack',
652
+ 'animation',
653
+ 'ajax'
654
+ ];
655
+
656
+ allowedKeys.forEach((key) => {
657
+ if (key in this._params.alert) {
658
+ flatToastParams[key] = this._params.alert[key];
659
+ }
660
+ });
661
+
662
+ return mergeDeepObject({
663
+ theme: theme || 'dark',
664
+ enableButtonClose: true,
665
+ autohide: this._params.alert.delay > 0,
666
+ delay: delay
667
+ }, flatToastParams, this._params.alert.toast || {});
668
+ }
669
+
605
670
  /**
606
671
  * Формирование содержимого алерта (заголовок, текст, иконка)
607
672
  * @param {HTMLElement} $element - Родительский элемент
@@ -611,15 +676,51 @@ class VGFormSender extends BaseModule {
611
676
  * @returns {HTMLElement} - DOM-элемент с контентом
612
677
  */
613
678
  setDataRelationStatus($element, status, data, type) {
614
- let response = normalizeData(data.response) || data,
615
- $alert = Selectors.find('.'+ CLASS_NAME_ALERT +'-content', $element);
679
+ let $alert = Selectors.find('.'+ CLASS_NAME_ALERT +'-content', $element);
680
+ const response = this._prepareAlertResponse(status, data);
681
+ const title = response.title
682
+ ? Html('string').h4({class: CLASS_NAME_ALERT +'-content--title'}, response.title)
683
+ : '';
684
+ const content = title + response.message;
685
+
686
+ if (!$alert) {
687
+ const elm = Html('dom');
688
+
689
+ $alert = elm.div({
690
+ class: CLASS_NAME_ALERT + '-' + type
691
+ }, [
692
+ elm.div({class: CLASS_NAME_ALERT + '-content'}, [
693
+ elm.i({class: CLASS_NAME_ALERT + '-content--icon'}, getSVG(status), {isHTML: true}),
694
+ elm.div({class: CLASS_NAME_ALERT + '-content--text'}, content, {isHTML: true})
695
+ ]),
696
+ ]);
697
+ } else {
698
+ let text = Selectors.find('.vg-modal-body', $element);
699
+ text.innerHTML = content;
700
+ }
701
+
702
+ return $alert;
703
+ }
704
+
705
+ /**
706
+ * Подготовка содержимого алерта
707
+ * @param {string} status - Статус алерта
708
+ * @param {Object|string} data - Данные ответа
709
+ * @returns {{title: string, message: string}}
710
+ * @private
711
+ */
712
+ _prepareAlertResponse(status, data) {
713
+ let response = normalizeData(data?.response) || data;
616
714
 
617
715
  if (isObject(data)) {
618
- let view = '';
716
+ if (isObject(data.response) && 'view' in data.response) {
717
+ return {
718
+ title: '',
719
+ message: data.response.view
720
+ };
721
+ }
619
722
 
620
- if ('view' in data.response) {
621
- response = data.response.view
622
- } else if (typeof response !== 'string') {
723
+ if (typeof response !== 'string') {
623
724
  if (status === 'danger') {
624
725
  response.title = ('title' in response) ? response.title : lang_titles(this._params.lang, 'errors').title;
625
726
 
@@ -646,7 +747,7 @@ class VGFormSender extends BaseModule {
646
747
  if (isObject(errors)) {
647
748
  for (const error in errors) {
648
749
  if (Array.isArray(errors[error])) {
649
- errors[error].forEach((t) => response.message.push(t))
750
+ errors[error].forEach((text) => response.message.push(text))
650
751
  } else {
651
752
  response.message.push(errors[error]);
652
753
  }
@@ -656,42 +757,31 @@ class VGFormSender extends BaseModule {
656
757
  }
657
758
 
658
759
  const elm = Html('string');
659
-
660
- view = elm.h4({class: CLASS_NAME_ALERT +'-content--title'}, response.title);
760
+ let message = '';
661
761
 
662
762
  if (Array.isArray(response.message)) {
663
- response.message.forEach(message => {
664
- view += elm.div({
763
+ response.message.forEach((text) => {
764
+ message += elm.div({
665
765
  class: CLASS_NAME_ALERT +'-content--message'
666
- }, message);
766
+ }, text);
667
767
  })
668
768
  } else {
669
- view += elm.div({
769
+ message = elm.div({
670
770
  class: CLASS_NAME_ALERT +'-content--message'
671
771
  }, response.message);
672
772
  }
673
773
 
674
- response = view;
774
+ return {
775
+ title: response.title || '',
776
+ message: message
777
+ };
675
778
  }
676
779
  }
677
780
 
678
- if (!$alert) {
679
- const elm = Html('dom');
680
-
681
- $alert = elm.div({
682
- class: CLASS_NAME_ALERT + '-' + type
683
- }, [
684
- elm.div({class: CLASS_NAME_ALERT + '-content'}, [
685
- elm.i({class: CLASS_NAME_ALERT + '-content--icon'}, getSVG(status), {isHTML: true}),
686
- elm.div({class: CLASS_NAME_ALERT + '-content--text'}, response, {isHTML: true})
687
- ]),
688
- ]);
689
- } else {
690
- let text = Selectors.find('.vg-modal-body', $element);
691
- text.innerHTML = response;
692
- }
693
-
694
- return $alert;
781
+ return {
782
+ title: '',
783
+ message: typeof response === 'string' ? response : ''
784
+ };
695
785
  }
696
786
 
697
787
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vgapp",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "",
5
5
  "author": {
6
6
  "name": "Vegas Studio",