aloha-vue 1.2.171 → 1.2.172

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.
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "Vue.js"
15
15
  ],
16
16
  "homepage": "https://github.com/ilia-brykin/aloha/#README.md",
17
- "version": "1.2.171",
17
+ "version": "1.2.172",
18
18
  "author": {
19
19
  "name": "Ilia Brykin",
20
20
  "email": "brykin.ilia@gmail.com"
@@ -1,125 +1,125 @@
1
- import {
2
- h,
3
- } from "vue";
4
-
5
- import AButton from "../../AButton/AButton";
6
- import AFilterCenterItem from "./AFilterCenterItem/AFilterCenterItem";
7
- import ASpinner from "../../ASpinner/ASpinner";
8
-
9
- import LoadingFiltersAPI from "./compositionAPI/LoadingFiltersAPI";
10
- import VisibleFiltersAPI from "./compositionAPI/VisibleFiltersAPI";
11
-
12
- export default {
13
- name: "AFilterCenter",
14
- props: {
15
- appliedModel: {
16
- type: Object,
17
- required: true,
18
- },
19
- closeAllFilters: {
20
- type: Function,
21
- required: true,
22
- },
23
- closeFilterValue: {
24
- type: Function,
25
- required: true,
26
- },
27
- dataKeyByKeyIdPerFilter: {
28
- type: Object,
29
- required: true,
30
- },
31
- disabled: {
32
- type: Boolean,
33
- required: false,
34
- },
35
- filtersGroup: {
36
- type: Object,
37
- required: true,
38
- default: () => ({
39
- alwaysVisible: [],
40
- filters: [],
41
- }),
42
- },
43
- filtersKeyById: {
44
- type: Object,
45
- required: true,
46
- },
47
- filtersVisibleAll: {
48
- type: Array,
49
- required: true,
50
- },
51
- id: {
52
- type: String,
53
- required: true,
54
- },
55
- },
56
- setup(props) {
57
- const {
58
- hasFilters,
59
- styleHide,
60
- updateVisibleChildFilters,
61
- } = VisibleFiltersAPI(props);
62
-
63
- const {
64
- isLeastOneChildFilterLoading,
65
- updateLoadingChildFilters,
66
- } = LoadingFiltersAPI(props);
67
-
68
- return {
69
- hasFilters,
70
- isLeastOneChildFilterLoading,
71
- styleHide,
72
- updateLoadingChildFilters,
73
- updateVisibleChildFilters,
74
- };
75
- },
76
- render() {
77
- if (!this.hasFilters) {
78
- return null;
79
- }
80
-
81
- return h("div", {
82
- class: "a_filters_center",
83
- }, [
84
- h("div", {
85
- class: "a_filters_center__item",
86
- }, [
87
- this.$slots.filtersHorizontal ?
88
- this.$slots.filtersHorizontal() :
89
- "",
90
- ]),
91
- this.filtersVisibleAll.map(filter => {
92
- return h(AFilterCenterItem, {
93
- key: filter.id,
94
- id: this.id,
95
- disabled: this.disabled,
96
- filter,
97
- closeFilterValue: this.closeFilterValue,
98
- dataKeyByKeyIdPerFilter: this.dataKeyByKeyIdPerFilter,
99
- model: this.appliedModel[filter.modelId || filter.id],
100
- onUpdateVisibleChildFilters: this.updateVisibleChildFilters,
101
- onUpdateLoadingChildFilters: this.updateLoadingChildFilters,
102
- }, this.$slots);
103
- }),
104
- this.isLeastOneChildFilterLoading ? h("div", {
105
- style: this.styleHide,
106
- class: "a_filters_center__item",
107
- }, [
108
- h(ASpinner, {
109
- class: "a_spinner_small",
110
- }),
111
- ]) : "",
112
- h("div", {
113
- class: "a_filters_center__item",
114
- style: this.styleHide,
115
- }, [
116
- h(AButton, {
117
- class: "a_btn a_btn_link a_btn_small",
118
- iconLeft: "Reset",
119
- text: "_A_FILTERS_DESELECT_ALL_",
120
- onClick: this.closeAllFilters,
121
- }),
122
- ]),
123
- ]);
124
- },
125
- };
1
+ import {
2
+ h,
3
+ } from "vue";
4
+
5
+ import AButton from "../../AButton/AButton";
6
+ import AFilterCenterItem from "./AFilterCenterItem/AFilterCenterItem";
7
+ import ASpinner from "../../ASpinner/ASpinner";
8
+
9
+ import LoadingFiltersAPI from "./compositionAPI/LoadingFiltersAPI";
10
+ import VisibleFiltersAPI from "./compositionAPI/VisibleFiltersAPI";
11
+
12
+ export default {
13
+ name: "AFilterCenter",
14
+ props: {
15
+ appliedModel: {
16
+ type: Object,
17
+ required: true,
18
+ },
19
+ closeAllFilters: {
20
+ type: Function,
21
+ required: true,
22
+ },
23
+ closeFilterValue: {
24
+ type: Function,
25
+ required: true,
26
+ },
27
+ dataKeyByKeyIdPerFilter: {
28
+ type: Object,
29
+ required: true,
30
+ },
31
+ disabled: {
32
+ type: Boolean,
33
+ required: false,
34
+ },
35
+ filtersGroup: {
36
+ type: Object,
37
+ required: true,
38
+ default: () => ({
39
+ alwaysVisible: [],
40
+ filters: [],
41
+ }),
42
+ },
43
+ filtersKeyById: {
44
+ type: Object,
45
+ required: true,
46
+ },
47
+ filtersVisibleAll: {
48
+ type: Array,
49
+ required: true,
50
+ },
51
+ id: {
52
+ type: String,
53
+ required: true,
54
+ },
55
+ },
56
+ setup(props) {
57
+ const {
58
+ hasFilters,
59
+ styleHide,
60
+ updateVisibleChildFilters,
61
+ } = VisibleFiltersAPI(props);
62
+
63
+ const {
64
+ isLeastOneChildFilterLoading,
65
+ updateLoadingChildFilters,
66
+ } = LoadingFiltersAPI(props);
67
+
68
+ return {
69
+ hasFilters,
70
+ isLeastOneChildFilterLoading,
71
+ styleHide,
72
+ updateLoadingChildFilters,
73
+ updateVisibleChildFilters,
74
+ };
75
+ },
76
+ render() {
77
+ if (!this.hasFilters) {
78
+ return null;
79
+ }
80
+
81
+ return h("div", {
82
+ class: "a_filters_center",
83
+ }, [
84
+ h("div", {
85
+ class: "a_filters_center__item",
86
+ }, [
87
+ this.$slots.filtersHorizontal ?
88
+ this.$slots.filtersHorizontal() :
89
+ "",
90
+ ]),
91
+ this.filtersVisibleAll.map(filter => {
92
+ return h(AFilterCenterItem, {
93
+ key: filter.modelId || filter.id,
94
+ id: this.id,
95
+ disabled: this.disabled,
96
+ filter,
97
+ closeFilterValue: this.closeFilterValue,
98
+ dataKeyByKeyIdPerFilter: this.dataKeyByKeyIdPerFilter,
99
+ model: this.appliedModel[filter.modelId || filter.id],
100
+ onUpdateVisibleChildFilters: this.updateVisibleChildFilters,
101
+ onUpdateLoadingChildFilters: this.updateLoadingChildFilters,
102
+ }, this.$slots);
103
+ }),
104
+ this.isLeastOneChildFilterLoading ? h("div", {
105
+ style: this.styleHide,
106
+ class: "a_filters_center__item",
107
+ }, [
108
+ h(ASpinner, {
109
+ class: "a_spinner_small",
110
+ }),
111
+ ]) : "",
112
+ h("div", {
113
+ class: "a_filters_center__item",
114
+ style: this.styleHide,
115
+ }, [
116
+ h(AButton, {
117
+ class: "a_btn a_btn_link a_btn_small",
118
+ iconLeft: "Reset",
119
+ text: "_A_FILTERS_DESELECT_ALL_",
120
+ onClick: this.closeAllFilters,
121
+ }),
122
+ ]),
123
+ ]);
124
+ },
125
+ };
@@ -1,42 +1,42 @@
1
- import {
2
- computed,
3
- toRef,
4
- } from "vue";
5
-
6
- import {
7
- typesWithData,
8
- } from "../../../../ui/const/AUiTypes";
9
- import {
10
- isEmpty,
11
- } from "lodash-es";
12
-
13
- export default function IsDataLoadingAPI(props, { emit }, {
14
- hasCurrentFilter = computed(() => false),
15
- }) {
16
- const dataKeyByKeyIdPerFilter = toRef(props, "dataKeyByKeyIdPerFilter");
17
- const filter = toRef(props, "filter");
18
-
19
- const isFilterLoading = computed(() => {
20
- if (!hasCurrentFilter.value) {
21
- return false;
22
- }
23
- if (typesWithData.value[filter.value.type]) {
24
- return isEmpty(dataKeyByKeyIdPerFilter.value[filter.value.id]);
25
- }
26
- return false;
27
- });
28
-
29
- const updateLoadingChildFilters = (newValue, altValue) => {
30
- if (newValue !== altValue) {
31
- emit("updateLoadingChildFilters", {
32
- id: filter.value.id,
33
- isLoading: isFilterLoading.value,
34
- });
35
- }
36
- };
37
-
38
- return {
39
- isFilterLoading,
40
- updateLoadingChildFilters,
41
- };
42
- }
1
+ import {
2
+ computed,
3
+ toRef,
4
+ } from "vue";
5
+
6
+ import {
7
+ typesWithData,
8
+ } from "../../../../ui/const/AUiTypes";
9
+ import {
10
+ isEmpty,
11
+ } from "lodash-es";
12
+
13
+ export default function IsDataLoadingAPI(props, { emit }, {
14
+ hasCurrentFilter = computed(() => false),
15
+ }) {
16
+ const dataKeyByKeyIdPerFilter = toRef(props, "dataKeyByKeyIdPerFilter");
17
+ const filter = toRef(props, "filter");
18
+
19
+ const isFilterLoading = computed(() => {
20
+ if (!hasCurrentFilter.value) {
21
+ return false;
22
+ }
23
+ if (typesWithData.value[filter.value.type]) {
24
+ return isEmpty(dataKeyByKeyIdPerFilter.value[filter.value.modelId || filter.value.id]);
25
+ }
26
+ return false;
27
+ });
28
+
29
+ const updateLoadingChildFilters = (newValue, altValue) => {
30
+ if (newValue !== altValue) {
31
+ emit("updateLoadingChildFilters", {
32
+ id: filter.value.modelId || filter.value.id,
33
+ isLoading: isFilterLoading.value,
34
+ });
35
+ }
36
+ };
37
+
38
+ return {
39
+ isFilterLoading,
40
+ updateLoadingChildFilters,
41
+ };
42
+ }
@@ -1,133 +1,151 @@
1
- import {
2
- computed,
3
- ref,
4
- toRef,
5
- } from "vue";
6
-
7
- import {
8
- typesContainer,
9
- } from "../../../ui/const/AUiTypes";
10
- import {
11
- isModelForFilterNotEmpty,
12
- } from "../../../utils/utilsUi";
13
- import {
14
- cloneDeep,
15
- forEach,
16
- } from "lodash-es";
17
-
18
- export default function FiltersAPI(props, { emit }) {
19
- const filters = toRef(props, "filters");
20
- const filterMain = toRef(props, "filterMain");
21
- const unappliedModel = toRef(props, "unappliedModel");
22
-
23
- const filtersDataKeyById = ref({});
24
- const filtersHorizontalRef = ref(undefined);
25
- const filtersVisibleIds = ref([]);
26
-
27
- const hasFilters = computed(() => {
28
- return !!(filters.value.length || filterMain.value);
29
- });
30
-
31
- const filtersKeyById = computed(() => {
32
- const FILTERS = {};
33
- forEach(cloneDeep(filters.value), filter => {
34
- FILTERS[filter.id] = filter;
35
- if (typesContainer.value[filter.type] &&
36
- filter.children &&
37
- filter.children.length) {
38
- forEach(filter.children, filterChild => {
39
- FILTERS[filterChild.id] = filterChild;
40
- });
41
- }
42
- });
43
- return FILTERS;
44
- });
45
-
46
- const filtersGroup = computed(() => {
47
- const FILTER_GROUP = {
48
- alwaysVisible: [],
49
- filters: [],
50
- };
51
- forEach(cloneDeep(filters.value), filter => {
52
- if (filter.alwaysVisible) {
53
- FILTER_GROUP.alwaysVisible.push(filter);
54
- } else {
55
- FILTER_GROUP.filters.push(filter);
56
- }
57
- });
58
-
59
- return FILTER_GROUP;
60
- });
61
-
62
- const filtersVisible = computed(() => {
63
- const FILTERS = [];
64
- forEach(filtersVisibleIds.value, filterId => {
65
- if (filtersKeyById.value[filterId]) {
66
- FILTERS.push(filtersKeyById.value[filterId]);
67
- }
68
- });
69
- return FILTERS;
70
- });
71
-
72
- const filtersVisibleAll = computed(() => {
73
- const FILTERS = [];
74
- FILTERS.push(...filters.value);
75
- const FILERS_NEW = [];
76
- forEach(FILTERS, filter => {
77
- FILERS_NEW.push(filter);
78
- if (typesContainer.value[filter.type] &&
79
- filter.children &&
80
- filter.children.length) {
81
- forEach(filter.children, filterChild => {
82
- FILERS_NEW.push(filterChild);
83
- });
84
- }
85
- });
86
- return FILERS_NEW;
87
- });
88
-
89
- const setFiltersVisibleIds = () => {
90
- const FILTERS_VISIBLE_IDS = [];
91
- forEach(filtersGroup.value.filters, filter => {
92
- if (isModelForFilterNotEmpty({
93
- filter, modelObj: unappliedModel.value,
94
- })) {
95
- FILTERS_VISIBLE_IDS.push(filter.id);
96
- }
97
- });
98
- filtersVisibleIds.value = FILTERS_VISIBLE_IDS;
99
- };
100
-
101
- const onUpdateModelFilters = ({ model, isUpdateFiltersVisible }) => {
102
- emit("update:unappliedModel", model);
103
-
104
- if (isUpdateFiltersVisible) {
105
- setTimeout(() => {
106
- setFiltersVisibleIds();
107
- });
108
- }
109
- };
110
-
111
- const updateAppliedModel = ({ model }) => {
112
- emit("update:appliedModel", model);
113
- };
114
-
115
- const updateMainModel = ({ model }) => {
116
- emit("update:mainModel", model);
117
- };
118
-
119
- return {
120
- filtersDataKeyById,
121
- filtersGroup,
122
- filtersHorizontalRef,
123
- filtersKeyById,
124
- filtersVisible,
125
- filtersVisibleAll,
126
- filtersVisibleIds,
127
- hasFilters,
128
- onUpdateModelFilters,
129
- setFiltersVisibleIds,
130
- updateAppliedModel,
131
- updateMainModel,
132
- };
133
- }
1
+ import {
2
+ computed,
3
+ ref,
4
+ toRef,
5
+ } from "vue";
6
+
7
+ import {
8
+ typesContainer,
9
+ } from "../../../ui/const/AUiTypes";
10
+ import {
11
+ isModelForFilterNotEmpty,
12
+ } from "../../../utils/utilsUi";
13
+ import {
14
+ cloneDeep,
15
+ forEach,
16
+ } from "lodash-es";
17
+
18
+ export default function FiltersAPI(props, { emit }) {
19
+ const filters = toRef(props, "filters");
20
+ const filterMain = toRef(props, "filterMain");
21
+ const unappliedModel = toRef(props, "unappliedModel");
22
+
23
+ const filtersDataKeyById = ref({});
24
+ const filtersHorizontalRef = ref(undefined);
25
+ const filtersVisibleIds = ref([]);
26
+
27
+ const hasFilters = computed(() => {
28
+ return !!(filters.value.length || filterMain.value);
29
+ });
30
+
31
+ const filtersKeyById = computed(() => {
32
+ const FILTERS = {};
33
+ forEach(cloneDeep(filters.value), filter => {
34
+ FILTERS[filter.modelId || filter.id] = {
35
+ ...filter,
36
+ id: filter.modelId || filter.id,
37
+ };
38
+ if (typesContainer.value[filter.type] &&
39
+ filter.children &&
40
+ filter.children.length) {
41
+ forEach(filter.children, filterChild => {
42
+ FILTERS[filter.modelId || filterChild.id] = {
43
+ ...filterChild,
44
+ id: filterChild.modelId || filterChild.id,
45
+ };
46
+ });
47
+ }
48
+ });
49
+ return FILTERS;
50
+ });
51
+
52
+ const filtersGroup = computed(() => {
53
+ const FILTER_GROUP = {
54
+ alwaysVisible: [],
55
+ filters: [],
56
+ };
57
+ forEach(cloneDeep(filters.value), filter => {
58
+ if (filter.alwaysVisible) {
59
+ FILTER_GROUP.alwaysVisible.push({
60
+ ...filter,
61
+ id: filter.modelId || filter.id,
62
+ });
63
+ } else {
64
+ FILTER_GROUP.filters.push({
65
+ ...filter,
66
+ id: filter.modelId || filter.id,
67
+ });
68
+ }
69
+ });
70
+
71
+ return FILTER_GROUP;
72
+ });
73
+
74
+ const filtersVisible = computed(() => {
75
+ const FILTERS = [];
76
+ forEach(filtersVisibleIds.value, filterId => {
77
+ if (filtersKeyById.value[filterId]) {
78
+ FILTERS.push(filtersKeyById.value[filterId]);
79
+ }
80
+ });
81
+ return FILTERS;
82
+ });
83
+
84
+ const filtersVisibleAll = computed(() => {
85
+ const FILTERS = [];
86
+ FILTERS.push(...filters.value);
87
+ const FILERS_NEW = [];
88
+ forEach(FILTERS, filter => {
89
+ FILERS_NEW.push({
90
+ ...filter,
91
+ id: filter.modelId || filter.id,
92
+ });
93
+ if (typesContainer.value[filter.type] &&
94
+ filter.children &&
95
+ filter.children.length) {
96
+ forEach(filter.children, filterChild => {
97
+ FILERS_NEW.push({
98
+ ...filterChild,
99
+ id: filterChild.modelId || filterChild.id,
100
+ });
101
+ });
102
+ }
103
+ });
104
+ return FILERS_NEW;
105
+ });
106
+
107
+ const setFiltersVisibleIds = () => {
108
+ const FILTERS_VISIBLE_IDS = [];
109
+ forEach(filtersGroup.value.filters, filter => {
110
+ if (isModelForFilterNotEmpty({
111
+ filter, modelObj: unappliedModel.value,
112
+ })) {
113
+ FILTERS_VISIBLE_IDS.push(filter.id);
114
+ }
115
+ });
116
+ filtersVisibleIds.value = FILTERS_VISIBLE_IDS;
117
+ };
118
+
119
+ const onUpdateModelFilters = ({ model, isUpdateFiltersVisible }) => {
120
+ emit("update:unappliedModel", model);
121
+
122
+ if (isUpdateFiltersVisible) {
123
+ setTimeout(() => {
124
+ setFiltersVisibleIds();
125
+ });
126
+ }
127
+ };
128
+
129
+ const updateAppliedModel = ({ model }) => {
130
+ emit("update:appliedModel", model);
131
+ };
132
+
133
+ const updateMainModel = ({ model }) => {
134
+ emit("update:mainModel", model);
135
+ };
136
+
137
+ return {
138
+ filtersDataKeyById,
139
+ filtersGroup,
140
+ filtersHorizontalRef,
141
+ filtersKeyById,
142
+ filtersVisible,
143
+ filtersVisibleAll,
144
+ filtersVisibleIds,
145
+ hasFilters,
146
+ onUpdateModelFilters,
147
+ setFiltersVisibleIds,
148
+ updateAppliedModel,
149
+ updateMainModel,
150
+ };
151
+ }
@@ -1,207 +1,216 @@
1
- import {
2
- computed,
3
- ref,
4
- toRef,
5
- } from "vue";
6
-
7
- import AHttpAPI from "../../compositionAPI/AHttpAPI";
8
-
9
- import AKeyId from "../../const/AKeyId";
10
- import {
11
- cloneDeep,
12
- filter,
13
- find,
14
- forEach,
15
- get,
16
- isArray,
17
- isMatch,
18
- isNil,
19
- isNumber,
20
- isString,
21
- map,
22
- uniq,
23
- uniqBy,
24
- } from "lodash-es";
25
-
26
- export default function UiDataFromServerAPI(props, {
27
- changeModel = () => {},
28
- dataExtraLocal = computed(() => []),
29
- dataFromServer = ref([])
30
- } = {}) {
31
- const apiSaveId = toRef(props, "apiSaveId");
32
- const isDataSimpleArray = toRef(props, "isDataSimpleArray");
33
- const keyId = toRef(props, "keyId");
34
- const keyLabel = toRef(props, "keyLabel");
35
- const searchApi = toRef(props, "searchApi");
36
- const modelValue = toRef(props, "modelValue");
37
- const searchApiKey = toRef(props, "searchApiKey");
38
- const type = toRef(props, "type");
39
- const url = toRef(props, "url");
40
- const urlParams = toRef(props, "urlParams");
41
-
42
- const loadingSearchApi = ref(false);
43
- const loadingDataFromServer = ref(undefined);
44
-
45
- const {
46
- getListHttp,
47
- } = AHttpAPI();
48
-
49
- const searchApiLocal = computed(() => {
50
- return !!(searchApi.value && searchApiKey.value && url.value);
51
- });
52
-
53
- const urlPropsComputed = computed(() => {
54
- return [
55
- apiSaveId,
56
- url,
57
- urlParams,
58
- ];
59
- });
60
-
61
- const loadDataFromServer = () => {
62
- if (!url.value || searchApiLocal.value) {
63
- dataFromServer.value = [];
64
- return;
65
- }
66
- loadingDataFromServer.value = true;
67
- getListHttp({
68
- url: url.value,
69
- urlParams: urlParams.value,
70
- apiSaveId: apiSaveId.value,
71
- }).then(
72
- response => {
73
- dataFromServer.value = response;
74
- }
75
- ).finally(
76
- () => {
77
- loadingDataFromServer.value = false;
78
- }
79
- );
80
- };
81
-
82
- const isTypeList = computed(() => {
83
- return type.value === "checkbox" || type.value === "multiselect";
84
- });
85
-
86
- const updateUrlPropsComputed = (newVal, oldVal) => {
87
- if (!isNil(oldVal) && !isMatch(oldVal, newVal)) {
88
- loadDataFromServer();
89
- if (isTypeList.value) {
90
- changeModel({
91
- model: [],
92
- currentModel: [],
93
- });
94
- } else {
95
- changeModel({
96
- model: undefined,
97
- currentModel: undefined,
98
- });
99
- }
100
- }
101
- };
102
-
103
- const uniqueList = list => {
104
- if (isDataSimpleArray.value) {
105
- return uniq(list);
106
- }
107
- return uniqBy(list, keyId.value);
108
- };
109
-
110
- const isValidModelValue = value => {
111
- if (dataExtraLocal.value.length) {
112
- return !!find(dataExtraLocal.value, [AKeyId, value]);
113
- }
114
- return isString(value) || isNumber(value);
115
- };
116
-
117
- const modelArrayWithoutDataExtra = computed(() => {
118
- let model = [];
119
- if (isArray(modelValue.value) && modelValue.value.length) {
120
- model = filter(modelValue.value, currentModel => isValidModelValue(currentModel));
121
- } else if (isValidModelValue(modelValue.value)) {
122
- model = [modelValue.value];
123
- }
124
- return model;
125
- });
126
-
127
- const changeDataFromServerWithModel = ({ response }) => {
128
- const DATA_FROM_SERVER_ALT = cloneDeep(dataFromServer.value);
129
- const DATA_FROM_SERVER_NEW = [];
130
- const MODEL_ARRAY = cloneDeep(modelArrayWithoutDataExtra.value);
131
-
132
- forEach(DATA_FROM_SERVER_ALT, itemAlt => {
133
- if (MODEL_ARRAY.length === 0) {
134
- return false;
135
- }
136
- const ID = isDataSimpleArray.value ? itemAlt : get(itemAlt, keyId.value);
137
- const INDEX_IN_MODEL = MODEL_ARRAY.indexOf(ID);
138
- if (INDEX_IN_MODEL !== -1) {
139
- MODEL_ARRAY.splice(INDEX_IN_MODEL, 1);
140
- DATA_FROM_SERVER_NEW.push(itemAlt);
141
- }
142
- });
143
- DATA_FROM_SERVER_NEW.push(...response);
144
- dataFromServer.value = uniqueList(DATA_FROM_SERVER_NEW);
145
- };
146
-
147
- const onSearchInApi = ({ search, data = [] }) => {
148
- if (!searchApiLocal.value) {
149
- return;
150
- }
151
- loadingSearchApi.value = true;
152
- getListHttp({
153
- url: url.value,
154
- urlParams: {
155
- ...(urlParams.value || {}),
156
- [searchApiKey.value]: search,
157
- },
158
- }).then(
159
- response => {
160
- if (isDataSimpleArray.value) {
161
- response = map(response, item => item[keyLabel.value]);
162
- }
163
- changeDataFromServerWithModel({ response, data });
164
- },
165
- ).then(
166
- () => {
167
- loadingSearchApi.value = false;
168
- }
169
- );
170
- };
171
-
172
- const loadDataFromServerForSearchAPI = () => {
173
- if (!searchApiLocal.value ||
174
- !modelArrayWithoutDataExtra.value.length) {
175
- return;
176
- }
177
- loadingDataFromServer.value = true;
178
- const URL_PARAMS = {
179
- ...urlParams.value,
180
- ...{
181
- [keyId.value]: modelArrayWithoutDataExtra.value,
182
- limit: modelArrayWithoutDataExtra.value.length,
183
- },
184
- };
185
- return getListHttp({
186
- url: url.value,
187
- urlParams: URL_PARAMS,
188
- }).then(
189
- response => {
190
- dataFromServer.value = response || [];
191
- }
192
- ).finally(() => {
193
- loadingDataFromServer.value = false;
194
- });
195
- };
196
-
197
- return {
198
- loadDataFromServer,
199
- loadDataFromServerForSearchAPI,
200
- loadingDataFromServer,
201
- loadingSearchApi,
202
- onSearchInApi,
203
- searchApiLocal,
204
- updateUrlPropsComputed,
205
- urlPropsComputed,
206
- };
207
- }
1
+ import {
2
+ computed,
3
+ ref,
4
+ toRef,
5
+ } from "vue";
6
+
7
+ import AHttpAPI from "../../compositionAPI/AHttpAPI";
8
+
9
+ import AKeyId from "../../const/AKeyId";
10
+ import {
11
+ cloneDeep,
12
+ filter,
13
+ find,
14
+ forEach,
15
+ get,
16
+ isArray,
17
+ isMatch,
18
+ isNil,
19
+ isNumber,
20
+ isString,
21
+ map,
22
+ uniq,
23
+ uniqBy,
24
+ } from "lodash-es";
25
+
26
+ export default function UiDataFromServerAPI(props, {
27
+ changeModel = () => {},
28
+ dataExtraLocal = computed(() => []),
29
+ dataFromServer = ref([])
30
+ } = {}) {
31
+ const apiSaveId = toRef(props, "apiSaveId");
32
+ const isDataSimpleArray = toRef(props, "isDataSimpleArray");
33
+ const keyId = toRef(props, "keyId");
34
+ const keyLabel = toRef(props, "keyLabel");
35
+ const searchApi = toRef(props, "searchApi");
36
+ const modelValue = toRef(props, "modelValue");
37
+ const searchApiKey = toRef(props, "searchApiKey");
38
+ const type = toRef(props, "type");
39
+ const url = toRef(props, "url");
40
+ const urlParams = toRef(props, "urlParams");
41
+
42
+ const loadingSearchApi = ref(false);
43
+ const loadingDataFromServer = ref(undefined);
44
+
45
+ const {
46
+ getListHttp,
47
+ } = AHttpAPI();
48
+
49
+ const searchApiLocal = computed(() => {
50
+ return !!(searchApi.value && searchApiKey.value && url.value);
51
+ });
52
+
53
+ const urlPropsComputed = computed(() => {
54
+ return [
55
+ apiSaveId,
56
+ url,
57
+ urlParams,
58
+ ];
59
+ });
60
+
61
+ const loadDataFromServer = () => {
62
+ if (!url.value || searchApiLocal.value) {
63
+ dataFromServer.value = [];
64
+ return;
65
+ }
66
+ loadingDataFromServer.value = true;
67
+ getListHttp({
68
+ url: url.value,
69
+ urlParams: urlParams.value,
70
+ apiSaveId: apiSaveId.value,
71
+ }).then(
72
+ response => {
73
+ dataFromServer.value = response;
74
+ }
75
+ ).finally(
76
+ () => {
77
+ loadingDataFromServer.value = false;
78
+ }
79
+ );
80
+ };
81
+
82
+ const isTypeList = computed(() => {
83
+ return type.value === "checkbox" || type.value === "multiselect";
84
+ });
85
+
86
+ const updateUrlPropsComputed = (newVal, oldVal) => {
87
+ if (!isNil(oldVal) && !isMatch(oldVal, newVal)) {
88
+ loadDataFromServer();
89
+ if (isTypeList.value) {
90
+ changeModel({
91
+ model: [],
92
+ currentModel: [],
93
+ });
94
+ } else {
95
+ changeModel({
96
+ model: undefined,
97
+ currentModel: undefined,
98
+ });
99
+ }
100
+ }
101
+ };
102
+
103
+ const uniqueList = list => {
104
+ if (isDataSimpleArray.value) {
105
+ return uniq(list);
106
+ }
107
+ return uniqBy(list, keyId.value);
108
+ };
109
+
110
+ const isValidModelValue = value => {
111
+ if (dataExtraLocal.value.length) {
112
+ return !find(dataExtraLocal.value, [AKeyId, value]);
113
+ }
114
+ return isString(value) || isNumber(value);
115
+ };
116
+
117
+ const modelArrayWithoutDataExtra = computed(() => {
118
+ let model = [];
119
+ if (isArray(modelValue.value) && modelValue.value.length) {
120
+ model = filter(modelValue.value, currentModel => isValidModelValue(currentModel));
121
+ } else if (isValidModelValue(modelValue.value)) {
122
+ model = [modelValue.value];
123
+ }
124
+ return model;
125
+ });
126
+
127
+ const changeDataFromServerWithModel = ({ response }) => {
128
+ const DATA_FROM_SERVER_ALT = cloneDeep(dataFromServer.value);
129
+ const DATA_FROM_SERVER_NEW = [];
130
+ const MODEL_ARRAY = cloneDeep(modelArrayWithoutDataExtra.value);
131
+
132
+ forEach(DATA_FROM_SERVER_ALT, itemAlt => {
133
+ if (MODEL_ARRAY.length === 0) {
134
+ return false;
135
+ }
136
+ const ID = isDataSimpleArray.value ? itemAlt : get(itemAlt, keyId.value);
137
+ const INDEX_IN_MODEL = MODEL_ARRAY.indexOf(ID);
138
+ if (INDEX_IN_MODEL !== -1) {
139
+ MODEL_ARRAY.splice(INDEX_IN_MODEL, 1);
140
+ DATA_FROM_SERVER_NEW.push(itemAlt);
141
+ }
142
+ });
143
+ DATA_FROM_SERVER_NEW.push(...response);
144
+ dataFromServer.value = uniqueList(DATA_FROM_SERVER_NEW);
145
+ };
146
+
147
+ const onSearchInApi = ({ search, data = [] }) => {
148
+ if (!searchApiLocal.value) {
149
+ return;
150
+ }
151
+ loadingSearchApi.value = true;
152
+ getListHttp({
153
+ url: url.value,
154
+ urlParams: {
155
+ ...(urlParams.value || {}),
156
+ [searchApiKey.value]: search,
157
+ },
158
+ }).then(
159
+ response => {
160
+ if (isDataSimpleArray.value) {
161
+ response = map(response, item => item[keyLabel.value]);
162
+ }
163
+ changeDataFromServerWithModel({ response, data });
164
+ },
165
+ ).then(
166
+ () => {
167
+ loadingSearchApi.value = false;
168
+ }
169
+ );
170
+ };
171
+
172
+ const loadDataFromServerForSearchAPI = () => {
173
+ if (!searchApiLocal.value ||
174
+ !modelArrayWithoutDataExtra.value.length) {
175
+ return;
176
+ }
177
+ let MODEL_ARRAY;
178
+ if (keyId.value === "pk") {
179
+ MODEL_ARRAY = filter(modelArrayWithoutDataExtra.value, value => !isNil(value));
180
+ } else {
181
+ MODEL_ARRAY = cloneDeep(modelArrayWithoutDataExtra.value);
182
+ }
183
+ if (!MODEL_ARRAY.length) {
184
+ return;
185
+ }
186
+ loadingDataFromServer.value = true;
187
+ const URL_PARAMS = {
188
+ ...urlParams.value,
189
+ ...{
190
+ [keyId.value]: MODEL_ARRAY,
191
+ limit: MODEL_ARRAY.length,
192
+ },
193
+ };
194
+ return getListHttp({
195
+ url: url.value,
196
+ urlParams: URL_PARAMS,
197
+ }).then(
198
+ response => {
199
+ dataFromServer.value = response || [];
200
+ }
201
+ ).finally(() => {
202
+ loadingDataFromServer.value = false;
203
+ });
204
+ };
205
+
206
+ return {
207
+ loadDataFromServer,
208
+ loadDataFromServerForSearchAPI,
209
+ loadingDataFromServer,
210
+ loadingSearchApi,
211
+ onSearchInApi,
212
+ searchApiLocal,
213
+ updateUrlPropsComputed,
214
+ urlPropsComputed,
215
+ };
216
+ }