@iamproperty/components 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Integrate YouTube videos as a way of hosting videos without the overhead and worry surrounding hosting vides. i.e. file sizes, performance and accessibility.
3
+ */
4
+ class youtubeVideo {
5
+
6
+ /** @param {HTMLElement} embed dom element */
7
+ constructor(embed){
8
+
9
+ let createEmbed = this.createEmbed;
10
+
11
+ // If the scripts is already loaded then lets just create the embed
12
+ if(document.body.classList.contains('youtubeLoaded')){
13
+ embed.addEventListener('click', function(e){
14
+
15
+ // loop parent nodes from the target to the delegation node
16
+ for (var target = e.target; target && target != this; target = target.parentNode) {
17
+
18
+ if (target.matches('a:not([data-modal-youtube]')) {
19
+
20
+ e.preventDefault();
21
+ createEmbed(embed,target);
22
+ break;
23
+ }
24
+ }
25
+ }, false);
26
+ }
27
+ else {
28
+ this.loadScripts(embed, this.createEmbed);
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Load the YouTube scripts before trying to create the embed
34
+ * @param {HTMLElement} embed dom element
35
+ * @param {Function} createEmbed function to create the embed after script loaded.
36
+ */
37
+ loadScripts(embed, createEmbed){
38
+
39
+ return new Promise((resolve, reject) => {
40
+
41
+ const image = new Image();
42
+ image.onload = function(){
43
+
44
+ // This code loads the IFrame Player API code asynchronously.
45
+ var tag = document.createElement('script');
46
+ tag.src = "https://www.youtube.com/iframe_api";
47
+ var firstScriptTag = document.getElementsByTagName('script')[0];
48
+ firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
49
+ document.body.classList.add('youtubeLoaded');
50
+ resolve(true);
51
+
52
+ // script has loaded, you can now use it safely
53
+ tag.onload = () => {
54
+
55
+ embed.addEventListener('click', function(e){
56
+ // loop parent nodes from the target to the delegation node
57
+ for (var target = e.target; target && target != this; target = target.parentNode) {
58
+
59
+ if (target.matches('a:not([data-modal-youtube]')) {
60
+
61
+ e.preventDefault();
62
+ createEmbed(embed,target);
63
+ break;
64
+ }
65
+ }
66
+ }, false);
67
+ }
68
+
69
+ };
70
+ image.onerror = function(){
71
+ reject(false);
72
+ };
73
+ image.src = "https://youtube.com/favicon.ico";
74
+ });
75
+
76
+ }
77
+
78
+ /**
79
+ * Create the YouTube embed after the user has clicked on it.
80
+ * @param {HTMLElement} embed dom element
81
+ */
82
+ createEmbed(embed,target){
83
+
84
+ // If there is more than one video lets make sure there is only one playing at a time.
85
+ if(typeof window.player != "undefined" && typeof window.player.pauseVideo == "function")
86
+ window.player.pauseVideo();
87
+
88
+
89
+ var video_id = target.getAttribute('data-id');
90
+ var link_id = target.getAttribute('id')
91
+
92
+ // create an id to pass t the script if one isn't present
93
+ if(typeof link_id == 'undefined' || link_id == null){
94
+
95
+ var randLetter = String.fromCharCode(65 + Math.floor(Math.random() * 26));
96
+ link_id = randLetter + Date.now();
97
+ target.setAttribute('id',link_id);
98
+ }
99
+
100
+ // This function creates an <iframe> (and YouTube player) after the API code downloads.
101
+ function onYouTubeIframeAPIReady() {
102
+
103
+ window.player = new YT.Player(link_id, {
104
+ height: '100%',
105
+ width: '100%',
106
+ videoId: video_id,
107
+ playerVars: {
108
+ 'modestbranding': 1,
109
+ 'playsinline': 1,
110
+ 'rel': 0,
111
+ 'showinfo': 0
112
+ },
113
+ events: {
114
+ 'onReady': onPlayerReady,
115
+ 'onStateChange': onPlayerStateChange
116
+ }
117
+ });
118
+
119
+ }
120
+ onYouTubeIframeAPIReady();
121
+
122
+ // The API will call this function when the video player is ready.
123
+ function onPlayerReady(event) {
124
+ // Play the video straight away
125
+ event.target.playVideo();
126
+
127
+ }
128
+
129
+ // The API calls this function when the player's state changes.
130
+ // The function indicates that when playing a video (state=1)
131
+ var done = false;
132
+ function onPlayerStateChange(event) {
133
+
134
+ if (event.data == YT.PlayerState.PLAYING && !done) {
135
+
136
+ var link = document.getElementById(link_id);
137
+ link.classList.add('player-ready');
138
+
139
+ done = true;
140
+ }
141
+ }
142
+ }
143
+ }
144
+
145
+ export default youtubeVideo