Skip to content

Commit b9d7b61

Browse files
Copilotsebastienros
andcommitted
Migrate Vue 2 to Vue 3 in OrchardCore.Media module - JavaScript components
Co-authored-by: sebastienros <[email protected]>
1 parent dce1d92 commit b9d7b61

File tree

13 files changed

+131
-84
lines changed

13 files changed

+131
-84
lines changed

src/OrchardCore.Modules/OrchardCore.Media/Assets/js/app/MediaApp/app.js

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
11
var initialized;
22
var mediaApp;
33

4-
var bus = new Vue();
4+
// Simple event emitter to replace Vue 2 event bus
5+
class EventBus {
6+
constructor() {
7+
this.events = {};
8+
}
9+
10+
$on(event, callback) {
11+
if (!this.events[event]) {
12+
this.events[event] = [];
13+
}
14+
this.events[event].push(callback);
15+
}
16+
17+
$emit(event, ...args) {
18+
if (this.events[event]) {
19+
this.events[event].forEach(callback => callback(...args));
20+
}
21+
}
22+
23+
$off(event, callback) {
24+
if (this.events[event]) {
25+
if (callback) {
26+
this.events[event] = this.events[event].filter(cb => cb !== callback);
27+
} else {
28+
delete this.events[event];
29+
}
30+
}
31+
}
32+
}
33+
34+
var bus = new EventBus();
535

636
function initializeMediaApplication(displayMediaApplication, mediaApplicationUrl, pathBase) {
737

@@ -31,20 +61,21 @@ function initializeMediaApplication(displayMediaApplication, mediaApplicationUrl
3161
canCreateFolder: $('#allowNewRootFolders').val() === 'true'
3262
};
3363

34-
mediaApp = new Vue({
35-
el: '#mediaApp',
36-
data: {
37-
selectedFolder: {},
38-
mediaItems: [],
39-
selectedMedias: [],
40-
errors: [],
41-
dragDropThumbnail: new Image(),
42-
smallThumbs: false,
43-
gridView: false,
44-
mediaFilter: '',
45-
sortBy: '',
46-
sortAsc: true,
47-
itemsInPage: []
64+
const app = Vue.createApp({
65+
data() {
66+
return {
67+
selectedFolder: {},
68+
mediaItems: [],
69+
selectedMedias: [],
70+
errors: [],
71+
dragDropThumbnail: new Image(),
72+
smallThumbs: false,
73+
gridView: false,
74+
mediaFilter: '',
75+
sortBy: '',
76+
sortAsc: true,
77+
itemsInPage: []
78+
};
4879
},
4980
created: function () {
5081
var self = this;
@@ -452,6 +483,17 @@ function initializeMediaApplication(displayMediaApplication, mediaApplicationUrl
452483
}
453484
});
454485

486+
// Register components
487+
app.component('folder', folderComponent);
488+
app.component('media-items-grid', mediaItemsGridComponent);
489+
app.component('media-items-table', mediaItemsTableComponent);
490+
app.component('pager', pagerComponent);
491+
app.component('sortIndicator', sortIndicatorComponent);
492+
app.component('upload', uploadComponent);
493+
app.component('uploadList', uploadListComponent);
494+
495+
mediaApp = app.mount('#mediaApp');
496+
455497
$('#create-folder-name').keydown(function (e) {
456498
if (e.key == 'Enter') {
457499
$('#modalFooterOk').click();

src/OrchardCore.Modules/OrchardCore.Media/Assets/js/app/MediaApp/folderComponent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <folder> component
2-
Vue.component('folder', {
2+
var folderComponent = {
33
template: `
44
<li :class="{selected: isSelected}"
55
v-on:dragleave.prevent = "handleDragLeave($event);"
@@ -190,4 +190,4 @@ Vue.component('folder', {
190190
}
191191

192192
}
193-
});
193+
};

src/OrchardCore.Modules/OrchardCore.Media/Assets/js/app/MediaApp/mediaItemsGridComponent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <media-items-grid> component
2-
Vue.component('media-items-grid', {
2+
var mediaItemsGridComponent = {
33
template: `
44
<ol class="row media-items-grid">
55
<li v-for="media in filteredMediaItems"
@@ -67,4 +67,4 @@ Vue.component('media-items-grid', {
6767
return getClassNameForFilename(filename) + ' ' + thumbsize;
6868
}
6969
}
70-
});
70+
};

src/OrchardCore.Modules/OrchardCore.Media/Assets/js/app/MediaApp/mediaItemsTableComponent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <media-items-table> component
2-
Vue.component('media-items-table', {
2+
var mediaItemsTableComponent = {
33
template: `
44
<table class="table media-items-table m-0">
55
<thead>
@@ -119,4 +119,4 @@ Vue.component('media-items-table', {
119119
return getClassNameForFilename(filename) + ' ' + thumbsize;
120120
}
121121
}
122-
});
122+
};

src/OrchardCore.Modules/OrchardCore.Media/Assets/js/app/MediaApp/pagerComponent.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
// This component receives a list of all the items, unpaged.
2-
// As the user interacts with the pager, it raises events with the items in the current page.
3-
// It's the parent's responsibility to listen for these events and display the received items
41
// <pager> component
5-
Vue.component('pager', {
2+
var pagerComponent = {
63
template: `
74
<div>
85
<nav id="media-pager" class="d-flex justify-content-center" aria-label="Pagination Navigation" role="navigation" :data-computed-trigger="itemsInCurrentPage.length">
@@ -157,4 +154,4 @@ Vue.component('pager', {
157154
this.current = 0;
158155
}
159156
}
160-
});
157+
};

src/OrchardCore.Modules/OrchardCore.Media/Assets/js/app/MediaApp/sortIndicatorComponent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <sort-indicator> component
2-
Vue.component('sortIndicator', {
2+
var sortIndicatorComponent = {
33
template: `
44
<div v-show="isActive" class="sort-indicator">
55
<span v-show="asc"><i class="small fa fa-chevron-up"></i></span>
@@ -16,4 +16,4 @@ Vue.component('sortIndicator', {
1616
return this.colname.toLowerCase() == this.selectedcolname.toLowerCase();
1717
}
1818
}
19-
});
19+
};

src/OrchardCore.Modules/OrchardCore.Media/Assets/js/app/MediaField/attachedMediaField.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ function initializeAttachedMediaField(el, idOfUploadButton, uploadAction, mediaI
77
var idprefix = mediaFieldEditor.attr("id");
88
var mediaFieldApp;
99

10-
mediaFieldApps.push(mediaFieldApp = new Vue({
11-
el: mediaFieldEditor.get(0),
12-
data: {
13-
mediaItems: [],
14-
selectedMedia: null,
15-
smallThumbs: false,
16-
idPrefix: idprefix,
17-
initialized: false,
18-
allowMediaText: allowMediaText,
19-
backupMediaText: '',
20-
allowAnchors: allowAnchors,
21-
backupAnchor: null,
22-
mediaTextmodal: null,
23-
anchoringModal: null
10+
const app = Vue.createApp({
11+
data() {
12+
return {
13+
mediaItems: [],
14+
selectedMedia: null,
15+
smallThumbs: false,
16+
idPrefix: idprefix,
17+
initialized: false,
18+
allowMediaText: allowMediaText,
19+
backupMediaText: '',
20+
allowAnchors: allowAnchors,
21+
backupAnchor: null,
22+
mediaTextmodal: null,
23+
anchoringModal: null
24+
};
2425
},
2526
created: function () {
2627
var self = this;
@@ -339,5 +340,14 @@ function initializeAttachedMediaField(el, idOfUploadButton, uploadAction, mediaI
339340
localStorage.setItem('mediaFieldPrefs', JSON.stringify(newPrefs));
340341
}
341342
}
342-
}));
343+
});
344+
345+
// Register components
346+
app.component('mediaFieldThumbsContainer', mediaFieldThumbsContainerComponent);
347+
app.component('mediaFieldGalleryContainer', mediaFieldGalleryContainerComponent);
348+
app.component('mediaFieldGalleryListItem', mediaFieldGalleryListItemComponent);
349+
app.component('mediaFieldGalleryCardItem', mediaFieldGalleryCardItemComponent);
350+
351+
mediaFieldApp = app.mount(mediaFieldEditor.get(0));
352+
mediaFieldApps.push(mediaFieldApp);
343353
}

src/OrchardCore.Modules/OrchardCore.Media/Assets/js/app/MediaField/galleryMediaField.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const MEDIA_FIELD_GALLERY = "mediaFieldGallery_";
22

3-
Vue.component("mediaFieldGalleryListItem", {
3+
var mediaFieldGalleryListItemComponent = {
44
template:
55
/*html*/
66
`
@@ -78,9 +78,9 @@ Vue.component("mediaFieldGalleryListItem", {
7878
return url + (url.indexOf("?") == -1 ? "?" : "&") + "width=32&height=32";
7979
},
8080
},
81-
});
81+
};
8282

83-
Vue.component("mediaFieldGalleryCardItem", {
83+
var mediaFieldGalleryCardItemComponent = {
8484
template:
8585
/*html*/
8686
`
@@ -169,9 +169,9 @@ Vue.component("mediaFieldGalleryCardItem", {
169169
return (url + (url.indexOf("?") == -1 ? "?" : "&") + "width=240&height=240");
170170
},
171171
},
172-
});
172+
};
173173

174-
Vue.component("mediaFieldGalleryContainer", {
174+
var mediaFieldGalleryContainerComponent = {
175175
template:
176176
/*html*/
177177
`
@@ -445,4 +445,4 @@ Vue.component("mediaFieldGalleryContainer", {
445445
);
446446
},
447447
}
448-
});
448+
};

src/OrchardCore.Modules/OrchardCore.Media/Assets/js/app/MediaField/mediaFieldThumbsContainer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// <media-field-thumbs-container> component
22
// different media field editors share this component to present the thumbs.
3-
Vue.component('mediaFieldThumbsContainer', {
3+
var mediaFieldThumbsContainerComponent = {
44
template:
55
/* html */
66
`
@@ -140,4 +140,4 @@ Vue.component('mediaFieldThumbsContainer', {
140140
return getClassNameForFilename(filename) + ' ' + thumbsize;
141141
}
142142
}
143-
});
143+
};

src/OrchardCore.Modules/OrchardCore.Media/Assets/js/app/MediaField/mediafield.js

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,22 @@ function initializeMediaField(el, modalBodyElement, mediaItemUrl, allowMultiple,
1515
document.getElementById("mediaApp").classList.add("d-none");
1616
});
1717

18-
mediaFieldApps.push(mediaFieldApp = new Vue({
19-
el: mediaFieldEditor.get(0),
20-
data: {
21-
mediaItems: [],
22-
selectedMedia: null,
23-
smallThumbs: false,
24-
idPrefix: idprefix,
25-
initialized: false,
26-
allowMediaText: allowMediaText,
27-
backupMediaText: '',
28-
allowAnchors: allowAnchors,
29-
allowedExtensions: allowedExtensions,
30-
backupAnchor: null,
31-
mediaTextModal: null,
32-
anchoringModal: null
18+
const app = Vue.createApp({
19+
data() {
20+
return {
21+
mediaItems: [],
22+
selectedMedia: null,
23+
smallThumbs: false,
24+
idPrefix: idprefix,
25+
initialized: false,
26+
allowMediaText: allowMediaText,
27+
backupMediaText: '',
28+
allowAnchors: allowAnchors,
29+
allowedExtensions: allowedExtensions,
30+
backupAnchor: null,
31+
mediaTextModal: null,
32+
anchoringModal: null
33+
};
3334
},
3435
created: function () {
3536
var self = this;
@@ -127,18 +128,6 @@ function initializeMediaField(el, modalBodyElement, mediaItemUrl, allowMultiple,
127128
var self = this;
128129

129130
self.paths = initialPaths;
130-
131-
self.$on('selectAndDeleteMediaRequested', function (media) {
132-
self.selectAndDeleteMedia(media);
133-
});
134-
135-
self.$on('selectMediaRequested', function (media) {
136-
self.selectMedia(media);
137-
});
138-
139-
self.$on('filesUploaded', function (files) {
140-
self.addMediaFiles(files);
141-
});
142131
},
143132
methods: {
144133
selectMedia: function (media) {
@@ -291,5 +280,14 @@ function initializeMediaField(el, modalBodyElement, mediaItemUrl, allowMultiple,
291280
localStorage.setItem('mediaFieldPrefs', JSON.stringify(newPrefs));
292281
}
293282
}
294-
}));
283+
});
284+
285+
// Register components
286+
app.component('mediaFieldThumbsContainer', mediaFieldThumbsContainerComponent);
287+
app.component('mediaFieldGalleryContainer', mediaFieldGalleryContainerComponent);
288+
app.component('mediaFieldGalleryListItem', mediaFieldGalleryListItemComponent);
289+
app.component('mediaFieldGalleryCardItem', mediaFieldGalleryCardItemComponent);
290+
291+
mediaFieldApp = app.mount(mediaFieldEditor.get(0));
292+
mediaFieldApps.push(mediaFieldApp);
295293
}

0 commit comments

Comments
 (0)