6
6
import delegate from 'delegate' ;
7
7
import _ from 'lodash' ;
8
8
import * as tools from '../../utils/tools' ;
9
- import { resourceCard , tabButton , tabCardsContent , tabContentContainer , paginationLink , noDataAvailable } from '../templates/resources' ;
9
+ import { resourceCard , resourceVideoCard , tabButton , tabCardsContent , tabVideoCardsContent , tabContentContainer , paginationLink , noDataAvailable , videoPlaylist } from '../templates/resources' ;
10
10
import { ADMIN_IMAGES } from '../config/wp-settings' ;
11
11
import { on , trigger } from '../../utils/events' ;
12
12
import scrollTo from '../../utils/dom/scroll-to' ;
@@ -20,6 +20,7 @@ const el = {
20
20
const tabInstances = {
21
21
tabs : [ ] ,
22
22
cards : [ ] ,
23
+ videos : [ ] ,
23
24
cards_per_page : 12 ,
24
25
} ;
25
26
@@ -58,6 +59,41 @@ const createCards = (cards, label) => {
58
59
} ) ;
59
60
} ;
60
61
62
+ /**
63
+ * @function createVideoCards
64
+ * @description Build the HTML elements for each video card data available in the bigcommerce_resources_json JSON.
65
+ * @param videos
66
+ * @param label
67
+ * @param playlistLabel
68
+ */
69
+ const createVideoCards = ( videos = { } , label = '' , playlistLabel = '' ) => {
70
+ Object . values ( videos ) . forEach ( ( card ) => {
71
+ const smallThumb = _ . get ( card , 'thumbnail.small' ) ;
72
+ const largeThumb = _ . get ( card , 'thumbnail.large' ) ;
73
+ // If we have images, use them. Otherwise, we'll use the BigCommerce logo thumbnail placeholder.
74
+ const image1 = smallThumb ? `${ smallThumb } 1x,` : `${ ADMIN_IMAGES } bigcommerce-resource-thumbnail.png 1x,` ;
75
+ const image2 = largeThumb ? `${ largeThumb } 2x,` : `${ ADMIN_IMAGES } bigcommerce-resource-thumbnail-2x.png 2x` ;
76
+ // Create a card and push it as a new indexed item in the cards array.
77
+ tabInstances . videos [ playlistLabel ] . push ( resourceVideoCard ( image1 , image2 , card . name , card . video_length , card . description , card . url ) ) ;
78
+ } ) ;
79
+ } ;
80
+
81
+ /**
82
+ * @function createVideoPlaylists
83
+ * @description Creates an array of playlists with completed markup.
84
+ * @param playlists
85
+ * @param label
86
+ */
87
+ const createVideoPlaylists = ( playlists = { } , label = '' ) => {
88
+ Object . values ( playlists ) . forEach ( ( playlist ) => {
89
+ const playlistLabel = playlist . playlist_label ;
90
+ tabInstances . videos [ playlistLabel ] = [ ] ;
91
+
92
+ tabInstances . videos [ playlistLabel ] . push ( createVideoCards ( playlist . videos , label , playlistLabel ) ) ;
93
+ tabInstances . cards [ label ] . push ( videoPlaylist ( playlistLabel , playlist . playlist , tabInstances . videos [ playlistLabel ] . join ( '' ) , tabInstances . videos [ playlistLabel ] . length - 1 ) ) ;
94
+ } ) ;
95
+ } ;
96
+
61
97
/**
62
98
* @function createTabs
63
99
* @description Create a new tab button for each available tabInstances.tabs;
@@ -104,7 +140,6 @@ const createTabContentContainers = () => {
104
140
// Create a wrapper element to attach the containers to.
105
141
const contentWrapper = document . createElement ( 'div' ) ;
106
142
tools . addClass ( contentWrapper , 'bc-resources-tabs__content' ) ;
107
- tools . addClass ( contentWrapper , 'bc-resources-tabs__max-width' ) ;
108
143
el . contentContainer . appendChild ( contentWrapper ) ;
109
144
110
145
// Loop through the available keys and create the parent container tied to the tab button.
@@ -223,7 +258,12 @@ const getPaginatedItems = (e, cardKey = '', currentPage = 1) => {
223
258
const offset = ( currentPage - 1 ) * tabInstances . cards_per_page ;
224
259
const paginatedItems = tabInstances . cards [ key ] . slice ( offset , offset + tabInstances . cards_per_page ) ;
225
260
226
- cardPageWrapper . insertAdjacentHTML ( 'beforeend' , tabCardsContent ( key , paginatedItems . join ( '' ) , currentPage ) ) ;
261
+ if ( key === 'Tutorials' ) {
262
+ cardPageWrapper . insertAdjacentHTML ( 'beforeend' , tabVideoCardsContent ( key , paginatedItems . join ( '' ) , currentPage ) ) ;
263
+ } else {
264
+ cardPageWrapper . insertAdjacentHTML ( 'beforeend' , tabCardsContent ( key , paginatedItems . join ( '' ) , currentPage ) ) ;
265
+ }
266
+
227
267
focusFirstPagedCard ( key , currentPage ) ;
228
268
} ;
229
269
@@ -274,7 +314,12 @@ const initResourceCards = () => {
274
314
275
315
tabInstances . tabs . push ( tabButton ( section . label ) ) ;
276
316
tabInstances . cards [ section . label ] = [ ] ;
277
- createCards ( section . resources , section . label ) ;
317
+
318
+ if ( section . label === 'Tutorials' ) {
319
+ createVideoPlaylists ( section . resources , section . label ) ;
320
+ } else {
321
+ createCards ( section . resources , section . label ) ;
322
+ }
278
323
} ) ;
279
324
280
325
createTabs ( ) ;
0 commit comments