|
120 | 120 |
|
121 | 121 | var plugins = []; |
122 | 122 | var compressImages = false; |
| 123 | + var changeTimer; |
| 124 | + var lastImage=null; |
123 | 125 |
|
124 | 126 | var lazyLoad = { |
125 | 127 | loadImage: function (item) { |
|
378 | 380 | pluginItemFrame.appendChild(pluginItemSlidesContainer); |
379 | 381 | pluginsContainer.appendChild(pluginItemFrame); |
380 | 382 |
|
381 | | - renderCarousel(null, layout); |
| 383 | + renderCarousel(null, layout,0,0,0); |
382 | 384 | } |
383 | 385 |
|
384 | 386 | //load image in first render |
|
387 | 389 |
|
388 | 390 | if (layout != 12) { |
389 | 391 | //Render Carousel |
390 | | - renderCarousel(obj.data.content.carouselImages, layout); |
| 392 | + var speed=(obj.data.content&&obj.data.content.speed)?obj.data.content.speed:5000; |
| 393 | + var order=(obj.data.content&&obj.data.content.order)?obj.data.content.order:0; |
| 394 | + var display=(obj.data.content&&obj.data.content.display)?obj.data.content.display:0; |
| 395 | + renderCarousel(obj.data.content.carouselImages, layout,speed,order,display); |
391 | 396 | } |
392 | 397 | } |
393 | 398 |
|
|
676 | 681 | }); |
677 | 682 | } |
678 | 683 |
|
679 | | - function renderCarousel(carouselImages, layout) { |
| 684 | + function shuffle(a,first,last) { |
| 685 | + var j, x, i; |
| 686 | + for (i = a.length - 1; i > 0; i--) { |
| 687 | + j = Math.floor(Math.random() * (i + 1)); |
| 688 | + x = a[i]; |
| 689 | + a[i] = a[j]; |
| 690 | + a[j] = x; |
| 691 | + } |
| 692 | + if(last==a[0]||first==a[a.length-1]) return shuffle(a,first,last); |
| 693 | + else return a; |
| 694 | + } |
| 695 | + |
| 696 | + function randomizeArray(sent){ |
| 697 | + shuffle(sent,null,null); |
| 698 | + var dup=Array.from(sent); |
| 699 | + shuffle(dup,null,sent[sent.length - 1]); |
| 700 | + sent.push(...dup); |
| 701 | + shuffle(dup,sent[0],sent[sent.length - 1]); |
| 702 | + sent.push(...dup); |
| 703 | + } |
| 704 | + function changeImage(carouselImages,random){ |
| 705 | + var oldState=carouselImages; |
| 706 | + if(random){ |
| 707 | + carouselImages=[carouselImages[Math.floor(Math.random() * carouselImages.length)]]; |
| 708 | + |
| 709 | + if(carouselImages[0]!=lastImage[0]){ |
| 710 | + lastImage=carouselImages; |
| 711 | + appendOneImage(carouselImages); |
| 712 | + }else changeImage(oldState); |
| 713 | + }else{ |
| 714 | + var index=carouselImages.indexOf(lastImage[0]); |
| 715 | + var sendIndex=0; |
| 716 | + if(index==-1||index==carouselImages.length-1)carouselImages=[carouselImages[0]]; |
| 717 | + else {carouselImages=[carouselImages[index+1]];sendIndex=index+1;} |
| 718 | + lastImage=carouselImages; |
| 719 | + var isHome=(new URLSearchParams(window.location.search).get('fid').split("=")[0]=="launcherPluginv"); |
| 720 | + var storagePlace=(isHome)?"carouselLastImageHome":"carouselLastImage"; |
| 721 | + buildfire.localStorage.setItem(storagePlace,sendIndex, function(e,r){ |
| 722 | + appendOneImage(carouselImages); |
| 723 | + }); |
| 724 | + } |
| 725 | + |
| 726 | + } |
| 727 | + function appendOneImage(carouselImages){ |
| 728 | + var myImg=document.getElementById("one_img"); |
| 729 | + if(myImg==null){ |
| 730 | + carouselContainer.innerHTML = ''; |
| 731 | + var img = document.createElement('img'); |
| 732 | + img.setAttribute("id", "one_img"); |
| 733 | + img.setAttribute("src", buildfire.imageLib.cropImage(carouselImages[0].iconUrl, { |
| 734 | + width: window.innerWidth, |
| 735 | + height: Math.ceil(9 * (window.innerWidth) / 16) |
| 736 | + })); |
| 737 | + img.alt = "Carousel Image"; |
| 738 | + carouselContainer.appendChild(img); |
| 739 | + img.addEventListener("click", function () { |
| 740 | + buildfire.actionItems.execute(carouselImages[0], function (err, result) { |
| 741 | + if (err) { |
| 742 | + console.warn('Error openning slider action: ', err); |
| 743 | + } |
| 744 | + }); |
| 745 | + }); |
| 746 | + }else{ |
| 747 | + myImg.setAttribute("src", buildfire.imageLib.cropImage(carouselImages[0].iconUrl, { |
| 748 | + width: window.innerWidth, |
| 749 | + height: Math.ceil(9 * (window.innerWidth) / 16) |
| 750 | + })); |
| 751 | + myImg.removeEventListener("click",function(){}); |
| 752 | + myImg.addEventListener("click", function () { |
| 753 | + buildfire.actionItems.execute(carouselImages[0], function (err, result) { |
| 754 | + if (err) { |
| 755 | + console.warn('Error openning slider action: ', err); |
| 756 | + } |
| 757 | + }); |
| 758 | + }); |
| 759 | + } |
| 760 | + } |
| 761 | + function renderCarousel(carouselImages, layout,speed,order,display) { |
| 762 | + if(changeTimer) clearInterval(changeTimer); |
680 | 763 | if (layout != 12) { |
681 | 764 | if (carouselImages && carouselImages.length > 0) { |
682 | 765 | /* |
683 | 766 | if more than one image add carousel else add image directly to the carousel container |
684 | 767 | */ |
| 768 | + var isHome=(new URLSearchParams(window.location.search).get('fid').split("=")[0]=="launcherPluginv"); |
| 769 | + var storagePlace=(isHome)?"carouselLastImageHome":"carouselLastImage"; |
| 770 | + if(order == 0 && display== 1 && carouselImages.length > 1){ |
| 771 | + buildfire.localStorage.getItem(storagePlace, function(e,r) { |
| 772 | + var images=carouselImages; |
| 773 | + var sendIndex=0; |
| 774 | + if(r==null){ |
| 775 | + carouselImages=[carouselImages[0]]; |
| 776 | + }else{ |
| 777 | + var index=Number(r); |
| 778 | + if(index==-1||index==carouselImages.length-1)carouselImages=[carouselImages[0]]; |
| 779 | + else {carouselImages=[carouselImages[index+1]];sendIndex=index+1;} |
| 780 | + } |
| 781 | + buildfire.localStorage.setItem(storagePlace,sendIndex, function(e,r){ |
| 782 | + lastImage=carouselImages; |
| 783 | + if(speed!=0)changeTimer=setInterval(changeImage, speed,images,false); |
| 784 | + }); |
| 785 | + }); |
| 786 | + } |
| 787 | + else if(order == 1 && display== 1 && carouselImages.length > 1){ |
| 788 | + if(speed!=0){ |
| 789 | + changeTimer=setInterval(changeImage, speed,carouselImages,true); |
| 790 | + } |
| 791 | + carouselImages=[carouselImages[Math.floor(Math.random() * carouselImages.length)]]; |
| 792 | + lastImage=carouselImages; |
| 793 | + buildfire.localStorage.removeItem(storagePlace); |
| 794 | + |
| 795 | + }else if(order == 1 && display== 0 && carouselImages.length > 1){ |
| 796 | + randomizeArray(carouselImages); |
| 797 | + buildfire.localStorage.removeItem(storagePlace); |
| 798 | + } |
685 | 799 | if (carouselImages.length > 1) { |
686 | 800 |
|
687 | 801 | var carousel = new buildfire.components.carousel.view({ |
688 | 802 | selector: carouselContainer, |
689 | 803 | items: carouselImages, |
690 | | - speed: 1000 |
| 804 | + loop: (speed!=0), |
| 805 | + infinite:false, |
| 806 | + autoInterval:speed |
691 | 807 | }); |
692 | 808 |
|
693 | 809 | } else { |
694 | | - //add image directly to carousel container without adding the carousel lib |
695 | | - carouselContainer.innerHTML = ''; |
696 | | - //append image tag |
697 | | - var img = document.createElement('img'); |
698 | | - img.setAttribute("src", buildfire.imageLib.cropImage(carouselImages[0].iconUrl, { |
699 | | - width: window.innerWidth, |
700 | | - height: Math.ceil(9 * (window.innerWidth) / 16) |
701 | | - })); |
702 | | - img.alt = "Carousel Image"; |
703 | | - carouselContainer.appendChild(img); |
704 | | - img.addEventListener("click", function () { |
705 | | - buildfire.actionItems.execute(carouselImages[0], function (err, result) { |
706 | | - if (err) { |
707 | | - console.warn('Error openning slider action: ', err); |
708 | | - } |
709 | | - }); |
710 | | - }); |
| 810 | + appendOneImage(carouselImages); |
711 | 811 | } |
712 | 812 |
|
713 | 813 | carouselContainer.classList.remove('hide'); |
|
0 commit comments