/** * * ----------------------------------------------------------------------------- * A horizontally scrolling row of tweets with an auto-updating page indicator * * @type {Component} * * ----------------------------------------------------------------------------- */ parasails.registerComponent('scrollableTweets', { // ╔═╗╦═╗╔═╗╔═╗╔═╗ // ╠═╝╠╦╝║ ║╠═╝╚═╗ // ╩ ╩╚═╚═╝╩ ╚═╝ props: [ 'testimonials' ], // ╦╔╗╔╦╔╦╗╦╔═╗╦ ╔═╗╔╦╗╔═╗╔╦╗╔═╗ // ║║║║║ ║ ║╠═╣║ ╚═╗ ║ ╠═╣ ║ ║╣ // ╩╝╚╝╩ ╩ ╩╩ ╩╩═╝ ╚═╝ ╩ ╩ ╩ ╩ ╚═╝ data: function () { return { quotesToDisplay: [], quotesWithVideoLinks: [], tweetsDiv: undefined, tweetCards: undefined, pageWidth: undefined, numberOfTweetCardsDisplayedOnThisPage: undefined, showPreviousPageButton: false, showNextPageButton: true, numberOfTweetsPerPage: 0, syncing: false, firstCardPosition: 0, modal: '', }; }, // ╦ ╦╔╦╗╔╦╗╦ // ╠═╣ ║ ║║║║ // ╩ ╩ ╩ ╩ ╩╩═╝ template: `

{{testimonial.quote}} See the video.

{{testimonial.quoteAuthorName}}

{{testimonial.quoteAuthorJobTitle}}

`, // ╦ ╦╔═╗╔═╗╔═╗╦ ╦╔═╗╦ ╔═╗ // ║ ║╠╣ ║╣ ║ ╚╦╝║ ║ ║╣ // ╩═╝╩╚ ╚═╝╚═╝ ╩ ╚═╝╩═╝╚═╝ beforeMount: function() { if(!this.testimonials){ throw new Error('Incomplete usage of : Please pass in a `testimonials` prop (an array of testimonials from sails.config.builtStaticContent.testimonials). For example: ``'); } if(!_.isArray(this.testimonials)){ throw new Error('Incomplete usage of : The `testimonials` prop provided is an invalid type. Please provide an array of testimonial values.'); } this.quotesToDisplay = _.clone(this.testimonials); for(let quote of this.testimonials){ if(quote.youtubeVideoUrl){ this.quotesWithVideoLinks.push({ modalId: _.kebabCase(quote.quoteAuthorName), embedId: quote.videoIdForEmbed, }); } } }, mounted: async function(){ this.tweetsDiv = $('div[purpose="tweets"]')[0]; this.tweetCards = $('a[purpose="tweet-card"]'); this.firstCardPosition = this.tweetCards[0].getBoundingClientRect().x; this.numberOfTweetCardsDisplayedOnThisPage = this.tweetCards.length; this.calculateHowManyFullTweetsCanBeDisplayed(); $(window).on('resize', this.calculateHowManyFullTweetsCanBeDisplayed); $(window).on('wheel', this.updatePageIndicators); }, beforeDestroy: function() { }, // ╦╔╗╔╔╦╗╔═╗╦═╗╔═╗╔═╗╔╦╗╦╔═╗╔╗╔╔═╗ // ║║║║ ║ ║╣ ╠╦╝╠═╣║ ║ ║║ ║║║║╚═╗ // ╩╝╚╝ ╩ ╚═╝╩╚═╩ ╩╚═╝ ╩ ╩╚═╝╝╚╝╚═╝ methods: { calculateHowManyFullTweetsCanBeDisplayed: function() { let firstTweetCard = this.tweetCards[0]; let nextTweetCard = this.tweetCards[1]; this.tweetCardWidth = nextTweetCard.getBoundingClientRect().x - firstTweetCard.getBoundingClientRect().x; this.numberOfTweetsPerPage = Math.floor((document.body.clientWidth - this.firstCardPosition)/this.tweetCardWidth); if(this.numberOfTweetsPerPage < 1){ this.numberOfTweetsPerPage = 1; } this.pageWidth = this.tweetCardWidth * this.numberOfTweetsPerPage; if(this.numberOfTweetsPerPage >= this.numberOfTweetCardsDisplayedOnThisPage){ $(this.tweetsDiv).addClass('mx-auto'); } else { $(this.tweetsDiv).removeClass('mx-auto'); } this.updatePageIndicators(); }, clickNextPage: async function() { if(!this.syncing){ this.tweetsDiv.scrollLeft += this.pageWidth; await setTimeout(()=>{ this.updatePageIndicators(); }, 600); } }, clickPreviousPage: async function() { if(!this.syncing){ this.tweetsDiv.scrollLeft -= this.pageWidth; await setTimeout(()=>{ this.updatePageIndicators(); }, 600); } }, updatePageIndicators: function() { this.syncing = false; this.showPreviousPageButton = this.tweetsDiv.scrollLeft > (this.firstCardPosition * 0.5); this.showNextPageButton = (this.tweetsDiv.scrollWidth - this.tweetsDiv.scrollLeft - this.tweetsDiv.clientWidth) >= this.tweetCardWidth * .25; }, clickOpenVideoModal: function(modalName) { this.modal = _.kebabCase(modalName); }, closeModal: function() { this.modal = undefined; }, } });