|
4 | 4 | vm.loading = false;
|
5 | 5 |
|
6 | 6 | vm.searchQuery = "";
|
7 |
| - vm.searchIndex = {}; |
| 7 | + vm.selectedSearchIndex = {}; |
8 | 8 | vm.searchResults = {};
|
9 | 9 |
|
10 | 10 | vm.viewState = "list";
|
|
20 | 20 | vm.search = search;
|
21 | 21 |
|
22 | 22 | function init() {
|
23 |
| - |
24 | 23 | // contentData property:
|
25 | 24 | // array of objects:
|
26 | 25 | // contentType -> string value
|
| 26 | + // contentTypeIcon -> string value |
27 | 27 | // properties -> string array
|
28 | 28 | vm.manageIndex = {
|
29 | 29 | id: 0,
|
30 | 30 | name: "",
|
31 |
| - selectedContentType: "", |
32 |
| - contentTypes: [], |
33 |
| - properties: [], |
34 |
| - contentData: [], |
35 |
| - selectContentType: function (contentType) { |
36 |
| - |
37 |
| - this.properties = []; |
38 |
| - |
39 |
| - var checked = !contentType.checked; |
40 |
| - |
41 |
| - if (checked) { |
42 |
| - |
43 |
| - this.selectedContentType = contentType.alias; |
44 |
| - this.contentTypes.find(p => p.alias == contentType.alias).checked = true; |
45 |
| - |
46 |
| - var contentItem = { |
47 |
| - contentType: contentType.alias, |
48 |
| - properties: [] |
49 |
| - }; |
50 |
| - |
51 |
| - this.contentData.push(contentItem); |
| 31 | + selectedContentType: {}, |
| 32 | + contentTypesList: [], |
| 33 | + propertiesList: [], |
| 34 | + includeProperties: [ |
| 35 | + { |
| 36 | + "alias": "alias", |
| 37 | + "header": "Alias" |
| 38 | + }, |
| 39 | + { |
| 40 | + "alias": "group", |
| 41 | + "header": "Group" |
52 | 42 | }
|
53 |
| - else { |
54 |
| - this.contentTypes.find(p => p.alias == contentType.alias).checked = false; |
55 |
| - |
56 |
| - const contentTypeIndex = this.contentData.findIndex((obj) => obj.contentType === contentType.alias); |
57 |
| - |
58 |
| - if (contentTypeIndex > -1) this.contentData.splice(contentTypeIndex, 1); |
| 43 | + ], |
| 44 | + contentData: [], |
| 45 | + showProperties: function (contentType) { |
59 | 46 |
|
60 |
| - this.selectedContentType = ""; |
| 47 | + this.selectedContentType = contentType; |
61 | 48 |
|
62 |
| - this.properties = []; |
63 |
| - } |
64 |
| - }, |
65 |
| - showProperties: function (contentType) { |
66 | 49 | algoliaService.getPropertiesByContentTypeId(contentType.id, (response) => {
|
67 |
| - this.properties = response; |
| 50 | + this.propertiesList = response; |
68 | 51 |
|
69 |
| - var contentTypeData = this.contentData.find(p => p.contentType == contentType.alias); |
| 52 | + var contentTypeData = this.contentData.find(obj => obj.contentType == contentType.alias); |
70 | 53 | if (contentTypeData && contentTypeData.properties.length > 0) {
|
71 |
| - vm.manageIndex.properties = vm.manageIndex.properties.map((obj) => { |
| 54 | + vm.manageIndex.propertiesList = vm.manageIndex.propertiesList.map((obj) => { |
72 | 55 | if (contentTypeData.properties.find(p => p == obj.alias)) {
|
73 |
| - obj.checked = true; |
| 56 | + obj.selected = true; |
74 | 57 | }
|
75 | 58 |
|
76 | 59 | return obj;
|
77 | 60 | });
|
78 | 61 | }
|
79 | 62 | });
|
80 | 63 | },
|
| 64 | + removeContentType: function (contentType) { |
| 65 | + |
| 66 | + const contentTypeIndex = this.contentData.map(obj => obj.contentType).indexOf(contentType.alias); |
| 67 | + this.contentData.splice(contentTypeIndex, 1); |
| 68 | + |
| 69 | + this.selectedContentType = {}; |
| 70 | + this.contentTypesList.forEach(obj => { |
| 71 | + if (obj.alias == contentType.alias) { |
| 72 | + obj.selected = false; |
| 73 | + obj.allowRemove = false; |
| 74 | + } |
| 75 | + }); |
| 76 | + this.propertiesList = []; |
| 77 | + }, |
81 | 78 | selectProperty: function (property) {
|
82 |
| - var checked = !property.checked; |
83 | 79 |
|
84 |
| - if (this.contentData.length == 0 || this.contentData.find(p => p.contentType === this.selectedContentType) === undefined) { |
85 |
| - notificationsService.warning("Please select the property matching content type."); |
86 |
| - return false; |
87 |
| - } |
| 80 | + var contentDataItem = vm.manageIndex.contentData.find(obj => obj.contentType == vm.manageIndex.selectedContentType.alias); |
| 81 | + |
| 82 | + var selected = !property.selected; |
| 83 | + if (selected) { |
| 84 | + // mark item selected |
| 85 | + vm.manageIndex.propertiesList.find(obj => obj.alias == property.alias).selected = true; |
| 86 | + |
| 87 | + // check if content type exists in the contentData array |
| 88 | + if (!contentDataItem) { |
| 89 | + var contentItem = { |
| 90 | + contentType: vm.manageIndex.selectedContentType.alias, |
| 91 | + contentTypeIcon: vm.manageIndex.selectedContentType.icon, |
| 92 | + properties: [] |
| 93 | + }; |
| 94 | + vm.manageIndex.contentData.push(contentItem); |
| 95 | + |
| 96 | + // select content type |
| 97 | + vm.manageIndex.contentTypesList.forEach(obj => { |
| 98 | + if (obj.alias == vm.manageIndex.selectedContentType.alias) { |
| 99 | + obj.selected = true; |
| 100 | + obj.allowRemove = true; |
| 101 | + } |
| 102 | + }); |
| 103 | + } |
88 | 104 |
|
89 |
| - if (checked) { |
90 |
| - this.properties.find(p => p.alias == property.alias).checked = true; |
91 |
| - this.contentData.find(p => p.contentType === this.selectedContentType).properties.push(property.alias); |
| 105 | + // add property |
| 106 | + vm.manageIndex.contentData.find(obj => obj.contentType == vm.manageIndex.selectedContentType.alias).properties.push(property.alias); |
92 | 107 | }
|
93 | 108 | else {
|
94 |
| - const propertyIndex = this.contentData.find(p => p.contentType === this.selectedContentType).properties.indexOf(property.alias); |
95 |
| - if (propertyIndex > -1) this.contentData.find(p => p.contentType === this.selectedContentType).properties.splice(propertyIndex, 1); |
| 109 | + // deselect item |
| 110 | + vm.manageIndex.propertiesList.find(obj => obj.alias == property.alias).selected = false; |
| 111 | + |
| 112 | + // remove property item |
| 113 | + const propertyIndex = vm.manageIndex.contentData |
| 114 | + .find(obj => obj.contentType == vm.manageIndex.selectedContentType.alias).properties.indexOf(property.alias); |
| 115 | + vm.manageIndex.contentData |
| 116 | + .find(obj => obj.contentType == vm.manageIndex.selectedContentType.alias).properties.splice(propertyIndex, 1); |
96 | 117 |
|
97 |
| - this.properties.find(p => p.alias == property.alias).checked = false; |
| 118 | + // remove content type item with no properties and deselect |
| 119 | + if (vm.manageIndex.contentData.find(obj => obj.contentType == vm.manageIndex.selectedContentType.alias).properties.length == 0) { |
| 120 | + vm.manageIndex.contentTypesList.find(obj => obj.alias == vm.manageIndex.selectedContentType.alias).selected = false; |
| 121 | + |
| 122 | + const contentTypeIndex = vm.manageIndex.contentData.map(obj => obj.contentType).indexOf(vm.manageIndex.selectedContentType.alias); |
| 123 | + vm.manageIndex.contentData.splice(contentTypeIndex, 1); |
| 124 | + } |
98 | 125 | }
|
99 | 126 | },
|
100 | 127 | reset: function () {
|
101 | 128 | this.visible = false;
|
| 129 | + this.id = 0; |
102 | 130 | this.name = "";
|
103 |
| - this.selectedContentType = ""; |
104 |
| - this.contentTypes = []; |
105 |
| - this.properties = []; |
| 131 | + this.selectedContentType = {}; |
| 132 | + this.contentTypesList = []; |
| 133 | + this.propertiesList = []; |
106 | 134 | this.contentData = [];
|
107 | 135 | }
|
108 | 136 | };
|
|
120 | 148 |
|
121 | 149 | function addIndex() {
|
122 | 150 | vm.viewState = "manage";
|
123 |
| - algoliaService.getContentTypes((response) => vm.manageIndex.contentTypes = response); |
| 151 | + algoliaService.getContentTypes((response) => vm.manageIndex.contentTypesList = response); |
124 | 152 | }
|
125 | 153 |
|
126 | 154 | function saveIndex() {
|
127 | 155 |
|
128 | 156 | if (vm.manageIndex.name.length == 0 || vm.manageIndex.contentData.length == 0) {
|
129 |
| - notificationsService.error("Index name and content schema are required"); |
130 |
| - return false; |
131 |
| - } |
132 |
| - |
133 |
| - if (vm.manageIndex.contentData.filter(p => p.properties.length == 0).length > 0) { |
134 |
| - notificationsService.error("Selected content types must have at least one property."); |
| 157 | + notificationsService.error("Algolia", "Index name and content schema are required."); |
135 | 158 | return false;
|
136 | 159 | }
|
137 |
| - |
| 160 | + |
138 | 161 | vm.loading = true;
|
139 | 162 |
|
140 | 163 | umbracoCmsIntegrationsSearchAlgoliaResource
|
|
143 | 166 | if (response.success) {
|
144 | 167 | vm.manageIndex.reset();
|
145 | 168 | algoliaService.getContentTypes((response) => vm.manageIndex.contentTypes = response);
|
| 169 | + notificationsService.success("Algolia", "Index saved."); |
146 | 170 | } else {
|
147 |
| - notificationsService.error(response.error); |
| 171 | + notificationsService.error("Algolia", response.error); |
148 | 172 | }
|
149 | 173 |
|
150 | 174 | vm.viewState = "list";
|
|
165 | 189 |
|
166 | 190 | algoliaService.getContentTypes((response) => {
|
167 | 191 |
|
168 |
| - vm.manageIndex.contentTypes = response; |
| 192 | + vm.manageIndex.contentTypesList = response; |
169 | 193 |
|
170 | 194 | for (var i = 0; i < vm.manageIndex.contentData.length; i++) {
|
171 |
| - vm.manageIndex.contentTypes.find(p => p.alias === vm.manageIndex.contentData[i].contentType).checked = true; |
| 195 | + |
| 196 | + vm.manageIndex.contentTypesList.forEach(obj => { |
| 197 | + if (obj.alias == vm.manageIndex.contentData[i].contentType) { |
| 198 | + obj.selected = true; |
| 199 | + obj.allowRemove = true; |
| 200 | + } |
| 201 | + }); |
172 | 202 | }
|
173 | 203 | });
|
174 | 204 | }
|
|
182 | 212 |
|
183 | 213 | umbracoCmsIntegrationsSearchAlgoliaResource.buildIndex(model.index.id).then(function (response) {
|
184 | 214 | if (response.failure)
|
185 |
| - notificationsService.warning("An error has occurred while building the index: " + response.error); |
| 215 | + notificationsService.warning("Algolia", "An error has occurred while building the index: " + response.error); |
186 | 216 | else {
|
| 217 | + notificationsService.success("Algolia", "Index built successfully."); |
187 | 218 | vm.loading = false;
|
188 | 219 | overlayService.close();
|
189 | 220 | }
|
|
199 | 230 |
|
200 | 231 | function searchIndex(index) {
|
201 | 232 | vm.viewState = "search";
|
202 |
| - vm.searchIndex = index; |
| 233 | + vm.selectedSearchIndex = index; |
203 | 234 | }
|
204 | 235 |
|
205 | 236 | function deleteIndex(index) {
|
206 |
| - umbracoCmsIntegrationsSearchAlgoliaResource.deleteIndex(index.id).then(function (response) { |
207 |
| - getIndices(); |
208 |
| - }); |
| 237 | + const dialogOptions = { |
| 238 | + title: "Delete", |
| 239 | + content: "Are you sure you want to delete index <b>" + index.name + "</b>?", |
| 240 | + confirmType: "delete", |
| 241 | + submit: function () { |
| 242 | + umbracoCmsIntegrationsSearchAlgoliaResource.deleteIndex(index.id).then(function (response) { |
| 243 | + if (response.success) { |
| 244 | + notificationsService.success("Algolia", "Index deleted."); |
| 245 | + getIndices(); |
| 246 | + } else |
| 247 | + notificationsService.error("Algolia", response.error); |
| 248 | + |
| 249 | + overlayService.close(); |
| 250 | + }); |
| 251 | + } |
| 252 | + }; |
| 253 | + |
| 254 | + overlayService.confirm(dialogOptions); |
209 | 255 | }
|
210 | 256 |
|
211 | 257 | function search() {
|
212 |
| - umbracoCmsIntegrationsSearchAlgoliaResource.search(vm.searchIndex.id, vm.searchQuery).then(function (response) { |
| 258 | + umbracoCmsIntegrationsSearchAlgoliaResource.search(vm.selectedSearchIndex.id, vm.searchQuery).then(function (response) { |
213 | 259 | vm.searchResults = response;
|
214 | 260 | });
|
215 | 261 | }
|
|
0 commit comments