Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove implicit dependencies on jQuery #55

Merged
merged 2 commits into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions addon/components/ember-youtube.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* global YT, window */

import $ from 'jquery';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

byebye

import Component from '@ember/component';
import RSVP from 'rsvp'
import { computed, getProperties, setProperties, observer } from '@ember/object';
Expand Down Expand Up @@ -65,7 +64,7 @@ export default Component.extend({
didInsertElement() {
this._super(...arguments);

this.progressBarClick();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed

this.addProgressBarClickHandler();

if (!this.get('lazyload') && this.get('ytid')) {
// If "lazyload" is not enabled and we have an ID, we can start immediately.
Expand All @@ -80,6 +79,9 @@ export default Component.extend({
// clear the timer
this.stopTimer();

// remove progress bar click handler
this.removeProgressBarClickHandler();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here i remove the manual click handler event binding.


// destroy video player
const player = this.get('player');
if (player) {
Expand Down Expand Up @@ -137,7 +139,9 @@ export default Component.extend({
// If already loaded, make sure not to load the script again.
resolve(window.YT);
} else {
$.getScript('https://www.youtube.com/iframe_api');
let ytScript = document.createElement("script");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since ember-ajax and jquery have been removed, $.getScript has been replaced with a native solution borrowed from jQuery.

Under the hood, all jQuery getScript was doing was creating a <script> tag and appending it to the page, thus evaluating the script on page. Here I just do that in 3 lines of code.

ytScript.src = "https://www.youtube.com/iframe_api";
document.head.appendChild(ytScript);
}
});
},
Expand Down Expand Up @@ -325,17 +329,31 @@ export default Component.extend({

// OK, this is stupid but couldn't access the "event" inside
// an ember action so here's a manual click handler instead.
progressBarClick() {
addProgressBarClickHandler() {
this.element.addEventListener(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without jquery, i just use addEventListener instead of $().on(...).

"click",
this.progressBarClick.bind(this),
false
);
},
progressBarClick(event) {
let self = this;
this.$().on('click', 'progress', function (event) {
// get the x position of the click inside our progress el
let x = event.pageX - $(this).position().left;
// convert it to a value relative to the duration (max)
let clickedValue = x * this.max / this.offsetWidth;
// 250 = 0.25 seconds into player
self.set("currentTime", clickedValue);
self.send('seekTo', clickedValue);
});
let element = event.srcElement;
if (element.tagName.toLowerCase() !== "progress") return;
// get the x position of the click inside our progress el
let x = event.pageX - element.getBoundingClientRect().x;
// convert it to a value relative to the duration (max)
let clickedValue = (x * element.max) / element.offsetWidth;
// 250 = 0.25 seconds into player
self.set("currentTime", clickedValue);
self.send("seekTo", clickedValue);
},
removeProgressBarClickHandler() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the event handler wasn't removed previously, so now it is cleaned up when the component is removed from the DOM.

this.element.removeEventListener(
"click",
this.progressBarClick.bind(this),
false
);
},

actions: {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
},
"devDependencies": {
"broccoli-asset-rev": "^2.4.5",
"ember-ajax": "^3.0.0",
"ember-cli": "~3.0.0",
"ember-cli-dependency-checker": "^2.0.0",
"ember-cli-eslint": "^4.2.1",
Expand Down
Loading