66import delegate from 'delegate' ;
77import _ from 'lodash' ;
88import * 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' ;
1010import { ADMIN_IMAGES } from '../config/wp-settings' ;
1111import { on , trigger } from '../../utils/events' ;
1212import scrollTo from '../../utils/dom/scroll-to' ;
@@ -20,6 +20,7 @@ const el = {
2020const tabInstances = {
2121 tabs : [ ] ,
2222 cards : [ ] ,
23+ videos : [ ] ,
2324 cards_per_page : 12 ,
2425} ;
2526
@@ -58,6 +59,41 @@ const createCards = (cards, label) => {
5859 } ) ;
5960} ;
6061
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+
6197/**
6298 * @function createTabs
6399 * @description Create a new tab button for each available tabInstances.tabs;
@@ -104,7 +140,6 @@ const createTabContentContainers = () => {
104140 // Create a wrapper element to attach the containers to.
105141 const contentWrapper = document . createElement ( 'div' ) ;
106142 tools . addClass ( contentWrapper , 'bc-resources-tabs__content' ) ;
107- tools . addClass ( contentWrapper , 'bc-resources-tabs__max-width' ) ;
108143 el . contentContainer . appendChild ( contentWrapper ) ;
109144
110145 // 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) => {
223258 const offset = ( currentPage - 1 ) * tabInstances . cards_per_page ;
224259 const paginatedItems = tabInstances . cards [ key ] . slice ( offset , offset + tabInstances . cards_per_page ) ;
225260
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+
227267 focusFirstPagedCard ( key , currentPage ) ;
228268} ;
229269
@@ -274,7 +314,12 @@ const initResourceCards = () => {
274314
275315 tabInstances . tabs . push ( tabButton ( section . label ) ) ;
276316 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+ }
278323 } ) ;
279324
280325 createTabs ( ) ;
0 commit comments